Google Ads Script – Daily Budget Target Email Alert

153 views
Skip to first unread message

Alejandro Perez

unread,
Feb 21, 2025, 6:04:11 PM2/21/25
to Google Ads Scripts Forum
Hello,

I'm trying to find a script that can send me an email when my daily budget hits for any of the enable campaigns that I have in my account during the day. The idea is to run it with an hourly frequency.

I found one script online (it works partially), but it does send an email hourly even if I didn't hit the daily budget.

I appreciate your help

Alex

Google Ads Scripts Forum

unread,
Feb 21, 2025, 6:07:38 PM2/21/25
to Google Ads Scripts Forum

Hi Alex,

Thank you for reaching out to the Google Ads Scripts support team.

I would suggest that you use the method “AdsApp.campaigns().getStatsFor("TODAY").getCost()” to retrieve Search & Display campaign budgets. recommend that you use the sample codes available in the Budgets documentation I would recommend that you use AdsApp.shoppingCampaigns(), AdsApp.performanceMaxCampaigns(), and AdsApp.videoCampaigns() methods in place of ‘AdsApp.campaigns()’ to get budgets of Shopping, PMax, and Video campaigns. Write an ‘if’ condition to check if the campaign budget exceeds the limit or not and if the condition becomes true, write code to send email by referring to the code available in the MailApp. Schedule the script frequency to hourly so that the script will execute hourly.

I hope this helps! Feel free to get back to us if you still face any issues.

Thanks,
Google Ads Scripts team

Nils Rooijmans

unread,
Feb 22, 2025, 3:22:45 AM2/22/25
to Google Ads Scripts Forum
This script logic is relatively simple, and a great example of the type of scripts that can easily be created by the latest AI'LLMs like ChatGPT/Gemini/Claude/Grok.

Here's a prompt you can use to get the result:

'
You are an experienced senior Google Ads Specialist with excellent Google Ads Script development skills. I need you to create a Google Ads Script that runs hourly and sends me an email alert when one or more of the enabled campaigns in my account has a total cost for today that is equal to or higher then the daily budget of the campaign.  
'

Hope this helps,

Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Google Ads Scripts Forum Advisor

unread,
Feb 23, 2025, 11:45:45 PM2/23/25
to adwords...@googlegroups.com
Hi,

@Nils, thank you for the insights on this issue.

I would recommend you to follow the provided solutions by Nils and use the predefined methods suggested by the Google Ads Scripts team in the earlier response. Feel free to get back to us in case of any further issue with the below details:
  • Google Ads account ID/CID
  • Name of the script
You can share the requested details via Reply privately to the author option or a direct private reply to this email.
 

Thanks,
 
Google Logo Google Ads Scripts Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5
[2025-02-24 04:45:13Z GMT] This message is in relation to case "ref:!00D1U01174p.!5004Q02vH3Ej:ref" (ADR-00289276)



Alejandro Perez

unread,
Feb 24, 2025, 6:02:17 PM2/24/25
to Google Ads Scripts Forum
Chat GPT generated the script, but Google Ads is showing this error.

Any ideas how could I fix the error?

Thanks!


TypeError: campaign.getType is not a function at main (Code:22:18) at Object.<anonymous> (adsapp_compiled:20555:54)

---

