snipt

Ctrl+h for KB shortcuts

JavaScript

adwords link checker script

  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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
    // Copyright 2015, Google Inc. All Rights Reserved.
    
//https://developers.google.com/adwords/scripts/docs/solutions/mccapp-link-checker#source-code 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @name Link Checker - Manager Accounts
 *
 * @overview The Link Checker script iterates through the ads, keywords, and
 *     sitelinks in your manager account hierarchy and makes sure their URLs
 *     do not produce "Page not found" or other types of error responses. See
 *     https://developers.google.com/adwords/scripts/docs/solutions/mccapp-link-checker
 *     for more details.
 *
 * @author AdWords Scripts Team [[email protected]]
 *
 * @version 2.0
 *
 * @changelog
 * - version 2.0
 *   - Completely revised the script to work on larger accounts and hierarchies.
 * - version 1.3
 *   - Enhanced to include ad group sitelinks.
 *   - Updated to track completion across runs and send at most one email
 *     per day.
 * - version 1.2
 *   - Remove label flushing code from the script.
 * - version 1.1
 *   - Remove some debug code.
 * - version 1.0
 *   - Released initial version.
 */

var CONFIG = {
  // URL of the spreadsheet template.
  // This should be a copy of https://goo.gl/8YLeMj.
  SPREADSHEET_URL: 'YOUR_SPREADSHEET_URL',

  // Array of addresses to be alerted via email if issues are found.
  RECIPIENT_EMAILS: [
    'YOUR_EMAIL_HERE'
  ],

  // Label to use when a link has been checked.
  LABEL: 'LinkChecker_Done',

  // A list of ManagedAccountSelector conditions to restrict the population
  // of child accounts that will be processed. Leave blank or comment out
  // to include all child accounts.
  ACCOUNT_CONDITIONS: [],

  // Number of milliseconds to sleep after each URL request. Use this throttle
  // to reduce the load that the script imposes on your web server(s).
  THROTTLE: 0,

  // Number of seconds before timeout that the script should stop checking URLs
  // to make sure it has time to output its findings.
  TIMEOUT_BUFFER: 120
};

/**
 * Parameters controlling the script's behavior after hitting a UrlFetchApp
 * QPS quota limit.
 */
var QUOTA_CONFIG = {
  // Initial number of milliseconds to sleep.
  INIT_SLEEP_TIME: 250,

  // Multiplicative factor to increase sleep time by on each retry.
  BACKOFF_FACTOR: 2,

  // Maximum number of tries for a single URL.
  MAX_TRIES: 5
};

/**
 * Exceptions that prevent the script from finishing checking all URLs in an
 * account but allow it to resume next time.
 */
var EXCEPTIONS = {
  QPS: 'Reached UrlFetchApp QPS limit',
  LIMIT: 'Reached UrlFetchApp daily quota',
  TIMEOUT: 'Approached script execution time limit'
};

/**
 * Named ranges in the spreadsheet.
 */
var NAMES = {
  CHECK_AD_URLS: 'checkAdUrls',
  CHECK_KEYWORD_URLS: 'checkKeywordUrls',
  CHECK_SITELINK_URLS: 'checkSitelinkUrls',
  CHECK_PAUSED_ADS: 'checkPausedAds',
  CHECK_PAUSED_KEYWORDS: 'checkPausedKeywords',
  CHECK_PAUSED_SITELINKS: 'checkPausedSitelinks',
  VALID_CODES: 'validCodes',
  EMAIL_EACH_RUN: 'emailEachRun',
  EMAIL_NON_ERRORS: 'emailNonErrors',
  EMAIL_ON_COMPLETION: 'emailOnCompletion',
  SAVE_ALL_URLS: 'saveAllUrls',
  FREQUENCY: 'frequency',
  DATE_STARTED: 'dateStarted',
  DATE_COMPLETED: 'dateCompleted',
  DATE_EMAILED: 'dateEmailed',
  NUM_ERRORS: 'numErrors',
  RESULT_HEADERS: 'resultHeaders',
  ARCHIVE_HEADERS: 'archiveHeaders'
};

function main() {
  var spreadsheet = SpreadsheetApp.openByUrl(CONFIG.SPREADSHEET_URL);
  spreadsheet.setSpreadsheetTimeZone(AdWordsApp.currentAccount().getTimeZone());

  var options = loadOptions(spreadsheet);
  var status = loadStatus(spreadsheet);

  if (!status.dateStarted) {
    // This is the very first execution of the script.
    startNewAnalysis(spreadsheet);
  } else if (status.dateStarted > status.dateCompleted) {
    Logger.log('Resuming work from a previous execution.');
  } else if (dayDifference(status.dateStarted, new Date()) <
             options.frequency) {
    Logger.log('Waiting until ' + options.frequency +
               ' days have elapsed since the start of the last analysis.');
    return;
  } else {
    // Enough time has passed since the last analysis to start a new one.
    removeLabelsInAccounts();
    removeAccountLabels([CONFIG.LABEL]);
    startNewAnalysis(spreadsheet);
  }

  ensureAccountLabels([CONFIG.LABEL]);

  // Get up to 50 accounts that have not yet had all of their URLs checked.
  var accountSelector = getAccounts(false).withLimit(50);
  if (accountSelector.get().hasNext()) {
    accountSelector.executeInParallel('processAccount', 'processResults',
                                      JSON.stringify(options));
  } else {
    processResults([]);
  }
}

/**
 * Retrieves all accounts that either have had their URLs checked or need to
 * have their URLs checked.
 *
 * @param {boolean} isChecked True to get accounts that have been checked
 *     already, false to get accounts that have not have been checked already.
 *     Ignored if the label does not exist.
 * @return {Object} An account selector.
 */
function getAccounts(isChecked) {
  var accountSelector = MccApp.accounts();

  if (getAccountLabel(CONFIG.LABEL)) {
    accountSelector = accountSelector.
      withCondition('LabelNames ' +
                    (isChecked ? 'CONTAINS' : 'DOES_NOT_CONTAIN') +
                     ' "' + CONFIG.LABEL + '"');
  }

  if (CONFIG.ACCOUNT_CONDITIONS) {
    for (var i = 0; i < CONFIG.ACCOUNT_CONDITIONS.length; i++) {
      accountSelector =
          accountSelector.withCondition(CONFIG.ACCOUNT_CONDITIONS[i]);
    }
  }

  return accountSelector;
}

/**
 * Removes the tracking in each account that was previously analyzed, thereby
 * clearing that account for a new analysis.
 */
function removeLabelsInAccounts() {
  var managerAccount = AdWordsApp.currentAccount();
  var accounts = getAccounts(true).get();

  while (accounts.hasNext()) {
    MccApp.select(accounts.next());
    removeLabels([CONFIG.LABEL]);
  }

  MccApp.select(managerAccount);
}

/**
 * Performs the link checking analysis on the current account.
 *
 * @param {string} options Options from the spreadsheet as JSON.
 * @return {string} JSON stringified results of the analysis.
 */
function processAccount(options) {
  return JSON.stringify(analyzeAccount(JSON.parse(options)));
}

/**
 * Consolidates results from each account and outputs them.
 *
 * @param {Array.<Object>} executionResults A list of ExecutionResult objects.
 */
function processResults(executionResults) {
  var spreadsheet = SpreadsheetApp.openByUrl(CONFIG.SPREADSHEET_URL);
  var options = loadOptions(spreadsheet);
  var results = {
    urlChecks: [],
    didComplete: true
  };

  for (var i = 0; i < executionResults.length; i++) {
    if (!executionResults[i].getError()) {
      var accountResult = JSON.parse(executionResults[i].getReturnValue());
      results.urlChecks = results.urlChecks.concat(accountResult.urlChecks);
      results.didComplete = results.didComplete && accountResult.didComplete;

      if (accountResult.didComplete) {
        MccApp.accounts().withIds([executionResults[i].getCustomerId()]).get().
            next().applyLabel(CONFIG.LABEL);
      }
    } else {
      Logger.log('Processing for ' + executionResults[i].getCustomerId() +
          ' failed.');
    }
  }

  // The entire analysis is not complete if there are any accounts that have
  // not been labeled (i.e., the account was not started, or not all URLs in
  // the account have been checked).
  results.didComplete = results.didComplete &&
      !getAccounts(false).get().hasNext();

  outputResults(results, options);
}

/**
 * Checks as many new URLs as possible that have not previously been checked,
 * subject to quota and time limits.
 *
 * @param {Object} options Dictionary of options.
 * @return {Object} An object with fields for the URLs checked and an indication
 *     if the analysis was completed (no remaining URLs to check).
 */
function analyzeAccount(options) {
  // Ensure the label exists before attempting to retrieve already checked URLs.
  ensureLabels([CONFIG.LABEL]);

  var checkedUrls = getAlreadyCheckedUrls(options);
  var urlChecks = [];
  var didComplete = false;

  try {
    // If the script throws an exception, didComplete will remain false.
    didComplete = checkUrls(checkedUrls, urlChecks, options);
  } catch(e) {
    if (e == EXCEPTIONS.QPS ||
        e == EXCEPTIONS.LIMIT ||
        e == EXCEPTIONS.TIMEOUT) {
      Logger.log('Stopped checking URLs early because: ' + e);
      Logger.log('Checked URLs will still be output.');
    } else {
      throw e;
    }
  }

  return {
    urlChecks: urlChecks,
    didComplete: didComplete
  };
}

/**
 * Outputs the results to a spreadsheet and sends emails if appropriate.
 *
 * @param {Object} results An object with fields for the URLs checked and an
 *     indication if the analysis was completed (no remaining URLs to check).
 * @param {Object} options Dictionary of options.
 */
function outputResults(results, options) {
  var spreadsheet = SpreadsheetApp.openByUrl(CONFIG.SPREADSHEET_URL);

  var numErrors = countErrors(results.urlChecks, options);
  Logger.log('Found ' + numErrors + ' this execution.');

  saveUrlsToSpreadsheet(spreadsheet, results.urlChecks, options);

  // Reload the status to get the total number of errors for the entire
  // analysis, which is calculated by the spreadsheet.
  status = loadStatus(spreadsheet);

  if (results.didComplete) {
    spreadsheet.getRangeByName(NAMES.DATE_COMPLETED).setValue(new Date());
    Logger.log('Found ' + status.numErrors + ' across the entire analysis.');
  }

  if (CONFIG.RECIPIENT_EMAILS) {
    if (!results.didComplete && options.emailEachRun &&
        (options.emailNonErrors || numErrors > 0)) {
      sendIntermediateEmail(spreadsheet, numErrors);
    }

    if (results.didComplete &&
        (options.emailEachRun || options.emailOnCompletion) &&
        (options.emailNonErrors || status.numErrors > 0)) {
      sendFinalEmail(spreadsheet, status.numErrors);
    }
  }
}

/**
 * Loads data from a spreadsheet based on named ranges. Strings 'Yes' and 'No'
 * are converted to booleans. One-dimensional ranges are converted to arrays
 * with blank cells omitted. Assumes each named range exists.
 *
 * @param {Object} spreadsheet The spreadsheet object.
 * @param {Array.<string>} names A list of named ranges that should be loaded.
 * @return {Object} A dictionary with the names as keys and the values
 *     as the cell values from the spreadsheet.
 */
function loadDatabyName(spreadsheet, names) {
  var data = {};

  for (var i = 0; i < names.length; i++) {
    var name = names[i];
    var range = spreadsheet.getRangeByName(name);

    if (range.getNumRows() > 1 && range.getNumColumns() > 1) {
      // Name refers to a 2d range, so load it as a 2d array.
      data[name] = range.getValues();
    } else if (range.getNumRows() == 1 && range.getNumColumns() == 1) {
      // Name refers to a single cell, so load it as a value and replace
      // Yes/No with boolean true/false.
      data[name] = range.getValue();
      data[name] = data[name] === 'Yes' ? true : data[name];
      data[name] = data[name] === 'No' ? false : data[name];
    } else {
      // Name refers to a 1d range, so load it as an array (regardless of
      // whether the 1d range is oriented horizontally or vertically).
      var isByRow = range.getNumRows() > 1;
      var limit = isByRow ? range.getNumRows() : range.getNumColumns();
      var cellValues = range.getValues();

      data[name] = [];
      for (var j = 0; j < limit; j++) {
        var cellValue = isByRow ? cellValues[j][0] : cellValues[0][j];
        if (cellValue) {
          data[name].push(cellValue);
        }
      }
    }
  }

  return data;
}

/**
 * Loads options from the spreadsheet.
 *
 * @param {Object} spreadsheet The spreadsheet object.
 * @return {Object} A dictionary of options.
 */
function loadOptions(spreadsheet) {
  return loadDatabyName(spreadsheet,
      [NAMES.CHECK_AD_URLS, NAMES.CHECK_KEYWORD_URLS,
       NAMES.CHECK_SITELINK_URLS, NAMES.CHECK_PAUSED_ADS,
       NAMES.CHECK_PAUSED_KEYWORDS, NAMES.CHECK_PAUSED_SITELINKS,
       NAMES.VALID_CODES, NAMES.EMAIL_EACH_RUN,
       NAMES.EMAIL_NON_ERRORS, NAMES.EMAIL_ON_COMPLETION,
       NAMES.SAVE_ALL_URLS, NAMES.FREQUENCY]);
}

/**
 * Loads state information from the spreadsheet.
 *
 * @param {Object} spreadsheet The spreadsheet object.
 * @return {Object} A dictionary of status information.
 */
function loadStatus(spreadsheet) {
  return loadDatabyName(spreadsheet,
      [NAMES.DATE_STARTED, NAMES.DATE_COMPLETED,
       NAMES.DATE_EMAILED, NAMES.NUM_ERRORS]);
}

/**
 * Saves the start date to the spreadsheet and archives results of the last
 * analysis to a separate sheet.
 *
 * @param {Object} spreadsheet The spreadsheet object.
 */
function startNewAnalysis(spreadsheet) {
  Logger.log('Starting a new analysis.');

  spreadsheet.getRangeByName(NAMES.DATE_STARTED).setValue(new Date());

  // Helper method to get the output area on the results or archive sheets.
  var getOutputRange = function(rangeName) {
    var headers = spreadsheet.getRangeByName(rangeName);
    return headers.offset(1, 0, headers.getSheet().getDataRange().getLastRow());
  };

  getOutputRange(NAMES.ARCHIVE_HEADERS).clearContent();

  var results = getOutputRange(NAMES.RESULT_HEADERS);
  results.copyTo(getOutputRange(NAMES.ARCHIVE_HEADERS));

  getOutputRange(NAMES.RESULT_HEADERS).clearContent();
}

/**
 * Counts the number of errors in the results.
 *
 * @param {Array.<Object>} urlChecks A list of URL check results.
 * @param {Object} options Dictionary of options.
 * @return {number} The number of errors in the results.
 */
function countErrors(urlChecks, options) {
  var numErrors = 0;

  for (var i = 0; i < urlChecks.length; i++) {
    if (options.validCodes.indexOf(urlChecks[i].responseCode) == -1) {
      numErrors++;
    }
  }

  return numErrors;
}

/**
 * Saves URLs for a particular account to the spreadsheet starting at the first
 * unused row.
 *
 * @param {Object} spreadsheet The spreadsheet object.
 * @param {Array.<Object>} urlChecks A list of URL check results.
 * @param {Object} options Dictionary of options.
 */
function saveUrlsToSpreadsheet(spreadsheet, urlChecks, options) {
  // Build each row of output values in the order of the columns.
  var outputValues = [];
  for (var i = 0; i < urlChecks.length; i++) {
    var urlCheck = urlChecks[i];

    if (options.saveAllUrls ||
        options.validCodes.indexOf(urlCheck.responseCode) == -1) {
      outputValues.push([
        urlCheck.customerId,
        new Date(urlCheck.timestamp),
        urlCheck.url,
        urlCheck.responseCode,
        urlCheck.entityType,
        urlCheck.campaign,
        urlCheck.adGroup,
        urlCheck.ad,
        urlCheck.keyword,
        urlCheck.sitelink
      ]);
    }
  }

  if (outputValues.length > 0) {
    // Find the first open row on the Results tab below the headers and create a
    // range large enough to hold all of the output, one per row.
    var headers = spreadsheet.getRangeByName(NAMES.RESULT_HEADERS);
    var lastRow = headers.getSheet().getDataRange().getLastRow();
    var outputRange = headers.offset(lastRow - headers.getRow() + 1,
                                     0, outputValues.length);
    outputRange.setValues(outputValues);
  }

  for (var i = 0; i < CONFIG.RECIPIENT_EMAILS.length; i++) {
    spreadsheet.addEditor(CONFIG.RECIPIENT_EMAILS[i]);
  }
}

/**
 * Sends an email to a list of email addresses with a link to the spreadsheet
 * and the results of this execution of the script.
 *
 * @param {Object} spreadsheet The spreadsheet object.
 * @param {boolean} numErrors The number of errors found in this execution.
 */
function sendIntermediateEmail(spreadsheet, numErrors) {
  spreadsheet.getRangeByName(NAMES.DATE_EMAILED).setValue(new Date());

  MailApp.sendEmail(CONFIG.RECIPIENT_EMAILS.join(','),
      'Link Checker Results',
      'The Link Checker script found ' + numErrors + ' URLs with errors in ' +
      'an execution that just finished. See ' +
      spreadsheet.getUrl() + ' for details.');
}

/**
 * Sends an email to a list of email addresses with a link to the spreadsheet
 * and the results across the entire account.
 *
 * @param {Object} spreadsheet The spreadsheet object.
 * @param {boolean} numErrors The number of errors found in the entire account.
 */
function sendFinalEmail(spreadsheet, numErrors) {
  spreadsheet.getRangeByName(NAMES.DATE_EMAILED).setValue(new Date());

  MailApp.sendEmail(CONFIG.RECIPIENT_EMAILS.join(','),
      'Link Checker Results',
      'The Link Checker script found ' + numErrors + ' URLs with errors ' +
      'across its entire analysis. See ' +
      spreadsheet.getUrl() + ' for details.');
}

/**
 * Retrieves all final URLs and mobile final URLs in the account across ads,
 * keywords, and sitelinks that were checked in a previous run, as indicated by
 * them having been labeled.
 *
 * @param {Object} options Dictionary of options.
 * @return {Object} A map of previously checked URLs with the URL as the key.
 */
function getAlreadyCheckedUrls(options) {
  var urlMap = {};

  var addToMap = function(items) {
    for (var i = 0; i < items.length; i++) {
      urlMap[items[i]] = true;
    }
  };

  if (options.checkAdUrls) {
    addToMap(getUrlsBySelector(AdWordsApp.ads().
                               withCondition(labelCondition(true))));
  }

  if (options.checkKeywordUrls) {
    addToMap(getUrlsBySelector(AdWordsApp.keywords().
                               withCondition(labelCondition(true))));
  }

  if (options.checkSitelinkUrls) {
    addToMap(getAlreadyCheckedSitelinkUrls());
  }

  return urlMap;
}

/**
 * Retrieves all final URLs and mobile final URLs for campaign and ad group
 * sitelinks.
 *
 * @return {Array.<string>} An array of URLs.
 */
function getAlreadyCheckedSitelinkUrls() {
  var urls = [];

  // Helper method to get campaign or ad group sitelink URLs.
  var addSitelinkUrls = function(selector) {
    var iterator = selector.withCondition(labelCondition(true)).get();

    while (iterator.hasNext()) {
      var entity = iterator.next();
      var sitelinks = entity.extensions().sitelinks();
      urls = urls.concat(getUrlsBySelector(sitelinks));
    }
  };

  addSitelinkUrls(AdWordsApp.campaigns());
  addSitelinkUrls(AdWordsApp.adGroups());

  return urls;
}

/**
 * Retrieves all URLs in the entities specified by a selector.
 *
 * @param {Object} selector The selector specifying the entities to use.
 *     The entities should be of a type that has a urls() method.
 * @return {Array.<string>} An array of URLs.
 */
function getUrlsBySelector(selector) {
  var urls = [];
  var entities = selector.get();

  // Helper method to add the url to the list if it exists.
  var addToList = function(url) {
    if (url) {
      urls.push(url);
    }
  };

  while (entities.hasNext()) {
    var entity = entities.next();

    addToList(entity.urls().getFinalUrl());
    addToList(entity.urls().getMobileFinalUrl());
  }

  return urls;
}

/**
 * Retrieves all final URLs and mobile final URLs in the account across ads,
 * keywords, and sitelinks, and checks their response code. Does not check
 * previously checked URLs.
 *
 * @param {Object} checkedUrls A map of previously checked URLs with the URL as
 *     the key.
 * @param {Array.<Object>} urlChecks An array into which the results of each URL
 *     check will be inserted.
 * @param {Object} options Dictionary of options.
 * @return {boolean} True if all URLs were checked.
 */
function checkUrls(checkedUrls, urlChecks, options) {
  var didComplete = true;

  // Helper method to add common conditions to ad group and keyword selectors.
  var addConditions = function(selector, includePaused) {
    var statuses = ['ENABLED'];
    if (includePaused) {
      statuses.push('PAUSED');
    }

    var predicate = ' IN [' + statuses.join(',') + ']';
    return selector.withCondition(labelCondition(false)).
        withCondition('Status' + predicate).
        withCondition('CampaignStatus' + predicate).
        withCondition('AdGroupStatus' + predicate);
  };

  if (options.checkAdUrls) {
    didComplete = didComplete && checkUrlsBySelector(checkedUrls, urlChecks,
        addConditions(AdWordsApp.ads().withCondition('CreativeFinalUrls != ""'),
                      options.checkPausedAds));
  }

  if (options.checkKeywordUrls) {
    didComplete = didComplete && checkUrlsBySelector(checkedUrls, urlChecks,
        addConditions(AdWordsApp.keywords().withCondition('FinalUrls != ""'),
                      options.checkPausedKeywords));
  }

  if (options.checkSitelinkUrls) {
    didComplete = didComplete &&
        checkSitelinkUrls(checkedUrls, urlChecks, options);
  }

  return didComplete;
}

/**
 * Retrieves all final URLs and mobile final URLs in a selector and checks them
 * for a valid response code. Does not check previously checked URLs. Labels the
 * entity that it was checked, if possible.
 *
 * @param {Object} checkedUrls A map of previously checked URLs with the URL as
 *     the key.
 * @param {Array.<Object>} urlChecks An array into which the results of each URL
 *     check will be inserted.
 * @param {Object} selector The selector specifying the entities to use.
 *     The entities should be of a type that has a urls() method.
 * @return {boolean} True if all URLs were checked.
 */
function checkUrlsBySelector(checkedUrls, urlChecks, selector) {
  var customerId = AdWordsApp.currentAccount().getCustomerId();
  var iterator = selector.get();
  var entities = [];

  // Helper method to check a URL.
  var checkUrl = function(url) {
    if (!url || checkedUrls[url]) {
      return;
    }

    var responseCode = testUrl(cleanUrl(url));
    var entityType = entity.getEntityType();

    urlChecks.push({
      customerId: customerId,
      timestamp: new Date(),
      url: url,
      responseCode: responseCode,
      entityType: entity.getEntityType(),
      campaign: entity.getCampaign ? entity.getCampaign().getName() : '',
      adGroup: entity.getAdGroup ? entity.getAdGroup().getName() : '',
      ad: entityType == 'Ad' ? entity.getHeadline() : '',
      keyword: entityType == 'Keyword' ? entity.getText() : '',
      sitelink: entityType.indexOf('Sitelink') != -1 ? entity.getLinkText() : ''
    });

    checkedUrls[url] = true;
  };

  while (iterator.hasNext()) {
    entities.push(iterator.next());
  }

  for (var i = 0; i < entities.length; i++) {
    var entity = entities[i];

    checkUrl(entity.urls().getFinalUrl());
    checkUrl(entity.urls().getMobileFinalUrl());

    // Sitelinks do not have labels.
    if (entity.applyLabel) {
      entity.applyLabel(CONFIG.LABEL);
      checkTimeout();
    }
  }

  // True only if we did not breach an iterator limit.
  return entities.length == iterator.totalNumEntities();
}

/**
 * Retrieves all final URLs and mobile final URLs for campaign and ad group
 * sitelinks and checks them for a valid response code. Does not check
 * previously checked URLs. Labels the containing campaign or ad group that it
 * has been checked.
 *
 * @param {Object} checkedUrls A map of previously checked URLs with the URL as
 *     the key.
 * @param {Array.<Object>} urlChecks An array into which the results of each URL
 *     check will be inserted.
 * @param {Object} options Dictionary of options.
 * @return {boolean} True if all URLs were checked.
 */
function checkSitelinkUrls(checkedUrls, urlChecks, options) {
  var didComplete = true;

  // Helper method to check URLs for sitelinks in a campaign or ad group
  // selector.
  var checkSitelinkUrls = function(selector) {
    var iterator = selector.withCondition(labelCondition(false)).get();
    var entities = [];

    while (iterator.hasNext()) {
      entities.push(iterator.next());
    }

    for (var i = 0; i < entities.length; i++) {
      var entity = entities[i];
      var sitelinks = entity.extensions().sitelinks();

      if (sitelinks.get().hasNext()) {
        didComplete = didComplete &&
            checkUrlsBySelector(checkedUrls, urlChecks, sitelinks);
        entity.applyLabel(CONFIG.LABEL);
        checkTimeout();
      }
    }

    // True only if we did not breach an iterator limit.
    didComplete = didComplete &&
        entities.length == iterator.totalNumEntities();
  };

  var statuses = ['ENABLED'];
  if (options.checkPausedSitelinks) {
    statuses.push('PAUSED');
  }

  var predicate = ' IN [' + statuses.join(',') + ']';
  checkSitelinkUrls(AdWordsApp.campaigns().
                    withCondition('Status' + predicate));
  checkSitelinkUrls(AdWordsApp.adGroups().
                    withCondition('Status' + predicate).
                    withCondition('CampaignStatus' + predicate));

  return didComplete;
}

/**
 * Cleans a URL by removing ValueTrack and custom parameters.
 *
 * @param {string} url The url to clean.
 * @return {string} The cleaned URL.
 */
function cleanUrl(url) {
  // Custom parameters are alphanumeric with a leading underscore.
  // ValueTrack parameters may also contain a ':' or '+'.
  return url.replace(/{[0-9a-zA-Z\_\+\:]+}/g, '');
}

/**
 * Requests a given URL. Retries if the UrlFetchApp QPS limit was reached,
 * exponentially backing off on each retry. Throws an exception if it reaches
 * the maximum number of retries. Throws an exception if the UrlFetchApp daily
 * quota limit was reached.
 *
 * @param {string} url The URL to test.
 * @return {number} The response code received when requesting the URL.
 */
function testUrl(url) {
  var responseCode;
  var sleepTime = QUOTA_CONFIG.INIT_SLEEP_TIME;
  var numTries = 0;

  while (numTries < QUOTA_CONFIG.MAX_TRIES && !responseCode) {
    try {
      // If UrlFetchApp.fetch() throws an exception, responseCode will remain
      // undefined.
      responseCode =
        UrlFetchApp.fetch(url, {muteHttpExceptions: true}).getResponseCode();

      if (CONFIG.THROTTLE > 0) {
        Utilities.sleep(CONFIG.THROTTLE);
      }
    } catch(e) {
      if (e.message.indexOf('Service invoked too many times in a short time:')
          != -1) {
        Utilities.sleep(sleepTime);
        sleepTime *= QUOTA_CONFIG.BACKOFF_FACTOR;
      } else if (e.message.indexOf('Service invoked too many times:') != -1) {
        throw EXCEPTIONS.LIMIT;
      } else if (e.message.indexOf('DNS error') != -1) {
        return 404;
      } else {
        throw e.message;
      }
    }

    numTries++;
  }

  if (!responseCode) {
    throw EXCEPTIONS.QPS;
  } else {
    return responseCode;
  }
}

/**
 * Throws an exception if the script is close to timing out.
 */
