Just a simple script to automate the addition/customization of Tracking Templates that will specify the Campaign Name in each campaign's UTMs for any third party software that cannot fully read GCLID data. Since Google's ValueTrack parameters do not allow for Campaign Names to be dynamically inserted into Tracking Templates, websites with third party tools (SalesForce, Pardot, etc.) may need the Campaign Name to be specified. This script will make the addition of each campaign's name in the UTMs a bit quicker and more dynamic. You can set it up in the account and even schedule it to run hourly to ensure that the Campaign Names are always up-to-date!
By default, this Script will only customize the "campaign" UTM parameter with each Campaign's Name at the Campaign-level Tracking Template. You can customize further by editing the UTM structure specified in line 10 of the script – just don't edit the encodedCampaignName portion!
Go to your Google Ads Account
Go To Tools > Bulk Actions > Scripts
Create a new script
Name the Script something identifiable ("Campaign Tracking Template Formatting")
Copy & Paste the script below into the Script Editor and Save. You'll need to authorize the script prior to running it.
Feel free to make any further adjustments to the UTM structure if desired. Any non-specified UTMs will still be sent to Google Analytics through GCLID data, provided Auto-Tagging for the account is enabled.
Back on the main Scripts page, you can adjust the Frequency of this script to run Hourly to ensure that any changes to Campaign Names are filtered into the UTM parameters – however that may make the data a bit shaky if Campaign Names in the account are being changed consistently, so do so at your own risk!
If you want this script to only make adjustments on active campaigns, you can just replace AdsApp.campaigns().get(); with AdsApp.campaigns().withCondition("Status = ENABLED").get(); and only active campaigns will be adjusted!
function main() {
//adds a custom tracking template at the campaign level to specify campaign names in the UTM parameters. You can customize the UTMs in line 10, but do not edit the encodedCampaignName portion.
//get all campaigns in account. To filter see guidelines: https://developers.google.com/google-ads/scripts/docs/reference/adsapp/adsapp_campaignselector
var campaignIterator = AdsApp.campaigns().get();
//iterate addition of tracking template over every campaign that meets your filter criteria. Gets name of each campaign, encodes them for the URL, and sets the tracking template at the Campaign level.
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignName=campaign.getName();
var encodedCampaignName=encodeURIComponent(campaignName);
campaign.urls().setTrackingTemplate('{lpurl}?utm_campaign='+encodedCampaignName)
}
}
Alternatively, if you have a more complicated tracking configuration, you can use the following Account-level Tracking Template. It will set the UTM_Source (google), UTM_Medium (cpc), and UTM_Term (Keyword, using ValueTrack) values automatically. The UTM_Campaign and UTM_Content values will be Custom Parameters that are set at the Campaign and Ad/Sitelink level, respectively.
Implement the Account-Level Tracking Template below.
Add the following two scripts to automatically configure the Campaign and Sitelink Custom Parameters.
You can run them once or set them to run on a schedule to keep up with link/naming convention changes automatically.
Be sure to Preview and check the values of these Custom Parameters before Running. They'll most likely be fine, but all accounts are different!
Google Ads Scripts do not yet support adding Custom Parameters to existing ads. You can only do this while setting up new Ads via the API. As a result, the easiest way to handle existing ads is to run through them manually in the Google Ads Editor.
{lpurl}?utm_source=google&utm_medium=cpc&utm_campaign={_utmcampaign}&utm_content={_utmcontent}&utm_term={keyword}
This script will auto-apply the Custom Parameter utmcampaign to all Campaigns in your account. The value will be the name of the Campaign in a URI Encoded format. (i.e. a Campaign with the name: Search - NonBrand - Example Campaign will have its utmcampaign value set to Search%20-%20NonBrand%20-%20Example%20Campaign.
function main() {
//adds a custom tracking template at the campaign level to specify campaign names in the UTM parameters. You can customize the UTMs in line 10, but do not edit the encodedCampaignName portion.
//get all campaigns in account. To filter see guidelines: https://developers.google.com/google-ads/scripts/docs/reference/adsapp/adsapp_campaignselector
var campaignIterator = AdsApp.campaigns().withCondition("Status = ENABLED").get();
//iterate addition of tracking template over every campaign that meets your filter criteria. Gets name of each campaign, encodes them for the URL, and sets the tracking template at the Campaign level.
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignName=campaign.getName();
var encodedCampaignName=encodeURIComponent(campaignName);
campaign.urls().setCustomParameters({utmcampaign: encodedCampaignName});
}
}
This script will auto-apply the Custom Parameter utmcontent to all Sitelinks in your account. The value will be the final segment of the URL's path (i.e. example.com/info-center/sample-guide-download would have its UTM_Content set to sample-guide-download
function main() {
//adds a custom tracking template at the sitelink level to specify url names in the UTM parameters. Preview the script before running.
//IF THE PARAMETER VALUE IS NOT SET TO THE FINAL PATH OF THE URL: ADJUST THE "length - 1" VALUE IN LINE 11. INCREASE OR DECREASE THE 1 BASED ON YOUR URL STRUCTURE & TEST.
//get all campaigns in account. To filter see guidelines: https://developers.google.com/google-ads/scripts/docs/reference/adsapp/adsapp_campaignselector
var sitelinkIterator = AdsApp.extensions().sitelinks().get();
//iterate addition of tracking template over every campaign that meets your filter criteria. Gets name of each campaign, encodes them for the URL, and sets the tracking template at the Campaign level.
while (sitelinkIterator.hasNext()) {
var sitelink = sitelinkIterator.next();
var sitelinkUrl = sitelink.urls().getFinalUrl();
Logger.log(sitelinkUrl);
var sitelinkUrlPath = sitelinkUrl.split("/")[sitelinkUrl.split("/").length - 1].replace(/\?.*$/,"");
Logger.log(sitelinkUrlPath);
sitelink.urls().setCustomParameters({utmcontent: sitelinkUrlPath});
}
}