Monday, March 11, 2013

Merge Multiple Campaigns Together For Enhanced Campaigns Migration

UPDATE 2013-04-14: Check Merge Labels from Multiple Campaigns for some help on merging labels.

Everyone is getting ready for Enhanced Campaigns and for a lot of people, that means finding an easy way to merge multiple campaigns into a single campaign. You may have a campaign for desktop traffic and one for tablet, or maybe one for each major mobile platform.

Well the beauty and the pain of Enhanced Campaigns is that you can do more with less. I haven't found a great way to easily merge multiple campaigns into one, so I made my own. All you need to do is specify a destination campaign where you want all the keywords, adgroups, and ads to end up and the set of campaigns you want to copy from. This script will pause any keyword and ad it manages to move over so you can see results and make sure everything is set up properly before deleting the old campaigns.

Thanks,
Russ

//-----------------------------------
// Merge Multiple Campaigns Together
// Created By: Russ Savage
// FreeAdWordsScripts.com
//-----------------------------------
function main() {
  var DESTINATION_CAMPAIGN_NAME = "dest_camp_name";
  var ORIGIN_CAMPAIGN_NAMES = ["to_merge_camp_name_1","to_merge_camp_name_2"/*,...*/];
  var DEFAULT_KW_BID = 0.01; //used in case we can't get the origin kw bid
  
  //build a list of adgroups in the original 
  var dest_adgroups = [];
  var ag_iter = AdWordsApp.adGroups()
    .withCondition("CampaignName = '"+DESTINATION_CAMPAIGN_NAME+"'")
    .get();
  
  while(ag_iter.hasNext()) {
    dest_adgroups.push(ag_iter.next());
  }
  
  var dest_camp;
  if(dest_adgroups.length > 0) {
     dest_camp = dest_adgroups[0].getCampaign();
  }
  
  for(var i in ORIGIN_CAMPAIGN_NAMES) {
    var camp_name = ORIGIN_CAMPAIGN_NAMES[i];
    var kw_iter = AdWordsApp.keywords()
      .withCondition("CampaignName = '"+camp_name+"'")
      .get();
    while(kw_iter.hasNext()) {
      var kw = kw_iter.next();
      var dest_adgroup = _find_adgroup(dest_adgroups,kw.getAdGroup());
      if(!dest_adgroup) {
        dest_adgroup = dest_camp.newAdGroupBuilder()
          .withName(kw.getAdGroup().getName())
          .withStatus((kw.getAdGroup().isPaused()) ? "PAUSED" : "ENABLED")
          .withKeywordMaxCpc(kw.getAdGroup().getKeywordMaxCpc())
          .create();
        dest_adgroups.push(dest_adgroup);
        //now we move all the ads over
        var ad_iter = kw.getAdGroup().ads().get();
        while(ad_iter.hasNext()) {
          var ad = ad_iter.next();
          dest_adgroup.createTextAd(
            ad.getHeadline(),
            ad.getDescription1(),
            ad.getDescription2(),
            ad.getDisplayUrl(),
            ad.getDestinationUrl(),
            { isMobilePreferred : ad.isMobilePreferred() }
          );
          ad.pause();
        }
      }
      var max_cpc = kw.getMaxCpc() || DEFAULT_KW_BID;
      var dest_url = kw.getDestinationUrl() || "";
      var kw_text = kw.getText();
      dest_adgroup.createKeyword(kw_text,max_cpc,dest_url);

      kw.pause();
    }
  }

  function _find_adgroup(ag_list,ag) {
    for(var i in ag_list) {
      if(ag_list[i].getName() == ag.getName()) {
        return ag_list[i];
      }
    }
    return null;
  }
}