-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
268 lines (219 loc) · 6.97 KB
/
script.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
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
/************************/
/***** URL CHECKING *****/
/************************/
/**
* The response codes of the target url that will cause to ad pausing
*/
var BAD_RESPONSE_CODES = [404, 301];
/**
* How may times to perform a request in the target url if it keeps failing
*/
var NUM_OF_TRIES = 4;
/**
* Pause time between the retries
*/
var SLEEP_TIME_FOR_RETRY = 250;
/**
* If the script has to follow any redirect of the target url if it exists
*/
var FOLLOW_REDIRECTS = false;
/**
* Filter the ads to be check, by providing a url' part to be checked against ad's target url
*/
var AD_URL_FILTER = "/frontend/deals/view/";
/************************/
/****** SPREADSHEET *****/
/************************/
/**
* Google spreadsheet url to keep the logs
*/
var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/1nSZ00U-S9YOnxjqZKOSTrPIIBu3esxcLcdIfe1EXWwE/edit?usp=sharing';
/**
* If logging to shreadsheet is enabled or not
*/
var SAVE_RESULTS_TO_SPREADSHEET = true;
/**
* The name of the spreadsheet tab where the results will be logging
*/
var RESULTS_SHEET_NAME = 'results';
/**
* The name of the spreadsheet tab where the general logs will be kept
*/
var LOGS_SHEET_NAME = 'logs';
/************************/
/********* EMAIL ********/
/************************/
/**
* If an email will be send in case the script perform any action
*/
var SEND_EMAIL = true;
/**
* Email recipients
*/
var RECIPIENTS = ['[email protected]'];
/**
* The subject of the email
*/
var SUBJECT = "Google Ads // Paused ads";
/************************/
/***** RUNTIME VARS *****/
/************************/
/**
* If the dry run of the script is enabled. If that's the case, the script will not submit any action.
*/
var DRY_RUN = true;
/**
* An identifier for each script execution
*/
var TASK_ID = '#PAUSE-' + new Date().toISOString().slice(0, 19).replace('T', ' ');
/**
* The label to apply on each ad that get paused
*/
var LABEL_TO_APPLY = 'AUTO PAUSED AD';
/**
* An array to keep the results
*/
var RESULTS = [];
/**
* Main fn
*/
function main() {
log('Start Execution', true);
checkLabel();
// Modify below to get only the ads you want
var AdsIterator = AdWordsApp.ads()
.withCondition("CreativeFinalUrls CONTAINS '"+AD_URL_FILTER+"'")
.withCondition("Status = ENABLED")
.withCondition("AdGroupStatus = ENABLED")
.withCondition("CampaignStatus = ENABLED")
.get();
if (AdsIterator.totalNumEntities() === 0) {
log('No ads found', true);
} else {
var totalAds = AdsIterator.totalNumEntities();
var counter = 0;
log('Found ' + totalAds + ' ads', true);
while (AdsIterator.hasNext()) {
counter++;
var Ad = AdsIterator.next();
var responseCode = requestUrl(Ad.urls().getFinalUrl());
if (BAD_RESPONSE_CODES.indexOf(responseCode) != -1) {
var action = 'PAUSE';
if (!DRY_RUN) {
Ad.applyLabel(LABEL_TO_APPLY);
Ad.pause();
}
} else {
var action = '-';
}
var logItem = {
"id": Ad.getId(),
"campaign": Ad.getCampaign().getName(),
"group": Ad.getAdGroup().getName(),
"url": Ad.urls().getFinalUrl(),
"response": responseCode,
"action": action
};
log(counter + '/' + totalAds + ' [' + logItem.id + '] [' + logItem.campaign + '] [' + logItem.group + '] [' + logItem.url + '] [' + logItem.response + '] [' + logItem.action + ']', false);
RESULTS.push(logItem);
}
if (SAVE_RESULTS_TO_SPREADSHEET)
saveResultsToSpreadsheet(RESULTS);
}
var affectedEntries = RESULTS.filter(function (el) {
return el.action != '-';
});
if (SEND_EMAIL && affectedEntries.length > 0) {
log("Remaining email quota: " + MailApp.getRemainingDailyQuota(), true);
MailApp.sendEmail({
to: RECIPIENTS.join(","),
subject: SUBJECT,
htmlBody: '<pre>' + JSON.stringify(affectedEntries, null, 2) + '</pre>'
});
}
log('End Execution', true);
}
/**
* Saves results to the spreadsheet
* @todo add a check for the spreadsheet size to avoid row limits
* @param {array} results
*/
function saveResultsToSpreadsheet(results) {
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = spreadsheet.getSheetByName(RESULTS_SHEET_NAME);
sheet.appendRow(['-']);
for (var i = 0; i < results.length; i++) {
sheet.appendRow([
TASK_ID,
results[i].id,
results[i].campaign,
results[i].group,
results[i].url,
results[i].response,
results[i].action
]);
}
}
/**
* Logs a message to script' logs and to a spreadsheet
*
* @param {string} log A message to log
* @param {boolean} toSpreadsheet if it will be logged to the spreadsheet too
*/
function log(log, toSpreadsheet) {
Logger.log(log);
if (toSpreadsheet) {
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = spreadsheet.getSheetByName(LOGS_SHEET_NAME);
var date = new Date().toISOString().slice(0, 19).replace('T', ' ');
sheet.appendRow([TASK_ID, date, log]);
}
}
/**
* Checks if there is a label and if not, creates it.
*/
function checkLabel() {
var labelIterator = AdsApp.labels().withCondition("Name CONTAINS '" + LABEL_TO_APPLY + "'").get();
if (labelIterator.hasNext()) {
log("Label '" + LABEL_TO_APPLY + "' exists", false);
} else {
AdsApp.createLabel(LABEL_TO_APPLY);
log("Label '" + LABEL_TO_APPLY + "' has been created", true);
}
}
/**
* Performs a http request
* @param {string} url The target url
* @returns {number} The response code
*/
function requestUrl(url) {
var responseCode;
var sleepTime = SLEEP_TIME_FOR_RETRY;
var numTries = 0;
while (numTries < NUM_OF_TRIES && !responseCode) {
try {
var response = UrlFetchApp.fetch(url, { muteHttpExceptions: true, followRedirects: FOLLOW_REDIRECTS });
responseCode = response.getResponseCode();
} catch (e) {
if (e.message.indexOf('Service invoked too many times in a short time:') != -1) {
log('Request Flooding. Retry in ' + sleepTime, true);
Utilities.sleep(sleepTime);
sleepTime *= 2;
} else if (e.message.indexOf('Service invoked too many times:') != -1) {
log('Daily quota reached', true);
throw "Reached UrlFetchApp daily quota";
} else {
log(e.message, true);
return e.message;
}
}
numTries++;
}
if (!responseCode) {
var message = "Reached UrlFetchApp QPS limit";
log(message, true);
throw message;
} else {
return responseCode;
}
}