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

35 views
Skip to first unread message

Alexandre Severo

unread,
Aug 30, 2023, 2:53:34 PM8/30/23
to Google Ads API and AdWords API Forum
Hi, I have the below script for Google Ads. The problem is, every time the spending is greater or equal 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 on 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 API Forum Advisor

unread,
Aug 30, 2023, 5:59:05 PM8/30/23
to alex...@blueprintim.com, adwor...@googlegroups.com
Hi Alexandre,

Thank you for reaching out to the Google Ads API Forum. I hope that you are doing well today.

I can see that your concern is related to Google Ads Scripts. Please note that our team only provides support for the implementation and guidance of Google Ads API. With that being said, what we can do is to advise you to reach out the Google Ads Scripts Forum as they are more equipped with regards to Google Ads Scripts related concerns.
 
This message is in relation to case "ref:_00D1U1174p._5004Q2o9ab3:ref"

Thanks,
 
Google Logo Google Ads API Team


Reply all
Reply to author
Forward
0 new messages