-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.gs
More file actions
78 lines (63 loc) · 2.95 KB
/
script.gs
File metadata and controls
78 lines (63 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function main() {
// ===== CONFIGURATION =====
var threshold = 0.30; // 30% increase threshold
var emailRecipient = "your.email@example.com, anothermail@example.com"; // Replace with your email
// ===== DATE CALCULATIONS =====
// Get the current account time zone for proper date formatting.
var timeZone = AdsApp.currentAccount().getTimeZone();
// Calculate dates: Yesterday and the day before yesterday
var today = new Date();
// Yesterday:
var yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
var yesterdayStr = Utilities.formatDate(yesterday, timeZone, "yyyyMMdd");
// Day before yesterday:
var dayBeforeYesterday = new Date(today);
dayBeforeYesterday.setDate(today.getDate() - 2);
var dayBeforeYesterdayStr = Utilities.formatDate(dayBeforeYesterday, timeZone, "yyyyMMdd");
// ===== PROCESS CAMPAIGNS =====
var anomaliesFound = [];
var campaignIterator = AdsApp.campaigns().get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignName = campaign.getName();
// Retrieve spend statistics for the two days
var statsYesterday = campaign.getStatsFor(yesterdayStr, yesterdayStr);
var statsDayBefore = campaign.getStatsFor(dayBeforeYesterdayStr, dayBeforeYesterdayStr);
var costYesterday = statsYesterday.getCost();
var costDayBefore = statsDayBefore.getCost();
// Only check if there was spend on the day before yesterday to avoid division by zero.
if (costDayBefore > 0) {
// Calculate the percentage change in spend.
var percentageChange = (costYesterday - costDayBefore) / costDayBefore;
// Check if the increase is at least the threshold (30%).
if (percentageChange >= threshold) {
anomaliesFound.push({
campaignName: campaignName,
costDayBefore: costDayBefore,
costYesterday: costYesterday,
percentageChange: percentageChange
});
}
}
}
// ===== SEND EMAIL IF ANY ANOMALIES DETECTED =====
if (anomaliesFound.length > 0) {
// Get the account name
var accountName = AdsApp.currentAccount().getName();
// Include account name in the subject
var subject = "Google Ads (" + accountName + ") Spend Anomaly Alert";
var body = "The following campaigns have exceeded the 30% spend increase threshold:\n\n";
for (var i = 0; i < anomaliesFound.length; i++) {
var anomaly = anomaliesFound[i];
body += "Campaign: " + anomaly.campaignName + "\n" +
"Spend on " + dayBeforeYesterdayStr + ": $" + anomaly.costDayBefore.toFixed(2) + "\n" +
"Spend on " + yesterdayStr + ": $" + anomaly.costYesterday.toFixed(2) + "\n" +
"Increase: " + (anomaly.percentageChange * 100).toFixed(2) + "%\n\n";
}
MailApp.sendEmail(emailRecipient, subject, body);
Logger.log("Alert email sent to " + emailRecipient);
} else {
Logger.log("No anomalies found.");
}
}