Pages

Tuesday, November 20, 2012

Update Your Keywords for the Holiday Season

The other day, RKGBlog had a great post about updating your keywords for the holiday season. One of the mentions was to update all the years in your Keywords to the current year. Here is a little script that will find all the Keywords with the previous year in them and create new Keywords in the same AdGroup with the current year.

Thanks,
Russ
/*********************************************
* Update Keywords for the New Year
* Version 1.1
* Changelog v1.1
*   - Updated for speed and added comments 
* Created By: Russ Savage
* FreeAdWordsScripts.com
**********************************************/
function main() {
  var sameDayLastYear = new Date();
  sameDayLastYear.setYear(sameDayLastYear.getYear()-1);
  var oldYearStr = sameDayLastYear.getYear().toString();
  var newYearStr = new Date().getYear().toString();
  
  Logger.log('Updating keywords with old year: '+oldYearStr+' to new year: '+newYearStr);
  
  // Let's start by getting all of the keywords
  var kwIter = AdWordsApp.keywords()
    .withCondition("Text CONTAINS " + oldYearStr)
    .withCondition("Status = ENABLED")
    .withCondition("AdGroupStatus = ENABLED")
    .withCondition("CampaignStatus = ENABLED")
    .get();
 
  // It is always better to store and batch process afterwards
  var toPause = [];
  var toCreate = [];
  while (kwIter.hasNext()) {
    var kw = kwIter.next();
    var ag = kw.getAdGroup();
    var oldText = kw.getText();
    var newText = oldText.replace(oldYearStr,newYearStr);
    // Save the info so that we can create them as a batch later
    toCreate.push({ ag: ag, text: newText, cpc:kw.getMaxCpc(), destUrl : kw.getDestinationUrl() });
    // Same with the ones we want to pause
    toPause.push(kw) 
  }
  // Now we create the new keywords all at once
  for(var i in toCreate) {
    var elem = toCreate[i];
    elem.ag.createKeyword(elem.text, elem.cpc, elem.destUrl);
  }
  // And pause the old ones all at once
  for(var i in toPause) {
    toPause[i].pause();
    //or toPause[i].remove(); to delete the old keyword
  }
}