-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroken-destination-urls.js
More file actions
57 lines (48 loc) · 1.68 KB
/
broken-destination-urls.js
File metadata and controls
57 lines (48 loc) · 1.68 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
// ID: ff803b2fcb813a43fcff09bbfeff2260
function main() {
var validCodes = [200, 301, 302];
//Use commas to separate recipients
var email_recipients = "userA@example.com, userB@example.com"
var urls = [];
var badUrls = [];
var keywordIterator = AdWordsApp.keywords()
.withCondition("Status = ENABLED")
.withCondition("AdGroupStatus = ENABLED")
.withCondition("CampaignStatus = ENABLED")
.get();
while (keywordIterator.hasNext()) {
var keyword = keywordIterator.next();
var destinationUrl = keyword.getDestinationUrl();
if (destinationUrl !== null && destinationUrl !== "") {
var url = destinationUrl.split('?')[0];
if (urls.indexOf(url) === -1) {
urls.push(url);
}
}
}
var urlFetchOptions = { muteHttpExceptions: true };
for (var x = 0; x < urls.length; x++) {
try {
var response = UrlFetchApp.fetch(urls[x], urlFetchOptions);
var code = response.getResponseCode();
}
catch (err) {
Logger.log("The Url " + urls[x] + " could not be processed");
}
if (validCodes.indexOf(code) === -1) {
badUrls.push(urls[x]);
Logger.log(urls[x]);
}
}
if (badUrls.length !== 0) {
var accountName = AdWordsApp.currentAccount().getName();
var subject = accountName + " - Broken Destination URLs";
var body = "The following are broken URLs in the account " + accountName +
". Also attached is a CSV file containing the URLs. \n\n" + badUrls.join("\n");
var options = {
attachments: [Utilities.newBlob(badUrls.join("\n"), "text/csv", accountName +
" - Broken destination URLs.csv")]
};
MailApp.sendEmail(email_recipients, subject, body, options);
}
}