function checkTimeout() {
  if (AdWordsApp.getExecutionInfo().getRemainingTime() <
      CONFIG.TIMEOUT_BUFFER) {
    throw EXCEPTIONS.TIMEOUT;
  }
}

/**
 * Returns the number of days between two dates.
 *
 * @param {Object} from The older Date object.
 * @param {Object} to The newer (more recent) Date object.
 * @return {number} The number of days between the given dates (possibly
 *     fractional).
 */
function dayDifference(from, to) {
  return (to.getTime() - from.getTime()) / (24 * 3600 * 1000);
}

/**
 * Builds a string to be used for withCondition() filtering for whether the
 * label is present or not.
 *
 * @param {boolean} hasLabel True if the label should be present, false if the
 *     label should not be present.
 * @return {string} A condition that can be used in withCondition().
 */
function labelCondition(hasLabel) {
  return 'LabelNames ' + (hasLabel ? 'CONTAINS_ANY' : 'CONTAINS_NONE') +
      ' ["' + CONFIG.LABEL + '"]';
}

/**
 * Retrieves an entity by name.
 *
 * @param {Object} selector A selector for an entity type with a Name field.
 * @param {string} name The name to retrieve the entity by.
 * @return {Object} The entity, if it exists, or null otherwise.
 */
function getEntityByName(selector, name) {
  var entities = selector.withCondition('Name = ' + name).get();

  if (entities.hasNext()) {
    return entities.next();
  } else {
    return null;
  }
}

/**
 * Retrieves a Label object by name.
 *
 * @param {string} labelName The label name to retrieve.
 * @return {Object} The Label object, if it exists, or null otherwise.
 */
function getLabel(labelName) {
  return getEntityByName(AdWordsApp.labels(), labelName);
}

/**
 * Retrieves an AccountLabel object by name.
 *
 * @param {string} labelName The label name to retrieve.
 * @return {Object} The AccountLabel object, if it exists, or null otherwise.
 */
function getAccountLabel(labelName) {
  return getEntityByName(MccApp.accountLabels(), labelName);
}

/**
 * Checks that the account has all provided labels and creates any that are
 * missing. Since labels cannot be created in preview mode, throws an exception
 * if a label is missing.
 *
 * @param {Array.<string>} labelNames An array of label names.
 */
function ensureLabels(labelNames) {
  for (var i = 0; i < labelNames.length; i++) {
    var labelName = labelNames[i];
    var label = getLabel(labelName);

    if (!label) {
      if (!AdWordsApp.getExecutionInfo().isPreview()) {
        AdWordsApp.createLabel(labelName);
      } else {
        throw 'Label ' + labelName + ' is missing and cannot be created in ' +
            'preview mode. Please run the script or create the label manually.';
      }
    }
  }
}

/**
 * Checks that the account has all provided account labels and creates any that
 * are missing. Since labels cannot be created in preview mode, throws an
 * exception if a label is missing.
 *
 * @param {Array.<string>} labelNames An array of label names.
 */
function ensureAccountLabels(labelNames) {
  for (var i = 0; i < labelNames.length; i++) {
    var labelName = labelNames[i];
    var label = getAccountLabel(labelName);

    if (!label) {
      if (!AdWordsApp.getExecutionInfo().isPreview()) {
        MccApp.createAccountLabel(labelName);
      } else {
        throw 'Account label ' + labelName + ' is missing and cannot be ' +
            'created in preview mode. Please run the script or create the ' +
            'label manually.';
      }
    }
  }
}

/**
 * Removes all provided labels from the account. Since labels cannot be removed
 * in preview mode, throws an exception in preview mode.
 *
 * @param {Array.<string>} labelNames An array of label names.
 */
function removeLabels(labelNames) {
  if (AdWordsApp.getExecutionInfo().isPreview()) {
    throw 'Cannot remove labels in preview mode. Please run the script or ' +
        'remove the labels manually.';
  }

  for (var i = 0; i < labelNames.length; i++) {
    var label = getLabel(labelNames[i]);

    if (label) {
      label.remove();
    }
  }
}

/**
 * Removes all provided account labels from the account. Since labels cannot be
 * removed in preview mode, throws an exception in preview mode.
 *
 * @param {Array.<string>} labelNames An array of label names.
 */
function removeAccountLabels(labelNames) {
  if (AdWordsApp.getExecutionInfo().isPreview()) {
    throw 'Cannot remove account labels in preview mode. Please run the ' +
        'script or remove the labels manually.';
  }

  for (var i = 0; i < labelNames.length; i++) {
    var label = getAccountLabel(labelNames[i]);

    if (label) {
      label.remove();
    }
  }
}

Description

