-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathad-label-scheduler.js
More file actions
343 lines (286 loc) · 8.23 KB
/
ad-label-scheduler.js
File metadata and controls
343 lines (286 loc) · 8.23 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* @name Ad Label Scheduler
*
* @overview This scripts enables/disables ads with assigned labels based on the specific start/end dates.
* Updates are reflected on the Google Sheet and changes are notified on the Slack channel.
*
* @author Said Tezel [saidtezel@gmail.com]
*
* @version 1.0
*
**/
var CONFIG = {
// Copy the template here https://bit.ly/2LRuDa3 and change the spreadsheet URL below
SPREADSHEET_URL: 'SPREADSHEET_URL',
// Slack webhook endpoint to talk to the #ppc channel
SLACK_ENDPOINT: 'SLACK_ENDPOINT',
// Set true if you want to send Slack notifications for recent changes.
UPDATE_SLACK: true,
};
var REPORTING_OPTIONS = {
apiVersion: 'v201809'
};
function main() {
var ss = initialiseSpreadsheet();
var timezone = ss.getSpreadsheetTimeZone();
var range = ss.getRangeByName('dataRange');
var scanQueue = {};
var recentChanges = [];
var rangeVal = range.getValues();
// Grouping the executions for each account ID.
for (var k = 0; k < rangeVal.length; k++) {
var rowVal = rangeVal[k];
if (rowVal[0] == '' || rowVal[1] == '') {
continue
};
var accountId = rowVal[1];
if (!scanQueue[accountId]) {
scanQueue[accountId] = [];
}
scanQueue[accountId].push({
label: rowVal[0],
start: new Date(rowVal[3]),
end: rowVal[4] == '' ? new Date('2099-01-01') : new Date(rowVal[4]),
lastStatus: rowVal[5],
lastUpdated: rowVal[6],
rowIndex: k + 1
});
}
var accountIds = Object.keys(scanQueue);
for (var j = 0; j < accountIds.length; j++) {
var accountId = accountIds[j];
var account = getAccountWithId(accountId);
var accountName = account.getName();
AdsManagerApp.select(account);
Logger.log('Processing label updates for ' + accountName);
var labels = scanQueue[accountId];
for (var m = 0; m < labels.length; m++) {
var labelData = labels[m];
Logger.log('***');
Logger.log('Processing label ' + labelData.label);
var labelDataIsValid = validateLabelData(labelData);
var labelNeedsUpdate = shouldLabelUpdate(labelData);
if (labelDataIsValid && labelNeedsUpdate) {
Logger.log('Status change detected. Updating ads...');
var updateStatus = processLabelUpdates(labelData);
range.getCell(labelData.rowIndex, 6).setValue(updateStatus);
range.getCell(labelData.rowIndex, 7).setValue(Utilities.formatDate(new Date(), timezone, 'YYYY-MM-dd HH:mm'))
Logger.log('Successfully changed the status of ads to ' + updateStatus);
recentChanges.push({
label: labelData.label,
accountName: accountName,
status: updateStatus
});
}
}
}
var updateFinishTime = new Date();
var updateFinishTimeLocal = Utilities.formatDate(updateFinishTime, timezone, 'YYYY-MM-dd HH:mm');
var updateRange = ss.getRangeByName('updateDate');
updateRange.setValue(updateFinishTimeLocal);
if (recentChanges.length > 0 && CONFIG.UPDATE_SLACK) {
sendSlackUpdate(recentChanges, updateFinishTimeLocal);
}
}
function processLabelUpdates(labelData) {
var status;
var start = labelData.start;
var end = labelData.end;
var now = new Date();
var label = labelData.label
if (now >= start && now < end) {
status = 'ACTIVE';
} else if (now < start) {
status = 'QUEUED';
} else {
status = 'INACTIVE';
}
var ads = getAdsWithLabel(label);
while (ads.hasNext()) {
var ad = ads.next();
if (status == 'ACTIVE') {
ad.enable();
} else {
ad.pause()
}
};
return status;
}
function shouldLabelUpdate(labelData) {
var now = new Date();
var lastStatus = labelData.lastStatus;
var currStatus;
if (now >= labelData.start && now < labelData.end) {
currStatus = 'ACTIVE'
} else if (now < labelData.start) {
currStatus = 'QUEUED'
} else {
currStatus = 'INACTIVE'
}
Logger.log('Last known status ' + lastStatus);
Logger.log('Current status ' + currStatus);
return !(currStatus == lastStatus);
}
function validateLabelData(labelData) {
var label = labelData.label;
if (!checkIfLabelExists(label)) {
Logger.log('Error: Label doesn\'n exist.');
return false
}
if (isNaN(labelData.start) || isNaN(labelData.end) || labelData.end <= labelData.start) {
Logger.log('Error: Input dates are invalid.');
return false
}
return true;
}
/**
* Initialises the spreadsheet from the config URL.
*
* @return {Object} Spreadsheet instance
**/
function initialiseSpreadsheet() {
var ss = SpreadsheetApp.openByUrl(CONFIG.SPREADSHEET_URL);
return ss;
}
/**
* Checks if the current timestamp is within the date range.
*
* @param {string} activation date on row
* @param {string} deactivation date on row
* @return {boolean} true if date is in range
*/
function checkIfDateInRange(start, end) {
var now = new Date()
var startDate = new Date(start);
var endDate = new Date(end);
if (now > startDate && (end === '' || now < endDate)) {
return true
} else {
return false
}
}
/**
* Checks if the label assigned exists within the ad accont.
*
* @param {string} label name
* @return {boolean} true if label exists
*/
function checkIfLabelExists(label) {
var labelSelector = AdsApp.labels()
.withCondition("Name CONTAINS '" + label + "'")
.get()
if (!labelSelector.hasNext()) {
return false
}
return true
}
/**
* Finds and returns the ad account with ID.
*
* @param {string} account ID
* @return {Object} ad account
*/
function getAccountWithId(id) {
var accountSelector = AdsManagerApp.accounts()
.withIds([id])
.get();
if (accountSelector.hasNext()) {
return accountSelector.next();
}
return null
}
/**
* Finds and returns an ad selector with a specific label
*
* @param {string} label name
* @return {Object} ad selector
*/
function getAdsWithLabel(label) {
var adSelector = AdsApp.ads()
.withCondition("LabelNames CONTAINS_ANY ['" + label + "']")
.get()
if (!adSelector.hasNext()) {
return null;
}
return adSelector;
}
/**
* Sends a notification to the configured Slack webhook endpoint
* with the latest changes on account ads
*
* @param {Array.<Object>} An object array of latest changes on accounts.
**/
function sendSlackUpdate(results, updateTime) {
var slackMessage = {
blocks: [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "I've just mades some changes on your ad accounts based on ad labels."
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*<https://docs.google.com/spreadsheets/d/1B4xrSdDQH5Wcblc_UwBHEQ-Aty9niavwPfe7K_4ct4M/edit#gid=0|Time Based Ad Copy Activator - Google Sheets>*\nActivate/deactivate ads on your accounts based on date ranges."
},
"accessory": {
"type": "image",
"image_url": "https://api.slack.com/img/blocks/bkb_template_images/approvalsNewDevice.png",
"alt_text": "computer icon"
}
},
{
"type": "context",
"elements": [
{
"type": "image",
"image_url": "https://api.slack.com/img/blocks/bkb_template_images/notificationsWarningIcon.png",
"alt_text": "notifications warning icon"
},
{
"type": "mrkdwn",
"text": "*Last scan completed on " + updateTime + "*"
}
]
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Here are the latest changes:*"
}
},
]
};
for (var j = 0; j < results.length; j++) {
slackMessage.blocks.push({
type: 'section',
text: {
type: 'mrkdwn',
text: "*" + results[j].accountName + "*\nStatus of ads with _" + results[j].label + "_ label changed to *" + results[j].status.toLowerCase() + "*."
}
});
};
slackMessage.blocks.push({
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*<https://docs.google.com/spreadsheets/d/1B4xrSdDQH5Wcblc_UwBHEQ-Aty9niavwPfe7K_4ct4M/edit#gid=0|See all changes...>*"
}
});
var options = {
method: 'POST',
contentType: 'application/json',
payload: JSON.stringify(slackMessage)
};
UrlFetchApp.fetch(CONFIG.SLACK_ENDPOINT, options);
}