Change Template Tracking URL in Google Ads To Include Campaign Name, Medium, MatchType, Keyword, Ad Group… Automatically Through Google Script

Use the following script to change all of your Google Ads Tracking Template to include all the Campaign Name, Ad Group, Medium, MatchType, Keyword Used automatically using Google Script. 

I used this script originally when I was using CallRail and wanted to automatically pull in the Keyword, Campaign Name into the CallRail Report. Instead of sitting there and changing each ad Final URL I used this script. 

function  main()
{
  //{CampaignName} ---> Campaignlevel Template
  //{AdGroupName} ----> AdgroupLevel Template
 

 
// Enter your template with at least one of {CampaignName} or {AdGroupName}
  var TrackingTemplate="{lpurl}?matchtype={matchtype}&network={network}&device={device}&adposition={adposition}&keyword={keyword}&utm_source=google&utm_medium=cpc&utm_adgroup={AdGroupName}&utm_campaign={CampaignName}";  //Example
  
var _CAMPAIGN_CONTAINS="";               //Filter by Campaign name   
var _ADGROUP_CONTAINS="";               //Filter by Adgroup name 
var STATUS="ENABLED";                    //ENABLED, PAUSED


// Hit Preview to see the changes/logs. 


//////////////////////////////////////////////  
if(TrackingTemplate.search("{CampaignName}")>0&&TrackingTemplate.search("{AdGroupName}")==-1)
{
var TempSplit=TrackingTemplate.split("&");
for(var i in TempSplit)
{
 if(TempSplit[i].split("=").indexOf("{CampaignName}")>0)
  {var No=i;
   break;
  }
 }
var Temp=TempSplit[No].split("=");

 var campaignIterator=_CAMPAIGN_CONTAINS==""?AdWordsApp.campaigns().withCondition("Status = "+STATUS).get():AdWordsApp.campaigns().withCondition("Name contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Status = "+STATUS).get();

if(!campaignIterator.hasNext()){Logger.log("No Campaigns matched with this condition")}
while(campaignIterator.hasNext())
  {
  
  var campaign=campaignIterator.next();
  Temp[1]=campaign.getName(); 
  TempSplit.splice(No,1,Temp.join("="));
  var campaigntemplate=TempSplit.join("&");;
  campaign.urls().setTrackingTemplate(campaigntemplate);
  }
  
}  

if(TrackingTemplate.search("{AdGroupName}")>0)
{
var CampaignCondition=false;  
var TempSplit=TrackingTemplate.split("&");
for(var i in TempSplit)
{
 if(TempSplit[i].split("=").indexOf("{AdGroupName}")>0)
  {var No=i;}
  if(TempSplit[i].split("=").indexOf("{CampaignName}")>0)
  {var Cn=i;CampaignCondition=true;}
}
var Temp=TempSplit[No].split("=");
  
if(_ADGROUP_CONTAINS==""&&_CAMPAIGN_CONTAINS=="")
{var adgroupIterator=AdWordsApp.adGroups().withCondition("Status = "+STATUS).get();}
  else if(_ADGROUP_CONTAINS==""&&_CAMPAIGN_CONTAINS!=="")
var adgroupIterator=AdWordsApp.adGroups().withCondition("Name contains '"+_ADGROUP_CONTAINS+"'").withCondition("Status = "+STATUS).get();
 else if(_ADGROUP_CONTAINS!==""&&_CAMPAIGN_CONTAINS!=="")
var adgroupIterator=AdWordsApp.adGroups().withCondition("CampaignName contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Name contains '"+_ADGROUP_CONTAINS+"'").withCondition("Status = "+STATUS).get();  
  var adgroupIterator=AdWordsApp.adGroups().withCondition("CampaignName contains '"+_CAMPAIGN_CONTAINS+"'").withCondition("Name contains '"+_ADGROUP_CONTAINS+"'").withCondition("Status = "+STATUS).get();
  if(!adgroupIterator.hasNext()){Logger.log("No Campaigns/Adgroups matched with this condition")}
  if(CampaignCondition==false){
  while(adgroupIterator.hasNext())
  {   
  var adgroup=adgroupIterator.next();
  Temp[1]=adgroup.getName();
  TempSplit.splice(No,1,Temp.join("="));
  var adgrouptemplate=TempSplit.join("&");
    adgroup.urls().setTrackingTemplate(adgrouptemplate);
  }
  } else{
    var TempCamp=TempSplit[Cn].split("=");
    while(adgroupIterator.hasNext())
  {   
  var adgroup=adgroupIterator.next();
  Temp[1]=encodeURIC(adgroup.getName());
  TempCamp[1]=encodeURIC(adgroup.getCampaign().getName()); 
  TempSplit.splice(No,1,Temp.join("="));
  TempSplit.splice(Cn,1,TempCamp.join("="));  
  var adgrouptemplate=TempSplit.join("&");
    adgroup.urls().setTrackingTemplate(adgrouptemplate);
  }
  }
} else {Logger.log("Enter at least one of the {CampaignName} or {AdGroupName}")}    

}    


function encodeURIC( r ) {
return r.replace(/\W+/g, "");
}

You may also like