-
-
Save caseypage/5fc3b5bd4ae2fbcdff1286c7bce94a05 to your computer and use it in GitHub Desktop.
// Sample Tracking Template | |
// {lpurl}?utm_medium=adwords&utm_campaign={_campaign}&utm_source={_adgroup}&utm_term={keyword} | |
// This script will set custom parameters {_campaign} and {_adgroup} at the campaign and adgroup level respectively. | |
function main() { | |
///// Update Campaigns | |
// get all campaigns | |
var campaignSelector = AdsApp | |
.campaigns() | |
.withCondition("Status NOT_IN ['REMOVED']") | |
.withCondition('CampaignStatus != REMOVED'); | |
// Update those campaigns with the new tracking template | |
// {_campaign = <name of campaign>} | |
update_campaign_parameters(campaignSelector); | |
///// Update AdGroups | |
// Get All Adgroups | |
var adgroupSelector = AdsApp | |
.adGroups() | |
.withCondition("Status NOT_IN ['REMOVED']") | |
.withCondition('CampaignStatus != REMOVED'); | |
// Update the adgroups with the new tracking template | |
// {_adgroup = <name of adgroup>} | |
update_adgroup_parameters(adgroupSelector); | |
} | |
function update_campaign_parameters(campaignSelector) { | |
// loop through campaigns and update the tracking template | |
var iterator = campaignSelector.get(); | |
while (iterator.hasNext()) { | |
var campaign = iterator.next(); | |
var campaign_name = campaign.getName(); | |
param_value = format_parameter_value(campaign_name); | |
new_custom_parameters = get_new_custom_parameters(campaign, 'campaign', param_value); | |
// Update the campaign and set the custom parameters | |
Logger.log("Campaign: " + campaign_name + "|" + new_custom_parameters['campaign']); | |
campaign.urls().setCustomParameters(new_custom_parameters); | |
} | |
} | |
function update_adgroup_parameters(adgroupSelector) { | |
// loop through campaigns and update the tracking template | |
var iterator = adgroupSelector.get(); | |
while (iterator.hasNext()) { | |
var adgroup = iterator.next(); | |
var adgroup_name = adgroup.getName(); | |
param_value = format_parameter_value(adgroup_name); | |
new_custom_parameters = get_new_custom_parameters(adgroup, 'adgroup', param_value); | |
// Update the adgroup and set the custom parameters | |
Logger.log("Adgroup: " + adgroup_name + "|" + new_custom_parameters['adgroup']); | |
adgroup.urls().setCustomParameters(new_custom_parameters); | |
} | |
} | |
// replace spaces with underscores, remove all characters that is not a number or letter. | |
function format_parameter_value(adwords_name) { | |
return adwords_name.replace(/[^a-z_\d ]+/ig,'').toLowerCase().replace(/ /g, ' ').replace(/ /g, '_').substr(0, 50); | |
} | |
// Whether it be an adgroup, ad, or campaign - bget the new tracking parameter. | |
function get_new_custom_parameters(adwords_object, parameter_name, parameter_value) { | |
var custom_parameters = adwords_object.urls().getCustomParameters(); | |
var new_custom_parameters = {}; | |
if (custom_parameters == null) { | |
new_custom_parameters[parameter_name] = parameter_value; | |
} else { | |
if (custom_parameters[parameter_name]) { | |
// update existing value | |
custom_parameters[parameter_name] = parameter_value; | |
} else { | |
// has parameters, but not the adwords parameter. Make sure we have room for one more parameter. | |
var num = Object.keys(custom_parameters).length; | |
if (num < 3) { | |
custom_parameters[parameter_name] = parameter_value; | |
} | |
} | |
new_custom_parameters = custom_parameters; | |
} | |
return new_custom_parameters; | |
} |
Thanks, this is awesome! Is there by chance a MCC version of this script?
Hi Casey,
Thank you very much for this script!
I would like to add a custom parameter at the ad level, how do I do that?
Thank you
I don't think this is possible. Based on the ads script documentation: https://developers.google.com/google-ads/scripts/docs/reference/adsapp/adsapp_adurls - there is not a "setCustomParameters" method at the ad level. So I do not know of a way to do that.
Hey Casey,
Thanks for the script and a tutorial on YouTube. It is very helpful.
Can you guide me on how I can place this tag {{utm_keyword}} on a website?
If you have a guided video, it will be useful.
Best Wishes,
Alamgir
Hey,
is it possible to modify this script to run it only on campaigns with some specific campaign name? For example only on campaigns which have TEST in name?
Thank you Casey! Now is all clear! :)