Pause Ads if 404, Enable if no longer

754 views
Skip to first unread message

Alberto Ayala

unread,
Nov 28, 2016, 11:25:08 PM11/28/16
to AdWords Scripts Forum
Hi everyone,

I've gone through the forum and found a few scripts on the topic, but I haven't found one that does exactly this:

Check if final URL gives error (keyword level URL), if they do, pause keywords, if they no longer do, resume keywords. - I would schedule this to run hourly.
I would need to label the keywords that have been paused due to 404 (or similar), so then the scripts only checks those keywords, and check if they should be re-enabled, otherwise all paused keywords would be considered to be enabled, even those that we want to keep paused for other reasons.

This script from brainlabsdigital.com is well explained, I would like to slightly modify it in order to pause the keywords and re-enable them as described above.
Could someone give me a hand?

Thanks a lot!

/**
*
* AdWords Script for checking the contents of landing pages. 
* Goes to the final URL of keywords or ads, then searches the source code for
* user defined strings.
*
* Version: 1.0
* Google AdWords Script maintained by brainlabsdigital.com
*
**/

function main() {
  
  var messagesToCheckFor = ["Page not found"];
  // What out of stock messages appear on the source code of your landing pages?
  // Enter like ["Out of stock", "<em>0 available</em>"]
  
  var trimAtQuestionMark = true;
  // Do you want to remove all parameters which occur after the '?' character?
  // Enter true or false
  
  var type = "keywords";
  // Choose "keywords" or "ads"
  // Are your final URLs at the keyword or ad level?
  
  var recipients = ["exa...@example.com"];
  // If set, these addresses will be emailed with a list of any bad URLs.
  // Enter like ["a...@b.com"] or ["a...@b.com","c...@d.com","e...@g.co.uk"]
  // Leave as [] to skip.
  
  
  // Optional filtering options
  // Enter like ["hey", "jude"]
  // Leave as [] to skip
  var containsArray = [];
  // If set, only campaigns whose names contain these phrases will be checked
  
  var excludesArray = [];
  // If set, campaigns whose names contain any of these phrases will be ignored.
  
  var labelArray = [];
  // If set, only keywords / ads with these labels will be checked
  // Case sensitive.
  
  
  // Status options
  // Choose from ["ENABLED"] if you only want enabled entities
  // ["PAUSED"] if you only want paused entities
  // ["ENABLED","PAUSED"] if you want enabled and paused entities
  var campaignStatus = ["ENABLED"];
  // The status of the campaigns
  
  var adGroupStatus = ["ENABLED"];
  // The status of the ad groups
  
  var status = ["ENABLED"];
  // The status of the keywords / ads
  
  
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  
  
  
  var urls = [];
  var bad_urls = [];
  var urlFetchOptions = {muteHttpExceptions: true};
  var countEntities = 0;
  
  
  var conditions = [];
  if (containsArray.length > 0) {
    conditions.push(" where the campaign name contains " + containsArray.join(", "));
  }
  if (excludesArray.length > 0) {
    conditions.push(" where the campaign name excludes " + excludesArray.join(", "));
  }
  if (labelArray.length > 0) {
    conditions.push(" where the " + type + " are labelled " + labelArray.join(", "));
  }    
  
  if (containsArray.length === 0) {
    containsArray.push("");
  }
  
  for(var i = 0; i < containsArray.length; i++){
    var string = iteratorConstructor(type, containsArray[i], excludesArray, labelArray, status, campaignStatus, adGroupStatus);
    eval(string);
    countEntities += iterator.totalNumEntities();
    excludesArray.push(containsArray[i]);
    while(iterator.hasNext()){
      var object = iterator.next();
      var url = object.urls().getFinalUrl();
      
      if(url == null || url == undefined){
        url = object.getDestinationUrl();
      }
      
      if(url !== null && url !== undefined){
        if(trimAtQuestionMark){
          url = url.split('?')[0];
        }
        if(urls.indexOf(url) === -1) {
          urls.push(url);
        }
      }
    }
  }
  
  if (countEntities == 0) {
    throw "No " + type + " found" + conditions.join("; and");
  }
  Logger.log(countEntities + " " + type + " found" + conditions.join("; and"));
  Logger.log(urls.length + " unique URLs to check.");
  
  for(var x in urls){
    var response = UrlFetchApp.fetch(urls[x],urlFetchOptions);
    var code = response.getContentText();
    for(var y = 0; y < messagesToCheckFor.length; y++){
      var message = messagesToCheckFor[y];
      if(code.indexOf(message) !== -1){
        bad_urls.push(urls[x]);
        break;
      }
    }
  }
  
  if (bad_urls.length === 0) {
    Logger.log("No bad URLs found.");
  } else {  
    Logger.log(bad_urls.length + " found:");
    Logger.log(bad_urls.join("\n"));
  }
  
  if(recipients.length > 0 && bad_urls.length > 0){
    var name = AdWordsApp.currentAccount().getName();
    var subject = name + " URL checking";
    var body = 'The following URLs were found to have one of the following phrases in their web page source code. \n\nPhrases:\n"' + messagesToCheckFor.join('",\n"') + '"\n\nURLs:\n';
    body += bad_urls.join("\n");
    MailApp.sendEmail(recipients.join(","),subject,body);
    Logger.log("Email sent to " + recipients.join(", "));
  }
  
  function iteratorConstructor(type, containsString, excludesArray, labelArray, status, campaignStatus, adGroupStatus){
    
    var string = "var iterator = AdWordsApp."+type+"()";
    if (containsString != "") {
      string = string + ".withCondition('CampaignName CONTAINS_IGNORE_CASE " + '"' + containsString + '"' + "')";
    }
    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('","') + '"]' + "')";
    }
    
    string = string + ".withCondition('Status IN [" + status.join(",") + "]')";
    string = string + ".withCondition('CampaignStatus IN [" + campaignStatus.join(",") + "]')";
    string = string + ".withCondition('AdGroupStatus IN [" + adGroupStatus.join(",") + "]')";
    string = string + ".orderBy('Cost DESC').forDateRange('LAST_30_DAYS')";
    string = string + ".withLimit(50000)";
    
    string = string + ".get();"
    
    return string;
    
  }
  
}


