Thanks,
Russ
//-----------------------------------
// Merge Labels from Multiple Campaigns
// Created By: Russ Savage
// FreeAdWordsScripts.com
//-----------------------------------
var DESTINATION_CAMPAIGN_NAME = "Destination Campaign Name";
var ORIGIN_CAMPAIGN_NAMES = ["Origin Campaign Name 1","Origin Campaign Name 2"];
function main() {
var label_iter = AdWordsApp.labels().get();
while(label_iter.hasNext()) {
var label = label_iter.next();
//Pre-build all the iterators
var iters = [
label.campaigns().withCondition("Name IN ['"+ORIGIN_CAMPAIGN_NAMES.join("','")+"']").get(),
label.adGroups().withCondition("CampaignName IN ['"+ORIGIN_CAMPAIGN_NAMES.join("','")+"']").get(),
label.ads().withCondition("CampaignName IN ['"+ORIGIN_CAMPAIGN_NAMES.join("','")+"']").get(),
label.keywords().withCondition("CampaignName IN ['"+ORIGIN_CAMPAIGN_NAMES.join("','")+"']").get()
];
for(var i in iters) {
var iter = iters[i];
while(iter.hasNext()) {
_copyLabels(iter.next());
}
}
}
}
//Copies the labels from entity in Origin campaign
//to entity with the same name in dest campaign
function _copyLabels(entity) {
var iter;
if(_getEntityType(entity) == "Campaign") {
// it's a campaign
iter = AdWordsApp.campaigns()
.withCondition("Name = '"+DESTINATION_CAMPAIGN_NAME+"'")
.get();
} else if(_getEntityType(entity) == "AdGroup") {
// it's an adgroup
iter = AdWordsApp.adGroups()
.withCondition("CampaignName = '"+DESTINATION_CAMPAIGN_NAME+"'")
.withCondition("Name = '"+entity.getName()+"'")
.get();
} else if(_getEntityType(entity) == "Ad") {
// it's an ad
iter = AdWordsApp.ads()
.withCondition("CampaignName = '"+DESTINATION_CAMPAIGN_NAME+"'")
.withCondition("AdGroupName = '"+entity.getAdGroup().getName()+"'")
.withCondition("Headline = '"+entity.getHeadline()+"'")
.withCondition("Description1 = '"+entity.getDescription1()+"'")
.withCondition("Description2 = '"+entity.getDescription2()+"'")
.withCondition("DisplayUrl = '"+entity.getDisplayUrl()+"'")
.get();
} else if(_getEntityType(entity) == "Keyword") {
// it's a keyword
iter = AdWordsApp.keywords()
.withCondition("CampaignName = '"+DESTINATION_CAMPAIGN_NAME+"'")
.withCondition("AdGroupName = '"+entity.getAdGroup().getName()+"'")
.withCondition("Text = '"+entity.getText()+"'")
.withCondition("KeywordMatchType = '"+entity.getMatchType()+"'")
.get();
}
while(iter.hasNext()) {
_copyLabelsHelper(entity,iter.next());
}
}
//Copy the labels form orig entity to dest entity
function _copyLabelsHelper(orig,dest) {
var label_iter = orig.labels().get();
while(label_iter.hasNext()) {
dest.applyLabel(label_iter.next().getName());
}
}
//Returns a text representation of an entity
//For a better way, check: http://goo.gl/kZL3X
function _getEntityType(obj) {
if(typeof(obj['getCampaign']) == "undefined") {
return 'Campaign';
}
if(typeof(obj['getAdGroup']) == "undefined") {
return 'AdGroup';
}
if(typeof(obj['getHeadline']) != "undefined") {
return "Ad";
}
if(typeof(obj['getText']) != "undefined") {
return "Keyword";
}
return null;
}