Last active
February 23, 2024 00:51
-
-
Save caseypage/5fc3b5bd4ae2fbcdff1286c7bce94a05 to your computer and use it in GitHub Desktop.
This will automate setting custom parameters for your adwords tracking template at the campaign and adgroup level.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?