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 = ["[email protected]"]; // 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
});
}
}
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"Google Ads API and AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/f52b13f5-b6a1-43c5-9884-851432f37584n%40googlegroups.com.