-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapacity-controller.js
More file actions
177 lines (148 loc) · 7.1 KB
/
capacity-controller.js
File metadata and controls
177 lines (148 loc) · 7.1 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// ID: 43a5209fd8bde1cd70c49da8bc34729c
/**
*
* Capacity Controller pauses and labels all enabled campaigns
* in your account when conversions in a day exceed a defined limit
* Campaigns can then be re-enabled at midnight
* Must be scheduled to run hourly
*
* Version: 1.0
* Google AdWords Script maintained by brainlabsdigital.com
*
*/
// INPUTS
var CONVERSION_LIMIT_INCLUSIVE = 37; // Pause account if conversions are greater than or equal to this
var LABEL_NAME = "Paused Due To Near Conversion Limit"; // This label MUST already exist in account or script will error
var DRY_RUN = true; // If true the script will send emails but not pause the account, if false the account will be paused
var SEND_EMAILS = true; // Whether or not you want to receive emails when changes are made
var EMAIL_RECIPIENTS = "example@domain.com"; // People to receive emails (comma separated list)
var ENABLE_CAMPAIGNS = true; // Whether you want the script to re-enable campaigns just after midnight
// DON'T MODIFY ANYTHING BELOW THIS LINE
//--------------------------------------
function main() {
if (DRY_RUN == true) {
Logger.log("Dry run mode enabled");
}
var timezone = AdsApp.currentAccount().getTimeZone();
Logger.log("Account timezone: " + timezone);
var hour = Utilities.formatDate(new Date, timezone, "HH");
Logger.log("Current account hour: " + hour);
if (hour == "00" && ENABLE_CAMPAIGNS == true) {
try {
enableCampaigns();
}
catch (e) {
var accountName = AdsApp.currentAccount().getName();
Logger.log("ERROR: " + e);
MailApp.sendEmail(EMAIL_RECIPIENTS, "ERROR | Conversion Threshold Script", "There has been the following error in the script to pause/enable activity in the account " + accountName + " when conversions exceed a set limit:\n\n" + e);
}
}
else if (hour != "00") {
try {
pauseCampaignsIfOverLimit();
}
catch (e) {
var accountName = AdsApp.currentAccount().getName();
Logger.log("ERROR: " + e);
MailApp.sendEmail(EMAIL_RECIPIENTS, "ERROR | Conversion Threshold Script", "There has been the following error in the script to pause/enable activity in the account " + accountName + " when conversions exceed a set limit:\n\n" + e);
}
}
}
// Pause and label active campaigns if required
function pauseCampaignsIfOverLimit() {
Logger.log("Conversion limit: " + CONVERSION_LIMIT_INCLUSIVE);
var todayConversions = getTodayConversions();
Logger.log("Today's Conversions so far: " + todayConversions);
if (todayConversions >= CONVERSION_LIMIT_INCLUSIVE) {
Logger.log("Number of conversions today greater than or equal to limit.");
var notAlreadyPaused = pauseAndLabelCampaigns(LABEL_NAME, DRY_RUN);
if (SEND_EMAILS == true && notAlreadyPaused == true) {
var accountName = AdsApp.currentAccount().getName();
var normalBody = "<HTML><BODY>Hi all, <br>" +
"<br>" +
"The account <b>" + accountName + "</b> has been paused as the number of conversions in the account today is <b>" + todayConversions + "</b>, which is above the defined limit of " + CONVERSION_LIMIT_INCLUSIVE + ". " +
"Paused campaigns have been labelled as <b>" + LABEL_NAME + "</b><br>" +
"<br>" +
"Thanks.</BODY></HTML>";
var dryRunBody = "<HTML><BODY>Hi all, <br>" +
"<br>" +
"The account <b>" + accountName + "</b> WOULD HAVE been paused due as the number of conversions in the account today is <b>" + todayConversions + "</b>, which is above the defined limit of " + CONVERSION_LIMIT_INCLUSIVE + ". " +
"Campaigns have not been paused as the script is set to dry run.<br>" +
"<br>" +
"Thanks.</BODY></HTML>";
if (DRY_RUN == true) {
var dryRunSubject = " Dry Run | " + accountName + " Would Be Paused";
MailApp.sendEmail({ to: EMAIL_RECIPIENTS, subject: dryRunSubject, htmlBody: dryRunBody });
}
else if (DRY_RUN == false) {
var normalSubject = accountName + " Paused";
MailApp.sendEmail({ to: EMAIL_RECIPIENTS, subject: normalSubject, htmlBody: normalBody });
}
}
}
}
// Get conversions in account so far today
function getTodayConversions() {
var accountTimeZone = AdsApp.currentAccount().getTimeZone();
var dateToday = Utilities.formatDate(new Date(), accountTimeZone, "yyyy-MM-dd");
Logger.log("Today's date (account timezone): " + dateToday);
var report = AdsApp.report("SELECT Conversions FROM ACCOUNT_PERFORMANCE_REPORT DURING TODAY");
var rows = report.rows();
//report only has one row
var row = rows.next();
var todayConversions = row["Conversions"];
var convAsNumber = Number(todayConversions.replace(",", ""));
return convAsNumber;
}
// Pause and label enabled campaigns in account
// Will not make changes if isDryRun = true
// Returns whether or not there are any enabled campaigns to pause
function pauseAndLabelCampaigns(labelName, isDryRun) {
var enabledCampaignsExist = false;
var enabledCampaignsSelector = AdsApp.campaigns().withCondition("CampaignStatus = 'ENABLED'");
var enabledCampaignsIterator = enabledCampaignsSelector.get();
while (enabledCampaignsIterator.hasNext()) {
enabledCampaignsExist = true;
var campaign = enabledCampaignsIterator.next();
var name = campaign.getName();
if (isDryRun == true) {
Logger.log("DRY RUN. Would pause and label: " + name);
}
else if (isDryRun == false) {
Logger.log("Pausing and labelling: " + name);
campaign.pause();
campaign.applyLabel(labelName);
}
}
return enabledCampaignsExist;
}
function enableCampaigns() {
var wasEnabled = enablePausedCampaignsWithLabel(LABEL_NAME);
var accountName = AdsApp.currentAccount().getName();
if (SEND_EMAILS == true && wasEnabled == true) {
MailApp.sendEmail(EMAIL_RECIPIENTS, accountName + " Enabled", "Hi all,\n\nThe campaigns in the account " + accountName + " which were previously paused due to hitting the account conversion limit have now been re-enabled");
}
}
// Get selector of campaigns with label
function getCampaignsWithLabel(labelName) {
var labelSelector = AdsApp.labels().withCondition("LabelName = '" + labelName + "'");
var labelIterator = labelSelector.get();
var labelledCampaignsSelector = labelIterator.next().campaigns();
return labelledCampaignsSelector;
}
// Enable paused campaigns with label, and remove label
function enablePausedCampaignsWithLabel(labelName) {
var campaignSelector = getCampaignsWithLabel(labelName).withCondition("CampaignStatus = PAUSED");
var campaignIterator = campaignSelector.get();
var somethingChanged = false;
Logger.log("Enabling and removing labels from the following campaign(s):");
while (campaignIterator.hasNext()) {
var currentCampaign = campaignIterator.next();
currentCampaign.removeLabel(labelName);
currentCampaign.enable();
Logger.log(currentCampaign.getName());
somethingChanged = true;
}
if (somethingChanged == false) { Logger.log("No campaigns to enable."); }
return somethingChanged;
}