Paul Justine de Honor

unread,
Nov 29, 2016, 3:01:13 AM11/29/16
to AdWords Scripts Forum
Hi Alberto,

I am afraid to comment as I am not familiar with the script. We suggest reaching out to the developer at Brain Labs and to see if they can help.

You can use Link Checker to get all the broken links through all of your ads, keywords, sitelinks and the results will be exported to the spreadsheet. Having that said, you can create a script that will pause all of the keywords that exists on that spreadsheet.

Thanks,
Paul Justine De Honor
AdWords Scripts Team

Scripts Noob

unread,
Apr 27, 2021, 12:47:01 PM4/27/21
to Google Ads Scripts Forum
Hello,

Is this still possible? 

So if all Ads in the Ad Group are disapproved: Pause Keyword/Ad Group?

Many thanks!

Google Ads Scripts Forum Advisor

unread,
Apr 28, 2021, 1:27:08 AM4/28/21
to adwords...@googlegroups.com

Hello there,

Thank you for reaching out to us.

Could you confirm on what you mean by the question (Is this still possible?)? Are you referring to your 2nd question (So if all Ads in the Ad Group are disapproved: Pause Keyword/Ad Group?) if this is possible? If you're referring to the possibility of your 2nd question then it is indeed possible.

The fastest way to do that is by using Reports and utilize the AD_PERFORMANCE_REPORT. You can refer to the CombinedApprovalStatus field to check for the approval status of the ad. Once you have determine your disapproved ads, you can see these samples (Pause an ad group | Pause an existing keyword in an ad group) for pausing the Keyword/Ad Group.

Hope this helps.

Regards,

Google Logo
Mark Kevin Albios
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2GI0KZ:ref
Reply all
Reply to author
Forward
0 new messages