Hello!
I have the script that I post below. I place it in several different accounts and in some of them I get the error "An error occurred. Please try again later" several times (one for every campaign) and in some it runs without errors.
What the script does is to set the ad group final url suffix to a certain value, putting the name of the campaign, the name of the ad group and other fix utms.
I tryed to change a campaign's name and execute the script, and for that particular campaign it performs well, but I get the error for the rest of campaigns that had not changed.
Can someone help me understand the errors?
Thanks!
Here is the script code:
function main() {
var SuffixTemplate="utm_source=google&utm_medium=pl&utm_campaign={CampaignName}&utm_adgroup={AdGroupName}&utm_ad={creative}&utm_term={keyword}";
var _CAMPAIGN_CONTAINS=""; //Filter by Campaign name
var _ADGROUP_CONTAINS=""; //Filter by Adgroup name
var STATUS="ENABLED"; //ENABLED, PAUSED
if (SuffixTemplate.search(/{AdGroupName}|{CampaignName}/g)==-1) {
Logger.log("Enter at least one of the {CampaignName} or {AdGroupName} parameter in the tracking template");
return
}
if (SuffixTemplate.search("{CampaignName}")>0&&SuffixTemplate.search("{AdGroupName}")==-1) {
var campaignIterator=_CAMPAIGN_CONTAINS==""?AdsApp.campaigns().withCondition("Status = "+STATUS).get():AdsApp.campaigns().withCondition("Name contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Status = "+STATUS).get();
if (!campaignIterator.hasNext()) {
Logger.log("No Campaigns matched with this condition");
return
}
while (campaignIterator.hasNext()) {
var campaign=campaignIterator.next();
var campaigntemplate=SuffixTemplate.replace(/{CampaignName}/g,campaign.getName().replace(/\s/g,'%20'))
campaign.urls().setFinalUrlSuffix(campaigntemplate);
Logger.log(campaign.getName()+" => "+campaigntemplate)
}
}
if (SuffixTemplate.search("{AdGroupName}")>0) {
var adgroupIterator = {hasNext:function(){return false}}
if (_ADGROUP_CONTAINS==""&&_CAMPAIGN_CONTAINS=="") {
adgroupIterator=AdsApp.adGroups().withCondition("Status = "+STATUS).get();
} else if (_ADGROUP_CONTAINS==""&&_CAMPAIGN_CONTAINS!=="") {
adgroupIterator=AdsApp.adGroups().withCondition("CampaignName contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Status = "+STATUS).get();
} else if (_ADGROUP_CONTAINS!==""&&_CAMPAIGN_CONTAINS!=="") {
adgroupIterator=AdsApp.adGroups().withCondition("CampaignName contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Name contains '"+_ADGROUP_CONTAINS+"'").withCondition("Status = "+STATUS).get();
} else if (_ADGROUP_CONTAINS!==""&&_CAMPAIGN_CONTAINS=="") {
adgroupIterator=AdsApp.adGroups().withCondition("Name contains '"+_ADGROUP_CONTAINS+"'").withCondition("Status = "+STATUS).get();
}
if (!adgroupIterator.hasNext()) {
Logger.log("No Campaigns/Adgroups matched with this condition");return
}
while (adgroupIterator.hasNext()) {
var adgroup=adgroupIterator.next();
var adgrouptemplate=SuffixTemplate.replace(/{AdGroupName}/g,adgroup.getName().replace(/\s/g,'%20'))
if (SuffixTemplate.search("{CampaignName}")>0) {
adgrouptemplate=adgrouptemplate.replace(/{CampaignName}/g,adgroup.getCampaign().getName().replace(/\s/g,'%20'))
}
adgroup.urls().setFinalUrlSuffix(adgrouptemplate);
Logger.log(adgroup.getCampaign().getName()+" => "+adgroup.getName()+" => "+adgrouptemplate)
}
}
}