-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
78 lines (72 loc) · 3.66 KB
/
main.js
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
// Configurations
let config = {
impressionThreshold: '5', // Change Impr. Threshold
emailAddresses: "Enter your email address here", // Separate email addresses with a comma
emailMessage: '' // Do not add anything here
};
function main() {
Logger.log('Starting the script...');
// Select Account
let accountName = AdsApp.currentAccount().getName();
Logger.log('Account Name: ' + accountName);
let accountId = AdsApp.currentAccount().getCustomerId();
Logger.log('Account ID: ' + accountId);
// Set the email subject dynamically
config.emailSubject = "Daily Campaign Visibility Checks for: " + accountName + " - ID: " + accountId;
// Initialize the email message
config.emailMessage = `
<html>
<body>
<h2>Daily Campaign Visibility Checks</h2>
<p>The following campaigns have received low impressions below the threshold of <b>${config.impressionThreshold}</b> impressions:</p>
<table style="border-collapse: collapse; width: 100%; font-size: 12px;">
<tr>
<th style="border: 1px solid #ddd; padding: 4px; text-align: center; background-color: #f2f2f2; width: 15%;">Engine</th>
<th style="border: 1px solid #ddd; padding: 4px; text-align: center; background-color: #f2f2f2; width: 25%;">Account ID</th>
<th style="border: 1px solid #ddd; padding: 4px; text-align: center; background-color: #f2f2f2; width: 25%;">Account Name</th>
<th style="border: 1px solid #ddd; padding: 4px; text-align: center; background-color: #f2f2f2; width: 40%;">Campaign Name</th>
<th style="border: 1px solid #ddd; padding: 4px; text-align: center; background-color: #f2f2f2; width: 20%;">Impressions</th>
</tr>`;
// Select Campaigns
let campaignIterator = AdsApp.campaigns().get();
let foundLowImpressionCampaign = false;
// Iterate over campaigns to search for impressions below set threshold
while (campaignIterator.hasNext()) {
let campaign = campaignIterator.next();
// Check if the campaign is enabled
if (!campaign.isEnabled()) {
continue;
}
let stats = campaign.getStatsFor('TODAY');
let impressions = stats.getImpressions();
if (impressions <= config.impressionThreshold) {
Logger.log('Campaign: ' + campaign.getName() + ' has low impressions: ' + impressions);
config.emailMessage += `
<tr style="background-color: #f8d7da;">
<td style="border: 1px solid #ddd; padding: 4px; text-align: center;">Google Ads</td>
<td style="border: 1px solid #ddd; padding: 4px; text-align: center;">${accountId}</td>
<td style="border: 1px solid #ddd; padding: 4px; text-align: center;">${accountName}</td>
<td style="border: 1px solid #ddd; padding: 4px; text-align: center;">${campaign.getName()}</td>
<td style="border: 1px solid #ddd; padding: 4px;text-align: center;">${impressions}</td>
</tr>`;
foundLowImpressionCampaign = true;
}
}
// Close the table and body tags
config.emailMessage += `
</table>
</body>
</html>`;
// Send the email alert
if (foundLowImpressionCampaign) {
MailApp.sendEmail({
to: config.emailAddresses,
subject: config.emailSubject,
htmlBody: config.emailMessage
});
Logger.log('Sent email alert to ' + config.emailAddresses);
} else {
Logger.log('No campaigns with low impressions found.');
}
Logger.log('Script completed.');
}