-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain-name-checker.js
More file actions
252 lines (211 loc) · 8.13 KB
/
domain-name-checker.js
File metadata and controls
252 lines (211 loc) · 8.13 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
// ID: ed7e945f7a48e1f1d6c1455676e14b5b
/**
*
* Domain Name Checker
*
* This script will scan through your keyword and ad URLs, checking the domain
* names for anything out of place, and output any discrepancies it finds into a
* Google Sheet.
*
* Version: 1.0
* Google AdWords Script maintained on brainlabsdigital.com
*
**/
////////////////////////////////////////////////////////////////////////////////
// Options
var domainName = "brainlabsdigital.com";
// The domain you expect to be in all your keyword and ad URLs.
// Can be a whole URL (www.brainlabsdigital.com) or a partial URL
// (brainlabsdigital.com) to cover multiple subdomains.
var isWholeDomainName = true;
// If the domain name you gave is a whole URL, set this to true. Otherwise,
// leave it as false.
var targetSheetUrl = "https://docs.google.com/YOUR-SPREADSHEET-URL-HERE";
// Replace this with the URL of a blank Google Sheet; this is where the script
// will output its results
var campaignNameContains = [];
// Use this if you only want to look at some campaigns.
// For example ["Generic"] would only look at campaigns with 'generic' in the
// name, while ["Generic", "Competitor"] would only look at campaigns with
// either 'generic' or 'competitor' in the name.
// Leave as [] to include all campaigns.
var campaignNameDoesNotContain = [];
// Use this if you want to exclude some campaigns.
// For example ["Brand"] would ignore any campaigns with 'brand' in the name,
// while ["Brand", "Key Terms"] would ignore any campaigns with 'brand' or
// 'key terms' in the name.
// Leave as [] to not exclude any campaigns.
var ignorePausedCampaigns = true;
// Set this to true to only look at currently active campaigns.
// Set to false to include campaigns that had impressions but are currently paused.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Functions
function main() {
// Escape any special characters in the given domain name.
prepareDomainName();
Logger.log("Prepared domain name for checking.");
// Fetch the URLs of keywords and ads attached to valid campaigns, filtering
// out those with the correct domain name.
var urlData = getUrlData();
Logger.log("Fetched all URLs.");
var numberOfBadUrls = Object.keys(urlData).length;
if (numberOfBadUrls === 0) {
Logger.log("No incorrect URLs found.");
} else {
// Output the bad URLs and their keywords and ads to the target sheet.
outputToSheet(urlData);
Logger.log("Output " + numberOfBadUrls + " incorrect URLs to sheet.");
}
Logger.log("Finished.");
}
// Escape any special characters in the given domain name.
function prepareDomainName() {
domainName = domainName.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
// This function returns an object containing the URLs and details of keywords
// and ads attached to valid campaigns.
function getUrlData() {
var urlData = new Object();
var expectedPattern = /./;
var whereStatements = ["Status = 'ENABLED'",
"AdGroupStatus = 'ENABLED'"
];
if (ignorePausedCampaigns) {
whereStatements.push("CampaignStatus IN ['ENABLED']");
} else {
whereStatements.push("CampaignStatus IN ['ENABLED','PAUSED']");
}
if (isWholeDomainName) {
expectedPattern = new RegExp("^https?://" + domainName);
} else {
expectedPattern = new RegExp("^https?://([^/]*?\\.)*" + domainName);
}
if (campaignNameContains.length == 0) {
campaignNameContains.push(false);
}
for (var i = 0; i < campaignNameDoesNotContain.length; i++) {
whereStatements.push("CampaignName DOES_NOT_CONTAIN_IGNORE_CASE '"
+ campaignNameDoesNotContain[i].replace(/"/g, '\\\"') + "'");
}
for (var i = 0; i < campaignNameContains.length; i++) {
if (campaignNameContains[i] === false) {
var finalWhereStatements = whereStatements;
} else {
var finalWhereStatements = whereStatements.concat(
["CampaignName CONTAINS_IGNORE_CASE '" + campaignNameContains[i] + "'"]
);
}
var keywordReport = AdWordsApp.report(
"SELECT CampaignName, AdGroupName, Criteria, FinalMobileUrls, FinalUrls " +
"FROM KEYWORDS_PERFORMANCE_REPORT " +
"WHERE FinalUrls != '--' AND " + finalWhereStatements.join(" AND "));
var rows = keywordReport.rows();
while (rows.hasNext()) {
var row = rows.next();
var urls = jsonToArray(row['FinalMobileUrls']).concat(
jsonToArray(row['FinalUrls'])
);
for (var j in urls) {
var url = urls[j].toLowerCase();
if (url.match(expectedPattern) === null) {
var rowData = {
"CampaignName": row['CampaignName'],
"AdGroupName": row['AdGroupName'],
"Keyword": row['Criteria']
};
if (!urlData.hasOwnProperty(url)) {
urlData[url] = { "keywords": {}, "ads": {} };
}
urlData[url]["keywords"][row['Id']] = rowData;
}
}
}
var adReport = AdWordsApp.report(
"SELECT CampaignName, AdGroupName, HeadlinePart1, HeadlinePart2, " +
"CreativeFinalMobileUrls, CreativeFinalUrls " +
"FROM AD_PERFORMANCE_REPORT " +
"WHERE CreativeFinalUrls != '--' AND "
+ finalWhereStatements.join(" AND "));
var rows = adReport.rows();
while (rows.hasNext()) {
var row = rows.next();
var urls = jsonToArray(row['CreativeFinalMobileUrls']).concat(
jsonToArray(row['CreativeFinalUrls'])
);
for (var j in urls) {
var url = urls[j].toLowerCase();
if (url.match(expectedPattern) === null) {
var rowData = {
"CampaignName": row['CampaignName'],
"AdGroupName": row['AdGroupName'],
"Headline": row['HeadlinePart1'] + " - "
+ row['HeadlinePart2']
};
if (!urlData.hasOwnProperty(url)) {
urlData[url] = { "keywords": {}, "ads": {} };
}
urlData[url]["ads"][rowData['Headline']] = rowData;
}
}
}
whereStatements.push("CampaignName DOES_NOT_CONTAIN_IGNORE_CASE '"
+ campaignNameContains[i] + "'");
}
return urlData;
}
// This function outputs details about any invalid URLs to the given Google
// Sheet.
function outputToSheet(urlData) {
var ss = checkSpreadsheet(targetSheetUrl, "the spreadsheet");
var keywordsSheet = ss.getSheetByName("Results - Keywords");
var adsSheet = ss.getSheetByName("Results - Ads");
if (keywordsSheet == null) {
keywordsSheet = ss.insertSheet("Results - Keywords");
}
if (adsSheet == null) {
adsSheet = ss.insertSheet("Results - Ads");
}
keywordsSheet.clear();
adsSheet.clear();
var keywordsRange = [["Bad URL", "Keyword", "Ad Group", "Campaign"]];
var adsRange = [["Bad URL", "Ad Headline", "Ad Group", "Campaign"]];
for (var url in urlData) {
for (var j in urlData[url]["keywords"]) {
var data = urlData[url]["keywords"][j];
keywordsRange.push([url,
data["Keyword"],
data["AdGroupName"],
data["CampaignName"]]);
}
for (var j in urlData[url]["ads"]) {
var data = urlData[url]["ads"][j];
adsRange.push([url,
data["Headline"],
data["AdGroupName"],
data["CampaignName"]]);
}
}
keywordsSheet.getRange(1, 1, keywordsRange.length, 4).setValues(keywordsRange);
adsSheet.getRange(1, 1, adsRange.length, 4).setValues(adsRange);
}
// A small helper function for processing AdWords report fields.
function jsonToArray(str) {
return str == "--" ? [] : JSON.parse(str);
}
// Check the spreadsheet URL has been entered, and that it works
function checkSpreadsheet(spreadsheetUrl, spreadsheetName) {
if (spreadsheetUrl.replace(/[AEIOU]/g, "X") == "https://docs.google.com/YXXR-SPRXXDSHXXT-XRL-HXRX") {
throw ("Problem with " + spreadsheetName + " URL: make sure you've replaced the default with a valid spreadsheet URL.");
}
try {
var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
// Checks if you can edit the spreadsheet
var sheet = spreadsheet.getSheets()[0];
var sheetName = sheet.getName();
sheet.setName(sheetName);
return spreadsheet;
} catch (e) {
throw ("Problem with " + spreadsheetName + " URL: '" + e + "'");
}
}