Wednesday, November 21, 2012

Pause AdGroups With No Active Keywords

This is a quick script to pause all the AdGroups with no active keywords in them. Not sure if this is super useful, but for large accounts, it might help identify AdGroups you can get rid of.

Thanks,
Russ

/*********************************************
* Pause AdGroups With No Active Keywords
* Version 1.1
* Changelog v1.1
*   - Updated for speed and added comments 
* Created By: Russ Savage
* FreeAdWordsScripts.com
**********************************************/
function main() {
  // Let's start by getting all of the active AdGroups
  var agIter = AdWordsApp.adGroups()
    .withCondition('CampaignStatus = ENABLED')
    .withCondition('Status = ENABLED')
    .get();
 
  // It is faster to store them and process them all at once later
  var toPause = [];
  // Then we will go through each one
  while(agIter.hasNext()) {
    var ag = agIter.next();
    //get all the keywords that are enabled
    var kwIter = ag.keywords()
      .withCondition("Status = ENABLED")
      .get();
    
    //If .hasNext() is true, there is at least 1 kw in the AdGroup
    var hasKw = kwIter.hasNext(); 
    if(!hasKw) {
      toPause.push(ag);
    }
  }
  
  // Now we process them all at once to take advantage of batch processing
  for(var i in toPause) {
    toPause[i].pause();
  }
}

5 comments:

  1. This rule seems to time out every time? Any idea how I would manipulate this script to only search within ACTIVE campaigns? I believe it is timing out because I have a lot of deleted and paused campaigns.

    Thanks

    ReplyDelete
    Replies
    1. I'm assuming you have to add 'isEnabled()' in there somewhere.

      Delete
  2. Yeah it's easy to update. Instead of the line:

    var ag_iter = AdWordsApp.adGroups().get();

    Just use the following:

    var ag_iter = AdWordsApp.adGroups().withCondition("Status = ENABLED").get();

    Let me know if that works for you.

    Thanks,
    Russ

    ReplyDelete
  3. Hi! Is there a way to make sure that you keep DSA campaigns be excluded from this? I'm running into an issue where this script pauses all ad groups in my DSA campaign.

    ReplyDelete