function main() {
// Get all enabled campaigns
var campaignsIterator = AdsApp.campaigns()
.withCondition("Status = ENABLED")
.get();
// Initialize an empty array to store campaigns that meet the criteria
var eligibleCampaigns = [];
// Loop through each campaign
while (campaignsIterator.hasNext()) {
var campaign = campaignsIterator.next();
// Retrieve the campaign's statistics
var stats = campaign.getStatsFor("LAST_30_DAYS");
// Retrieve search lost top impression share (Budget)
var searchLostTopImpressionShare = stats.getSearchLostTopImpressionShare();
// Retrieve conversion value per cost
var convValuePerCost = stats.getConversionValue() / stats.getCost();
// Check if conditions are met
if (searchLostTopImpressionShare > 0.5 && convValuePerCost > 10) {
// If conditions are met, add the campaign to the eligibleCampaigns array
eligibleCampaigns.push(campaign);
}
}
// Check if there are any eligible campaigns
if (eligibleCampaigns.length > 0) {
// If there are eligible campaigns, construct the email content
var emailBody = "The following campaigns meet the criteria:\n\n";
for (var i = 0; i < eligibleCampaigns.length; i++) {
emailBody += "Campaign Name: " + eligibleCampaigns[i].getName() + "\n";
// Add more details if needed
emailBody += "\n";
}
// Send email to the admin
MailApp.sendEmail({
to: "
ad...@example.com", // Replace with the admin's email address
subject: "Eligible Campaigns for Optimization",
body: emailBody,
});
}
}