function main() {
  // Get the current date in the required format (yyyyMMdd)
  var date = new Date();
  var startDate = Utilities.formatDate(date, AdsApp.currentAccount().getTimeZone(), "yyyyMMdd");
 
  // Email to send alert
  var emailAddress = 'your-...@example.com'; // Update with your email address
 
  // Get all enabled campaigns (Search, Display, Performance Max, Shopping, and Video)
  var campaignIterator = AdsApp.campaigns()
                                .withCondition("Status = 'ENABLED'")
                                .get();
 
  // Array to store alerts
  var alerts = [];
 
  // Loop through the campaigns
  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
   
    // Check the type of campaign and call the respective check function
    if (campaign.getType() === "PERFORMANCE_MAX") {
      checkPMaxCampaign(campaign, startDate, alerts);
    } else if (campaign.getType() === "SHOPPING") {
      checkShoppingCampaign(campaign, startDate, alerts);
    } else if (campaign.getType() === "VIDEO") {
      checkVideoCampaign(campaign, startDate, alerts);
    } else {
      checkStandardCampaign(campaign, startDate, alerts);
    }
  }
 
  // Send email if there are any alerts
  if (alerts.length > 0) {
    var subject = "Google Ads Campaign Daily Budget Alert";
    var body = "The following campaigns have reached or exceeded their daily budgets:\n\n" + alerts.join("\n\n");
    MailApp.sendEmail(emailAddress, subject, body);
  }
}

// Function to check standard Search/Display campaigns
function checkStandardCampaign(campaign, startDate, alerts) {
  var dailyBudget = campaign.getBudget().getAmount();
  var stats = campaign.getStatsFor(startDate, startDate);
  var totalCost = stats.getCost();
 
  if (totalCost >= dailyBudget) {
    alerts.push("Campaign '" + campaign.getName() + "' (Type: " + campaign.getType() + ") has reached or exceeded its daily budget. Total Cost: $" + totalCost.toFixed(2) + ", Daily Budget: $" + dailyBudget.toFixed(2));
  }
}

// Function to check Performance Max (PMax) campaigns
function checkPMaxCampaign(campaign, startDate, alerts) {
  // Get the campaign's daily budget
  var dailyBudget = campaign.getBudget().getAmount();
 
  // Fetch the campaign's stats for today (using stats for the last 24 hours)
  var stats = campaign.getStatsFor("TODAY", "TODAY");
  var totalCost = stats.getCost();
 
  if (totalCost >= dailyBudget) {
    alerts.push("Campaign '" + campaign.getName() + "' (Type: Performance Max) has reached or exceeded its daily budget. Total Cost: $" + totalCost.toFixed(2) + ", Daily Budget: $" + dailyBudget.toFixed(2));
  }
}

// Function to check Shopping campaigns
function checkShoppingCampaign(campaign, startDate, alerts) {
  var dailyBudget = campaign.getBudget().getAmount();
  var stats = campaign.getStatsFor(startDate, startDate);
  var totalCost = stats.getCost();
 
  if (totalCost >= dailyBudget) {
    alerts.push("Campaign '" + campaign.getName() + "' (Type: Shopping) has reached or exceeded its daily budget. Total Cost: $" + totalCost.toFixed(2) + ", Daily Budget: $" + dailyBudget.toFixed(2));
  }
}

// Function to check Video campaigns
function checkVideoCampaign(campaign, startDate, alerts) {
  var dailyBudget = campaign.getBudget().getAmount();
  var stats = campaign.getStatsFor(startDate, startDate);
  var totalCost = stats.getCost();
 
  if (totalCost >= dailyBudget) {
    alerts.push("Campaign '" + campaign.getName() + "' (Type: Video) has reached or exceeded its daily budget. Total Cost: $" + totalCost.toFixed(2) + ", Daily Budget: $" + dailyBudget.toFixed(2));
  }
}

Google Ads Scripts Forum

unread,
Feb 24, 2025, 6:06:58 PM2/24/25
to Google Ads Scripts Forum
Hi,


Thank you for reaching out to the Google Ads Scripts support team.

Please be informed that the method ‘AdsApp.campaigns()’ will only iterate through Search & Display campaigns, the method 'campaigns.getType' is not a valid method in Google Ads Scripts. To iterate through all types of campaigns, I would suggest that you use the selectors Video, Shopping, and PerformanceMax campaigns. Also, I would recommend that you refer to the Budgets, and AdsApp.​Budget document for more information.


Thanks,
Google Ads Scripts team

Reply all
Reply to author
Forward
0 new messages