AdWords script: Scan your landing pages and put an end to errors

AdWords script: Scan your landing pages and put an end to errors

This script scans your landing pages to detect errors on your pages or site. If you’ve not run a script before please read our Introduction to AdWords Scripts. For more open-source fun check out our AdWords Scripts directory. And if you want to use some of our technology that’s arrived back from the future then sign up for one of our paid plans on the Brainlabs Tech Stack.

function main() {

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

  var split = true; //true or false

  var type = "keywords"; //choose "keywords" or "ads"

  //Campaign name filters
  //Leave as [] to skip
  var containsArray = ["hey","jude"]; // enter like ["hey", "jude"]
  var excludesArray = ["hey","jude"]; // enter like ["hey", "jude"]

  //keyword labels
  //Leave as [] to skip
  var labelArray = ["hey","jude"]; // enter like ["hey", "jude"]

  //leave as "" to skip
  var status = "ENABLED"; //Choose from "ENABLED" or "PAUSED"
  var campaignStatus = "ENABLED"; //Choose from "ENABLED" or "PAUSED"
  var adGroupStatus = "ENABLED"; //Choose from "ENABLED" or "PAUSED"

  //300 301, 302 are redirects
  var allowedCodes = [200]; //enter like [200,301]

  //leave as "" to skip
  var recipients = ""; //enter like "a@b.com,c@d.com,e@g.co.uk"

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

  var urls = [];
  var bad_urls = [];
  var urlFetchOptions = {muteHttpExceptions: true};

  var string = iteratorConstructor(type, containsArray, excludesArray, labelArray, status, campaignStatus, adGroupStatus);
  eval(string);

  while(iterator.hasNext()){
    var object = iterator.next();
    var url = object.urls().getFinalUrl();
    if (url === null || url === undefined || url === ""){
      var url = object.getDestinationUrl();
    }
    if(url !== null && url !== undefined){
      if(split){
        url = url.split('?')[0];
      }
      if(urls.indexOf(url) === -1) {
        urls.push(url);
      }
    }
  }

  //Logger.log(urls);

  for(var x in urls){
    var response = UrlFetchApp.fetch(urls[x],urlFetchOptions);
    var code = response.getResponseCode();
    if(allowedCodes.indexOf(code) === -1){
      bad_urls.push(urls[x]);
    }
  }

  Logger.log(bad_urls.join("\n"));

  if(recipients.length > 0){

    var name = AdWordsApp.currentAccount().getName();
    var subject = name + " URL checking";
    var body = bad_urls.join("\n");
    MailApp.sendEmail(recipients,subject,body);
  }

  function iteratorConstructor(type, containsArray, excludesArray, labelArray, status, campaignStatus, adGroupStatus){

    var string = "var iterator = AdWordsApp."+type+"()";
    for(var i = 0; i < containsArray.length; i++){
      string = string + ".withCondition('CampaignName CONTAINS_IGNORE_CASE " + '"' + containsArray[i] + '"' + "')";
    }
    for(var i = 0; i < excludesArray.length; i++){
      string = string + ".withCondition('CampaignName DOES_NOT_CONTAIN_IGNORE_CASE " + '"' + excludesArray[i] + '"' + "')";
    }
    if(labelArray.length > 0){
      string = string + ".withCondition('LabelNames CONTAINS_ANY " + '["' + labelArray.join('","') + '"]' + "')";
    }
    if(status.length > 0){
      string = string + ".withCondition('Status = " + status + "')";
    }
    if(campaignStatus.length > 0){
      string = string + ".withCondition('CampaignStatus = " + campaignStatus + "')";
    }
    if(adGroupStatus.length > 0){
      string = string + ".withCondition('AdGroupStatus = " + adGroupStatus + "')";
    }

    string = string + ".orderBy('Cost DESC').forDateRange('LAST_30_DAYS')";
    string = string + ".withLimit(50000)";

    string = string + ".get();"

    return string;

  }

}

Share this post