[Google Ads script] Flag High Keyword Ad Groups

For any serious PPC marketer, single keyword ad groups are the holy grail when it comes to increasing PPC performance.

However, when you are managing accounts that contain thousands of different keywords and ad groups that are constantly being expanded, it can be hard to ensure you adhere to this structure.

So, we built a script that automatically builds a report showing you which ad groups contain more than a certain number of keywords, so you can go in and break the ad groups out into single keyword ad groups.

We programmed the script to output all ad groups that contain more than one keyword in an account that we were reviewing. As you can see below, the script shows the number of keywords within the ad group, as well as the URL of that ad group, so you can link directly to it.

How to use the script

There are four variables to set in the user area at the top of the script:

  • Number of keywords: Here you can set the maximum number of keywords that an ad group should have, otherwise it is flagged and the number of keywords that the ad group contains and the URL of that ad group is written into the Google Sheet. We suggest setting this to 1. Do this on line 15.
  • Campaigns to include: This is where you define which campaigns you want to be included by specifying them in a list. For example, [“campaign1”,”campaign2”]. If you want all campaigns to be included, leave the brackets empty: []. Set this on line 19.
  • Campaigns to exclude: This is where you define the list of campaigns you want to be excluded. For example, [“campaign3”,”campaign4”]. If you don’t want any campaigns to be excluded, leave the brackets empty: []. Set this on line 23.
  • Spreadsheet URL: This is the URL of the spreadsheet that you would like the report to be output to. This can be generated by going to Google Sheets, creating a new sheet, then copying the URL. Set this on line 27.
/**
*
* Ad group keyword number checker script
*
* Version: 1.0
* maintained by DemandMore
*
**/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//Options
//Specify the number of active keywords
NUMBEROFKEYWORDS = 1
//Specify what word the campaigns should include, use '' to include all campaigns. Use double quotation marks for words with spaces, for example '"x y"'
INCLUDEDCAMPAIGNS = '' 
//Specify what word the campaigns should not include, use '' to include all campaigns. Use double quotation marks for words with spaces.
EXCLUDEDCAMPAIGNS = ''
//Specify sheet url, for example example https://docs.google.com/spreadsheets/d/1V6DsB7kuNKumZPsycNo9z5lPZcuZGlCfbSVCTwHV50U/edit#gid=0
URL = 'https://docs.google.com/spreadsheets/d/11WDxR5Yt1Oh48kUInUv4J-AMPkL5efsiwOH9HmGGlpg/edit#gid=0'
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
function getCampaigns()
{  if(INCLUDEDCAMPAIGNS == '' && EXCLUDEDCAMPAIGNS == '')
  {
  var campaigns = AdWordsApp.campaigns().withCondition('Status = ENABLED').get()
  }
  else   if(INCLUDEDCAMPAIGNS != '')
  {
  var campaigns = AdWordsApp.campaigns().withCondition('Status = ENABLED AND CampaignName CONTAINS ' + INCLUDEDCAMPAIGNS).get()
  }
  
  else   if(EXCLUDEDCAMPAIGNS != '')
  {
  var campaigns = AdWordsApp.campaigns().withCondition('Status = ENABLED AND CampaignName DOES_NOT_CONTAIN ' + EXCLUDEDCAMPAIGNS).get()
  }
  
  return campaigns}
function writeToSheet(rows)
{
  var sheet = SpreadsheetApp.openByUrl(URL)
  sheet.getRange('A:B').clear()
  var range = sheet.getRange('A1:B' + rows.length)
  range.setValues(rows)
  
}
function main()
{
  var accId = AdWordsApp.currentAccount().getCustomerId().replace('-','').replace('-','')
  var campaigns = getCampaigns()
  var rows = [['number of keywords','Ad groups Url']]
  var urlPattern = 'https://adwords.google.com/aw/keywords?__e={accid}&campaignId={cid}&adGroupId={agid}'.replace('{accid}', accId)
  var urlPattern0 = urlPattern
  
  while(campaigns.hasNext())
  {
    
    var campaign = campaigns.next()
    var cid = campaign.getId()
    var urlPattern1 = urlPattern.replace('{cid}', cid)
    var adgroups = campaign.adGroups().withCondition('Status = ENABLED').get()
    
    while(adgroups.hasNext())
    {
      var adgroup = adgroups.next()
      var numOfKeywords = adgroup.keywords().withCondition('Status = ENABLED').get().totalNumEntities()
      
      if(numOfKeywords > NUMBEROFKEYWORDS)
      {
        urlPattern = urlPattern1
        var agname = adgroup.getName()
        var agid = adgroup.getId()
        
        urlPattern = urlPattern.replace('{agid}', agid)
        rows.push([numOfKeywords,urlPattern])
      }
           
    }
    
    urlPattern = urlPattern0    
  }
  
  writeToSheet(rows)
 
}

About Wes

Wes is the Managing Director of DemandMore. He is columnist for several leading marketing publications including Campaign Magazine, Econsultancy and Search Engine Land where he shares his expertise in search marketing.