link checker script for adwords -runs on ads and checks the url the ad gets to.
https://snipt.net/embed/94661c4820339170020751bb14bc5bac/
/raw/94661c4820339170020751bb14bc5bac/
94661c4820339170020751bb14bc5bac
js
JavaScript
999
2019-08-18T15:05:16
True
False
False
Jul 05, 2016 at 09:25 AM
/api/public/snipt/147336/
adwords-link-checker-script
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10"> 10</a> <a href="#L-11"> 11</a> <a href="#L-12"> 12</a> <a href="#L-13"> 13</a> <a href="#L-14"> 14</a> <a href="#L-15"> 15</a> <a href="#L-16"> 16</a> <a href="#L-17"> 17</a> <a href="#L-18"> 18</a> <a href="#L-19"> 19</a> <a href="#L-20"> 20</a> <a href="#L-21"> 21</a> <a href="#L-22"> 22</a> <a href="#L-23"> 23</a> <a href="#L-24"> 24</a> <a href="#L-25"> 25</a> <a href="#L-26"> 26</a> <a href="#L-27"> 27</a> <a href="#L-28"> 28</a> <a href="#L-29"> 29</a> <a href="#L-30"> 30</a> <a href="#L-31"> 31</a> <a href="#L-32"> 32</a> <a href="#L-33"> 33</a> <a href="#L-34"> 34</a> <a href="#L-35"> 35</a> <a href="#L-36"> 36</a> <a href="#L-37"> 37</a> <a href="#L-38"> 38</a> <a href="#L-39"> 39</a> <a href="#L-40"> 40</a> <a href="#L-41"> 41</a> <a href="#L-42"> 42</a> <a href="#L-43"> 43</a> <a href="#L-44"> 44</a> <a href="#L-45"> 45</a> <a href="#L-46"> 46</a> <a href="#L-47"> 47</a> <a href="#L-48"> 48</a> <a href="#L-49"> 49</a> <a href="#L-50"> 50</a> <a href="#L-51"> 51</a> <a href="#L-52"> 52</a> <a href="#L-53"> 53</a> <a href="#L-54"> 54</a> <a href="#L-55"> 55</a> <a href="#L-56"> 56</a> <a href="#L-57"> 57</a> <a href="#L-58"> 58</a> <a href="#L-59"> 59</a> <a href="#L-60"> 60</a> <a href="#L-61"> 61</a> <a href="#L-62"> 62</a> <a href="#L-63"> 63</a> <a href="#L-64"> 64</a> <a href="#L-65"> 65</a> <a href="#L-66"> 66</a> <a href="#L-67"> 67</a> <a href="#L-68"> 68</a> <a href="#L-69"> 69</a> <a href="#L-70"> 70</a> <a href="#L-71"> 71</a> <a href="#L-72"> 72</a> <a href="#L-73"> 73</a> <a href="#L-74"> 74</a> <a href="#L-75"> 75</a> <a href="#L-76"> 76</a> <a href="#L-77"> 77</a> <a href="#L-78"> 78</a> <a href="#L-79"> 79</a> <a href="#L-80"> 80</a> <a href="#L-81"> 81</a> <a href="#L-82"> 82</a> <a href="#L-83"> 83</a> <a href="#L-84"> 84</a> <a href="#L-85"> 85</a> <a href="#L-86"> 86</a> <a href="#L-87"> 87</a> <a href="#L-88"> 88</a> <a href="#L-89"> 89</a> <a href="#L-90"> 90</a> <a href="#L-91"> 91</a> <a href="#L-92"> 92</a> <a href="#L-93"> 93</a> <a href="#L-94"> 94</a> <a href="#L-95"> 95</a> <a href="#L-96"> 96</a> <a href="#L-97"> 97</a> <a href="#L-98"> 98</a> <a href="#L-99"> 99</a> <a href="#L-100">100</a> <a href="#L-101">101</a> <a href="#L-102">102</a> <a href="#L-103">103</a> <a href="#L-104">104</a> <a href="#L-105">105</a> <a href="#L-106">106</a> <a href="#L-107">107</a> <a href="#L-108">108</a> <a href="#L-109">109</a> <a href="#L-110">110</a> <a href="#L-111">111</a> <a href="#L-112">112</a> <a href="#L-113">113</a> <a href="#L-114">114</a> <a href="#L-115">115</a> <a href="#L-116">116</a> <a href="#L-117">117</a> <a href="#L-118">118</a> <a href="#L-119">119</a> <a href="#L-120">120</a> <a href="#L-121">121</a> <a href="#L-122">122</a> <a href="#L-123">123</a> <a href="#L-124">124</a> <a href="#L-125">125</a> <a href="#L-126">126</a> <a href="#L-127">127</a> <a href="#L-128">128</a> <a href="#L-129">129</a> <a href="#L-130">130</a> <a href="#L-131">131</a> <a href="#L-132">132</a> <a href="#L-133">133</a> <a href="#L-134">134</a> <a href="#L-135">135</a> <a href="#L-136">136</a> <a href="#L-137">137</a> <a href="#L-138">138</a> <a href="#L-139">139</a> <a href="#L-140">140</a> <a href="#L-141">141</a> <a href="#L-142">142</a> <a href="#L-143">143</a> <a href="#L-144">144</a> <a href="#L-145">145</a> <a href="#L-146">146</a> <a href="#L-147">147</a> <a href="#L-148">148</a> <a href="#L-149">149</a> <a href="#L-150">150</a> <a href="#L-151">151</a> <a href="#L-152">152</a> <a href="#L-153">153</a> <a href="#L-154">154</a> <a href="#L-155">155</a> <a href="#L-156">156</a> <a href="#L-157">157</a> <a href="#L-158">158</a> <a href="#L-159">159</a> <a href="#L-160">160</a> <a href="#L-161">161</a> <a href="#L-162">162</a> <a href="#L-163">163</a> <a href="#L-164">164</a> <a href="#L-165">165</a> <a href="#L-166">166</a> <a href="#L-167">167</a> <a href="#L-168">168</a> <a href="#L-169">169</a> <a href="#L-170">170</a> <a href="#L-171">171</a> <a href="#L-172">172</a> <a href="#L-173">173</a> <a href="#L-174">174</a> <a href="#L-175">175</a> <a href="#L-176">176</a> <a href="#L-177">177</a> <a href="#L-178">178</a> <a href="#L-179">179</a> <a href="#L-180">180</a> <a href="#L-181">181</a> <a href="#L-182">182</a> <a href="#L-183">183</a> <a href="#L-184">184</a> <a href="#L-185">185</a> <a href="#L-186">186</a> <a href="#L-187">187</a> <a href="#L-188">188</a> <a href="#L-189">189</a> <a href="#L-190">190</a> <a href="#L-191">191</a> <a href="#L-192">192</a> <a href="#L-193">193</a> <a href="#L-194">194</a> <a href="#L-195">195</a> <a href="#L-196">196</a> <a href="#L-197">197</a> <a href="#L-198">198</a> <a href="#L-199">199</a> <a href="#L-200">200</a> <a href="#L-201">201</a> <a href="#L-202">202</a> <a href="#L-203">203</a> <a href="#L-204">204</a> <a href="#L-205">205</a> <a href="#L-206">206</a> <a href="#L-207">207</a> <a href="#L-208">208</a> <a href="#L-209">209</a> <a href="#L-210">210</a> <a href="#L-211">211</a> <a href="#L-212">212</a> <a href="#L-213">213</a> <a href="#L-214">214</a> <a href="#L-215">215</a> <a href="#L-216">216</a> <a href="#L-217">217</a> <a href="#L-218">218</a> <a href="#L-219">219</a> <a href="#L-220">220</a> <a href="#L-221">221</a> <a href="#L-222">222</a> <a href="#L-223">223</a> <a href="#L-224">224</a> <a href="#L-225">225</a> <a href="#L-226">226</a> <a href="#L-227">227</a> <a href="#L-228">228</a> <a href="#L-229">229</a> <a href="#L-230">230</a> <a href="#L-231">231</a> <a href="#L-232">232</a> <a href="#L-233">233</a> <a href="#L-234">234</a> <a href="#L-235">235</a> <a href="#L-236">236</a> <a href="#L-237">237</a> <a href="#L-238">238</a> <a href="#L-239">239</a> <a href="#L-240">240</a> <a href="#L-241">241</a> <a href="#L-242">242</a> <a href="#L-243">243</a> <a href="#L-244">244</a> <a href="#L-245">245</a> <a href="#L-246">246</a> <a href="#L-247">247</a> <a href="#L-248">248</a> <a href="#L-249">249</a> <a href="#L-250">250</a> <a href="#L-251">251</a> <a href="#L-252">252</a> <a href="#L-253">253</a> <a href="#L-254">254</a> <a href="#L-255">255</a> <a href="#L-256">256</a> <a href="#L-257">257</a> <a href="#L-258">258</a> <a href="#L-259">259</a> <a href="#L-260">260</a> <a href="#L-261">261</a> <a href="#L-262">262</a> <a href="#L-263">263</a> <a href="#L-264">264</a> <a href="#L-265">265</a> <a href="#L-266">266</a> <a href="#L-267">267</a> <a href="#L-268">268</a> <a href="#L-269">269</a> <a href="#L-270">270</a> <a href="#L-271">271</a> <a href="#L-272">272</a> <a href="#L-273">273</a> <a href="#L-274">274</a> <a href="#L-275">275</a> <a href="#L-276">276</a> <a href="#L-277">277</a> <a href="#L-278">278</a> <a href="#L-279">279</a> <a href="#L-280">280</a> <a href="#L-281">281</a> <a href="#L-282">282</a> <a href="#L-283">283</a> <a href="#L-284">284</a> <a href="#L-285">285</a> <a href="#L-286">286</a> <a href="#L-287">287</a> <a href="#L-288">288</a> <a href="#L-289">289</a> <a href="#L-290">290</a> <a href="#L-291">291</a> <a href="#L-292">292</a> <a href="#L-293">293</a> <a href="#L-294">294</a> <a href="#L-295">295</a> <a href="#L-296">296</a> <a href="#L-297">297</a> <a href="#L-298">298</a> <a href="#L-299">299</a> <a href="#L-300">300</a> <a href="#L-301">301</a> <a href="#L-302">302</a> <a href="#L-303">303</a> <a href="#L-304">304</a> <a href="#L-305">305</a> <a href="#L-306">306</a> <a href="#L-307">307</a> <a href="#L-308">308</a> <a href="#L-309">309</a> <a href="#L-310">310</a> <a href="#L-311">311</a> <a href="#L-312">312</a> <a href="#L-313">313</a> <a href="#L-314">314</a> <a href="#L-315">315</a> <a href="#L-316">316</a> <a href="#L-317">317</a> <a href="#L-318">318</a> <a href="#L-319">319</a> <a href="#L-320">320</a> <a href="#L-321">321</a> <a href="#L-322">322</a> <a href="#L-323">323</a> <a href="#L-324">324</a> <a href="#L-325">325</a> <a href="#L-326">326</a> <a href="#L-327">327</a> <a href="#L-328">328</a> <a href="#L-329">329</a> <a href="#L-330">330</a> <a href="#L-331">331</a> <a href="#L-332">332</a> <a href="#L-333">333</a> <a href="#L-334">334</a> <a href="#L-335">335</a> <a href="#L-336">336</a> <a href="#L-337">337</a> <a href="#L-338">338</a> <a href="#L-339">339</a> <a href="#L-340">340</a> <a href="#L-341">341</a> <a href="#L-342">342</a> <a href="#L-343">343</a> <a href="#L-344">344</a> <a href="#L-345">345</a> <a href="#L-346">346</a> <a href="#L-347">347</a> <a href="#L-348">348</a> <a href="#L-349">349</a> <a href="#L-350">350</a> <a href="#L-351">351</a> <a href="#L-352">352</a> <a href="#L-353">353</a> <a href="#L-354">354</a> <a href="#L-355">355</a> <a href="#L-356">356</a> <a href="#L-357">357</a> <a href="#L-358">358</a> <a href="#L-359">359</a> <a href="#L-360">360</a> <a href="#L-361">361</a> <a href="#L-362">362</a> <a href="#L-363">363</a> <a href="#L-364">364</a> <a href="#L-365">365</a> <a href="#L-366">366</a> <a href="#L-367">367</a> <a href="#L-368">368</a> <a href="#L-369">369</a> <a href="#L-370">370</a> <a href="#L-371">371</a> <a href="#L-372">372</a> <a href="#L-373">373</a> <a href="#L-374">374</a> <a href="#L-375">375</a> <a href="#L-376">376</a> <a href="#L-377">377</a> <a href="#L-378">378</a> <a href="#L-379">379</a> <a href="#L-380">380</a> <a href="#L-381">381</a> <a href="#L-382">382</a> <a href="#L-383">383</a> <a href="#L-384">384</a> <a href="#L-385">385</a> <a href="#L-386">386</a> <a href="#L-387">387</a> <a href="#L-388">388</a> <a href="#L-389">389</a> <a href="#L-390">390</a> <a href="#L-391">391</a> <a href="#L-392">392</a> <a href="#L-393">393</a> <a href="#L-394">394</a> <a href="#L-395">395</a> <a href="#L-396">396</a> <a href="#L-397">397</a> <a href="#L-398">398</a> <a href="#L-399">399</a> <a href="#L-400">400</a> <a href="#L-401">401</a> <a href="#L-402">402</a> <a href="#L-403">403</a> <a href="#L-404">404</a> <a href="#L-405">405</a> <a href="#L-406">406</a> <a href="#L-407">407</a> <a href="#L-408">408</a> <a href="#L-409">409</a> <a href="#L-410">410</a> <a href="#L-411">411</a> <a href="#L-412">412</a> <a href="#L-413">413</a> <a href="#L-414">414</a> <a href="#L-415">415</a> <a href="#L-416">416</a> <a href="#L-417">417</a> <a href="#L-418">418</a> <a href="#L-419">419</a> <a href="#L-420">420</a> <a href="#L-421">421</a> <a href="#L-422">422</a> <a href="#L-423">423</a> <a href="#L-424">424</a> <a href="#L-425">425</a> <a href="#L-426">426</a> <a href="#L-427">427</a> <a href="#L-428">428</a> <a href="#L-429">429</a> <a href="#L-430">430</a> <a href="#L-431">431</a> <a href="#L-432">432</a> <a href="#L-433">433</a> <a href="#L-434">434</a> <a href="#L-435">435</a> <a href="#L-436">436</a> <a href="#L-437">437</a> <a href="#L-438">438</a> <a href="#L-439">439</a> <a href="#L-440">440</a> <a href="#L-441">441</a> <a href="#L-442">442</a> <a href="#L-443">443</a> <a href="#L-444">444</a> <a href="#L-445">445</a> <a href="#L-446">446</a> <a href="#L-447">447</a> <a href="#L-448">448</a> <a href="#L-449">449</a> <a href="#L-450">450</a> <a href="#L-451">451</a> <a href="#L-452">452</a> <a href="#L-453">453</a> <a href="#L-454">454</a> <a href="#L-455">455</a> <a href="#L-456">456</a> <a href="#L-457">457</a> <a href="#L-458">458</a> <a href="#L-459">459</a> <a href="#L-460">460</a> <a href="#L-461">461</a> <a href="#L-462">462</a> <a href="#L-463">463</a> <a href="#L-464">464</a> <a href="#L-465">465</a> <a href="#L-466">466</a> <a href="#L-467">467</a> <a href="#L-468">468</a> <a href="#L-469">469</a> <a href="#L-470">470</a> <a href="#L-471">471</a> <a href="#L-472">472</a> <a href="#L-473">473</a> <a href="#L-474">474</a> <a href="#L-475">475</a> <a href="#L-476">476</a> <a href="#L-477">477</a> <a href="#L-478">478</a> <a href="#L-479">479</a> <a href="#L-480">480</a> <a href="#L-481">481</a> <a href="#L-482">482</a> <a href="#L-483">483</a> <a href="#L-484">484</a> <a href="#L-485">485</a> <a href="#L-486">486</a> <a href="#L-487">487</a> <a href="#L-488">488</a> <a href="#L-489">489</a> <a href="#L-490">490</a> <a href="#L-491">491</a> <a href="#L-492">492</a> <a href="#L-493">493</a> <a href="#L-494">494</a> <a href="#L-495">495</a> <a href="#L-496">496</a> <a href="#L-497">497</a> <a href="#L-498">498</a> <a href="#L-499">499</a> <a href="#L-500">500</a> <a href="#L-501">501</a> <a href="#L-502">502</a> <a href="#L-503">503</a> <a href="#L-504">504</a> <a href="#L-505">505</a> <a href="#L-506">506</a> <a href="#L-507">507</a> <a href="#L-508">508</a> <a href="#L-509">509</a> <a href="#L-510">510</a> <a href="#L-511">511</a> <a href="#L-512">512</a> <a href="#L-513">513</a> <a href="#L-514">514</a> <a href="#L-515">515</a> <a href="#L-516">516</a> <a href="#L-517">517</a> <a href="#L-518">518</a> <a href="#L-519">519</a> <a href="#L-520">520</a> <a href="#L-521">521</a> <a href="#L-522">522</a> <a href="#L-523">523</a> <a href="#L-524">524</a> <a href="#L-525">525</a> <a href="#L-526">526</a> <a href="#L-527">527</a> <a href="#L-528">528</a> <a href="#L-529">529</a> <a href="#L-530">530</a> <a href="#L-531">531</a> <a href="#L-532">532</a> <a href="#L-533">533</a> <a href="#L-534">534</a> <a href="#L-535">535</a> <a href="#L-536">536</a> <a href="#L-537">537</a> <a href="#L-538">538</a> <a href="#L-539">539</a> <a href="#L-540">540</a> <a href="#L-541">541</a> <a href="#L-542">542</a> <a href="#L-543">543</a> <a href="#L-544">544</a> <a href="#L-545">545</a> <a href="#L-546">546</a> <a href="#L-547">547</a> <a href="#L-548">548</a> <a href="#L-549">549</a> <a href="#L-550">550</a> <a href="#L-551">551</a> <a href="#L-552">552</a> <a href="#L-553">553</a> <a href="#L-554">554</a> <a href="#L-555">555</a> <a href="#L-556">556</a> <a href="#L-557">557</a> <a href="#L-558">558</a> <a href="#L-559">559</a> <a href="#L-560">560</a> <a href="#L-561">561</a> <a href="#L-562">562</a> <a href="#L-563">563</a> <a href="#L-564">564</a> <a href="#L-565">565</a> <a href="#L-566">566</a> <a href="#L-567">567</a> <a href="#L-568">568</a> <a href="#L-569">569</a> <a href="#L-570">570</a> <a href="#L-571">571</a> <a href="#L-572">572</a> <a href="#L-573">573</a> <a href="#L-574">574</a> <a href="#L-575">575</a> <a href="#L-576">576</a> <a href="#L-577">577</a> <a href="#L-578">578</a> <a href="#L-579">579</a> <a href="#L-580">580</a> <a href="#L-581">581</a> <a href="#L-582">582</a> <a href="#L-583">583</a> <a href="#L-584">584</a> <a href="#L-585">585</a> <a href="#L-586">586</a> <a href="#L-587">587</a> <a href="#L-588">588</a> <a href="#L-589">589</a> <a href="#L-590">590</a> <a href="#L-591">591</a> <a href="#L-592">592</a> <a href="#L-593">593</a> <a href="#L-594">594</a> <a href="#L-595">595</a> <a href="#L-596">596</a> <a href="#L-597">597</a> <a href="#L-598">598</a> <a href="#L-599">599</a> <a href="#L-600">600</a> <a href="#L-601">601</a> <a href="#L-602">602</a> <a href="#L-603">603</a> <a href="#L-604">604</a> <a href="#L-605">605</a> <a href="#L-606">606</a> <a href="#L-607">607</a> <a href="#L-608">608</a> <a href="#L-609">609</a> <a href="#L-610">610</a> <a href="#L-611">611</a> <a href="#L-612">612</a> <a href="#L-613">613</a> <a href="#L-614">614</a> <a href="#L-615">615</a> <a href="#L-616">616</a> <a href="#L-617">617</a> <a href="#L-618">618</a> <a href="#L-619">619</a> <a href="#L-620">620</a> <a href="#L-621">621</a> <a href="#L-622">622</a> <a href="#L-623">623</a> <a href="#L-624">624</a> <a href="#L-625">625</a> <a href="#L-626">626</a> <a href="#L-627">627</a> <a href="#L-628">628</a> <a href="#L-629">629</a> <a href="#L-630">630</a> <a href="#L-631">631</a> <a href="#L-632">632</a> <a href="#L-633">633</a> <a href="#L-634">634</a> <a href="#L-635">635</a> <a href="#L-636">636</a> <a href="#L-637">637</a> <a href="#L-638">638</a> <a href="#L-639">639</a> <a href="#L-640">640</a> <a href="#L-641">641</a> <a href="#L-642">642</a> <a href="#L-643">643</a> <a href="#L-644">644</a> <a href="#L-645">645</a> <a href="#L-646">646</a> <a href="#L-647">647</a> <a href="#L-648">648</a> <a href="#L-649">649</a> <a href="#L-650">650</a> <a href="#L-651">651</a> <a href="#L-652">652</a> <a href="#L-653">653</a> <a href="#L-654">654</a> <a href="#L-655">655</a> <a href="#L-656">656</a> <a href="#L-657">657</a> <a href="#L-658">658</a> <a href="#L-659">659</a> <a href="#L-660">660</a> <a href="#L-661">661</a> <a href="#L-662">662</a> <a href="#L-663">663</a> <a href="#L-664">664</a> <a href="#L-665">665</a> <a href="#L-666">666</a> <a href="#L-667">667</a> <a href="#L-668">668</a> <a href="#L-669">669</a> <a href="#L-670">670</a> <a href="#L-671">671</a> <a href="#L-672">672</a> <a href="#L-673">673</a> <a href="#L-674">674</a> <a href="#L-675">675</a> <a href="#L-676">676</a> <a href="#L-677">677</a> <a href="#L-678">678</a> <a href="#L-679">679</a> <a href="#L-680">680</a> <a href="#L-681">681</a> <a href="#L-682">682</a> <a href="#L-683">683</a> <a href="#L-684">684</a> <a href="#L-685">685</a> <a href="#L-686">686</a> <a href="#L-687">687</a> <a href="#L-688">688</a> <a href="#L-689">689</a> <a href="#L-690">690</a> <a href="#L-691">691</a> <a href="#L-692">692</a> <a href="#L-693">693</a> <a href="#L-694">694</a> <a href="#L-695">695</a> <a href="#L-696">696</a> <a href="#L-697">697</a> <a href="#L-698">698</a> <a href="#L-699">699</a> <a href="#L-700">700</a> <a href="#L-701">701</a> <a href="#L-702">702</a> <a href="#L-703">703</a> <a href="#L-704">704</a> <a href="#L-705">705</a> <a href="#L-706">706</a> <a href="#L-707">707</a> <a href="#L-708">708</a> <a href="#L-709">709</a> <a href="#L-710">710</a> <a href="#L-711">711</a> <a href="#L-712">712</a> <a href="#L-713">713</a> <a href="#L-714">714</a> <a href="#L-715">715</a> <a href="#L-716">716</a> <a href="#L-717">717</a> <a href="#L-718">718</a> <a href="#L-719">719</a> <a href="#L-720">720</a> <a href="#L-721">721</a> <a href="#L-722">722</a> <a href="#L-723">723</a> <a href="#L-724">724</a> <a href="#L-725">725</a> <a href="#L-726">726</a> <a href="#L-727">727</a> <a href="#L-728">728</a> <a href="#L-729">729</a> <a href="#L-730">730</a> <a href="#L-731">731</a> <a href="#L-732">732</a> <a href="#L-733">733</a> <a href="#L-734">734</a> <a href="#L-735">735</a> <a href="#L-736">736</a> <a href="#L-737">737</a> <a href="#L-738">738</a> <a href="#L-739">739</a> <a href="#L-740">740</a> <a href="#L-741">741</a> <a href="#L-742">742</a> <a href="#L-743">743</a> <a href="#L-744">744</a> <a href="#L-745">745</a> <a href="#L-746">746</a> <a href="#L-747">747</a> <a href="#L-748">748</a> <a href="#L-749">749</a> <a href="#L-750">750</a> <a href="#L-751">751</a> <a href="#L-752">752</a> <a href="#L-753">753</a> <a href="#L-754">754</a> <a href="#L-755">755</a> <a href="#L-756">756</a> <a href="#L-757">757</a> <a href="#L-758">758</a> <a href="#L-759">759</a> <a href="#L-760">760</a> <a href="#L-761">761</a> <a href="#L-762">762</a> <a href="#L-763">763</a> <a href="#L-764">764</a> <a href="#L-765">765</a> <a href="#L-766">766</a> <a href="#L-767">767</a> <a href="#L-768">768</a> <a href="#L-769">769</a> <a href="#L-770">770</a> <a href="#L-771">771</a> <a href="#L-772">772</a> <a href="#L-773">773</a> <a href="#L-774">774</a> <a href="#L-775">775</a> <a href="#L-776">776</a> <a href="#L-777">777</a> <a href="#L-778">778</a> <a href="#L-779">779</a> <a href="#L-780">780</a> <a href="#L-781">781</a> <a href="#L-782">782</a> <a href="#L-783">783</a> <a href="#L-784">784</a> <a href="#L-785">785</a> <a href="#L-786">786</a> <a href="#L-787">787</a> <a href="#L-788">788</a> <a href="#L-789">789</a> <a href="#L-790">790</a> <a href="#L-791">791</a> <a href="#L-792">792</a> <a href="#L-793">793</a> <a href="#L-794">794</a> <a href="#L-795">795</a> <a href="#L-796">796</a> <a href="#L-797">797</a> <a href="#L-798">798</a> <a href="#L-799">799</a> <a href="#L-800">800</a> <a href="#L-801">801</a> <a href="#L-802">802</a> <a href="#L-803">803</a> <a href="#L-804">804</a> <a href="#L-805">805</a> <a href="#L-806">806</a> <a href="#L-807">807</a> <a href="#L-808">808</a> <a href="#L-809">809</a> <a href="#L-810">810</a> <a href="#L-811">811</a> <a href="#L-812">812</a> <a href="#L-813">813</a> <a href="#L-814">814</a> <a href="#L-815">815</a> <a href="#L-816">816</a> <a href="#L-817">817</a> <a href="#L-818">818</a> <a href="#L-819">819</a> <a href="#L-820">820</a> <a href="#L-821">821</a> <a href="#L-822">822</a> <a href="#L-823">823</a> <a href="#L-824">824</a> <a href="#L-825">825</a> <a href="#L-826">826</a> <a href="#L-827">827</a> <a href="#L-828">828</a> <a href="#L-829">829</a> <a href="#L-830">830</a> <a href="#L-831">831</a> <a href="#L-832">832</a> <a href="#L-833">833</a> <a href="#L-834">834</a> <a href="#L-835">835</a> <a href="#L-836">836</a> <a href="#L-837">837</a> <a href="#L-838">838</a> <a href="#L-839">839</a> <a href="#L-840">840</a> <a href="#L-841">841</a> <a href="#L-842">842</a> <a href="#L-843">843</a> <a href="#L-844">844</a> <a href="#L-845">845</a> <a href="#L-846">846</a> <a href="#L-847">847</a> <a href="#L-848">848</a> <a href="#L-849">849</a> <a href="#L-850">850</a> <a href="#L-851">851</a> <a href="#L-852">852</a> <a href="#L-853">853</a> <a href="#L-854">854</a> <a href="#L-855">855</a> <a href="#L-856">856</a> <a href="#L-857">857</a> <a href="#L-858">858</a> <a href="#L-859">859</a> <a href="#L-860">860</a> <a href="#L-861">861</a> <a href="#L-862">862</a> <a href="#L-863">863</a> <a href="#L-864">864</a> <a href="#L-865">865</a> <a href="#L-866">866</a> <a href="#L-867">867</a> <a href="#L-868">868</a> <a href="#L-869">869</a> <a href="#L-870">870</a> <a href="#L-871">871</a> <a href="#L-872">872</a> <a href="#L-873">873</a> <a href="#L-874">874</a> <a href="#L-875">875</a> <a href="#L-876">876</a> <a href="#L-877">877</a> <a href="#L-878">878</a> <a href="#L-879">879</a> <a href="#L-880">880</a> <a href="#L-881">881</a> <a href="#L-882">882</a> <a href="#L-883">883</a> <a href="#L-884">884</a> <a href="#L-885">885</a> <a href="#L-886">886</a> <a href="#L-887">887</a> <a href="#L-888">888</a> <a href="#L-889">889</a> <a href="#L-890">890</a> <a href="#L-891">891</a> <a href="#L-892">892</a> <a href="#L-893">893</a> <a href="#L-894">894</a> <a href="#L-895">895</a> <a href="#L-896">896</a> <a href="#L-897">897</a> <a href="#L-898">898</a> <a href="#L-899">899</a> <a href="#L-900">900</a> <a href="#L-901">901</a> <a href="#L-902">902</a> <a href="#L-903">903</a> <a href="#L-904">904</a> <a href="#L-905">905</a> <a href="#L-906">906</a> <a href="#L-907">907</a> <a href="#L-908">908</a> <a href="#L-909">909</a> <a href="#L-910">910</a> <a href="#L-911">911</a> <a href="#L-912">912</a> <a href="#L-913">913</a> <a href="#L-914">914</a> <a href="#L-915">915</a> <a href="#L-916">916</a> <a href="#L-917">917</a> <a href="#L-918">918</a> <a href="#L-919">919</a> <a href="#L-920">920</a> <a href="#L-921">921</a> <a href="#L-922">922</a> <a href="#L-923">923</a> <a href="#L-924">924</a> <a href="#L-925">925</a> <a href="#L-926">926</a> <a href="#L-927">927</a> <a href="#L-928">928</a> <a href="#L-929">929</a> <a href="#L-930">930</a> <a href="#L-931">931</a> <a href="#L-932">932</a> <a href="#L-933">933</a> <a href="#L-934">934</a> <a href="#L-935">935</a> <a href="#L-936">936</a> <a href="#L-937">937</a> <a href="#L-938">938</a> <a href="#L-939">939</a> <a href="#L-940">940</a> <a href="#L-941">941</a> <a href="#L-942">942</a> <a href="#L-943">943</a> <a href="#L-944">944</a> <a href="#L-945">945</a> <a href="#L-946">946</a> <a href="#L-947">947</a> <a href="#L-948">948</a> <a href="#L-949">949</a> <a href="#L-950">950</a> <a href="#L-951">951</a> <a href="#L-952">952</a> <a href="#L-953">953</a> <a href="#L-954">954</a> <a href="#L-955">955</a> <a href="#L-956">956</a> <a href="#L-957">957</a> <a href="#L-958">958</a> <a href="#L-959">959</a> <a href="#L-960">960</a> <a href="#L-961">961</a> <a href="#L-962">962</a> <a href="#L-963">963</a> <a href="#L-964">964</a> <a href="#L-965">965</a> <a href="#L-966">966</a> <a href="#L-967">967</a> <a href="#L-968">968</a> <a href="#L-969">969</a> <a href="#L-970">970</a> <a href="#L-971">971</a> <a href="#L-972">972</a> <a href="#L-973">973</a> <a href="#L-974">974</a> <a href="#L-975">975</a> <a href="#L-976">976</a> <a href="#L-977">977</a> <a href="#L-978">978</a> <a href="#L-979">979</a> <a href="#L-980">980</a> <a href="#L-981">981</a> <a href="#L-982">982</a> <a href="#L-983">983</a> <a href="#L-984">984</a> <a href="#L-985">985</a> <a href="#L-986">986</a> <a href="#L-987">987</a> <a href="#L-988">988</a> <a href="#L-989">989</a> <a href="#L-990">990</a> <a href="#L-991">991</a> <a href="#L-992">992</a> <a href="#L-993">993</a> <a href="#L-994">994</a> <a href="#L-995">995</a> <a href="#L-996">996</a> <a href="#L-997">997</a> <a href="#L-998">998</a> <a href="#L-999">999</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a> <span class="c1">// Copyright 2015, Google Inc. All Rights Reserved.</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="c1">//https://developers.google.com/adwords/scripts/docs/solutions/mccapp-link-checker#source-code </span> </span><span id="L-4"><a name="L-4"></a><span class="c1">// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span> </span><span id="L-5"><a name="L-5"></a><span class="c1">// you may not use this file except in compliance with the License.</span> </span><span id="L-6"><a name="L-6"></a><span class="c1">// You may obtain a copy of the License at</span> </span><span id="L-7"><a name="L-7"></a><span class="c1">//</span> </span><span id="L-8"><a name="L-8"></a><span class="c1">// http://www.apache.org/licenses/LICENSE-2.0</span> </span><span id="L-9"><a name="L-9"></a><span class="c1">//</span> </span><span id="L-10"><a name="L-10"></a><span class="c1">// Unless required by applicable law or agreed to in writing, software</span> </span><span id="L-11"><a name="L-11"></a><span class="c1">// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span> </span><span id="L-12"><a name="L-12"></a><span class="c1">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span> </span><span id="L-13"><a name="L-13"></a><span class="c1">// See the License for the specific language governing permissions and</span> </span><span id="L-14"><a name="L-14"></a><span class="c1">// limitations under the License.</span> </span><span id="L-15"><a name="L-15"></a> </span><span id="L-16"><a name="L-16"></a><span class="cm">/**</span> </span><span id="L-17"><a name="L-17"></a><span class="cm"> * @name Link Checker - Manager Accounts</span> </span><span id="L-18"><a name="L-18"></a><span class="cm"> *</span> </span><span id="L-19"><a name="L-19"></a><span class="cm"> * @overview The Link Checker script iterates through the ads, keywords, and</span> </span><span id="L-20"><a name="L-20"></a><span class="cm"> * sitelinks in your manager account hierarchy and makes sure their URLs</span> </span><span id="L-21"><a name="L-21"></a><span class="cm"> * do not produce &quot;Page not found&quot; or other types of error responses. See</span> </span><span id="L-22"><a name="L-22"></a><span class="cm"> * https://developers.google.com/adwords/scripts/docs/solutions/mccapp-link-checker</span> </span><span id="L-23"><a name="L-23"></a><span class="cm"> * for more details.</span> </span><span id="L-24"><a name="L-24"></a><span class="cm"> *</span> </span><span id="L-25"><a name="L-25"></a><span class="cm"> * @author AdWords Scripts Team [[email protected]]</span> </span><span id="L-26"><a name="L-26"></a><span class="cm"> *</span> </span><span id="L-27"><a name="L-27"></a><span class="cm"> * @version 2.0</span> </span><span id="L-28"><a name="L-28"></a><span class="cm"> *</span> </span><span id="L-29"><a name="L-29"></a><span class="cm"> * @changelog</span> </span><span id="L-30"><a name="L-30"></a><span class="cm"> * - version 2.0</span> </span><span id="L-31"><a name="L-31"></a><span class="cm"> * - Completely revised the script to work on larger accounts and hierarchies.</span> </span><span id="L-32"><a name="L-32"></a><span class="cm"> * - version 1.3</span> </span><span id="L-33"><a name="L-33"></a><span class="cm"> * - Enhanced to include ad group sitelinks.</span> </span><span id="L-34"><a name="L-34"></a><span class="cm"> * - Updated to track completion across runs and send at most one email</span> </span><span id="L-35"><a name="L-35"></a><span class="cm"> * per day.</span> </span><span id="L-36"><a name="L-36"></a><span class="cm"> * - version 1.2</span> </span><span id="L-37"><a name="L-37"></a><span class="cm"> * - Remove label flushing code from the script.</span> </span><span id="L-38"><a name="L-38"></a><span class="cm"> * - version 1.1</span> </span><span id="L-39"><a name="L-39"></a><span class="cm"> * - Remove some debug code.</span> </span><span id="L-40"><a name="L-40"></a><span class="cm"> * - version 1.0</span> </span><span id="L-41"><a name="L-41"></a><span class="cm"> * - Released initial version.</span> </span><span id="L-42"><a name="L-42"></a><span class="cm"> */</span> </span><span id="L-43"><a name="L-43"></a> </span><span id="L-44"><a name="L-44"></a><span class="kd">var</span> <span class="nx">CONFIG</span> <span class="o">=</span> <span class="p">{</span> </span><span id="L-45"><a name="L-45"></a> <span class="c1">// URL of the spreadsheet template.</span> </span><span id="L-46"><a name="L-46"></a> <span class="c1">// This should be a copy of https://goo.gl/8YLeMj.</span> </span><span id="L-47"><a name="L-47"></a> <span class="nx">SPREADSHEET_URL</span><span class="o">:</span> <span class="s1">&#39;YOUR_SPREADSHEET_URL&#39;</span><span class="p">,</span> </span><span id="L-48"><a name="L-48"></a> </span><span id="L-49"><a name="L-49"></a> <span class="c1">// Array of addresses to be alerted via email if issues are found.</span> </span><span id="L-50"><a name="L-50"></a> <span class="nx">RECIPIENT_EMAILS</span><span class="o">:</span> <span class="p">[</span> </span><span id="L-51"><a name="L-51"></a> <span class="s1">&#39;YOUR_EMAIL_HERE&#39;</span> </span><span id="L-52"><a name="L-52"></a> <span class="p">],</span> </span><span id="L-53"><a name="L-53"></a> </span><span id="L-54"><a name="L-54"></a> <span class="c1">// Label to use when a link has been checked.</span> </span><span id="L-55"><a name="L-55"></a> <span class="nx">LABEL</span><span class="o">:</span> <span class="s1">&#39;LinkChecker_Done&#39;</span><span class="p">,</span> </span><span id="L-56"><a name="L-56"></a> </span><span id="L-57"><a name="L-57"></a> <span class="c1">// A list of ManagedAccountSelector conditions to restrict the population</span> </span><span id="L-58"><a name="L-58"></a> <span class="c1">// of child accounts that will be processed. Leave blank or comment out</span> </span><span id="L-59"><a name="L-59"></a> <span class="c1">// to include all child accounts.</span> </span><span id="L-60"><a name="L-60"></a> <span class="nx">ACCOUNT_CONDITIONS</span><span class="o">:</span> <span class="p">[],</span> </span><span id="L-61"><a name="L-61"></a> </span><span id="L-62"><a name="L-62"></a> <span class="c1">// Number of milliseconds to sleep after each URL request. Use this throttle</span> </span><span id="L-63"><a name="L-63"></a> <span class="c1">// to reduce the load that the script imposes on your web server(s).</span> </span><span id="L-64"><a name="L-64"></a> <span class="nx">THROTTLE</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span> </span><span id="L-65"><a name="L-65"></a> </span><span id="L-66"><a name="L-66"></a> <span class="c1">// Number of seconds before timeout that the script should stop checking URLs</span> </span><span id="L-67"><a name="L-67"></a> <span class="c1">// to make sure it has time to output its findings.</span> </span><span id="L-68"><a name="L-68"></a> <span class="nx">TIMEOUT_BUFFER</span><span class="o">:</span> <span class="mi">120</span> </span><span id="L-69"><a name="L-69"></a><span class="p">};</span> </span><span id="L-70"><a name="L-70"></a> </span><span id="L-71"><a name="L-71"></a><span class="cm">/**</span> </span><span id="L-72"><a name="L-72"></a><span class="cm"> * Parameters controlling the script&#39;s behavior after hitting a UrlFetchApp</span> </span><span id="L-73"><a name="L-73"></a><span class="cm"> * QPS quota limit.</span> </span><span id="L-74"><a name="L-74"></a><span class="cm"> */</span> </span><span id="L-75"><a name="L-75"></a><span class="kd">var</span> <span class="nx">QUOTA_CONFIG</span> <span class="o">=</span> <span class="p">{</span> </span><span id="L-76"><a name="L-76"></a> <span class="c1">// Initial number of milliseconds to sleep.</span> </span><span id="L-77"><a name="L-77"></a> <span class="nx">INIT_SLEEP_TIME</span><span class="o">:</span> <span class="mi">250</span><span class="p">,</span> </span><span id="L-78"><a name="L-78"></a> </span><span id="L-79"><a name="L-79"></a> <span class="c1">// Multiplicative factor to increase sleep time by on each retry.</span> </span><span id="L-80"><a name="L-80"></a> <span class="nx">BACKOFF_FACTOR</span><span class="o">:</span> <span class="mi">2</span><span class="p">,</span> </span><span id="L-81"><a name="L-81"></a> </span><span id="L-82"><a name="L-82"></a> <span class="c1">// Maximum number of tries for a single URL.</span> </span><span id="L-83"><a name="L-83"></a> <span class="nx">MAX_TRIES</span><span class="o">:</span> <span class="mi">5</span> </span><span id="L-84"><a name="L-84"></a><span class="p">};</span> </span><span id="L-85"><a name="L-85"></a> </span><span id="L-86"><a name="L-86"></a><span class="cm">/**</span> </span><span id="L-87"><a name="L-87"></a><span class="cm"> * Exceptions that prevent the script from finishing checking all URLs in an</span> </span><span id="L-88"><a name="L-88"></a><span class="cm"> * account but allow it to resume next time.</span> </span><span id="L-89"><a name="L-89"></a><span class="cm"> */</span> </span><span id="L-90"><a name="L-90"></a><span class="kd">var</span> <span class="nx">EXCEPTIONS</span> <span class="o">=</span> <span class="p">{</span> </span><span id="L-91"><a name="L-91"></a> <span class="nx">QPS</span><span class="o">:</span> <span class="s1">&#39;Reached UrlFetchApp QPS limit&#39;</span><span class="p">,</span> </span><span id="L-92"><a name="L-92"></a> <span class="nx">LIMIT</span><span class="o">:</span> <span class="s1">&#39;Reached UrlFetchApp daily quota&#39;</span><span class="p">,</span> </span><span id="L-93"><a name="L-93"></a> <span class="nx">TIMEOUT</span><span class="o">:</span> <span class="s1">&#39;Approached script execution time limit&#39;</span> </span><span id="L-94"><a name="L-94"></a><span class="p">};</span> </span><span id="L-95"><a name="L-95"></a> </span><span id="L-96"><a name="L-96"></a><span class="cm">/**</span> </span><span id="L-97"><a name="L-97"></a><span class="cm"> * Named ranges in the spreadsheet.</span> </span><span id="L-98"><a name="L-98"></a><span class="cm"> */</span> </span><span id="L-99"><a name="L-99"></a><span class="kd">var</span> <span class="nx">NAMES</span> <span class="o">=</span> <span class="p">{</span> </span><span id="L-100"><a name="L-100"></a> <span class="nx">CHECK_AD_URLS</span><span class="o">:</span> <span class="s1">&#39;checkAdUrls&#39;</span><span class="p">,</span> </span><span id="L-101"><a name="L-101"></a> <span class="nx">CHECK_KEYWORD_URLS</span><span class="o">:</span> <span class="s1">&#39;checkKeywordUrls&#39;</span><span class="p">,</span> </span><span id="L-102"><a name="L-102"></a> <span class="nx">CHECK_SITELINK_URLS</span><span class="o">:</span> <span class="s1">&#39;checkSitelinkUrls&#39;</span><span class="p">,</span> </span><span id="L-103"><a name="L-103"></a> <span class="nx">CHECK_PAUSED_ADS</span><span class="o">:</span> <span class="s1">&#39;checkPausedAds&#39;</span><span class="p">,</span> </span><span id="L-104"><a name="L-104"></a> <span class="nx">CHECK_PAUSED_KEYWORDS</span><span class="o">:</span> <span class="s1">&#39;checkPausedKeywords&#39;</span><span class="p">,</span> </span><span id="L-105"><a name="L-105"></a> <span class="nx">CHECK_PAUSED_SITELINKS</span><span class="o">:</span> <span class="s1">&#39;checkPausedSitelinks&#39;</span><span class="p">,</span> </span><span id="L-106"><a name="L-106"></a> <span class="nx">VALID_CODES</span><span class="o">:</span> <span class="s1">&#39;validCodes&#39;</span><span class="p">,</span> </span><span id="L-107"><a name="L-107"></a> <span class="nx">EMAIL_EACH_RUN</span><span class="o">:</span> <span class="s1">&#39;emailEachRun&#39;</span><span class="p">,</span> </span><span id="L-108"><a name="L-108"></a> <span class="nx">EMAIL_NON_ERRORS</span><span class="o">:</span> <span class="s1">&#39;emailNonErrors&#39;</span><span class="p">,</span> </span><span id="L-109"><a name="L-109"></a> <span class="nx">EMAIL_ON_COMPLETION</span><span class="o">:</span> <span class="s1">&#39;emailOnCompletion&#39;</span><span class="p">,</span> </span><span id="L-110"><a name="L-110"></a> <span class="nx">SAVE_ALL_URLS</span><span class="o">:</span> <span class="s1">&#39;saveAllUrls&#39;</span><span class="p">,</span> </span><span id="L-111"><a name="L-111"></a> <span class="nx">FREQUENCY</span><span class="o">:</span> <span class="s1">&#39;frequency&#39;</span><span class="p">,</span> </span><span id="L-112"><a name="L-112"></a> <span class="nx">DATE_STARTED</span><span class="o">:</span> <span class="s1">&#39;dateStarted&#39;</span><span class="p">,</span> </span><span id="L-113"><a name="L-113"></a> <span class="nx">DATE_COMPLETED</span><span class="o">:</span> <span class="s1">&#39;dateCompleted&#39;</span><span class="p">,</span> </span><span id="L-114"><a name="L-114"></a> <span class="nx">DATE_EMAILED</span><span class="o">:</span> <span class="s1">&#39;dateEmailed&#39;</span><span class="p">,</span> </span><span id="L-115"><a name="L-115"></a> <span class="nx">NUM_ERRORS</span><span class="o">:</span> <span class="s1">&#39;numErrors&#39;</span><span class="p">,</span> </span><span id="L-116"><a name="L-116"></a> <span class="nx">RESULT_HEADERS</span><span class="o">:</span> <span class="s1">&#39;resultHeaders&#39;</span><span class="p">,</span> </span><span id="L-117"><a name="L-117"></a> <span class="nx">ARCHIVE_HEADERS</span><span class="o">:</span> <span class="s1">&#39;archiveHeaders&#39;</span> </span><span id="L-118"><a name="L-118"></a><span class="p">};</span> </span><span id="L-119"><a name="L-119"></a> </span><span id="L-120"><a name="L-120"></a><span class="kd">function</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span> </span><span id="L-121"><a name="L-121"></a> <span class="kd">var</span> <span class="nx">spreadsheet</span> <span class="o">=</span> <span class="nx">SpreadsheetApp</span><span class="p">.</span><span class="nx">openByUrl</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">SPREADSHEET_URL</span><span class="p">);</span> </span><span id="L-122"><a name="L-122"></a> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">setSpreadsheetTimeZone</span><span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">currentAccount</span><span class="p">().</span><span class="nx">getTimeZone</span><span class="p">());</span> </span><span id="L-123"><a name="L-123"></a> </span><span id="L-124"><a name="L-124"></a> <span class="kd">var</span> <span class="nx">options</span> <span class="o">=</span> <span class="nx">loadOptions</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">);</span> </span><span id="L-125"><a name="L-125"></a> <span class="kd">var</span> <span class="nx">status</span> <span class="o">=</span> <span class="nx">loadStatus</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">);</span> </span><span id="L-126"><a name="L-126"></a> </span><span id="L-127"><a name="L-127"></a> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">status</span><span class="p">.</span><span class="nx">dateStarted</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-128"><a name="L-128"></a> <span class="c1">// This is the very first execution of the script.</span> </span><span id="L-129"><a name="L-129"></a> <span class="nx">startNewAnalysis</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">);</span> </span><span id="L-130"><a name="L-130"></a> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">status</span><span class="p">.</span><span class="nx">dateStarted</span> <span class="o">&gt;</span> <span class="nx">status</span><span class="p">.</span><span class="nx">dateCompleted</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-131"><a name="L-131"></a> <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Resuming work from a previous execution.&#39;</span><span class="p">);</span> </span><span id="L-132"><a name="L-132"></a> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">dayDifference</span><span class="p">(</span><span class="nx">status</span><span class="p">.</span><span class="nx">dateStarted</span><span class="p">,</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">())</span> <span class="o">&lt;</span> </span><span id="L-133"><a name="L-133"></a> <span class="nx">options</span><span class="p">.</span><span class="nx">frequency</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-134"><a name="L-134"></a> <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Waiting until &#39;</span> <span class="o">+</span> <span class="nx">options</span><span class="p">.</span><span class="nx">frequency</span> <span class="o">+</span> </span><span id="L-135"><a name="L-135"></a> <span class="s1">&#39; days have elapsed since the start of the last analysis.&#39;</span><span class="p">);</span> </span><span id="L-136"><a name="L-136"></a> <span class="k">return</span><span class="p">;</span> </span><span id="L-137"><a name="L-137"></a> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-138"><a name="L-138"></a> <span class="c1">// Enough time has passed since the last analysis to start a new one.</span> </span><span id="L-139"><a name="L-139"></a> <span class="nx">removeLabelsInAccounts</span><span class="p">();</span> </span><span id="L-140"><a name="L-140"></a> <span class="nx">removeAccountLabels</span><span class="p">([</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">LABEL</span><span class="p">]);</span> </span><span id="L-141"><a name="L-141"></a> <span class="nx">startNewAnalysis</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">);</span> </span><span id="L-142"><a name="L-142"></a> <span class="p">}</span> </span><span id="L-143"><a name="L-143"></a> </span><span id="L-144"><a name="L-144"></a> <span class="nx">ensureAccountLabels</span><span class="p">([</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">LABEL</span><span class="p">]);</span> </span><span id="L-145"><a name="L-145"></a> </span><span id="L-146"><a name="L-146"></a> <span class="c1">// Get up to 50 accounts that have not yet had all of their URLs checked.</span> </span><span id="L-147"><a name="L-147"></a> <span class="kd">var</span> <span class="nx">accountSelector</span> <span class="o">=</span> <span class="nx">getAccounts</span><span class="p">(</span><span class="kc">false</span><span class="p">).</span><span class="nx">withLimit</span><span class="p">(</span><span class="mi">50</span><span class="p">);</span> </span><span id="L-148"><a name="L-148"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">accountSelector</span><span class="p">.</span><span class="nx">get</span><span class="p">().</span><span class="nx">hasNext</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-149"><a name="L-149"></a> <span class="nx">accountSelector</span><span class="p">.</span><span class="nx">executeInParallel</span><span class="p">(</span><span class="s1">&#39;processAccount&#39;</span><span class="p">,</span> <span class="s1">&#39;processResults&#39;</span><span class="p">,</span> </span><span id="L-150"><a name="L-150"></a> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">options</span><span class="p">));</span> </span><span id="L-151"><a name="L-151"></a> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-152"><a name="L-152"></a> <span class="nx">processResults</span><span class="p">([]);</span> </span><span id="L-153"><a name="L-153"></a> <span class="p">}</span> </span><span id="L-154"><a name="L-154"></a><span class="p">}</span> </span><span id="L-155"><a name="L-155"></a> </span><span id="L-156"><a name="L-156"></a><span class="cm">/**</span> </span><span id="L-157"><a name="L-157"></a><span class="cm"> * Retrieves all accounts that either have had their URLs checked or need to</span> </span><span id="L-158"><a name="L-158"></a><span class="cm"> * have their URLs checked.</span> </span><span id="L-159"><a name="L-159"></a><span class="cm"> *</span> </span><span id="L-160"><a name="L-160"></a><span class="cm"> * @param {boolean} isChecked True to get accounts that have been checked</span> </span><span id="L-161"><a name="L-161"></a><span class="cm"> * already, false to get accounts that have not have been checked already.</span> </span><span id="L-162"><a name="L-162"></a><span class="cm"> * Ignored if the label does not exist.</span> </span><span id="L-163"><a name="L-163"></a><span class="cm"> * @return {Object} An account selector.</span> </span><span id="L-164"><a name="L-164"></a><span class="cm"> */</span> </span><span id="L-165"><a name="L-165"></a><span class="kd">function</span> <span class="nx">getAccounts</span><span class="p">(</span><span class="nx">isChecked</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-166"><a name="L-166"></a> <span class="kd">var</span> <span class="nx">accountSelector</span> <span class="o">=</span> <span class="nx">MccApp</span><span class="p">.</span><span class="nx">accounts</span><span class="p">();</span> </span><span id="L-167"><a name="L-167"></a> </span><span id="L-168"><a name="L-168"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">getAccountLabel</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">LABEL</span><span class="p">))</span> <span class="p">{</span> </span><span id="L-169"><a name="L-169"></a> <span class="nx">accountSelector</span> <span class="o">=</span> <span class="nx">accountSelector</span><span class="p">.</span> </span><span id="L-170"><a name="L-170"></a> <span class="nx">withCondition</span><span class="p">(</span><span class="s1">&#39;LabelNames &#39;</span> <span class="o">+</span> </span><span id="L-171"><a name="L-171"></a> <span class="p">(</span><span class="nx">isChecked</span> <span class="o">?</span> <span class="s1">&#39;CONTAINS&#39;</span> <span class="o">:</span> <span class="s1">&#39;DOES_NOT_CONTAIN&#39;</span><span class="p">)</span> <span class="o">+</span> </span><span id="L-172"><a name="L-172"></a> <span class="s1">&#39; &quot;&#39;</span> <span class="o">+</span> <span class="nx">CONFIG</span><span class="p">.</span><span class="nx">LABEL</span> <span class="o">+</span> <span class="s1">&#39;&quot;&#39;</span><span class="p">);</span> </span><span id="L-173"><a name="L-173"></a> <span class="p">}</span> </span><span id="L-174"><a name="L-174"></a> </span><span id="L-175"><a name="L-175"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">ACCOUNT_CONDITIONS</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-176"><a name="L-176"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">CONFIG</span><span class="p">.</span><span class="nx">ACCOUNT_CONDITIONS</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-177"><a name="L-177"></a> <span class="nx">accountSelector</span> <span class="o">=</span> </span><span id="L-178"><a name="L-178"></a> <span class="nx">accountSelector</span><span class="p">.</span><span class="nx">withCondition</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">ACCOUNT_CONDITIONS</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span> </span><span id="L-179"><a name="L-179"></a> <span class="p">}</span> </span><span id="L-180"><a name="L-180"></a> <span class="p">}</span> </span><span id="L-181"><a name="L-181"></a> </span><span id="L-182"><a name="L-182"></a> <span class="k">return</span> <span class="nx">accountSelector</span><span class="p">;</span> </span><span id="L-183"><a name="L-183"></a><span class="p">}</span> </span><span id="L-184"><a name="L-184"></a> </span><span id="L-185"><a name="L-185"></a><span class="cm">/**</span> </span><span id="L-186"><a name="L-186"></a><span class="cm"> * Removes the tracking in each account that was previously analyzed, thereby</span> </span><span id="L-187"><a name="L-187"></a><span class="cm"> * clearing that account for a new analysis.</span> </span><span id="L-188"><a name="L-188"></a><span class="cm"> */</span> </span><span id="L-189"><a name="L-189"></a><span class="kd">function</span> <span class="nx">removeLabelsInAccounts</span><span class="p">()</span> <span class="p">{</span> </span><span id="L-190"><a name="L-190"></a> <span class="kd">var</span> <span class="nx">managerAccount</span> <span class="o">=</span> <span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">currentAccount</span><span class="p">();</span> </span><span id="L-191"><a name="L-191"></a> <span class="kd">var</span> <span class="nx">accounts</span> <span class="o">=</span> <span class="nx">getAccounts</span><span class="p">(</span><span class="kc">true</span><span class="p">).</span><span class="nx">get</span><span class="p">();</span> </span><span id="L-192"><a name="L-192"></a> </span><span id="L-193"><a name="L-193"></a> <span class="k">while</span> <span class="p">(</span><span class="nx">accounts</span><span class="p">.</span><span class="nx">hasNext</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-194"><a name="L-194"></a> <span class="nx">MccApp</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="nx">accounts</span><span class="p">.</span><span class="nx">next</span><span class="p">());</span> </span><span id="L-195"><a name="L-195"></a> <span class="nx">removeLabels</span><span class="p">([</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">LABEL</span><span class="p">]);</span> </span><span id="L-196"><a name="L-196"></a> <span class="p">}</span> </span><span id="L-197"><a name="L-197"></a> </span><span id="L-198"><a name="L-198"></a> <span class="nx">MccApp</span><span class="p">.</span><span class="nx">select</span><span class="p">(</span><span class="nx">managerAccount</span><span class="p">);</span> </span><span id="L-199"><a name="L-199"></a><span class="p">}</span> </span><span id="L-200"><a name="L-200"></a> </span><span id="L-201"><a name="L-201"></a><span class="cm">/**</span> </span><span id="L-202"><a name="L-202"></a><span class="cm"> * Performs the link checking analysis on the current account.</span> </span><span id="L-203"><a name="L-203"></a><span class="cm"> *</span> </span><span id="L-204"><a name="L-204"></a><span class="cm"> * @param {string} options Options from the spreadsheet as JSON.</span> </span><span id="L-205"><a name="L-205"></a><span class="cm"> * @return {string} JSON stringified results of the analysis.</span> </span><span id="L-206"><a name="L-206"></a><span class="cm"> */</span> </span><span id="L-207"><a name="L-207"></a><span class="kd">function</span> <span class="nx">processAccount</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-208"><a name="L-208"></a> <span class="k">return</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">analyzeAccount</span><span class="p">(</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">options</span><span class="p">)));</span> </span><span id="L-209"><a name="L-209"></a><span class="p">}</span> </span><span id="L-210"><a name="L-210"></a> </span><span id="L-211"><a name="L-211"></a><span class="cm">/**</span> </span><span id="L-212"><a name="L-212"></a><span class="cm"> * Consolidates results from each account and outputs them.</span> </span><span id="L-213"><a name="L-213"></a><span class="cm"> *</span> </span><span id="L-214"><a name="L-214"></a><span class="cm"> * @param {Array.&lt;Object&gt;} executionResults A list of ExecutionResult objects.</span> </span><span id="L-215"><a name="L-215"></a><span class="cm"> */</span> </span><span id="L-216"><a name="L-216"></a><span class="kd">function</span> <span class="nx">processResults</span><span class="p">(</span><span class="nx">executionResults</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-217"><a name="L-217"></a> <span class="kd">var</span> <span class="nx">spreadsheet</span> <span class="o">=</span> <span class="nx">SpreadsheetApp</span><span class="p">.</span><span class="nx">openByUrl</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">SPREADSHEET_URL</span><span class="p">);</span> </span><span id="L-218"><a name="L-218"></a> <span class="kd">var</span> <span class="nx">options</span> <span class="o">=</span> <span class="nx">loadOptions</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">);</span> </span><span id="L-219"><a name="L-219"></a> <span class="kd">var</span> <span class="nx">results</span> <span class="o">=</span> <span class="p">{</span> </span><span id="L-220"><a name="L-220"></a> <span class="nx">urlChecks</span><span class="o">:</span> <span class="p">[],</span> </span><span id="L-221"><a name="L-221"></a> <span class="nx">didComplete</span><span class="o">:</span> <span class="kc">true</span> </span><span id="L-222"><a name="L-222"></a> <span class="p">};</span> </span><span id="L-223"><a name="L-223"></a> </span><span id="L-224"><a name="L-224"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">executionResults</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-225"><a name="L-225"></a> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">executionResults</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">getError</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-226"><a name="L-226"></a> <span class="kd">var</span> <span class="nx">accountResult</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">executionResults</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">getReturnValue</span><span class="p">());</span> </span><span id="L-227"><a name="L-227"></a> <span class="nx">results</span><span class="p">.</span><span class="nx">urlChecks</span> <span class="o">=</span> <span class="nx">results</span><span class="p">.</span><span class="nx">urlChecks</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">accountResult</span><span class="p">.</span><span class="nx">urlChecks</span><span class="p">);</span> </span><span id="L-228"><a name="L-228"></a> <span class="nx">results</span><span class="p">.</span><span class="nx">didComplete</span> <span class="o">=</span> <span class="nx">results</span><span class="p">.</span><span class="nx">didComplete</span> <span class="o">&amp;&amp;</span> <span class="nx">accountResult</span><span class="p">.</span><span class="nx">didComplete</span><span class="p">;</span> </span><span id="L-229"><a name="L-229"></a> </span><span id="L-230"><a name="L-230"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">accountResult</span><span class="p">.</span><span class="nx">didComplete</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-231"><a name="L-231"></a> <span class="nx">MccApp</span><span class="p">.</span><span class="nx">accounts</span><span class="p">().</span><span class="nx">withIds</span><span class="p">([</span><span class="nx">executionResults</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">getCustomerId</span><span class="p">()]).</span><span class="nx">get</span><span class="p">().</span> </span><span id="L-232"><a name="L-232"></a> <span class="nx">next</span><span class="p">().</span><span class="nx">applyLabel</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">LABEL</span><span class="p">);</span> </span><span id="L-233"><a name="L-233"></a> <span class="p">}</span> </span><span id="L-234"><a name="L-234"></a> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-235"><a name="L-235"></a> <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Processing for &#39;</span> <span class="o">+</span> <span class="nx">executionResults</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">getCustomerId</span><span class="p">()</span> <span class="o">+</span> </span><span id="L-236"><a name="L-236"></a> <span class="s1">&#39; failed.&#39;</span><span class="p">);</span> </span><span id="L-237"><a name="L-237"></a> <span class="p">}</span> </span><span id="L-238"><a name="L-238"></a> <span class="p">}</span> </span><span id="L-239"><a name="L-239"></a> </span><span id="L-240"><a name="L-240"></a> <span class="c1">// The entire analysis is not complete if there are any accounts that have</span> </span><span id="L-241"><a name="L-241"></a> <span class="c1">// not been labeled (i.e., the account was not started, or not all URLs in</span> </span><span id="L-242"><a name="L-242"></a> <span class="c1">// the account have been checked).</span> </span><span id="L-243"><a name="L-243"></a> <span class="nx">results</span><span class="p">.</span><span class="nx">didComplete</span> <span class="o">=</span> <span class="nx">results</span><span class="p">.</span><span class="nx">didComplete</span> <span class="o">&amp;&amp;</span> </span><span id="L-244"><a name="L-244"></a> <span class="o">!</span><span class="nx">getAccounts</span><span class="p">(</span><span class="kc">false</span><span class="p">).</span><span class="nx">get</span><span class="p">().</span><span class="nx">hasNext</span><span class="p">();</span> </span><span id="L-245"><a name="L-245"></a> </span><span id="L-246"><a name="L-246"></a> <span class="nx">outputResults</span><span class="p">(</span><span class="nx">results</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span> </span><span id="L-247"><a name="L-247"></a><span class="p">}</span> </span><span id="L-248"><a name="L-248"></a> </span><span id="L-249"><a name="L-249"></a><span class="cm">/**</span> </span><span id="L-250"><a name="L-250"></a><span class="cm"> * Checks as many new URLs as possible that have not previously been checked,</span> </span><span id="L-251"><a name="L-251"></a><span class="cm"> * subject to quota and time limits.</span> </span><span id="L-252"><a name="L-252"></a><span class="cm"> *</span> </span><span id="L-253"><a name="L-253"></a><span class="cm"> * @param {Object} options Dictionary of options.</span> </span><span id="L-254"><a name="L-254"></a><span class="cm"> * @return {Object} An object with fields for the URLs checked and an indication</span> </span><span id="L-255"><a name="L-255"></a><span class="cm"> * if the analysis was completed (no remaining URLs to check).</span> </span><span id="L-256"><a name="L-256"></a><span class="cm"> */</span> </span><span id="L-257"><a name="L-257"></a><span class="kd">function</span> <span class="nx">analyzeAccount</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-258"><a name="L-258"></a> <span class="c1">// Ensure the label exists before attempting to retrieve already checked URLs.</span> </span><span id="L-259"><a name="L-259"></a> <span class="nx">ensureLabels</span><span class="p">([</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">LABEL</span><span class="p">]);</span> </span><span id="L-260"><a name="L-260"></a> </span><span id="L-261"><a name="L-261"></a> <span class="kd">var</span> <span class="nx">checkedUrls</span> <span class="o">=</span> <span class="nx">getAlreadyCheckedUrls</span><span class="p">(</span><span class="nx">options</span><span class="p">);</span> </span><span id="L-262"><a name="L-262"></a> <span class="kd">var</span> <span class="nx">urlChecks</span> <span class="o">=</span> <span class="p">[];</span> </span><span id="L-263"><a name="L-263"></a> <span class="kd">var</span> <span class="nx">didComplete</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span> </span><span id="L-264"><a name="L-264"></a> </span><span id="L-265"><a name="L-265"></a> <span class="k">try</span> <span class="p">{</span> </span><span id="L-266"><a name="L-266"></a> <span class="c1">// If the script throws an exception, didComplete will remain false.</span> </span><span id="L-267"><a name="L-267"></a> <span class="nx">didComplete</span> <span class="o">=</span> <span class="nx">checkUrls</span><span class="p">(</span><span class="nx">checkedUrls</span><span class="p">,</span> <span class="nx">urlChecks</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span> </span><span id="L-268"><a name="L-268"></a> <span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-269"><a name="L-269"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">e</span> <span class="o">==</span> <span class="nx">EXCEPTIONS</span><span class="p">.</span><span class="nx">QPS</span> <span class="o">||</span> </span><span id="L-270"><a name="L-270"></a> <span class="nx">e</span> <span class="o">==</span> <span class="nx">EXCEPTIONS</span><span class="p">.</span><span class="nx">LIMIT</span> <span class="o">||</span> </span><span id="L-271"><a name="L-271"></a> <span class="nx">e</span> <span class="o">==</span> <span class="nx">EXCEPTIONS</span><span class="p">.</span><span class="nx">TIMEOUT</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-272"><a name="L-272"></a> <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Stopped checking URLs early because: &#39;</span> <span class="o">+</span> <span class="nx">e</span><span class="p">);</span> </span><span id="L-273"><a name="L-273"></a> <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Checked URLs will still be output.&#39;</span><span class="p">);</span> </span><span id="L-274"><a name="L-274"></a> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-275"><a name="L-275"></a> <span class="k">throw</span> <span class="nx">e</span><span class="p">;</span> </span><span id="L-276"><a name="L-276"></a> <span class="p">}</span> </span><span id="L-277"><a name="L-277"></a> <span class="p">}</span> </span><span id="L-278"><a name="L-278"></a> </span><span id="L-279"><a name="L-279"></a> <span class="k">return</span> <span class="p">{</span> </span><span id="L-280"><a name="L-280"></a> <span class="nx">urlChecks</span><span class="o">:</span> <span class="nx">urlChecks</span><span class="p">,</span> </span><span id="L-281"><a name="L-281"></a> <span class="nx">didComplete</span><span class="o">:</span> <span class="nx">didComplete</span> </span><span id="L-282"><a name="L-282"></a> <span class="p">};</span> </span><span id="L-283"><a name="L-283"></a><span class="p">}</span> </span><span id="L-284"><a name="L-284"></a> </span><span id="L-285"><a name="L-285"></a><span class="cm">/**</span> </span><span id="L-286"><a name="L-286"></a><span class="cm"> * Outputs the results to a spreadsheet and sends emails if appropriate.</span> </span><span id="L-287"><a name="L-287"></a><span class="cm"> *</span> </span><span id="L-288"><a name="L-288"></a><span class="cm"> * @param {Object} results An object with fields for the URLs checked and an</span> </span><span id="L-289"><a name="L-289"></a><span class="cm"> * indication if the analysis was completed (no remaining URLs to check).</span> </span><span id="L-290"><a name="L-290"></a><span class="cm"> * @param {Object} options Dictionary of options.</span> </span><span id="L-291"><a name="L-291"></a><span class="cm"> */</span> </span><span id="L-292"><a name="L-292"></a><span class="kd">function</span> <span class="nx">outputResults</span><span class="p">(</span><span class="nx">results</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-293"><a name="L-293"></a> <span class="kd">var</span> <span class="nx">spreadsheet</span> <span class="o">=</span> <span class="nx">SpreadsheetApp</span><span class="p">.</span><span class="nx">openByUrl</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">SPREADSHEET_URL</span><span class="p">);</span> </span><span id="L-294"><a name="L-294"></a> </span><span id="L-295"><a name="L-295"></a> <span class="kd">var</span> <span class="nx">numErrors</span> <span class="o">=</span> <span class="nx">countErrors</span><span class="p">(</span><span class="nx">results</span><span class="p">.</span><span class="nx">urlChecks</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span> </span><span id="L-296"><a name="L-296"></a> <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Found &#39;</span> <span class="o">+</span> <span class="nx">numErrors</span> <span class="o">+</span> <span class="s1">&#39; this execution.&#39;</span><span class="p">);</span> </span><span id="L-297"><a name="L-297"></a> </span><span id="L-298"><a name="L-298"></a> <span class="nx">saveUrlsToSpreadsheet</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">,</span> <span class="nx">results</span><span class="p">.</span><span class="nx">urlChecks</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span> </span><span id="L-299"><a name="L-299"></a> </span><span id="L-300"><a name="L-300"></a> <span class="c1">// Reload the status to get the total number of errors for the entire</span> </span><span id="L-301"><a name="L-301"></a> <span class="c1">// analysis, which is calculated by the spreadsheet.</span> </span><span id="L-302"><a name="L-302"></a> <span class="nx">status</span> <span class="o">=</span> <span class="nx">loadStatus</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">);</span> </span><span id="L-303"><a name="L-303"></a> </span><span id="L-304"><a name="L-304"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">results</span><span class="p">.</span><span class="nx">didComplete</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-305"><a name="L-305"></a> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">getRangeByName</span><span class="p">(</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">DATE_COMPLETED</span><span class="p">).</span><span class="nx">setValue</span><span class="p">(</span><span class="k">new</span> <span class="nb">Date</span><span class="p">());</span> </span><span id="L-306"><a name="L-306"></a> <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Found &#39;</span> <span class="o">+</span> <span class="nx">status</span><span class="p">.</span><span class="nx">numErrors</span> <span class="o">+</span> <span class="s1">&#39; across the entire analysis.&#39;</span><span class="p">);</span> </span><span id="L-307"><a name="L-307"></a> <span class="p">}</span> </span><span id="L-308"><a name="L-308"></a> </span><span id="L-309"><a name="L-309"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">RECIPIENT_EMAILS</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-310"><a name="L-310"></a> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">results</span><span class="p">.</span><span class="nx">didComplete</span> <span class="o">&amp;&amp;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">emailEachRun</span> <span class="o">&amp;&amp;</span> </span><span id="L-311"><a name="L-311"></a> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">emailNonErrors</span> <span class="o">||</span> <span class="nx">numErrors</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">))</span> <span class="p">{</span> </span><span id="L-312"><a name="L-312"></a> <span class="nx">sendIntermediateEmail</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">,</span> <span class="nx">numErrors</span><span class="p">);</span> </span><span id="L-313"><a name="L-313"></a> <span class="p">}</span> </span><span id="L-314"><a name="L-314"></a> </span><span id="L-315"><a name="L-315"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">results</span><span class="p">.</span><span class="nx">didComplete</span> <span class="o">&amp;&amp;</span> </span><span id="L-316"><a name="L-316"></a> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">emailEachRun</span> <span class="o">||</span> <span class="nx">options</span><span class="p">.</span><span class="nx">emailOnCompletion</span><span class="p">)</span> <span class="o">&amp;&amp;</span> </span><span id="L-317"><a name="L-317"></a> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">emailNonErrors</span> <span class="o">||</span> <span class="nx">status</span><span class="p">.</span><span class="nx">numErrors</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">))</span> <span class="p">{</span> </span><span id="L-318"><a name="L-318"></a> <span class="nx">sendFinalEmail</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">,</span> <span class="nx">status</span><span class="p">.</span><span class="nx">numErrors</span><span class="p">);</span> </span><span id="L-319"><a name="L-319"></a> <span class="p">}</span> </span><span id="L-320"><a name="L-320"></a> <span class="p">}</span> </span><span id="L-321"><a name="L-321"></a><span class="p">}</span> </span><span id="L-322"><a name="L-322"></a> </span><span id="L-323"><a name="L-323"></a><span class="cm">/**</span> </span><span id="L-324"><a name="L-324"></a><span class="cm"> * Loads data from a spreadsheet based on named ranges. Strings &#39;Yes&#39; and &#39;No&#39;</span> </span><span id="L-325"><a name="L-325"></a><span class="cm"> * are converted to booleans. One-dimensional ranges are converted to arrays</span> </span><span id="L-326"><a name="L-326"></a><span class="cm"> * with blank cells omitted. Assumes each named range exists.</span> </span><span id="L-327"><a name="L-327"></a><span class="cm"> *</span> </span><span id="L-328"><a name="L-328"></a><span class="cm"> * @param {Object} spreadsheet The spreadsheet object.</span> </span><span id="L-329"><a name="L-329"></a><span class="cm"> * @param {Array.&lt;string&gt;} names A list of named ranges that should be loaded.</span> </span><span id="L-330"><a name="L-330"></a><span class="cm"> * @return {Object} A dictionary with the names as keys and the values</span> </span><span id="L-331"><a name="L-331"></a><span class="cm"> * as the cell values from the spreadsheet.</span> </span><span id="L-332"><a name="L-332"></a><span class="cm"> */</span> </span><span id="L-333"><a name="L-333"></a><span class="kd">function</span> <span class="nx">loadDatabyName</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">,</span> <span class="nx">names</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-334"><a name="L-334"></a> <span class="kd">var</span> <span class="nx">data</span> <span class="o">=</span> <span class="p">{};</span> </span><span id="L-335"><a name="L-335"></a> </span><span id="L-336"><a name="L-336"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">names</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-337"><a name="L-337"></a> <span class="kd">var</span> <span class="nx">name</span> <span class="o">=</span> <span class="nx">names</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> </span><span id="L-338"><a name="L-338"></a> <span class="kd">var</span> <span class="nx">range</span> <span class="o">=</span> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">getRangeByName</span><span class="p">(</span><span class="nx">name</span><span class="p">);</span> </span><span id="L-339"><a name="L-339"></a> </span><span id="L-340"><a name="L-340"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">range</span><span class="p">.</span><span class="nx">getNumRows</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="nx">range</span><span class="p">.</span><span class="nx">getNumColumns</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-341"><a name="L-341"></a> <span class="c1">// Name refers to a 2d range, so load it as a 2d array.</span> </span><span id="L-342"><a name="L-342"></a> <span class="nx">data</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">range</span><span class="p">.</span><span class="nx">getValues</span><span class="p">();</span> </span><span id="L-343"><a name="L-343"></a> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">range</span><span class="p">.</span><span class="nx">getNumRows</span><span class="p">()</span> <span class="o">==</span> <span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="nx">range</span><span class="p">.</span><span class="nx">getNumColumns</span><span class="p">()</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-344"><a name="L-344"></a> <span class="c1">// Name refers to a single cell, so load it as a value and replace</span> </span><span id="L-345"><a name="L-345"></a> <span class="c1">// Yes/No with boolean true/false.</span> </span><span id="L-346"><a name="L-346"></a> <span class="nx">data</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">range</span><span class="p">.</span><span class="nx">getValue</span><span class="p">();</span> </span><span id="L-347"><a name="L-347"></a> <span class="nx">data</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">data</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">===</span> <span class="s1">&#39;Yes&#39;</span> <span class="o">?</span> <span class="kc">true</span> <span class="o">:</span> <span class="nx">data</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span> </span><span id="L-348"><a name="L-348"></a> <span class="nx">data</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">data</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">===</span> <span class="s1">&#39;No&#39;</span> <span class="o">?</span> <span class="kc">false</span> <span class="o">:</span> <span class="nx">data</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span> </span><span id="L-349"><a name="L-349"></a> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-350"><a name="L-350"></a> <span class="c1">// Name refers to a 1d range, so load it as an array (regardless of</span> </span><span id="L-351"><a name="L-351"></a> <span class="c1">// whether the 1d range is oriented horizontally or vertically).</span> </span><span id="L-352"><a name="L-352"></a> <span class="kd">var</span> <span class="nx">isByRow</span> <span class="o">=</span> <span class="nx">range</span><span class="p">.</span><span class="nx">getNumRows</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">;</span> </span><span id="L-353"><a name="L-353"></a> <span class="kd">var</span> <span class="nx">limit</span> <span class="o">=</span> <span class="nx">isByRow</span> <span class="o">?</span> <span class="nx">range</span><span class="p">.</span><span class="nx">getNumRows</span><span class="p">()</span> <span class="o">:</span> <span class="nx">range</span><span class="p">.</span><span class="nx">getNumColumns</span><span class="p">();</span> </span><span id="L-354"><a name="L-354"></a> <span class="kd">var</span> <span class="nx">cellValues</span> <span class="o">=</span> <span class="nx">range</span><span class="p">.</span><span class="nx">getValues</span><span class="p">();</span> </span><span id="L-355"><a name="L-355"></a> </span><span id="L-356"><a name="L-356"></a> <span class="nx">data</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">[];</span> </span><span id="L-357"><a name="L-357"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">j</span> <span class="o">&lt;</span> <span class="nx">limit</span><span class="p">;</span> <span class="nx">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-358"><a name="L-358"></a> <span class="kd">var</span> <span class="nx">cellValue</span> <span class="o">=</span> <span class="nx">isByRow</span> <span class="o">?</span> <span class="nx">cellValues</span><span class="p">[</span><span class="nx">j</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">:</span> <span class="nx">cellValues</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="nx">j</span><span class="p">];</span> </span><span id="L-359"><a name="L-359"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">cellValue</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-360"><a name="L-360"></a> <span class="nx">data</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">push</span><span class="p">(</span><span class="nx">cellValue</span><span class="p">);</span> </span><span id="L-361"><a name="L-361"></a> <span class="p">}</span> </span><span id="L-362"><a name="L-362"></a> <span class="p">}</span> </span><span id="L-363"><a name="L-363"></a> <span class="p">}</span> </span><span id="L-364"><a name="L-364"></a> <span class="p">}</span> </span><span id="L-365"><a name="L-365"></a> </span><span id="L-366"><a name="L-366"></a> <span class="k">return</span> <span class="nx">data</span><span class="p">;</span> </span><span id="L-367"><a name="L-367"></a><span class="p">}</span> </span><span id="L-368"><a name="L-368"></a> </span><span id="L-369"><a name="L-369"></a><span class="cm">/**</span> </span><span id="L-370"><a name="L-370"></a><span class="cm"> * Loads options from the spreadsheet.</span> </span><span id="L-371"><a name="L-371"></a><span class="cm"> *</span> </span><span id="L-372"><a name="L-372"></a><span class="cm"> * @param {Object} spreadsheet The spreadsheet object.</span> </span><span id="L-373"><a name="L-373"></a><span class="cm"> * @return {Object} A dictionary of options.</span> </span><span id="L-374"><a name="L-374"></a><span class="cm"> */</span> </span><span id="L-375"><a name="L-375"></a><span class="kd">function</span> <span class="nx">loadOptions</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-376"><a name="L-376"></a> <span class="k">return</span> <span class="nx">loadDatabyName</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">,</span> </span><span id="L-377"><a name="L-377"></a> <span class="p">[</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">CHECK_AD_URLS</span><span class="p">,</span> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">CHECK_KEYWORD_URLS</span><span class="p">,</span> </span><span id="L-378"><a name="L-378"></a> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">CHECK_SITELINK_URLS</span><span class="p">,</span> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">CHECK_PAUSED_ADS</span><span class="p">,</span> </span><span id="L-379"><a name="L-379"></a> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">CHECK_PAUSED_KEYWORDS</span><span class="p">,</span> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">CHECK_PAUSED_SITELINKS</span><span class="p">,</span> </span><span id="L-380"><a name="L-380"></a> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">VALID_CODES</span><span class="p">,</span> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">EMAIL_EACH_RUN</span><span class="p">,</span> </span><span id="L-381"><a name="L-381"></a> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">EMAIL_NON_ERRORS</span><span class="p">,</span> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">EMAIL_ON_COMPLETION</span><span class="p">,</span> </span><span id="L-382"><a name="L-382"></a> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">SAVE_ALL_URLS</span><span class="p">,</span> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">FREQUENCY</span><span class="p">]);</span> </span><span id="L-383"><a name="L-383"></a><span class="p">}</span> </span><span id="L-384"><a name="L-384"></a> </span><span id="L-385"><a name="L-385"></a><span class="cm">/**</span> </span><span id="L-386"><a name="L-386"></a><span class="cm"> * Loads state information from the spreadsheet.</span> </span><span id="L-387"><a name="L-387"></a><span class="cm"> *</span> </span><span id="L-388"><a name="L-388"></a><span class="cm"> * @param {Object} spreadsheet The spreadsheet object.</span> </span><span id="L-389"><a name="L-389"></a><span class="cm"> * @return {Object} A dictionary of status information.</span> </span><span id="L-390"><a name="L-390"></a><span class="cm"> */</span> </span><span id="L-391"><a name="L-391"></a><span class="kd">function</span> <span class="nx">loadStatus</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-392"><a name="L-392"></a> <span class="k">return</span> <span class="nx">loadDatabyName</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">,</span> </span><span id="L-393"><a name="L-393"></a> <span class="p">[</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">DATE_STARTED</span><span class="p">,</span> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">DATE_COMPLETED</span><span class="p">,</span> </span><span id="L-394"><a name="L-394"></a> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">DATE_EMAILED</span><span class="p">,</span> <span class="nx">NAMES</span><span class="p">.</span><span class="nx">NUM_ERRORS</span><span class="p">]);</span> </span><span id="L-395"><a name="L-395"></a><span class="p">}</span> </span><span id="L-396"><a name="L-396"></a> </span><span id="L-397"><a name="L-397"></a><span class="cm">/**</span> </span><span id="L-398"><a name="L-398"></a><span class="cm"> * Saves the start date to the spreadsheet and archives results of the last</span> </span><span id="L-399"><a name="L-399"></a><span class="cm"> * analysis to a separate sheet.</span> </span><span id="L-400"><a name="L-400"></a><span class="cm"> *</span> </span><span id="L-401"><a name="L-401"></a><span class="cm"> * @param {Object} spreadsheet The spreadsheet object.</span> </span><span id="L-402"><a name="L-402"></a><span class="cm"> */</span> </span><span id="L-403"><a name="L-403"></a><span class="kd">function</span> <span class="nx">startNewAnalysis</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-404"><a name="L-404"></a> <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Starting a new analysis.&#39;</span><span class="p">);</span> </span><span id="L-405"><a name="L-405"></a> </span><span id="L-406"><a name="L-406"></a> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">getRangeByName</span><span class="p">(</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">DATE_STARTED</span><span class="p">).</span><span class="nx">setValue</span><span class="p">(</span><span class="k">new</span> <span class="nb">Date</span><span class="p">());</span> </span><span id="L-407"><a name="L-407"></a> </span><span id="L-408"><a name="L-408"></a> <span class="c1">// Helper method to get the output area on the results or archive sheets.</span> </span><span id="L-409"><a name="L-409"></a> <span class="kd">var</span> <span class="nx">getOutputRange</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">rangeName</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-410"><a name="L-410"></a> <span class="kd">var</span> <span class="nx">headers</span> <span class="o">=</span> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">getRangeByName</span><span class="p">(</span><span class="nx">rangeName</span><span class="p">);</span> </span><span id="L-411"><a name="L-411"></a> <span class="k">return</span> <span class="nx">headers</span><span class="p">.</span><span class="nx">offset</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">headers</span><span class="p">.</span><span class="nx">getSheet</span><span class="p">().</span><span class="nx">getDataRange</span><span class="p">().</span><span class="nx">getLastRow</span><span class="p">());</span> </span><span id="L-412"><a name="L-412"></a> <span class="p">};</span> </span><span id="L-413"><a name="L-413"></a> </span><span id="L-414"><a name="L-414"></a> <span class="nx">getOutputRange</span><span class="p">(</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">ARCHIVE_HEADERS</span><span class="p">).</span><span class="nx">clearContent</span><span class="p">();</span> </span><span id="L-415"><a name="L-415"></a> </span><span id="L-416"><a name="L-416"></a> <span class="kd">var</span> <span class="nx">results</span> <span class="o">=</span> <span class="nx">getOutputRange</span><span class="p">(</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">RESULT_HEADERS</span><span class="p">);</span> </span><span id="L-417"><a name="L-417"></a> <span class="nx">results</span><span class="p">.</span><span class="nx">copyTo</span><span class="p">(</span><span class="nx">getOutputRange</span><span class="p">(</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">ARCHIVE_HEADERS</span><span class="p">));</span> </span><span id="L-418"><a name="L-418"></a> </span><span id="L-419"><a name="L-419"></a> <span class="nx">getOutputRange</span><span class="p">(</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">RESULT_HEADERS</span><span class="p">).</span><span class="nx">clearContent</span><span class="p">();</span> </span><span id="L-420"><a name="L-420"></a><span class="p">}</span> </span><span id="L-421"><a name="L-421"></a> </span><span id="L-422"><a name="L-422"></a><span class="cm">/**</span> </span><span id="L-423"><a name="L-423"></a><span class="cm"> * Counts the number of errors in the results.</span> </span><span id="L-424"><a name="L-424"></a><span class="cm"> *</span> </span><span id="L-425"><a name="L-425"></a><span class="cm"> * @param {Array.&lt;Object&gt;} urlChecks A list of URL check results.</span> </span><span id="L-426"><a name="L-426"></a><span class="cm"> * @param {Object} options Dictionary of options.</span> </span><span id="L-427"><a name="L-427"></a><span class="cm"> * @return {number} The number of errors in the results.</span> </span><span id="L-428"><a name="L-428"></a><span class="cm"> */</span> </span><span id="L-429"><a name="L-429"></a><span class="kd">function</span> <span class="nx">countErrors</span><span class="p">(</span><span class="nx">urlChecks</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-430"><a name="L-430"></a> <span class="kd">var</span> <span class="nx">numErrors</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> </span><span id="L-431"><a name="L-431"></a> </span><span id="L-432"><a name="L-432"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">urlChecks</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-433"><a name="L-433"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">validCodes</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">urlChecks</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">responseCode</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-434"><a name="L-434"></a> <span class="nx">numErrors</span><span class="o">++</span><span class="p">;</span> </span><span id="L-435"><a name="L-435"></a> <span class="p">}</span> </span><span id="L-436"><a name="L-436"></a> <span class="p">}</span> </span><span id="L-437"><a name="L-437"></a> </span><span id="L-438"><a name="L-438"></a> <span class="k">return</span> <span class="nx">numErrors</span><span class="p">;</span> </span><span id="L-439"><a name="L-439"></a><span class="p">}</span> </span><span id="L-440"><a name="L-440"></a> </span><span id="L-441"><a name="L-441"></a><span class="cm">/**</span> </span><span id="L-442"><a name="L-442"></a><span class="cm"> * Saves URLs for a particular account to the spreadsheet starting at the first</span> </span><span id="L-443"><a name="L-443"></a><span class="cm"> * unused row.</span> </span><span id="L-444"><a name="L-444"></a><span class="cm"> *</span> </span><span id="L-445"><a name="L-445"></a><span class="cm"> * @param {Object} spreadsheet The spreadsheet object.</span> </span><span id="L-446"><a name="L-446"></a><span class="cm"> * @param {Array.&lt;Object&gt;} urlChecks A list of URL check results.</span> </span><span id="L-447"><a name="L-447"></a><span class="cm"> * @param {Object} options Dictionary of options.</span> </span><span id="L-448"><a name="L-448"></a><span class="cm"> */</span> </span><span id="L-449"><a name="L-449"></a><span class="kd">function</span> <span class="nx">saveUrlsToSpreadsheet</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">,</span> <span class="nx">urlChecks</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-450"><a name="L-450"></a> <span class="c1">// Build each row of output values in the order of the columns.</span> </span><span id="L-451"><a name="L-451"></a> <span class="kd">var</span> <span class="nx">outputValues</span> <span class="o">=</span> <span class="p">[];</span> </span><span id="L-452"><a name="L-452"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">urlChecks</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-453"><a name="L-453"></a> <span class="kd">var</span> <span class="nx">urlCheck</span> <span class="o">=</span> <span class="nx">urlChecks</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> </span><span id="L-454"><a name="L-454"></a> </span><span id="L-455"><a name="L-455"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">saveAllUrls</span> <span class="o">||</span> </span><span id="L-456"><a name="L-456"></a> <span class="nx">options</span><span class="p">.</span><span class="nx">validCodes</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">urlCheck</span><span class="p">.</span><span class="nx">responseCode</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-457"><a name="L-457"></a> <span class="nx">outputValues</span><span class="p">.</span><span class="nx">push</span><span class="p">([</span> </span><span id="L-458"><a name="L-458"></a> <span class="nx">urlCheck</span><span class="p">.</span><span class="nx">customerId</span><span class="p">,</span> </span><span id="L-459"><a name="L-459"></a> <span class="k">new</span> <span class="nb">Date</span><span class="p">(</span><span class="nx">urlCheck</span><span class="p">.</span><span class="nx">timestamp</span><span class="p">),</span> </span><span id="L-460"><a name="L-460"></a> <span class="nx">urlCheck</span><span class="p">.</span><span class="nx">url</span><span class="p">,</span> </span><span id="L-461"><a name="L-461"></a> <span class="nx">urlCheck</span><span class="p">.</span><span class="nx">responseCode</span><span class="p">,</span> </span><span id="L-462"><a name="L-462"></a> <span class="nx">urlCheck</span><span class="p">.</span><span class="nx">entityType</span><span class="p">,</span> </span><span id="L-463"><a name="L-463"></a> <span class="nx">urlCheck</span><span class="p">.</span><span class="nx">campaign</span><span class="p">,</span> </span><span id="L-464"><a name="L-464"></a> <span class="nx">urlCheck</span><span class="p">.</span><span class="nx">adGroup</span><span class="p">,</span> </span><span id="L-465"><a name="L-465"></a> <span class="nx">urlCheck</span><span class="p">.</span><span class="nx">ad</span><span class="p">,</span> </span><span id="L-466"><a name="L-466"></a> <span class="nx">urlCheck</span><span class="p">.</span><span class="nx">keyword</span><span class="p">,</span> </span><span id="L-467"><a name="L-467"></a> <span class="nx">urlCheck</span><span class="p">.</span><span class="nx">sitelink</span> </span><span id="L-468"><a name="L-468"></a> <span class="p">]);</span> </span><span id="L-469"><a name="L-469"></a> <span class="p">}</span> </span><span id="L-470"><a name="L-470"></a> <span class="p">}</span> </span><span id="L-471"><a name="L-471"></a> </span><span id="L-472"><a name="L-472"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">outputValues</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-473"><a name="L-473"></a> <span class="c1">// Find the first open row on the Results tab below the headers and create a</span> </span><span id="L-474"><a name="L-474"></a> <span class="c1">// range large enough to hold all of the output, one per row.</span> </span><span id="L-475"><a name="L-475"></a> <span class="kd">var</span> <span class="nx">headers</span> <span class="o">=</span> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">getRangeByName</span><span class="p">(</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">RESULT_HEADERS</span><span class="p">);</span> </span><span id="L-476"><a name="L-476"></a> <span class="kd">var</span> <span class="nx">lastRow</span> <span class="o">=</span> <span class="nx">headers</span><span class="p">.</span><span class="nx">getSheet</span><span class="p">().</span><span class="nx">getDataRange</span><span class="p">().</span><span class="nx">getLastRow</span><span class="p">();</span> </span><span id="L-477"><a name="L-477"></a> <span class="kd">var</span> <span class="nx">outputRange</span> <span class="o">=</span> <span class="nx">headers</span><span class="p">.</span><span class="nx">offset</span><span class="p">(</span><span class="nx">lastRow</span> <span class="o">-</span> <span class="nx">headers</span><span class="p">.</span><span class="nx">getRow</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> </span><span id="L-478"><a name="L-478"></a> <span class="mi">0</span><span class="p">,</span> <span class="nx">outputValues</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span> </span><span id="L-479"><a name="L-479"></a> <span class="nx">outputRange</span><span class="p">.</span><span class="nx">setValues</span><span class="p">(</span><span class="nx">outputValues</span><span class="p">);</span> </span><span id="L-480"><a name="L-480"></a> <span class="p">}</span> </span><span id="L-481"><a name="L-481"></a> </span><span id="L-482"><a name="L-482"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">CONFIG</span><span class="p">.</span><span class="nx">RECIPIENT_EMAILS</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-483"><a name="L-483"></a> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">addEditor</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">RECIPIENT_EMAILS</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span> </span><span id="L-484"><a name="L-484"></a> <span class="p">}</span> </span><span id="L-485"><a name="L-485"></a><span class="p">}</span> </span><span id="L-486"><a name="L-486"></a> </span><span id="L-487"><a name="L-487"></a><span class="cm">/**</span> </span><span id="L-488"><a name="L-488"></a><span class="cm"> * Sends an email to a list of email addresses with a link to the spreadsheet</span> </span><span id="L-489"><a name="L-489"></a><span class="cm"> * and the results of this execution of the script.</span> </span><span id="L-490"><a name="L-490"></a><span class="cm"> *</span> </span><span id="L-491"><a name="L-491"></a><span class="cm"> * @param {Object} spreadsheet The spreadsheet object.</span> </span><span id="L-492"><a name="L-492"></a><span class="cm"> * @param {boolean} numErrors The number of errors found in this execution.</span> </span><span id="L-493"><a name="L-493"></a><span class="cm"> */</span> </span><span id="L-494"><a name="L-494"></a><span class="kd">function</span> <span class="nx">sendIntermediateEmail</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">,</span> <span class="nx">numErrors</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-495"><a name="L-495"></a> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">getRangeByName</span><span class="p">(</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">DATE_EMAILED</span><span class="p">).</span><span class="nx">setValue</span><span class="p">(</span><span class="k">new</span> <span class="nb">Date</span><span class="p">());</span> </span><span id="L-496"><a name="L-496"></a> </span><span id="L-497"><a name="L-497"></a> <span class="nx">MailApp</span><span class="p">.</span><span class="nx">sendEmail</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">RECIPIENT_EMAILS</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">),</span> </span><span id="L-498"><a name="L-498"></a> <span class="s1">&#39;Link Checker Results&#39;</span><span class="p">,</span> </span><span id="L-499"><a name="L-499"></a> <span class="s1">&#39;The Link Checker script found &#39;</span> <span class="o">+</span> <span class="nx">numErrors</span> <span class="o">+</span> <span class="s1">&#39; URLs with errors in &#39;</span> <span class="o">+</span> </span><span id="L-500"><a name="L-500"></a> <span class="s1">&#39;an execution that just finished. See &#39;</span> <span class="o">+</span> </span><span id="L-501"><a name="L-501"></a> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">getUrl</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39; for details.&#39;</span><span class="p">);</span> </span><span id="L-502"><a name="L-502"></a><span class="p">}</span> </span><span id="L-503"><a name="L-503"></a> </span><span id="L-504"><a name="L-504"></a><span class="cm">/**</span> </span><span id="L-505"><a name="L-505"></a><span class="cm"> * Sends an email to a list of email addresses with a link to the spreadsheet</span> </span><span id="L-506"><a name="L-506"></a><span class="cm"> * and the results across the entire account.</span> </span><span id="L-507"><a name="L-507"></a><span class="cm"> *</span> </span><span id="L-508"><a name="L-508"></a><span class="cm"> * @param {Object} spreadsheet The spreadsheet object.</span> </span><span id="L-509"><a name="L-509"></a><span class="cm"> * @param {boolean} numErrors The number of errors found in the entire account.</span> </span><span id="L-510"><a name="L-510"></a><span class="cm"> */</span> </span><span id="L-511"><a name="L-511"></a><span class="kd">function</span> <span class="nx">sendFinalEmail</span><span class="p">(</span><span class="nx">spreadsheet</span><span class="p">,</span> <span class="nx">numErrors</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-512"><a name="L-512"></a> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">getRangeByName</span><span class="p">(</span><span class="nx">NAMES</span><span class="p">.</span><span class="nx">DATE_EMAILED</span><span class="p">).</span><span class="nx">setValue</span><span class="p">(</span><span class="k">new</span> <span class="nb">Date</span><span class="p">());</span> </span><span id="L-513"><a name="L-513"></a> </span><span id="L-514"><a name="L-514"></a> <span class="nx">MailApp</span><span class="p">.</span><span class="nx">sendEmail</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">RECIPIENT_EMAILS</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">),</span> </span><span id="L-515"><a name="L-515"></a> <span class="s1">&#39;Link Checker Results&#39;</span><span class="p">,</span> </span><span id="L-516"><a name="L-516"></a> <span class="s1">&#39;The Link Checker script found &#39;</span> <span class="o">+</span> <span class="nx">numErrors</span> <span class="o">+</span> <span class="s1">&#39; URLs with errors &#39;</span> <span class="o">+</span> </span><span id="L-517"><a name="L-517"></a> <span class="s1">&#39;across its entire analysis. See &#39;</span> <span class="o">+</span> </span><span id="L-518"><a name="L-518"></a> <span class="nx">spreadsheet</span><span class="p">.</span><span class="nx">getUrl</span><span class="p">()</span> <span class="o">+</span> <span class="s1">&#39; for details.&#39;</span><span class="p">);</span> </span><span id="L-519"><a name="L-519"></a><span class="p">}</span> </span><span id="L-520"><a name="L-520"></a> </span><span id="L-521"><a name="L-521"></a><span class="cm">/**</span> </span><span id="L-522"><a name="L-522"></a><span class="cm"> * Retrieves all final URLs and mobile final URLs in the account across ads,</span> </span><span id="L-523"><a name="L-523"></a><span class="cm"> * keywords, and sitelinks that were checked in a previous run, as indicated by</span> </span><span id="L-524"><a name="L-524"></a><span class="cm"> * them having been labeled.</span> </span><span id="L-525"><a name="L-525"></a><span class="cm"> *</span> </span><span id="L-526"><a name="L-526"></a><span class="cm"> * @param {Object} options Dictionary of options.</span> </span><span id="L-527"><a name="L-527"></a><span class="cm"> * @return {Object} A map of previously checked URLs with the URL as the key.</span> </span><span id="L-528"><a name="L-528"></a><span class="cm"> */</span> </span><span id="L-529"><a name="L-529"></a><span class="kd">function</span> <span class="nx">getAlreadyCheckedUrls</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-530"><a name="L-530"></a> <span class="kd">var</span> <span class="nx">urlMap</span> <span class="o">=</span> <span class="p">{};</span> </span><span id="L-531"><a name="L-531"></a> </span><span id="L-532"><a name="L-532"></a> <span class="kd">var</span> <span class="nx">addToMap</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">items</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-533"><a name="L-533"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">items</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-534"><a name="L-534"></a> <span class="nx">urlMap</span><span class="p">[</span><span class="nx">items</span><span class="p">[</span><span class="nx">i</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span> </span><span id="L-535"><a name="L-535"></a> <span class="p">}</span> </span><span id="L-536"><a name="L-536"></a> <span class="p">};</span> </span><span id="L-537"><a name="L-537"></a> </span><span id="L-538"><a name="L-538"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">checkAdUrls</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-539"><a name="L-539"></a> <span class="nx">addToMap</span><span class="p">(</span><span class="nx">getUrlsBySelector</span><span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">ads</span><span class="p">().</span> </span><span id="L-540"><a name="L-540"></a> <span class="nx">withCondition</span><span class="p">(</span><span class="nx">labelCondition</span><span class="p">(</span><span class="kc">true</span><span class="p">))));</span> </span><span id="L-541"><a name="L-541"></a> <span class="p">}</span> </span><span id="L-542"><a name="L-542"></a> </span><span id="L-543"><a name="L-543"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">checkKeywordUrls</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-544"><a name="L-544"></a> <span class="nx">addToMap</span><span class="p">(</span><span class="nx">getUrlsBySelector</span><span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">keywords</span><span class="p">().</span> </span><span id="L-545"><a name="L-545"></a> <span class="nx">withCondition</span><span class="p">(</span><span class="nx">labelCondition</span><span class="p">(</span><span class="kc">true</span><span class="p">))));</span> </span><span id="L-546"><a name="L-546"></a> <span class="p">}</span> </span><span id="L-547"><a name="L-547"></a> </span><span id="L-548"><a name="L-548"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">checkSitelinkUrls</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-549"><a name="L-549"></a> <span class="nx">addToMap</span><span class="p">(</span><span class="nx">getAlreadyCheckedSitelinkUrls</span><span class="p">());</span> </span><span id="L-550"><a name="L-550"></a> <span class="p">}</span> </span><span id="L-551"><a name="L-551"></a> </span><span id="L-552"><a name="L-552"></a> <span class="k">return</span> <span class="nx">urlMap</span><span class="p">;</span> </span><span id="L-553"><a name="L-553"></a><span class="p">}</span> </span><span id="L-554"><a name="L-554"></a> </span><span id="L-555"><a name="L-555"></a><span class="cm">/**</span> </span><span id="L-556"><a name="L-556"></a><span class="cm"> * Retrieves all final URLs and mobile final URLs for campaign and ad group</span> </span><span id="L-557"><a name="L-557"></a><span class="cm"> * sitelinks.</span> </span><span id="L-558"><a name="L-558"></a><span class="cm"> *</span> </span><span id="L-559"><a name="L-559"></a><span class="cm"> * @return {Array.&lt;string&gt;} An array of URLs.</span> </span><span id="L-560"><a name="L-560"></a><span class="cm"> */</span> </span><span id="L-561"><a name="L-561"></a><span class="kd">function</span> <span class="nx">getAlreadyCheckedSitelinkUrls</span><span class="p">()</span> <span class="p">{</span> </span><span id="L-562"><a name="L-562"></a> <span class="kd">var</span> <span class="nx">urls</span> <span class="o">=</span> <span class="p">[];</span> </span><span id="L-563"><a name="L-563"></a> </span><span id="L-564"><a name="L-564"></a> <span class="c1">// Helper method to get campaign or ad group sitelink URLs.</span> </span><span id="L-565"><a name="L-565"></a> <span class="kd">var</span> <span class="nx">addSitelinkUrls</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">selector</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-566"><a name="L-566"></a> <span class="kd">var</span> <span class="nx">iterator</span> <span class="o">=</span> <span class="nx">selector</span><span class="p">.</span><span class="nx">withCondition</span><span class="p">(</span><span class="nx">labelCondition</span><span class="p">(</span><span class="kc">true</span><span class="p">)).</span><span class="nx">get</span><span class="p">();</span> </span><span id="L-567"><a name="L-567"></a> </span><span id="L-568"><a name="L-568"></a> <span class="k">while</span> <span class="p">(</span><span class="nx">iterator</span><span class="p">.</span><span class="nx">hasNext</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-569"><a name="L-569"></a> <span class="kd">var</span> <span class="nx">entity</span> <span class="o">=</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span> </span><span id="L-570"><a name="L-570"></a> <span class="kd">var</span> <span class="nx">sitelinks</span> <span class="o">=</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">extensions</span><span class="p">().</span><span class="nx">sitelinks</span><span class="p">();</span> </span><span id="L-571"><a name="L-571"></a> <span class="nx">urls</span> <span class="o">=</span> <span class="nx">urls</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">getUrlsBySelector</span><span class="p">(</span><span class="nx">sitelinks</span><span class="p">));</span> </span><span id="L-572"><a name="L-572"></a> <span class="p">}</span> </span><span id="L-573"><a name="L-573"></a> <span class="p">};</span> </span><span id="L-574"><a name="L-574"></a> </span><span id="L-575"><a name="L-575"></a> <span class="nx">addSitelinkUrls</span><span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">campaigns</span><span class="p">());</span> </span><span id="L-576"><a name="L-576"></a> <span class="nx">addSitelinkUrls</span><span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">adGroups</span><span class="p">());</span> </span><span id="L-577"><a name="L-577"></a> </span><span id="L-578"><a name="L-578"></a> <span class="k">return</span> <span class="nx">urls</span><span class="p">;</span> </span><span id="L-579"><a name="L-579"></a><span class="p">}</span> </span><span id="L-580"><a name="L-580"></a> </span><span id="L-581"><a name="L-581"></a><span class="cm">/**</span> </span><span id="L-582"><a name="L-582"></a><span class="cm"> * Retrieves all URLs in the entities specified by a selector.</span> </span><span id="L-583"><a name="L-583"></a><span class="cm"> *</span> </span><span id="L-584"><a name="L-584"></a><span class="cm"> * @param {Object} selector The selector specifying the entities to use.</span> </span><span id="L-585"><a name="L-585"></a><span class="cm"> * The entities should be of a type that has a urls() method.</span> </span><span id="L-586"><a name="L-586"></a><span class="cm"> * @return {Array.&lt;string&gt;} An array of URLs.</span> </span><span id="L-587"><a name="L-587"></a><span class="cm"> */</span> </span><span id="L-588"><a name="L-588"></a><span class="kd">function</span> <span class="nx">getUrlsBySelector</span><span class="p">(</span><span class="nx">selector</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-589"><a name="L-589"></a> <span class="kd">var</span> <span class="nx">urls</span> <span class="o">=</span> <span class="p">[];</span> </span><span id="L-590"><a name="L-590"></a> <span class="kd">var</span> <span class="nx">entities</span> <span class="o">=</span> <span class="nx">selector</span><span class="p">.</span><span class="nx">get</span><span class="p">();</span> </span><span id="L-591"><a name="L-591"></a> </span><span id="L-592"><a name="L-592"></a> <span class="c1">// Helper method to add the url to the list if it exists.</span> </span><span id="L-593"><a name="L-593"></a> <span class="kd">var</span> <span class="nx">addToList</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-594"><a name="L-594"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-595"><a name="L-595"></a> <span class="nx">urls</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">url</span><span class="p">);</span> </span><span id="L-596"><a name="L-596"></a> <span class="p">}</span> </span><span id="L-597"><a name="L-597"></a> <span class="p">};</span> </span><span id="L-598"><a name="L-598"></a> </span><span id="L-599"><a name="L-599"></a> <span class="k">while</span> <span class="p">(</span><span class="nx">entities</span><span class="p">.</span><span class="nx">hasNext</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-600"><a name="L-600"></a> <span class="kd">var</span> <span class="nx">entity</span> <span class="o">=</span> <span class="nx">entities</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span> </span><span id="L-601"><a name="L-601"></a> </span><span id="L-602"><a name="L-602"></a> <span class="nx">addToList</span><span class="p">(</span><span class="nx">entity</span><span class="p">.</span><span class="nx">urls</span><span class="p">().</span><span class="nx">getFinalUrl</span><span class="p">());</span> </span><span id="L-603"><a name="L-603"></a> <span class="nx">addToList</span><span class="p">(</span><span class="nx">entity</span><span class="p">.</span><span class="nx">urls</span><span class="p">().</span><span class="nx">getMobileFinalUrl</span><span class="p">());</span> </span><span id="L-604"><a name="L-604"></a> <span class="p">}</span> </span><span id="L-605"><a name="L-605"></a> </span><span id="L-606"><a name="L-606"></a> <span class="k">return</span> <span class="nx">urls</span><span class="p">;</span> </span><span id="L-607"><a name="L-607"></a><span class="p">}</span> </span><span id="L-608"><a name="L-608"></a> </span><span id="L-609"><a name="L-609"></a><span class="cm">/**</span> </span><span id="L-610"><a name="L-610"></a><span class="cm"> * Retrieves all final URLs and mobile final URLs in the account across ads,</span> </span><span id="L-611"><a name="L-611"></a><span class="cm"> * keywords, and sitelinks, and checks their response code. Does not check</span> </span><span id="L-612"><a name="L-612"></a><span class="cm"> * previously checked URLs.</span> </span><span id="L-613"><a name="L-613"></a><span class="cm"> *</span> </span><span id="L-614"><a name="L-614"></a><span class="cm"> * @param {Object} checkedUrls A map of previously checked URLs with the URL as</span> </span><span id="L-615"><a name="L-615"></a><span class="cm"> * the key.</span> </span><span id="L-616"><a name="L-616"></a><span class="cm"> * @param {Array.&lt;Object&gt;} urlChecks An array into which the results of each URL</span> </span><span id="L-617"><a name="L-617"></a><span class="cm"> * check will be inserted.</span> </span><span id="L-618"><a name="L-618"></a><span class="cm"> * @param {Object} options Dictionary of options.</span> </span><span id="L-619"><a name="L-619"></a><span class="cm"> * @return {boolean} True if all URLs were checked.</span> </span><span id="L-620"><a name="L-620"></a><span class="cm"> */</span> </span><span id="L-621"><a name="L-621"></a><span class="kd">function</span> <span class="nx">checkUrls</span><span class="p">(</span><span class="nx">checkedUrls</span><span class="p">,</span> <span class="nx">urlChecks</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-622"><a name="L-622"></a> <span class="kd">var</span> <span class="nx">didComplete</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span> </span><span id="L-623"><a name="L-623"></a> </span><span id="L-624"><a name="L-624"></a> <span class="c1">// Helper method to add common conditions to ad group and keyword selectors.</span> </span><span id="L-625"><a name="L-625"></a> <span class="kd">var</span> <span class="nx">addConditions</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">selector</span><span class="p">,</span> <span class="nx">includePaused</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-626"><a name="L-626"></a> <span class="kd">var</span> <span class="nx">statuses</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;ENABLED&#39;</span><span class="p">];</span> </span><span id="L-627"><a name="L-627"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">includePaused</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-628"><a name="L-628"></a> <span class="nx">statuses</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="s1">&#39;PAUSED&#39;</span><span class="p">);</span> </span><span id="L-629"><a name="L-629"></a> <span class="p">}</span> </span><span id="L-630"><a name="L-630"></a> </span><span id="L-631"><a name="L-631"></a> <span class="kd">var</span> <span class="nx">predicate</span> <span class="o">=</span> <span class="s1">&#39; IN [&#39;</span> <span class="o">+</span> <span class="nx">statuses</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;]&#39;</span><span class="p">;</span> </span><span id="L-632"><a name="L-632"></a> <span class="k">return</span> <span class="nx">selector</span><span class="p">.</span><span class="nx">withCondition</span><span class="p">(</span><span class="nx">labelCondition</span><span class="p">(</span><span class="kc">false</span><span class="p">)).</span> </span><span id="L-633"><a name="L-633"></a> <span class="nx">withCondition</span><span class="p">(</span><span class="s1">&#39;Status&#39;</span> <span class="o">+</span> <span class="nx">predicate</span><span class="p">).</span> </span><span id="L-634"><a name="L-634"></a> <span class="nx">withCondition</span><span class="p">(</span><span class="s1">&#39;CampaignStatus&#39;</span> <span class="o">+</span> <span class="nx">predicate</span><span class="p">).</span> </span><span id="L-635"><a name="L-635"></a> <span class="nx">withCondition</span><span class="p">(</span><span class="s1">&#39;AdGroupStatus&#39;</span> <span class="o">+</span> <span class="nx">predicate</span><span class="p">);</span> </span><span id="L-636"><a name="L-636"></a> <span class="p">};</span> </span><span id="L-637"><a name="L-637"></a> </span><span id="L-638"><a name="L-638"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">checkAdUrls</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-639"><a name="L-639"></a> <span class="nx">didComplete</span> <span class="o">=</span> <span class="nx">didComplete</span> <span class="o">&amp;&amp;</span> <span class="nx">checkUrlsBySelector</span><span class="p">(</span><span class="nx">checkedUrls</span><span class="p">,</span> <span class="nx">urlChecks</span><span class="p">,</span> </span><span id="L-640"><a name="L-640"></a> <span class="nx">addConditions</span><span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">ads</span><span class="p">().</span><span class="nx">withCondition</span><span class="p">(</span><span class="s1">&#39;CreativeFinalUrls != &quot;&quot;&#39;</span><span class="p">),</span> </span><span id="L-641"><a name="L-641"></a> <span class="nx">options</span><span class="p">.</span><span class="nx">checkPausedAds</span><span class="p">));</span> </span><span id="L-642"><a name="L-642"></a> <span class="p">}</span> </span><span id="L-643"><a name="L-643"></a> </span><span id="L-644"><a name="L-644"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">checkKeywordUrls</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-645"><a name="L-645"></a> <span class="nx">didComplete</span> <span class="o">=</span> <span class="nx">didComplete</span> <span class="o">&amp;&amp;</span> <span class="nx">checkUrlsBySelector</span><span class="p">(</span><span class="nx">checkedUrls</span><span class="p">,</span> <span class="nx">urlChecks</span><span class="p">,</span> </span><span id="L-646"><a name="L-646"></a> <span class="nx">addConditions</span><span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">keywords</span><span class="p">().</span><span class="nx">withCondition</span><span class="p">(</span><span class="s1">&#39;FinalUrls != &quot;&quot;&#39;</span><span class="p">),</span> </span><span id="L-647"><a name="L-647"></a> <span class="nx">options</span><span class="p">.</span><span class="nx">checkPausedKeywords</span><span class="p">));</span> </span><span id="L-648"><a name="L-648"></a> <span class="p">}</span> </span><span id="L-649"><a name="L-649"></a> </span><span id="L-650"><a name="L-650"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">checkSitelinkUrls</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-651"><a name="L-651"></a> <span class="nx">didComplete</span> <span class="o">=</span> <span class="nx">didComplete</span> <span class="o">&amp;&amp;</span> </span><span id="L-652"><a name="L-652"></a> <span class="nx">checkSitelinkUrls</span><span class="p">(</span><span class="nx">checkedUrls</span><span class="p">,</span> <span class="nx">urlChecks</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span> </span><span id="L-653"><a name="L-653"></a> <span class="p">}</span> </span><span id="L-654"><a name="L-654"></a> </span><span id="L-655"><a name="L-655"></a> <span class="k">return</span> <span class="nx">didComplete</span><span class="p">;</span> </span><span id="L-656"><a name="L-656"></a><span class="p">}</span> </span><span id="L-657"><a name="L-657"></a> </span><span id="L-658"><a name="L-658"></a><span class="cm">/**</span> </span><span id="L-659"><a name="L-659"></a><span class="cm"> * Retrieves all final URLs and mobile final URLs in a selector and checks them</span> </span><span id="L-660"><a name="L-660"></a><span class="cm"> * for a valid response code. Does not check previously checked URLs. Labels the</span> </span><span id="L-661"><a name="L-661"></a><span class="cm"> * entity that it was checked, if possible.</span> </span><span id="L-662"><a name="L-662"></a><span class="cm"> *</span> </span><span id="L-663"><a name="L-663"></a><span class="cm"> * @param {Object} checkedUrls A map of previously checked URLs with the URL as</span> </span><span id="L-664"><a name="L-664"></a><span class="cm"> * the key.</span> </span><span id="L-665"><a name="L-665"></a><span class="cm"> * @param {Array.&lt;Object&gt;} urlChecks An array into which the results of each URL</span> </span><span id="L-666"><a name="L-666"></a><span class="cm"> * check will be inserted.</span> </span><span id="L-667"><a name="L-667"></a><span class="cm"> * @param {Object} selector The selector specifying the entities to use.</span> </span><span id="L-668"><a name="L-668"></a><span class="cm"> * The entities should be of a type that has a urls() method.</span> </span><span id="L-669"><a name="L-669"></a><span class="cm"> * @return {boolean} True if all URLs were checked.</span> </span><span id="L-670"><a name="L-670"></a><span class="cm"> */</span> </span><span id="L-671"><a name="L-671"></a><span class="kd">function</span> <span class="nx">checkUrlsBySelector</span><span class="p">(</span><span class="nx">checkedUrls</span><span class="p">,</span> <span class="nx">urlChecks</span><span class="p">,</span> <span class="nx">selector</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-672"><a name="L-672"></a> <span class="kd">var</span> <span class="nx">customerId</span> <span class="o">=</span> <span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">currentAccount</span><span class="p">().</span><span class="nx">getCustomerId</span><span class="p">();</span> </span><span id="L-673"><a name="L-673"></a> <span class="kd">var</span> <span class="nx">iterator</span> <span class="o">=</span> <span class="nx">selector</span><span class="p">.</span><span class="nx">get</span><span class="p">();</span> </span><span id="L-674"><a name="L-674"></a> <span class="kd">var</span> <span class="nx">entities</span> <span class="o">=</span> <span class="p">[];</span> </span><span id="L-675"><a name="L-675"></a> </span><span id="L-676"><a name="L-676"></a> <span class="c1">// Helper method to check a URL.</span> </span><span id="L-677"><a name="L-677"></a> <span class="kd">var</span> <span class="nx">checkUrl</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-678"><a name="L-678"></a> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">url</span> <span class="o">||</span> <span class="nx">checkedUrls</span><span class="p">[</span><span class="nx">url</span><span class="p">])</span> <span class="p">{</span> </span><span id="L-679"><a name="L-679"></a> <span class="k">return</span><span class="p">;</span> </span><span id="L-680"><a name="L-680"></a> <span class="p">}</span> </span><span id="L-681"><a name="L-681"></a> </span><span id="L-682"><a name="L-682"></a> <span class="kd">var</span> <span class="nx">responseCode</span> <span class="o">=</span> <span class="nx">testUrl</span><span class="p">(</span><span class="nx">cleanUrl</span><span class="p">(</span><span class="nx">url</span><span class="p">));</span> </span><span id="L-683"><a name="L-683"></a> <span class="kd">var</span> <span class="nx">entityType</span> <span class="o">=</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">getEntityType</span><span class="p">();</span> </span><span id="L-684"><a name="L-684"></a> </span><span id="L-685"><a name="L-685"></a> <span class="nx">urlChecks</span><span class="p">.</span><span class="nx">push</span><span class="p">({</span> </span><span id="L-686"><a name="L-686"></a> <span class="nx">customerId</span><span class="o">:</span> <span class="nx">customerId</span><span class="p">,</span> </span><span id="L-687"><a name="L-687"></a> <span class="nx">timestamp</span><span class="o">:</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">(),</span> </span><span id="L-688"><a name="L-688"></a> <span class="nx">url</span><span class="o">:</span> <span class="nx">url</span><span class="p">,</span> </span><span id="L-689"><a name="L-689"></a> <span class="nx">responseCode</span><span class="o">:</span> <span class="nx">responseCode</span><span class="p">,</span> </span><span id="L-690"><a name="L-690"></a> <span class="nx">entityType</span><span class="o">:</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">getEntityType</span><span class="p">(),</span> </span><span id="L-691"><a name="L-691"></a> <span class="nx">campaign</span><span class="o">:</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">getCampaign</span> <span class="o">?</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">getCampaign</span><span class="p">().</span><span class="nx">getName</span><span class="p">()</span> <span class="o">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> </span><span id="L-692"><a name="L-692"></a> <span class="nx">adGroup</span><span class="o">:</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">getAdGroup</span> <span class="o">?</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">getAdGroup</span><span class="p">().</span><span class="nx">getName</span><span class="p">()</span> <span class="o">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> </span><span id="L-693"><a name="L-693"></a> <span class="nx">ad</span><span class="o">:</span> <span class="nx">entityType</span> <span class="o">==</span> <span class="s1">&#39;Ad&#39;</span> <span class="o">?</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">getHeadline</span><span class="p">()</span> <span class="o">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> </span><span id="L-694"><a name="L-694"></a> <span class="nx">keyword</span><span class="o">:</span> <span class="nx">entityType</span> <span class="o">==</span> <span class="s1">&#39;Keyword&#39;</span> <span class="o">?</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">getText</span><span class="p">()</span> <span class="o">:</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> </span><span id="L-695"><a name="L-695"></a> <span class="nx">sitelink</span><span class="o">:</span> <span class="nx">entityType</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">&#39;Sitelink&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">?</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">getLinkText</span><span class="p">()</span> <span class="o">:</span> <span class="s1">&#39;&#39;</span> </span><span id="L-696"><a name="L-696"></a> <span class="p">});</span> </span><span id="L-697"><a name="L-697"></a> </span><span id="L-698"><a name="L-698"></a> <span class="nx">checkedUrls</span><span class="p">[</span><span class="nx">url</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span> </span><span id="L-699"><a name="L-699"></a> <span class="p">};</span> </span><span id="L-700"><a name="L-700"></a> </span><span id="L-701"><a name="L-701"></a> <span class="k">while</span> <span class="p">(</span><span class="nx">iterator</span><span class="p">.</span><span class="nx">hasNext</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-702"><a name="L-702"></a> <span class="nx">entities</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">iterator</span><span class="p">.</span><span class="nx">next</span><span class="p">());</span> </span><span id="L-703"><a name="L-703"></a> <span class="p">}</span> </span><span id="L-704"><a name="L-704"></a> </span><span id="L-705"><a name="L-705"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">entities</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-706"><a name="L-706"></a> <span class="kd">var</span> <span class="nx">entity</span> <span class="o">=</span> <span class="nx">entities</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> </span><span id="L-707"><a name="L-707"></a> </span><span id="L-708"><a name="L-708"></a> <span class="nx">checkUrl</span><span class="p">(</span><span class="nx">entity</span><span class="p">.</span><span class="nx">urls</span><span class="p">().</span><span class="nx">getFinalUrl</span><span class="p">());</span> </span><span id="L-709"><a name="L-709"></a> <span class="nx">checkUrl</span><span class="p">(</span><span class="nx">entity</span><span class="p">.</span><span class="nx">urls</span><span class="p">().</span><span class="nx">getMobileFinalUrl</span><span class="p">());</span> </span><span id="L-710"><a name="L-710"></a> </span><span id="L-711"><a name="L-711"></a> <span class="c1">// Sitelinks do not have labels.</span> </span><span id="L-712"><a name="L-712"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">entity</span><span class="p">.</span><span class="nx">applyLabel</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-713"><a name="L-713"></a> <span class="nx">entity</span><span class="p">.</span><span class="nx">applyLabel</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">LABEL</span><span class="p">);</span> </span><span id="L-714"><a name="L-714"></a> <span class="nx">checkTimeout</span><span class="p">();</span> </span><span id="L-715"><a name="L-715"></a> <span class="p">}</span> </span><span id="L-716"><a name="L-716"></a> <span class="p">}</span> </span><span id="L-717"><a name="L-717"></a> </span><span id="L-718"><a name="L-718"></a> <span class="c1">// True only if we did not breach an iterator limit.</span> </span><span id="L-719"><a name="L-719"></a> <span class="k">return</span> <span class="nx">entities</span><span class="p">.</span><span class="nx">length</span> <span class="o">==</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">totalNumEntities</span><span class="p">();</span> </span><span id="L-720"><a name="L-720"></a><span class="p">}</span> </span><span id="L-721"><a name="L-721"></a> </span><span id="L-722"><a name="L-722"></a><span class="cm">/**</span> </span><span id="L-723"><a name="L-723"></a><span class="cm"> * Retrieves all final URLs and mobile final URLs for campaign and ad group</span> </span><span id="L-724"><a name="L-724"></a><span class="cm"> * sitelinks and checks them for a valid response code. Does not check</span> </span><span id="L-725"><a name="L-725"></a><span class="cm"> * previously checked URLs. Labels the containing campaign or ad group that it</span> </span><span id="L-726"><a name="L-726"></a><span class="cm"> * has been checked.</span> </span><span id="L-727"><a name="L-727"></a><span class="cm"> *</span> </span><span id="L-728"><a name="L-728"></a><span class="cm"> * @param {Object} checkedUrls A map of previously checked URLs with the URL as</span> </span><span id="L-729"><a name="L-729"></a><span class="cm"> * the key.</span> </span><span id="L-730"><a name="L-730"></a><span class="cm"> * @param {Array.&lt;Object&gt;} urlChecks An array into which the results of each URL</span> </span><span id="L-731"><a name="L-731"></a><span class="cm"> * check will be inserted.</span> </span><span id="L-732"><a name="L-732"></a><span class="cm"> * @param {Object} options Dictionary of options.</span> </span><span id="L-733"><a name="L-733"></a><span class="cm"> * @return {boolean} True if all URLs were checked.</span> </span><span id="L-734"><a name="L-734"></a><span class="cm"> */</span> </span><span id="L-735"><a name="L-735"></a><span class="kd">function</span> <span class="nx">checkSitelinkUrls</span><span class="p">(</span><span class="nx">checkedUrls</span><span class="p">,</span> <span class="nx">urlChecks</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-736"><a name="L-736"></a> <span class="kd">var</span> <span class="nx">didComplete</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span> </span><span id="L-737"><a name="L-737"></a> </span><span id="L-738"><a name="L-738"></a> <span class="c1">// Helper method to check URLs for sitelinks in a campaign or ad group</span> </span><span id="L-739"><a name="L-739"></a> <span class="c1">// selector.</span> </span><span id="L-740"><a name="L-740"></a> <span class="kd">var</span> <span class="nx">checkSitelinkUrls</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">selector</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-741"><a name="L-741"></a> <span class="kd">var</span> <span class="nx">iterator</span> <span class="o">=</span> <span class="nx">selector</span><span class="p">.</span><span class="nx">withCondition</span><span class="p">(</span><span class="nx">labelCondition</span><span class="p">(</span><span class="kc">false</span><span class="p">)).</span><span class="nx">get</span><span class="p">();</span> </span><span id="L-742"><a name="L-742"></a> <span class="kd">var</span> <span class="nx">entities</span> <span class="o">=</span> <span class="p">[];</span> </span><span id="L-743"><a name="L-743"></a> </span><span id="L-744"><a name="L-744"></a> <span class="k">while</span> <span class="p">(</span><span class="nx">iterator</span><span class="p">.</span><span class="nx">hasNext</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-745"><a name="L-745"></a> <span class="nx">entities</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">iterator</span><span class="p">.</span><span class="nx">next</span><span class="p">());</span> </span><span id="L-746"><a name="L-746"></a> <span class="p">}</span> </span><span id="L-747"><a name="L-747"></a> </span><span id="L-748"><a name="L-748"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">entities</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-749"><a name="L-749"></a> <span class="kd">var</span> <span class="nx">entity</span> <span class="o">=</span> <span class="nx">entities</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> </span><span id="L-750"><a name="L-750"></a> <span class="kd">var</span> <span class="nx">sitelinks</span> <span class="o">=</span> <span class="nx">entity</span><span class="p">.</span><span class="nx">extensions</span><span class="p">().</span><span class="nx">sitelinks</span><span class="p">();</span> </span><span id="L-751"><a name="L-751"></a> </span><span id="L-752"><a name="L-752"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">sitelinks</span><span class="p">.</span><span class="nx">get</span><span class="p">().</span><span class="nx">hasNext</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-753"><a name="L-753"></a> <span class="nx">didComplete</span> <span class="o">=</span> <span class="nx">didComplete</span> <span class="o">&amp;&amp;</span> </span><span id="L-754"><a name="L-754"></a> <span class="nx">checkUrlsBySelector</span><span class="p">(</span><span class="nx">checkedUrls</span><span class="p">,</span> <span class="nx">urlChecks</span><span class="p">,</span> <span class="nx">sitelinks</span><span class="p">);</span> </span><span id="L-755"><a name="L-755"></a> <span class="nx">entity</span><span class="p">.</span><span class="nx">applyLabel</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">LABEL</span><span class="p">);</span> </span><span id="L-756"><a name="L-756"></a> <span class="nx">checkTimeout</span><span class="p">();</span> </span><span id="L-757"><a name="L-757"></a> <span class="p">}</span> </span><span id="L-758"><a name="L-758"></a> <span class="p">}</span> </span><span id="L-759"><a name="L-759"></a> </span><span id="L-760"><a name="L-760"></a> <span class="c1">// True only if we did not breach an iterator limit.</span> </span><span id="L-761"><a name="L-761"></a> <span class="nx">didComplete</span> <span class="o">=</span> <span class="nx">didComplete</span> <span class="o">&amp;&amp;</span> </span><span id="L-762"><a name="L-762"></a> <span class="nx">entities</span><span class="p">.</span><span class="nx">length</span> <span class="o">==</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">totalNumEntities</span><span class="p">();</span> </span><span id="L-763"><a name="L-763"></a> <span class="p">};</span> </span><span id="L-764"><a name="L-764"></a> </span><span id="L-765"><a name="L-765"></a> <span class="kd">var</span> <span class="nx">statuses</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;ENABLED&#39;</span><span class="p">];</span> </span><span id="L-766"><a name="L-766"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">checkPausedSitelinks</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-767"><a name="L-767"></a> <span class="nx">statuses</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="s1">&#39;PAUSED&#39;</span><span class="p">);</span> </span><span id="L-768"><a name="L-768"></a> <span class="p">}</span> </span><span id="L-769"><a name="L-769"></a> </span><span id="L-770"><a name="L-770"></a> <span class="kd">var</span> <span class="nx">predicate</span> <span class="o">=</span> <span class="s1">&#39; IN [&#39;</span> <span class="o">+</span> <span class="nx">statuses</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;]&#39;</span><span class="p">;</span> </span><span id="L-771"><a name="L-771"></a> <span class="nx">checkSitelinkUrls</span><span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">campaigns</span><span class="p">().</span> </span><span id="L-772"><a name="L-772"></a> <span class="nx">withCondition</span><span class="p">(</span><span class="s1">&#39;Status&#39;</span> <span class="o">+</span> <span class="nx">predicate</span><span class="p">));</span> </span><span id="L-773"><a name="L-773"></a> <span class="nx">checkSitelinkUrls</span><span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">adGroups</span><span class="p">().</span> </span><span id="L-774"><a name="L-774"></a> <span class="nx">withCondition</span><span class="p">(</span><span class="s1">&#39;Status&#39;</span> <span class="o">+</span> <span class="nx">predicate</span><span class="p">).</span> </span><span id="L-775"><a name="L-775"></a> <span class="nx">withCondition</span><span class="p">(</span><span class="s1">&#39;CampaignStatus&#39;</span> <span class="o">+</span> <span class="nx">predicate</span><span class="p">));</span> </span><span id="L-776"><a name="L-776"></a> </span><span id="L-777"><a name="L-777"></a> <span class="k">return</span> <span class="nx">didComplete</span><span class="p">;</span> </span><span id="L-778"><a name="L-778"></a><span class="p">}</span> </span><span id="L-779"><a name="L-779"></a> </span><span id="L-780"><a name="L-780"></a><span class="cm">/**</span> </span><span id="L-781"><a name="L-781"></a><span class="cm"> * Cleans a URL by removing ValueTrack and custom parameters.</span> </span><span id="L-782"><a name="L-782"></a><span class="cm"> *</span> </span><span id="L-783"><a name="L-783"></a><span class="cm"> * @param {string} url The url to clean.</span> </span><span id="L-784"><a name="L-784"></a><span class="cm"> * @return {string} The cleaned URL.</span> </span><span id="L-785"><a name="L-785"></a><span class="cm"> */</span> </span><span id="L-786"><a name="L-786"></a><span class="kd">function</span> <span class="nx">cleanUrl</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-787"><a name="L-787"></a> <span class="c1">// Custom parameters are alphanumeric with a leading underscore.</span> </span><span id="L-788"><a name="L-788"></a> <span class="c1">// ValueTrack parameters may also contain a &#39;:&#39; or &#39;+&#39;.</span> </span><span id="L-789"><a name="L-789"></a> <span class="k">return</span> <span class="nx">url</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/{[0-9a-zA-Z\_\+\:]+}/g</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">);</span> </span><span id="L-790"><a name="L-790"></a><span class="p">}</span> </span><span id="L-791"><a name="L-791"></a> </span><span id="L-792"><a name="L-792"></a><span class="cm">/**</span> </span><span id="L-793"><a name="L-793"></a><span class="cm"> * Requests a given URL. Retries if the UrlFetchApp QPS limit was reached,</span> </span><span id="L-794"><a name="L-794"></a><span class="cm"> * exponentially backing off on each retry. Throws an exception if it reaches</span> </span><span id="L-795"><a name="L-795"></a><span class="cm"> * the maximum number of retries. Throws an exception if the UrlFetchApp daily</span> </span><span id="L-796"><a name="L-796"></a><span class="cm"> * quota limit was reached.</span> </span><span id="L-797"><a name="L-797"></a><span class="cm"> *</span> </span><span id="L-798"><a name="L-798"></a><span class="cm"> * @param {string} url The URL to test.</span> </span><span id="L-799"><a name="L-799"></a><span class="cm"> * @return {number} The response code received when requesting the URL.</span> </span><span id="L-800"><a name="L-800"></a><span class="cm"> */</span> </span><span id="L-801"><a name="L-801"></a><span class="kd">function</span> <span class="nx">testUrl</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-802"><a name="L-802"></a> <span class="kd">var</span> <span class="nx">responseCode</span><span class="p">;</span> </span><span id="L-803"><a name="L-803"></a> <span class="kd">var</span> <span class="nx">sleepTime</span> <span class="o">=</span> <span class="nx">QUOTA_CONFIG</span><span class="p">.</span><span class="nx">INIT_SLEEP_TIME</span><span class="p">;</span> </span><span id="L-804"><a name="L-804"></a> <span class="kd">var</span> <span class="nx">numTries</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> </span><span id="L-805"><a name="L-805"></a> </span><span id="L-806"><a name="L-806"></a> <span class="k">while</span> <span class="p">(</span><span class="nx">numTries</span> <span class="o">&lt;</span> <span class="nx">QUOTA_CONFIG</span><span class="p">.</span><span class="nx">MAX_TRIES</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">responseCode</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-807"><a name="L-807"></a> <span class="k">try</span> <span class="p">{</span> </span><span id="L-808"><a name="L-808"></a> <span class="c1">// If UrlFetchApp.fetch() throws an exception, responseCode will remain</span> </span><span id="L-809"><a name="L-809"></a> <span class="c1">// undefined.</span> </span><span id="L-810"><a name="L-810"></a> <span class="nx">responseCode</span> <span class="o">=</span> </span><span id="L-811"><a name="L-811"></a> <span class="nx">UrlFetchApp</span><span class="p">.</span><span class="nx">fetch</span><span class="p">(</span><span class="nx">url</span><span class="p">,</span> <span class="p">{</span><span class="nx">muteHttpExceptions</span><span class="o">:</span> <span class="kc">true</span><span class="p">}).</span><span class="nx">getResponseCode</span><span class="p">();</span> </span><span id="L-812"><a name="L-812"></a> </span><span id="L-813"><a name="L-813"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">THROTTLE</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-814"><a name="L-814"></a> <span class="nx">Utilities</span><span class="p">.</span><span class="nx">sleep</span><span class="p">(</span><span class="nx">CONFIG</span><span class="p">.</span><span class="nx">THROTTLE</span><span class="p">);</span> </span><span id="L-815"><a name="L-815"></a> <span class="p">}</span> </span><span id="L-816"><a name="L-816"></a> <span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-817"><a name="L-817"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">message</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">&#39;Service invoked too many times in a short time:&#39;</span><span class="p">)</span> </span><span id="L-818"><a name="L-818"></a> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-819"><a name="L-819"></a> <span class="nx">Utilities</span><span class="p">.</span><span class="nx">sleep</span><span class="p">(</span><span class="nx">sleepTime</span><span class="p">);</span> </span><span id="L-820"><a name="L-820"></a> <span class="nx">sleepTime</span> <span class="o">*=</span> <span class="nx">QUOTA_CONFIG</span><span class="p">.</span><span class="nx">BACKOFF_FACTOR</span><span class="p">;</span> </span><span id="L-821"><a name="L-821"></a> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">message</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">&#39;Service invoked too many times:&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-822"><a name="L-822"></a> <span class="k">throw</span> <span class="nx">EXCEPTIONS</span><span class="p">.</span><span class="nx">LIMIT</span><span class="p">;</span> </span><span id="L-823"><a name="L-823"></a> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">message</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">&#39;DNS error&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-824"><a name="L-824"></a> <span class="k">return</span> <span class="mi">404</span><span class="p">;</span> </span><span id="L-825"><a name="L-825"></a> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-826"><a name="L-826"></a> <span class="k">throw</span> <span class="nx">e</span><span class="p">.</span><span class="nx">message</span><span class="p">;</span> </span><span id="L-827"><a name="L-827"></a> <span class="p">}</span> </span><span id="L-828"><a name="L-828"></a> <span class="p">}</span> </span><span id="L-829"><a name="L-829"></a> </span><span id="L-830"><a name="L-830"></a> <span class="nx">numTries</span><span class="o">++</span><span class="p">;</span> </span><span id="L-831"><a name="L-831"></a> <span class="p">}</span> </span><span id="L-832"><a name="L-832"></a> </span><span id="L-833"><a name="L-833"></a> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">responseCode</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-834"><a name="L-834"></a> <span class="k">throw</span> <span class="nx">EXCEPTIONS</span><span class="p">.</span><span class="nx">QPS</span><span class="p">;</span> </span><span id="L-835"><a name="L-835"></a> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-836"><a name="L-836"></a> <span class="k">return</span> <span class="nx">responseCode</span><span class="p">;</span> </span><span id="L-837"><a name="L-837"></a> <span class="p">}</span> </span><span id="L-838"><a name="L-838"></a><span class="p">}</span> </span><span id="L-839"><a name="L-839"></a> </span><span id="L-840"><a name="L-840"></a><span class="cm">/**</span> </span><span id="L-841"><a name="L-841"></a><span class="cm"> * Throws an exception if the script is close to timing out.</span> </span><span id="L-842"><a name="L-842"></a><span class="cm"> */</span> </span><span id="L-843"><a name="L-843"></a><span class="kd">function</span> <span class="nx">checkTimeout</span><span class="p">()</span> <span class="p">{</span> </span><span id="L-844"><a name="L-844"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">getExecutionInfo</span><span class="p">().</span><span class="nx">getRemainingTime</span><span class="p">()</span> <span class="o">&lt;</span> </span><span id="L-845"><a name="L-845"></a> <span class="nx">CONFIG</span><span class="p">.</span><span class="nx">TIMEOUT_BUFFER</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-846"><a name="L-846"></a> <span class="k">throw</span> <span class="nx">EXCEPTIONS</span><span class="p">.</span><span class="nx">TIMEOUT</span><span class="p">;</span> </span><span id="L-847"><a name="L-847"></a> <span class="p">}</span> </span><span id="L-848"><a name="L-848"></a><span class="p">}</span> </span><span id="L-849"><a name="L-849"></a> </span><span id="L-850"><a name="L-850"></a><span class="cm">/**</span> </span><span id="L-851"><a name="L-851"></a><span class="cm"> * Returns the number of days between two dates.</span> </span><span id="L-852"><a name="L-852"></a><span class="cm"> *</span> </span><span id="L-853"><a name="L-853"></a><span class="cm"> * @param {Object} from The older Date object.</span> </span><span id="L-854"><a name="L-854"></a><span class="cm"> * @param {Object} to The newer (more recent) Date object.</span> </span><span id="L-855"><a name="L-855"></a><span class="cm"> * @return {number} The number of days between the given dates (possibly</span> </span><span id="L-856"><a name="L-856"></a><span class="cm"> * fractional).</span> </span><span id="L-857"><a name="L-857"></a><span class="cm"> */</span> </span><span id="L-858"><a name="L-858"></a><span class="kd">function</span> <span class="nx">dayDifference</span><span class="p">(</span><span class="nx">from</span><span class="p">,</span> <span class="nx">to</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-859"><a name="L-859"></a> <span class="k">return</span> <span class="p">(</span><span class="nx">to</span><span class="p">.</span><span class="nx">getTime</span><span class="p">()</span> <span class="o">-</span> <span class="nx">from</span><span class="p">.</span><span class="nx">getTime</span><span class="p">())</span> <span class="o">/</span> <span class="p">(</span><span class="mi">24</span> <span class="o">*</span> <span class="mi">3600</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">);</span> </span><span id="L-860"><a name="L-860"></a><span class="p">}</span> </span><span id="L-861"><a name="L-861"></a> </span><span id="L-862"><a name="L-862"></a><span class="cm">/**</span> </span><span id="L-863"><a name="L-863"></a><span class="cm"> * Builds a string to be used for withCondition() filtering for whether the</span> </span><span id="L-864"><a name="L-864"></a><span class="cm"> * label is present or not.</span> </span><span id="L-865"><a name="L-865"></a><span class="cm"> *</span> </span><span id="L-866"><a name="L-866"></a><span class="cm"> * @param {boolean} hasLabel True if the label should be present, false if the</span> </span><span id="L-867"><a name="L-867"></a><span class="cm"> * label should not be present.</span> </span><span id="L-868"><a name="L-868"></a><span class="cm"> * @return {string} A condition that can be used in withCondition().</span> </span><span id="L-869"><a name="L-869"></a><span class="cm"> */</span> </span><span id="L-870"><a name="L-870"></a><span class="kd">function</span> <span class="nx">labelCondition</span><span class="p">(</span><span class="nx">hasLabel</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-871"><a name="L-871"></a> <span class="k">return</span> <span class="s1">&#39;LabelNames &#39;</span> <span class="o">+</span> <span class="p">(</span><span class="nx">hasLabel</span> <span class="o">?</span> <span class="s1">&#39;CONTAINS_ANY&#39;</span> <span class="o">:</span> <span class="s1">&#39;CONTAINS_NONE&#39;</span><span class="p">)</span> <span class="o">+</span> </span><span id="L-872"><a name="L-872"></a> <span class="s1">&#39; [&quot;&#39;</span> <span class="o">+</span> <span class="nx">CONFIG</span><span class="p">.</span><span class="nx">LABEL</span> <span class="o">+</span> <span class="s1">&#39;&quot;]&#39;</span><span class="p">;</span> </span><span id="L-873"><a name="L-873"></a><span class="p">}</span> </span><span id="L-874"><a name="L-874"></a> </span><span id="L-875"><a name="L-875"></a><span class="cm">/**</span> </span><span id="L-876"><a name="L-876"></a><span class="cm"> * Retrieves an entity by name.</span> </span><span id="L-877"><a name="L-877"></a><span class="cm"> *</span> </span><span id="L-878"><a name="L-878"></a><span class="cm"> * @param {Object} selector A selector for an entity type with a Name field.</span> </span><span id="L-879"><a name="L-879"></a><span class="cm"> * @param {string} name The name to retrieve the entity by.</span> </span><span id="L-880"><a name="L-880"></a><span class="cm"> * @return {Object} The entity, if it exists, or null otherwise.</span> </span><span id="L-881"><a name="L-881"></a><span class="cm"> */</span> </span><span id="L-882"><a name="L-882"></a><span class="kd">function</span> <span class="nx">getEntityByName</span><span class="p">(</span><span class="nx">selector</span><span class="p">,</span> <span class="nx">name</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-883"><a name="L-883"></a> <span class="kd">var</span> <span class="nx">entities</span> <span class="o">=</span> <span class="nx">selector</span><span class="p">.</span><span class="nx">withCondition</span><span class="p">(</span><span class="s1">&#39;Name = &#39;</span> <span class="o">+</span> <span class="nx">name</span><span class="p">).</span><span class="nx">get</span><span class="p">();</span> </span><span id="L-884"><a name="L-884"></a> </span><span id="L-885"><a name="L-885"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">entities</span><span class="p">.</span><span class="nx">hasNext</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-886"><a name="L-886"></a> <span class="k">return</span> <span class="nx">entities</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span> </span><span id="L-887"><a name="L-887"></a> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-888"><a name="L-888"></a> <span class="k">return</span> <span class="kc">null</span><span class="p">;</span> </span><span id="L-889"><a name="L-889"></a> <span class="p">}</span> </span><span id="L-890"><a name="L-890"></a><span class="p">}</span> </span><span id="L-891"><a name="L-891"></a> </span><span id="L-892"><a name="L-892"></a><span class="cm">/**</span> </span><span id="L-893"><a name="L-893"></a><span class="cm"> * Retrieves a Label object by name.</span> </span><span id="L-894"><a name="L-894"></a><span class="cm"> *</span> </span><span id="L-895"><a name="L-895"></a><span class="cm"> * @param {string} labelName The label name to retrieve.</span> </span><span id="L-896"><a name="L-896"></a><span class="cm"> * @return {Object} The Label object, if it exists, or null otherwise.</span> </span><span id="L-897"><a name="L-897"></a><span class="cm"> */</span> </span><span id="L-898"><a name="L-898"></a><span class="kd">function</span> <span class="nx">getLabel</span><span class="p">(</span><span class="nx">labelName</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-899"><a name="L-899"></a> <span class="k">return</span> <span class="nx">getEntityByName</span><span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">labels</span><span class="p">(),</span> <span class="nx">labelName</span><span class="p">);</span> </span><span id="L-900"><a name="L-900"></a><span class="p">}</span> </span><span id="L-901"><a name="L-901"></a> </span><span id="L-902"><a name="L-902"></a><span class="cm">/**</span> </span><span id="L-903"><a name="L-903"></a><span class="cm"> * Retrieves an AccountLabel object by name.</span> </span><span id="L-904"><a name="L-904"></a><span class="cm"> *</span> </span><span id="L-905"><a name="L-905"></a><span class="cm"> * @param {string} labelName The label name to retrieve.</span> </span><span id="L-906"><a name="L-906"></a><span class="cm"> * @return {Object} The AccountLabel object, if it exists, or null otherwise.</span> </span><span id="L-907"><a name="L-907"></a><span class="cm"> */</span> </span><span id="L-908"><a name="L-908"></a><span class="kd">function</span> <span class="nx">getAccountLabel</span><span class="p">(</span><span class="nx">labelName</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-909"><a name="L-909"></a> <span class="k">return</span> <span class="nx">getEntityByName</span><span class="p">(</span><span class="nx">MccApp</span><span class="p">.</span><span class="nx">accountLabels</span><span class="p">(),</span> <span class="nx">labelName</span><span class="p">);</span> </span><span id="L-910"><a name="L-910"></a><span class="p">}</span> </span><span id="L-911"><a name="L-911"></a> </span><span id="L-912"><a name="L-912"></a><span class="cm">/**</span> </span><span id="L-913"><a name="L-913"></a><span class="cm"> * Checks that the account has all provided labels and creates any that are</span> </span><span id="L-914"><a name="L-914"></a><span class="cm"> * missing. Since labels cannot be created in preview mode, throws an exception</span> </span><span id="L-915"><a name="L-915"></a><span class="cm"> * if a label is missing.</span> </span><span id="L-916"><a name="L-916"></a><span class="cm"> *</span> </span><span id="L-917"><a name="L-917"></a><span class="cm"> * @param {Array.&lt;string&gt;} labelNames An array of label names.</span> </span><span id="L-918"><a name="L-918"></a><span class="cm"> */</span> </span><span id="L-919"><a name="L-919"></a><span class="kd">function</span> <span class="nx">ensureLabels</span><span class="p">(</span><span class="nx">labelNames</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-920"><a name="L-920"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">labelNames</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-921"><a name="L-921"></a> <span class="kd">var</span> <span class="nx">labelName</span> <span class="o">=</span> <span class="nx">labelNames</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> </span><span id="L-922"><a name="L-922"></a> <span class="kd">var</span> <span class="nx">label</span> <span class="o">=</span> <span class="nx">getLabel</span><span class="p">(</span><span class="nx">labelName</span><span class="p">);</span> </span><span id="L-923"><a name="L-923"></a> </span><span id="L-924"><a name="L-924"></a> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">label</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-925"><a name="L-925"></a> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">getExecutionInfo</span><span class="p">().</span><span class="nx">isPreview</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-926"><a name="L-926"></a> <span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">createLabel</span><span class="p">(</span><span class="nx">labelName</span><span class="p">);</span> </span><span id="L-927"><a name="L-927"></a> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-928"><a name="L-928"></a> <span class="k">throw</span> <span class="s1">&#39;Label &#39;</span> <span class="o">+</span> <span class="nx">labelName</span> <span class="o">+</span> <span class="s1">&#39; is missing and cannot be created in &#39;</span> <span class="o">+</span> </span><span id="L-929"><a name="L-929"></a> <span class="s1">&#39;preview mode. Please run the script or create the label manually.&#39;</span><span class="p">;</span> </span><span id="L-930"><a name="L-930"></a> <span class="p">}</span> </span><span id="L-931"><a name="L-931"></a> <span class="p">}</span> </span><span id="L-932"><a name="L-932"></a> <span class="p">}</span> </span><span id="L-933"><a name="L-933"></a><span class="p">}</span> </span><span id="L-934"><a name="L-934"></a> </span><span id="L-935"><a name="L-935"></a><span class="cm">/**</span> </span><span id="L-936"><a name="L-936"></a><span class="cm"> * Checks that the account has all provided account labels and creates any that</span> </span><span id="L-937"><a name="L-937"></a><span class="cm"> * are missing. Since labels cannot be created in preview mode, throws an</span> </span><span id="L-938"><a name="L-938"></a><span class="cm"> * exception if a label is missing.</span> </span><span id="L-939"><a name="L-939"></a><span class="cm"> *</span> </span><span id="L-940"><a name="L-940"></a><span class="cm"> * @param {Array.&lt;string&gt;} labelNames An array of label names.</span> </span><span id="L-941"><a name="L-941"></a><span class="cm"> */</span> </span><span id="L-942"><a name="L-942"></a><span class="kd">function</span> <span class="nx">ensureAccountLabels</span><span class="p">(</span><span class="nx">labelNames</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-943"><a name="L-943"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">labelNames</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-944"><a name="L-944"></a> <span class="kd">var</span> <span class="nx">labelName</span> <span class="o">=</span> <span class="nx">labelNames</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> </span><span id="L-945"><a name="L-945"></a> <span class="kd">var</span> <span class="nx">label</span> <span class="o">=</span> <span class="nx">getAccountLabel</span><span class="p">(</span><span class="nx">labelName</span><span class="p">);</span> </span><span id="L-946"><a name="L-946"></a> </span><span id="L-947"><a name="L-947"></a> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">label</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-948"><a name="L-948"></a> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">getExecutionInfo</span><span class="p">().</span><span class="nx">isPreview</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-949"><a name="L-949"></a> <span class="nx">MccApp</span><span class="p">.</span><span class="nx">createAccountLabel</span><span class="p">(</span><span class="nx">labelName</span><span class="p">);</span> </span><span id="L-950"><a name="L-950"></a> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-951"><a name="L-951"></a> <span class="k">throw</span> <span class="s1">&#39;Account label &#39;</span> <span class="o">+</span> <span class="nx">labelName</span> <span class="o">+</span> <span class="s1">&#39; is missing and cannot be &#39;</span> <span class="o">+</span> </span><span id="L-952"><a name="L-952"></a> <span class="s1">&#39;created in preview mode. Please run the script or create the &#39;</span> <span class="o">+</span> </span><span id="L-953"><a name="L-953"></a> <span class="s1">&#39;label manually.&#39;</span><span class="p">;</span> </span><span id="L-954"><a name="L-954"></a> <span class="p">}</span> </span><span id="L-955"><a name="L-955"></a> <span class="p">}</span> </span><span id="L-956"><a name="L-956"></a> <span class="p">}</span> </span><span id="L-957"><a name="L-957"></a><span class="p">}</span> </span><span id="L-958"><a name="L-958"></a> </span><span id="L-959"><a name="L-959"></a><span class="cm">/**</span> </span><span id="L-960"><a name="L-960"></a><span class="cm"> * Removes all provided labels from the account. Since labels cannot be removed</span> </span><span id="L-961"><a name="L-961"></a><span class="cm"> * in preview mode, throws an exception in preview mode.</span> </span><span id="L-962"><a name="L-962"></a><span class="cm"> *</span> </span><span id="L-963"><a name="L-963"></a><span class="cm"> * @param {Array.&lt;string&gt;} labelNames An array of label names.</span> </span><span id="L-964"><a name="L-964"></a><span class="cm"> */</span> </span><span id="L-965"><a name="L-965"></a><span class="kd">function</span> <span class="nx">removeLabels</span><span class="p">(</span><span class="nx">labelNames</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-966"><a name="L-966"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">getExecutionInfo</span><span class="p">().</span><span class="nx">isPreview</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-967"><a name="L-967"></a> <span class="k">throw</span> <span class="s1">&#39;Cannot remove labels in preview mode. Please run the script or &#39;</span> <span class="o">+</span> </span><span id="L-968"><a name="L-968"></a> <span class="s1">&#39;remove the labels manually.&#39;</span><span class="p">;</span> </span><span id="L-969"><a name="L-969"></a> <span class="p">}</span> </span><span id="L-970"><a name="L-970"></a> </span><span id="L-971"><a name="L-971"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">labelNames</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-972"><a name="L-972"></a> <span class="kd">var</span> <span class="nx">label</span> <span class="o">=</span> <span class="nx">getLabel</span><span class="p">(</span><span class="nx">labelNames</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span> </span><span id="L-973"><a name="L-973"></a> </span><span id="L-974"><a name="L-974"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">label</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-975"><a name="L-975"></a> <span class="nx">label</span><span class="p">.</span><span class="nx">remove</span><span class="p">();</span> </span><span id="L-976"><a name="L-976"></a> <span class="p">}</span> </span><span id="L-977"><a name="L-977"></a> <span class="p">}</span> </span><span id="L-978"><a name="L-978"></a><span class="p">}</span> </span><span id="L-979"><a name="L-979"></a> </span><span id="L-980"><a name="L-980"></a><span class="cm">/**</span> </span><span id="L-981"><a name="L-981"></a><span class="cm"> * Removes all provided account labels from the account. Since labels cannot be</span> </span><span id="L-982"><a name="L-982"></a><span class="cm"> * removed in preview mode, throws an exception in preview mode.</span> </span><span id="L-983"><a name="L-983"></a><span class="cm"> *</span> </span><span id="L-984"><a name="L-984"></a><span class="cm"> * @param {Array.&lt;string&gt;} labelNames An array of label names.</span> </span><span id="L-985"><a name="L-985"></a><span class="cm"> */</span> </span><span id="L-986"><a name="L-986"></a><span class="kd">function</span> <span class="nx">removeAccountLabels</span><span class="p">(</span><span class="nx">labelNames</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-987"><a name="L-987"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">AdWordsApp</span><span class="p">.</span><span class="nx">getExecutionInfo</span><span class="p">().</span><span class="nx">isPreview</span><span class="p">())</span> <span class="p">{</span> </span><span id="L-988"><a name="L-988"></a> <span class="k">throw</span> <span class="s1">&#39;Cannot remove account labels in preview mode. Please run the &#39;</span> <span class="o">+</span> </span><span id="L-989"><a name="L-989"></a> <span class="s1">&#39;script or remove the labels manually.&#39;</span><span class="p">;</span> </span><span id="L-990"><a name="L-990"></a> <span class="p">}</span> </span><span id="L-991"><a name="L-991"></a> </span><span id="L-992"><a name="L-992"></a> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">labelNames</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-993"><a name="L-993"></a> <span class="kd">var</span> <span class="nx">label</span> <span class="o">=</span> <span class="nx">getAccountLabel</span><span class="p">(</span><span class="nx">labelNames</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span> </span><span id="L-994"><a name="L-994"></a> </span><span id="L-995"><a name="L-995"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">label</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-996"><a name="L-996"></a> <span class="nx">label</span><span class="p">.</span><span class="nx">remove</span><span class="p">();</span> </span><span id="L-997"><a name="L-997"></a> <span class="p">}</span> </span><span id="L-998"><a name="L-998"></a> <span class="p">}</span> </span><span id="L-999"><a name="L-999"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
adwords, javascript
   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
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
--- 
+++ 
@@ -0,0 +1,999 @@
+    // Copyright 2015, Google Inc. All Rights Reserved.
+    
+//https://developers.google.com/adwords/scripts/docs/solutions/mccapp-link-checker#source-code 
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+/**
+ * @name Link Checker - Manager Accounts
+ *
+ * @overview The Link Checker script iterates through the ads, keywords, and
+ *     sitelinks in your manager account hierarchy and makes sure their URLs
+ *     do not produce "Page not found" or other types of error responses. See
+ *     https://developers.google.com/adwords/scripts/docs/solutions/mccapp-link-checker
+ *     for more details.
+ *
+ * @author AdWords Scripts Team [[email protected]]
+ *
+ * @version 2.0
+ *
+ * @changelog
+ * - version 2.0
+ *   - Completely revised the script to work on larger accounts and hierarchies.
+ * - version 1.3
+ *   - Enhanced to include ad group sitelinks.
+ *   - Updated to track completion across runs and send at most one email
+ *     per day.
+ * - version 1.2
+ *   - Remove label flushing code from the script.
+ * - version 1.1
+ *   - Remove some debug code.
+ * - version 1.0
+ *   - Released initial version.
+ */
+
+var CONFIG = {
+  // URL of the spreadsheet template.
+  // This should be a copy of https://goo.gl/8YLeMj.
+  SPREADSHEET_URL: 'YOUR_SPREADSHEET_URL',
+
+  // Array of addresses to be alerted via email if issues are found.
+  RECIPIENT_EMAILS: [
+    'YOUR_EMAIL_HERE'
+  ],
+
+  // Label to use when a link has been checked.
+  LABEL: 'LinkChecker_Done',
+
+  // A list of ManagedAccountSelector conditions to restrict the population
+  // of child accounts that will be processed. Leave blank or comment out
+  // to include all child accounts.
+  ACCOUNT_CONDITIONS: [],
+
+  // Number of milliseconds to sleep after each URL request. Use this throttle
+  // to reduce the load that the script imposes on your web server(s).
+  THROTTLE: 0,
+
+  // Number of seconds before timeout that the script should stop checking URLs
+  // to make sure it has time to output its findings.
+  TIMEOUT_BUFFER: 120
+};
+
+/**
+ * Parameters controlling the script's behavior after hitting a UrlFetchApp
+ * QPS quota limit.
+ */
+var QUOTA_CONFIG = {
+  // Initial number of milliseconds to sleep.
+  INIT_SLEEP_TIME: 250,
+
+  // Multiplicative factor to increase sleep time by on each retry.
+  BACKOFF_FACTOR: 2,
+
+  // Maximum number of tries for a single URL.
+  MAX_TRIES: 5
+};
+
+/**
+ * Exceptions that prevent the script from finishing checking all URLs in an
+ * account but allow it to resume next time.
+ */
+var EXCEPTIONS = {
+  QPS: 'Reached UrlFetchApp QPS limit',
+  LIMIT: 'Reached UrlFetchApp daily quota',
+  TIMEOUT: 'Approached script execution time limit'
+};
+
+/**
+ * Named ranges in the spreadsheet.
+ */
+var NAMES = {
+  CHECK_AD_URLS: 'checkAdUrls',
+  CHECK_KEYWORD_URLS: 'checkKeywordUrls',
+  CHECK_SITELINK_URLS: 'checkSitelinkUrls',
+  CHECK_PAUSED_ADS: 'checkPausedAds',
+  CHECK_PAUSED_KEYWORDS: 'checkPausedKeywords',
+  CHECK_PAUSED_SITELINKS: 'checkPausedSitelinks',
+  VALID_CODES: 'validCodes',
+  EMAIL_EACH_RUN: 'emailEachRun',
+  EMAIL_NON_ERRORS: 'emailNonErrors',
+  EMAIL_ON_COMPLETION: 'emailOnCompletion',
+  SAVE_ALL_URLS: 'saveAllUrls',
+  FREQUENCY: 'frequency',
+  DATE_STARTED: 'dateStarted',
+  DATE_COMPLETED: 'dateCompleted',
+  DATE_EMAILED: 'dateEmailed',
+  NUM_ERRORS: 'numErrors',
+  RESULT_HEADERS: 'resultHeaders',
+  ARCHIVE_HEADERS: 'archiveHeaders'
+};
+
+function main() {
+  var spreadsheet = SpreadsheetApp.openByUrl(CONFIG.SPREADSHEET_URL);
+  spreadsheet.setSpreadsheetTimeZone(AdWordsApp.currentAccount().getTimeZone());
+
+  var options = loadOptions(spreadsheet);
+  var status = loadStatus(spreadsheet);
+
+  if (!status.dateStarted) {
+    // This is the very first execution of the script.
+    startNewAnalysis(spreadsheet);
+  } else if (status.dateStarted > status.dateCompleted) {
+    Logger.log('Resuming work from a previous execution.');
+  } else if (dayDifference(status.dateStarted, new Date()) <
+             options.frequency) {
+    Logger.log('Waiting until ' + options.frequency +
+               ' days have elapsed since the start of the last analysis.');
+    return;
+  } else {
+    // Enough time has passed since the last analysis to start a new one.
+    removeLabelsInAccounts();
+    removeAccountLabels([CONFIG.LABEL]);
+    startNewAnalysis(spreadsheet);
+  }
+
+  ensureAccountLabels([CONFIG.LABEL]);
+
+  // Get up to 50 accounts that have not yet had all of their URLs checked.
+  var accountSelector = getAccounts(false).withLimit(50);
+  if (accountSelector.get().hasNext()) {
+    accountSelector.executeInParallel('processAccount', 'processResults',
+                                      JSON.stringify(options));
+  } else {
+    processResults([]);
+  }
+}
+
+/**
+ * Retrieves all accounts that either have had their URLs checked or need to
+ * have their URLs checked.
+ *
+ * @param {boolean} isChecked True to get accounts that have been checked
+ *     already, false to get accounts that have not have been checked already.
+ *     Ignored if the label does not exist.
+ * @return {Object} An account selector.
+ */
+function getAccounts(isChecked) {
+  var accountSelector = MccApp.accounts();
+
+  if (getAccountLabel(CONFIG.LABEL)) {
+    accountSelector = accountSelector.
+      withCondition('LabelNames ' +
+                    (isChecked ? 'CONTAINS' : 'DOES_NOT_CONTAIN') +
+                     ' "' + CONFIG.LABEL + '"');
+  }
+
+  if (CONFIG.ACCOUNT_CONDITIONS) {
+    for (var i = 0; i < CONFIG.ACCOUNT_CONDITIONS.length; i++) {
+      accountSelector =
+          accountSelector.withCondition(CONFIG.ACCOUNT_CONDITIONS[i]);
+    }
+  }
+
+  return accountSelector;
+}
+
+/**
+ * Removes the tracking in each account that was previously analyzed, thereby
+ * clearing that account for a new analysis.
+ */
+function removeLabelsInAccounts() {
+  var managerAccount = AdWordsApp.currentAccount();
+  var accounts = getAccounts(true).get();
+
+  while (accounts.hasNext()) {
+    MccApp.select(accounts.next());
+    removeLabels([CONFIG.LABEL]);
+  }
+
+  MccApp.select(managerAccount);
+}
+
+/**
+ * Performs the link checking analysis on the current account.
+ *
+ * @param {string} options Options from the spreadsheet as JSON.
+ * @return {string} JSON stringified results of the analysis.
+ */
+function processAccount(options) {
+  return JSON.stringify(analyzeAccount(JSON.parse(options)));
+}
+
+/**
+ * Consolidates results from each account and outputs them.
+ *
+ * @param {Array.<Object>} executionResults A list of ExecutionResult objects.
+ */
+function processResults(executionResults) {
+  var spreadsheet = SpreadsheetApp.openByUrl(CONFIG.SPREADSHEET_URL);
+  var options = loadOptions(spreadsheet);
+  var results = {
+    urlChecks: [],
+    didComplete: true
+  };
+
+  for (var i = 0; i < executionResults.length; i++) {
+    if (!executionResults[i].getError()) {
+      var accountResult = JSON.parse(executionResults[i].getReturnValue());
+      results.urlChecks = results.urlChecks.concat(accountResult.urlChecks);
+      results.didComplete = results.didComplete && accountResult.didComplete;
+
+      if (accountResult.didComplete) {
+        MccApp.accounts().withIds([executionResults[i].getCustomerId()]).get().
+            next().applyLabel(CONFIG.LABEL);
+      }
+    } else {
+      Logger.log('Processing for ' + executionResults[i].getCustomerId() +
+          ' failed.');
+    }
+  }
+
+  // The entire analysis is not complete if there are any accounts that have
+  // not been labeled (i.e., the account was not started, or not all URLs in
+  // the account have been checked).
+  results.didComplete = results.didComplete &&
+      !getAccounts(false).get().hasNext();
+
+  outputResults(results, options);
+}
+
+/**
+ * Checks as many new URLs as possible that have not previously been checked,
+ * subject to quota and time limits.
+ *
+ * @param {Object} options Dictionary of options.
+ * @return {Object} An object with fields for the URLs checked and an indication
+ *     if the analysis was completed (no remaining URLs to check).
+ */
+function analyzeAccount(options) {
+  // Ensure the label exists before attempting to retrieve already checked URLs.
+  ensureLabels([CONFIG.LABEL]);
+
+  var checkedUrls = getAlreadyCheckedUrls(options);
+  var urlChecks = [];
+  var didComplete = false;
+
+  try {
+    // If the script throws an exception, didComplete will remain false.
+    didComplete = checkUrls(checkedUrls, urlChecks, options);
+  } catch(e) {
+    if (e == EXCEPTIONS.QPS ||
+        e == EXCEPTIONS.LIMIT ||
+        e == EXCEPTIONS.TIMEOUT) {
+      Logger.log('Stopped checking URLs early because: ' + e);
+      Logger.log('Checked URLs will still be output.');
+    } else {
+      throw e;
+    }
+  }
+
+  return {
+    urlChecks: urlChecks,
+    didComplete: didComplete
+  };
+}
+
+/**
+ * Outputs the results to a spreadsheet and sends emails if appropriate.
+ *
+ * @param {Object} results An object with fields for the URLs checked and an
+ *     indication if the analysis was completed (no remaining URLs to check).
+ * @param {Object} options Dictionary of options.
+ */
+function outputResults(results, options) {
+  var spreadsheet = SpreadsheetApp.openByUrl(CONFIG.SPREADSHEET_URL);
+
+  var numErrors = countErrors(results.urlChecks, options);
+  Logger.log('Found ' + numErrors + ' this execution.');
+
+  saveUrlsToSpreadsheet(spreadsheet, results.urlChecks, options);
+
+  // Reload the status to get the total number of errors for the entire
+  // analysis, which is calculated by the spreadsheet.
+  status = loadStatus(spreadsheet);
+
+  if (results.didComplete) {
+    spreadsheet.getRangeByName(NAMES.DATE_COMPLETED).setValue(new Date());
+    Logger.log('Found ' + status.numErrors + ' across the entire analysis.');
+  }
+
+  if (CONFIG.RECIPIENT_EMAILS) {
+    if (!results.didComplete && options.emailEachRun &&
+        (options.emailNonErrors || numErrors > 0)) {
+      sendIntermediateEmail(spreadsheet, numErrors);
+    }
+
+    if (results.didComplete &&
+        (options.emailEachRun || options.emailOnCompletion) &&
+        (options.emailNonErrors || status.numErrors > 0)) {
+      sendFinalEmail(spreadsheet, status.numErrors);
+    }
+  }
+}
+
+/**
+ * Loads data from a spreadsheet based on named ranges. Strings 'Yes' and 'No'
+ * are converted to booleans. One-dimensional ranges are converted to arrays
+ * with blank cells omitted. Assumes each named range exists.
+ *
+ * @param {Object} spreadsheet The spreadsheet object.
+ * @param {Array.<string>} names A list of named ranges that should be loaded.
+ * @return {Object} A dictionary with the names as keys and the values
+ *     as the cell values from the spreadsheet.
+ */
+function loadDatabyName(spreadsheet, names) {
+  var data = {};
+
+  for (var i = 0; i < names.length; i++) {
+    var name = names[i];
+    var range = spreadsheet.getRangeByName(name);
+
+    if (range.getNumRows() > 1 && range.getNumColumns() > 1) {
+      // Name refers to a 2d range, so load it as a 2d array.
+      data[name] = range.getValues();
+    } else if (range.getNumRows() == 1 && range.getNumColumns() == 1) {
+      // Name refers to a single cell, so load it as a value and replace
+      // Yes/No with boolean true/false.
+      data[name] = range.getValue();
+      data[name] = data[name] === 'Yes' ? true : data[name];
+      data[name] = data[name] === 'No' ? false : data[name];
+    } else {
+      // Name refers to a 1d range, so load it as an array (regardless of
+      // whether the 1d range is oriented horizontally or vertically).
+      var isByRow = range.getNumRows() > 1;
+      var limit = isByRow ? range.getNumRows() : range.getNumColumns();
+      var cellValues = range.getValues();
+
+      data[name] = [];
+      for (var j = 0; j < limit; j++) {
+        var cellValue = isByRow ? cellValues[j][0] : cellValues[0][j];
+        if (cellValue) {
+          data[name].push(cellValue);
+        }
+      }
+    }
+  }
+
+  return data;
+}
+
+/**
+ * Loads options from the spreadsheet.
+ *
+ * @param {Object} spreadsheet The spreadsheet object.
+ * @return {Object} A dictionary of options.
+ */
+function loadOptions(spreadsheet) {
+  return loadDatabyName(spreadsheet,
+      [NAMES.CHECK_AD_URLS, NAMES.CHECK_KEYWORD_URLS,
+       NAMES.CHECK_SITELINK_URLS, NAMES.CHECK_PAUSED_ADS,
+       NAMES.CHECK_PAUSED_KEYWORDS, NAMES.CHECK_PAUSED_SITELINKS,
+       NAMES.VALID_CODES, NAMES.EMAIL_EACH_RUN,
+       NAMES.EMAIL_NON_ERRORS, NAMES.EMAIL_ON_COMPLETION,
+       NAMES.SAVE_ALL_URLS, NAMES.FREQUENCY]);
+}
+
+/**
+ * Loads state information from the spreadsheet.
+ *
+ * @param {Object} spreadsheet The spreadsheet object.
+ * @return {Object} A dictionary of status information.
+ */
+function loadStatus(spreadsheet) {
+  return loadDatabyName(spreadsheet,
+      [NAMES.DATE_STARTED, NAMES.DATE_COMPLETED,
+       NAMES.DATE_EMAILED, NAMES.NUM_ERRORS]);
+}
+
+/**
+ * Saves the start date to the spreadsheet and archives results of the last
+ * analysis to a separate sheet.
+ *
+ * @param {Object} spreadsheet The spreadsheet object.
+ */
+function startNewAnalysis(spreadsheet) {
+  Logger.log('Starting a new analysis.');
+
+  spreadsheet.getRangeByName(NAMES.DATE_STARTED).setValue(new Date());
+
+  // Helper method to get the output area on the results or archive sheets.
+  var getOutputRange = function(rangeName) {
+    var headers = spreadsheet.getRangeByName(rangeName);
+    return headers.offset(1, 0, headers.getSheet().getDataRange().getLastRow());
+  };
+
+  getOutputRange(NAMES.ARCHIVE_HEADERS).clearContent();
+
+  var results = getOutputRange(NAMES.RESULT_HEADERS);
+  results.copyTo(getOutputRange(NAMES.ARCHIVE_HEADERS));
+
+  getOutputRange(NAMES.RESULT_HEADERS).clearContent();
+}
+
+/**
+ * Counts the number of errors in the results.
+ *
+ * @param {Array.<Object>} urlChecks A list of URL check results.
+ * @param {Object} options Dictionary of options.
+ * @return {number} The number of errors in the results.
+ */
+function countErrors(urlChecks, options) {
+  var numErrors = 0;
+
+  for (var i = 0; i < urlChecks.length; i++) {
+    if (options.validCodes.indexOf(urlChecks[i].responseCode) == -1) {
+      numErrors++;
+    }
+  }
+
+  return numErrors;
+}
+
+/**
+ * Saves URLs for a particular account to the spreadsheet starting at the first
+ * unused row.
+ *
+ * @param {Object} spreadsheet The spreadsheet object.
+ * @param {Array.<Object>} urlChecks A list of URL check results.
+ * @param {Object} options Dictionary of options.
+ */
+function saveUrlsToSpreadsheet(spreadsheet, urlChecks, options) {
+  // Build each row of output values in the order of the columns.
+  var outputValues = [];
+  for (var i = 0; i < urlChecks.length; i++) {
+    var urlCheck = urlChecks[i];
+
+    if (options.saveAllUrls ||
+        options.validCodes.indexOf(urlCheck.responseCode) == -1) {
+      outputValues.push([
+        urlCheck.customerId,
+        new Date(urlCheck.timestamp),
+        urlCheck.url,
+        urlCheck.responseCode,
+        urlCheck.entityType,
+        urlCheck.campaign,
+        urlCheck.adGroup,
+        urlCheck.ad,
+        urlCheck.keyword,
+        urlCheck.sitelink
+      ]);
+    }
+  }
+
+  if (outputValues.length > 0) {
+    // Find the first open row on the Results tab below the headers and create a
+    // range large enough to hold all of the output, one per row.
+    var headers = spreadsheet.getRangeByName(NAMES.RESULT_HEADERS);
+    var lastRow = headers.getSheet().getDataRange().getLastRow();
+    var outputRange = headers.offset(lastRow - headers.getRow() + 1,
+                                     0, outputValues.length);
+    outputRange.setValues(outputValues);
+  }
+
+  for (var i = 0; i < CONFIG.RECIPIENT_EMAILS.length; i++) {
+    spreadsheet.addEditor(CONFIG.RECIPIENT_EMAILS[i]);
+  }
+}
+
+/**
+ * Sends an email to a list of email addresses with a link to the spreadsheet
+ * and the results of this execution of the script.
+ *
+ * @param {Object} spreadsheet The spreadsheet object.
+ * @param {boolean} numErrors The number of errors found in this execution.
+ */
+function sendIntermediateEmail(spreadsheet, numErrors) {
+  spreadsheet.getRangeByName(NAMES.DATE_EMAILED).setValue(new Date());
+
+  MailApp.sendEmail(CONFIG.RECIPIENT_EMAILS.join(','),
+      'Link Checker Results',
+      'The Link Checker script found ' + numErrors + ' URLs with errors in ' +
+      'an execution that just finished. See ' +
+      spreadsheet.getUrl() + ' for details.');
+}
+
+/**
+ * Sends an email to a list of email addresses with a link to the spreadsheet
+ * and the results across the entire account.
+ *
+ * @param {Object} spreadsheet The spreadsheet object.
+ * @param {boolean} numErrors The number of errors found in the entire account.
+ */
+function sendFinalEmail(spreadsheet, numErrors) {
+  spreadsheet.getRangeByName(NAMES.DATE_EMAILED).setValue(new Date());
+
+  MailApp.sendEmail(CONFIG.RECIPIENT_EMAILS.join(','),
+      'Link Checker Results',
+      'The Link Checker script found ' + numErrors + ' URLs with errors ' +
+      'across its entire analysis. See ' +
+      spreadsheet.getUrl() + ' for details.');
+}
+
+/**
+ * Retrieves all final URLs and mobile final URLs in the account across ads,
+ * keywords, and sitelinks that were checked in a previous run, as indicated by
+ * them having been labeled.
+ *
+ * @param {Object} options Dictionary of options.
+ * @return {Object} A map of previously checked URLs with the URL as the key.
+ */
+function getAlreadyCheckedUrls(options) {
+  var urlMap = {};
+
+  var addToMap = function(items) {
+    for (var i = 0; i < items.length; i++) {
+      urlMap[items[i]] = true;
+    }
+  };
+
+  if (options.checkAdUrls) {
+    addToMap(getUrlsBySelector(AdWordsApp.ads().
+                               withCondition(labelCondition(true))));
+  }
+
+  if (options.checkKeywordUrls) {
+    addToMap(getUrlsBySelector(AdWordsApp.keywords().
+                               withCondition(labelCondition(true))));
+  }
+
+  if (options.checkSitelinkUrls) {
+    addToMap(getAlreadyCheckedSitelinkUrls());
+  }
+
+  return urlMap;
+}
+
+/**
+ * Retrieves all final URLs and mobile final URLs for campaign and ad group
+ * sitelinks.
+ *
+ * @return {Array.<string>} An array of URLs.
+ */
+function getAlreadyCheckedSitelinkUrls() {
+  var urls = [];
+
+  // Helper method to get campaign or ad group sitelink URLs.
+  var addSitelinkUrls = function(selector) {
+    var iterator = selector.withCondition(labelCondition(true)).get();
+
+    while (iterator.hasNext()) {
+      var entity = iterator.next();
+      var sitelinks = entity.extensions().sitelinks();
+      urls = urls.concat(getUrlsBySelector(sitelinks));
+    }
+  };
+
+  addSitelinkUrls(AdWordsApp.campaigns());
+  addSitelinkUrls(AdWordsApp.adGroups());
+
+  return urls;
+}
+
+/**
+ * Retrieves all URLs in the entities specified by a selector.
+ *
+ * @param {Object} selector The selector specifying the entities to use.
+ *     The entities should be of a type that has a urls() method.
+ * @return {Array.<string>} An array of URLs.
+ */
+function getUrlsBySelector(selector) {
+  var urls = [];
+  var entities = selector.get();
+
+  // Helper method to add the url to the list if it exists.
+  var addToList = function(url) {
+    if (url) {
+      urls.push(url);
+    }
+  };
+
+  while (entities.hasNext()) {
+    var entity = entities.next();
+
+    addToList(entity.urls().getFinalUrl());
+    addToList(entity.urls().getMobileFinalUrl());
+  }
+
+  return urls;
+}
+
+/**
+ * Retrieves all final URLs and mobile final URLs in the account across ads,
+ * keywords, and sitelinks, and checks their response code. Does not check
+ * previously checked URLs.
+ *
+ * @param {Object} checkedUrls A map of previously checked URLs with the URL as
+ *     the key.
+ * @param {Array.<Object>} urlChecks An array into which the results of each URL
+ *     check will be inserted.
+ * @param {Object} options Dictionary of options.
+ * @return {boolean} True if all URLs were checked.
+ */
+function checkUrls(checkedUrls, urlChecks, options) {
+  var didComplete = true;
+
+  // Helper method to add common conditions to ad group and keyword selectors.
+  var addConditions = function(selector, includePaused) {
+    var statuses = ['ENABLED'];
+    if (includePaused) {
+      statuses.push('PAUSED');
+    }
+
+    var predicate = ' IN [' + statuses.join(',') + ']';
+    return selector.withCondition(labelCondition(false)).
+        withCondition('Status' + predicate).
+        withCondition('CampaignStatus' + predicate).
+        withCondition('AdGroupStatus' + predicate);
+  };
+
+  if (options.checkAdUrls) {
+    didComplete = didComplete && checkUrlsBySelector(checkedUrls, urlChecks,
+        addConditions(AdWordsApp.ads().withCondition('CreativeFinalUrls != ""'),
+                      options.checkPausedAds));
+  }
+
+  if (options.checkKeywordUrls) {
+    didComplete = didComplete && checkUrlsBySelector(checkedUrls, urlChecks,
+        addConditions(AdWordsApp.keywords().withCondition('FinalUrls != ""'),
+                      options.checkPausedKeywords));
+  }
+
+  if (options.checkSitelinkUrls) {
+    didComplete = didComplete &&
+        checkSitelinkUrls(checkedUrls, urlChecks, options);
+  }
+
+  return didComplete;
+}
+
+/**
+ * Retrieves all final URLs and mobile final URLs in a selector and checks them
+ * for a valid response code. Does not check previously checked URLs. Labels the
+ * entity that it was checked, if possible.
+ *
+ * @param {Object} checkedUrls A map of previously checked URLs with the URL as
+ *     the key.
+ * @param {Array.<Object>} urlChecks An array into which the results of each URL
+ *     check will be inserted.
+ * @param {Object} selector The selector specifying the entities to use.
+ *     The entities should be of a type that has a urls() method.
+ * @return {boolean} True if all URLs were checked.
+ */
+function checkUrlsBySelector(checkedUrls, urlChecks, selector) {
+  var customerId = AdWordsApp.currentAccount().getCustomerId();
+  var iterator = selector.get();
+  var entities = [];
+
+  // Helper method to check a URL.
+  var checkUrl = function(url) {
+    if (!url || checkedUrls[url]) {
+      return;
+    }
+
+    var responseCode = testUrl(cleanUrl(url));
+    var entityType = entity.getEntityType();
+
+    urlChecks.push({
+      customerId: customerId,
+      timestamp: new Date(),
+      url: url,
+      responseCode: responseCode,
+      entityType: entity.getEntityType(),
+      campaign: entity.getCampaign ? entity.getCampaign().getName() : '',
+      adGroup: entity.getAdGroup ? entity.getAdGroup().getName() : '',
+      ad: entityType == 'Ad' ? entity.getHeadline() : '',
+      keyword: entityType == 'Keyword' ? entity.getText() : '',
+      sitelink: entityType.indexOf('Sitelink') != -1 ? entity.getLinkText() : ''
+    });
+
+    checkedUrls[url] = true;
+  };
+
+  while (iterator.hasNext()) {
+    entities.push(iterator.next());
+  }
+
+  for (var i = 0; i < entities.length; i++) {
+    var entity = entities[i];
+
+    checkUrl(entity.urls().getFinalUrl());
+    checkUrl(entity.urls().getMobileFinalUrl());
+
+    // Sitelinks do not have labels.
+    if (entity.applyLabel) {
+      entity.applyLabel(CONFIG.LABEL);
+      checkTimeout();
+    }
+  }
+
+  // True only if we did not breach an iterator limit.
+  return entities.length == iterator.totalNumEntities();
+}
+
+/**
+ * Retrieves all final URLs and mobile final URLs for campaign and ad group
+ * sitelinks and checks them for a valid response code. Does not check
+ * previously checked URLs. Labels the containing campaign or ad group that it
+ * has been checked.
+ *
+ * @param {Object} checkedUrls A map of previously checked URLs with the URL as
+ *     the key.
+ * @param {Array.<Object>} urlChecks An array into which the results of each URL
+ *     check will be inserted.
+ * @param {Object} options Dictionary of options.
+ * @return {boolean} True if all URLs were checked.
+ */
+function checkSitelinkUrls(checkedUrls, urlChecks, options) {
+  var didComplete = true;
+
+  // Helper method to check URLs for sitelinks in a campaign or ad group
+  // selector.
+  var checkSitelinkUrls = function(selector) {
+    var iterator = selector.withCondition(labelCondition(false)).get();
+    var entities = [];
+
+    while (iterator.hasNext()) {
+      entities.push(iterator.next());
+    }
+
+    for (var i = 0; i < entities.length; i++) {
+      var entity = entities[i];
+      var sitelinks = entity.extensions().sitelinks();
+
+      if (sitelinks.get().hasNext()) {
+        didComplete = didComplete &&
+            checkUrlsBySelector(checkedUrls, urlChecks, sitelinks);
+        entity.applyLabel(CONFIG.LABEL);
+        checkTimeout();
+      }
+    }
+
+    // True only if we did not breach an iterator limit.
+    didComplete = didComplete &&
+        entities.length == iterator.totalNumEntities();
+  };
+
+  var statuses = ['ENABLED'];
+  if (options.checkPausedSitelinks) {
+    statuses.push('PAUSED');
+  }
+
+  var predicate = ' IN [' + statuses.join(',') + ']';
+  checkSitelinkUrls(AdWordsApp.campaigns().
+                    withCondition('Status' + predicate));
+  checkSitelinkUrls(AdWordsApp.adGroups().
+                    withCondition('Status' + predicate).
+                    withCondition('CampaignStatus' + predicate));
+
+  return didComplete;
+}
+
+/**
+ * Cleans a URL by removing ValueTrack and custom parameters.
+ *
+ * @param {string} url The url to clean.
+ * @return {string} The cleaned URL.
+ */
+function cleanUrl(url) {
+  // Custom parameters are alphanumeric with a leading underscore.
+  // ValueTrack parameters may also contain a ':' or '+'.
+  return url.replace(/{[0-9a-zA-Z\_\+\:]+}/g, '');
+}
+
+/**
+ * Requests a given URL. Retries if the UrlFetchApp QPS limit was reached,
+ * exponentially backing off on each retry. Throws an exception if it reaches
+ * the maximum number of retries. Throws an exception if the UrlFetchApp daily
+ * quota limit was reached.
+ *
+ * @param {string} url The URL to test.
+ * @return {number} The response code received when requesting the URL.
+ */
+function testUrl(url) {
+  var responseCode;
+  var sleepTime = QUOTA_CONFIG.INIT_SLEEP_TIME;
+  var numTries = 0;
+
+  while (numTries < QUOTA_CONFIG.MAX_TRIES && !responseCode) {
+    try {
+      // If UrlFetchApp.fetch() throws an exception, responseCode will remain
+      // undefined.
+      responseCode =
+        UrlFetchApp.fetch(url, {muteHttpExceptions: true}).getResponseCode();
+
+      if (CONFIG.THROTTLE > 0) {
+        Utilities.sleep(CONFIG.THROTTLE);
+      }
+    } catch(e) {
+      if (e.message.indexOf('Service invoked too many times in a short time:')
+          != -1) {
+        Utilities.sleep(sleepTime);
+        sleepTime *= QUOTA_CONFIG.BACKOFF_FACTOR;
+      } else if (e.message.indexOf('Service invoked too many times:') != -1) {
+        throw EXCEPTIONS.LIMIT;
+      } else if (e.message.indexOf('DNS error') != -1) {
+        return 404;
+      } else {
+        throw e.message;
+      }
+    }
+
+    numTries++;
+  }
+
+  if (!responseCode) {
+    throw EXCEPTIONS.QPS;
+  } else {
+    return responseCode;
+  }
+}
+
+/**
+ * Throws an exception if the script is close to timing out.
+ */
+function checkTimeout() {
+  if (AdWordsApp.getExecutionInfo().getRemainingTime() <
+      CONFIG.TIMEOUT_BUFFER) {
+    throw EXCEPTIONS.TIMEOUT;
+  }
+}
+
+/**
+ * Returns the number of days between two dates.
+ *
+ * @param {Object} from The older Date object.
+ * @param {Object} to The newer (more recent) Date object.
+ * @return {number} The number of days between the given dates (possibly
+ *     fractional).
+ */
+function dayDifference(from, to) {
+  return (to.getTime() - from.getTime()) / (24 * 3600 * 1000);
+}
+
+/**
+ * Builds a string to be used for withCondition() filtering for whether the
+ * label is present or not.
+ *
+ * @param {boolean} hasLabel True if the label should be present, false if the
+ *     label should not be present.
+ * @return {string} A condition that can be used in withCondition().
+ */
+function labelCondition(hasLabel) {
+  return 'LabelNames ' + (hasLabel ? 'CONTAINS_ANY' : 'CONTAINS_NONE') +
+      ' ["' + CONFIG.LABEL + '"]';
+}
+
+/**
+ * Retrieves an entity by name.
+ *
+ * @param {Object} selector A selector for an entity type with a Name field.
+ * @param {string} name The name to retrieve the entity by.
+ * @return {Object} The entity, if it exists, or null otherwise.
+ */
+function getEntityByName(selector, name) {
+  var entities = selector.withCondition('Name = ' + name).get();
+
+  if (entities.hasNext()) {
+    return entities.next();
+  } else {
+    return null;
+  }
+}
+
+/**
+ * Retrieves a Label object by name.
+ *
+ * @param {string} labelName The label name to retrieve.
+ * @return {Object} The Label object, if it exists, or null otherwise.
+ */
+function getLabel(labelName) {
+  return getEntityByName(AdWordsApp.labels(), labelName);
+}
+
+/**
+ * Retrieves an AccountLabel object by name.
+ *
+ * @param {string} labelName The label name to retrieve.
+ * @return {Object} The AccountLabel object, if it exists, or null otherwise.
+ */
+function getAccountLabel(labelName) {
+  return getEntityByName(MccApp.accountLabels(), labelName);
+}
+
+/**
+ * Checks that the account has all provided labels and creates any that are
+ * missing. Since labels cannot be created in preview mode, throws an exception
+ * if a label is missing.
+ *
+ * @param {Array.<string>} labelNames An array of label names.
+ */
+function ensureLabels(labelNames) {
+  for (var i = 0; i < labelNames.length; i++) {
+    var labelName = labelNames[i];
+    var label = getLabel(labelName);
+
+    if (!label) {
+      if (!AdWordsApp.getExecutionInfo().isPreview()) {
+        AdWordsApp.createLabel(labelName);
+      } else {
+        throw 'Label ' + labelName + ' is missing and cannot be created in ' +
+            'preview mode. Please run the script or create the label manually.';
+      }
+    }
+  }
+}
+
+/**
+ * Checks that the account has all provided account labels and creates any that
+ * are missing. Since labels cannot be created in preview mode, throws an
+ * exception if a label is missing.
+ *
+ * @param {Array.<string>} labelNames An array of label names.
+ */
+function ensureAccountLabels(labelNames) {
+  for (var i = 0; i < labelNames.length; i++) {
+    var labelName = labelNames[i];
+    var label = getAccountLabel(labelName);
+
+    if (!label) {
+      if (!AdWordsApp.getExecutionInfo().isPreview()) {
+        MccApp.createAccountLabel(labelName);
+      } else {
+        throw 'Account label ' + labelName + ' is missing and cannot be ' +
+            'created in preview mode. Please run the script or create the ' +
+            'label manually.';
+      }
+    }
+  }
+}
+
+/**
+ * Removes all provided labels from the account. Since labels cannot be removed
+ * in preview mode, throws an exception in preview mode.
+ *
+ * @param {Array.<string>} labelNames An array of label names.
+ */
+function removeLabels(labelNames) {
+  if (AdWordsApp.getExecutionInfo().isPreview()) {
+    throw 'Cannot remove labels in preview mode. Please run the script or ' +
+        'remove the labels manually.';
+  }
+
+  for (var i = 0; i < labelNames.length; i++) {
+    var label = getLabel(labelNames[i]);
+
+    if (label) {
+      label.remove();
+    }
+  }
+}
+
+/**
+ * Removes all provided account labels from the account. Since labels cannot be
+ * removed in preview mode, throws an exception in preview mode.
+ *
+ * @param {Array.<string>} labelNames An array of label names.
+ */
+function removeAccountLabels(labelNames) {
+  if (AdWordsApp.getExecutionInfo().isPreview()) {
+    throw 'Cannot remove account labels in preview mode. Please run the ' +
+        'script or remove the labels manually.';
+  }
+
+  for (var i = 0; i < labelNames.length; i++) {
+    var label = getAccountLabel(labelNames[i]);
+
+    if (label) {
+      label.remove();
+    }
+  }
+}