Budget verification + sending email alert - How to send email just once?

28 Views Asked by At

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. Thank you!

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 = ["someemailaddress"]; // 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
    });
  }
}

I am trying to get an alert about my budget spending in Google Ads. The problem is - I am getting an email every time because the spending already met the condition but I just want to receive the email once.

0

There are 0 best solutions below