Budget verification + sending email alert - How to send email JUST ONCE?

67 views
Skip to first unread message

Alexandre Severo

unread,
Sep 13, 2023, 9:43:15 AM9/13/23
to Google Ads Scripts Forum
Hi, I have the below script for Google Ads. The problem is, that every time the spending is greater or equal to the threshold of 90%, it will send an email.

In other words, I will get many emails. I do not want it. I just want to receive the email once.

As it will analyze the data monthly, I just want to receive an email alert per month.

What should I do? Any other help is welcome.

Google Ads Script:

function main() {
  var keywordsToMatch = ["KW1", "KW2"]; // Replace with the keywords you want to match
  var today = new Date();
  var startDate = new Date(today.getFullYear(), today.getMonth(), 1);
  var endDate = new Date(today.getFullYear(), today.getMonth() + 1, 0);
  var initialTotalBudget = 10000; // Initial total budget in account's currency
  var budgetThreshold = 0.9; // 90% budget threshold
 
  var totalSpending = 0;
 
  var campaignIterator = AdsApp.campaigns().get();

  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var campaignName = campaign.getName();
   
    var containsAllKeywords = keywordsToMatch.every(function(keyword) {
      return campaignName.indexOf(keyword) !== -1;
    });
   
    if (containsAllKeywords) {
      var stats = campaign.getStatsFor(formatDate(startDate), formatDate(endDate));
      var spentAmount = stats.getCost();
     
      totalSpending += spentAmount;
    }
  }
 
  Logger.log("Total spending for campaigns between " + formatDate(startDate) + " and " + formatDate(endDate) + " with keywords [" + keywordsToMatch.join(", ") + "]: " + totalSpending.toFixed(2) + " " + AdsApp.currentAccount().getCurrencyCode());

  var budgetUtilization = (totalSpending / initialTotalBudget) * 100;
 
  if (budgetUtilization >= budgetThreshold * 100) {
    var recipientEmails = ["mye...@mydomain.com"]; // List of email addresses
    var subject = "Budget Alert from Google Ads Script";
    var message = "HI XXX ... or exceeded 90% of the budget.\n- Planned budget: " + formatCurrency(initialTotalBudget.toFixed(2)) + " " + AdsApp.currentAccount().getCurrencyCode() + "\n- Total spending: " + formatCurrency(totalSpending.toFixed(2)) + " " + AdsApp.currentAccount().getCurrencyCode() + "\n- Budget utilization: " + budgetUtilization.toFixed(2) + "%\n- Date range: " + formatDate(startDate, true) + " to " + formatDate(endDate, true) + "\n\nThis is an automated message from Google Ads.";
   
    sendEmail(recipientEmails, subject, message);
  }
}

function formatDate(date, includeDashes) {
  var year = date.getFullYear();
  var month = (date.getMonth() + 1).toString().padStart(2, '0');
  var day = date.getDate().toString().padStart(2, '0');
  return includeDashes ? year + "-" + month + "-" + day : year + month + day;
}

function formatCurrency(amount) {
  return "$" + parseFloat(amount).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 });
}

function sendEmail(recipientEmails, subject, message) {
  for (var i = 0; i < recipientEmails.length; i++) {
    var recipientEmail = recipientEmails[i];
   
    Logger.log("Sending email to: " + recipientEmail);
   
    MailApp.sendEmail({
      to: recipientEmail,
      subject: subject,
      body: message
    });
  }
}

Google Ads Scripts Forum Advisor

unread,
Sep 14, 2023, 4:12:52 AM9/14/23
to adwords...@googlegroups.com
Hi,

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

After reviewing your concern, I understand that you want to get an email alert on the spending threshold, once a month. I would like to suggest you refer to the Dates and Times documentation to create a date object with a specified timezone. You can define the date object before the email sent function and add the date condition with “&& Operator” in the “if (budgetUtilization >= budgetThreshold * 100)” condition to satisfy both the conditions. You can also make use of different time zones in date object methods.
 
This message is in relation to case "ref:_00D1U1174p._5004Q2okwFB:ref"

Thanks,
 
Google Logo Google Ads Scripts Team


Reply all
Reply to author
Forward
0 new messages