Hide
AdWords scripts

Tips & Tricks

Below are tips and tricks to get the most out of AdWords scripts.

  1. Use labels for long-running scripts

Use labels for long-running scripts

Suppose you want to operate over 100,000 ad groups (pause bad ads, analyze stats, change bids, etc.), and the script is taking more than 30 minutes to complete. You can work around this by creating an "incremental"-type script and scheduling it to run hourly until completion.

Here is an example of such a script:

var LABEL_NAME = "already_processed";

function main() {
  // This line will start showing errors after first run, but can ignore.
  AdWordsApp.createLabel(LABEL_NAME);

  // Get all ad groups we haven't looked at yet.
  var adGroups = AdWordsApp.adGroups()
    .withCondition("LabelNames CONTAINS_NONE['" + LABEL_NAME + "']")
    .get();

  if (!adGroups.hasNext()) {
    MailApp.sendEmail(myself, "I'm done, turn off the schedule!");
    // Optionally, delete the label here.
  }

  while (adGroups.hasNext()) {
    var adGroup = adGroups.next();
    adGroup.applyLabel(LABEL_NAME);
    performExpensiveOperationOn(adGroup);
  }
}

An enhancement to this script can be safeguarding operation timeouts by applying and checking for "processing" and "processed" labels in the operations loop.