Skip to content

Instantly share code, notes, and snippets.

@caseypage
Last active February 23, 2024 00:51
Show Gist options
  • Save caseypage/5fc3b5bd4ae2fbcdff1286c7bce94a05 to your computer and use it in GitHub Desktop.
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.
// 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;
}
@igsm
Copy link

igsm commented May 26, 2020

Hey Casey,
Thanks for the script and a tutorial on YouTube. It is very helpful.
One thing, though. Do we need var new_custom_parameters = {}; in get_new_custom_parameters function? Otherwise, it throws an error.
Cheers,
Igor

@caseypage
Copy link
Author

Ah yes - thanks, I added that.

@princess-carolyn
Copy link

Hey Casey,
thanks for the script!
I've two question, do I have to put a tracking template in the GAds panel using this script or just running script is enough?
And next - how can I set up custom parameters to {_keywords}?

Cheers!

@caseypage
Copy link
Author

caseypage commented Jul 10, 2020

Yes - you have to create a tracking template inside google adwords first. You do this under the Account Settings. This is a video on how to do that.

Inside your tracking template, you'll have something like: "utm_term={keyword}" - this will capture the keyword using a value track parameter. Video about that here.

This video here is a walk through on how to use the above script

The script above only sets the "_campaign" and "_adgroup" custom parameter. If you need to set other custom parameters, then you'll need to modify the script to do what you need it to do.

Hope that helps!

@princess-carolyn
Copy link

Thank you Casey! Now is all clear! :)

@augustorebagliati
Copy link

Thanks, this is awesome! Is there by chance a MCC version of this script?

@Shani-b21
Copy link

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

@caseypage
Copy link
Author

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.

@AlamgirAnwar
Copy link

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

@princess-carolyn
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment