function main() {
// Threshold Settings
var campaignBudgetThreshold = 0.8; // Alert when 80% of the budget is spent
var sendEmailAlerts = true; // Set to false to disable emails
var recipientEmail = "your_email@example.com";
// Get all active campaigns
var campaignIterator = AdsApp.campaigns()
.withCondition("Status = ENABLED")
.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignName = campaign.getName();
var budget = campaign.getBudget().getAmount();
var spendToDate = campaign.getStatsFor("TODAY").getCost();
var spentPercentage = spendToDate / budget;
if (spentPercentage >= campaignBudgetThreshold && sendEmailAlerts) {
var subject = "Budget Alert: " + campaignName;
var body = "Campaign " + campaignName + " has spent " + (spentPercentage * 100).toFixed(0) + "% of its budget.";
MailApp.sendEmail(recipientEmail, subject, body);
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter