
Automate UTM parameters with campaign names in Google Ads: Anyone who works with Google Analytics, Matomo or any other web tracker knows the importance of UTM parameters. This makes it possible to determine the origin of traffic and place targeted ads.
However, manually setting the parameters is time-consuming and error-prone. In this article, you’ll learn why you should automate setting campaign names with UTM parameters in Google Ads and how to install the script for it.
Why are UTM parameters important in Google Ads?
Tracking parameters are attached to links and read by the web tracker. This makes it possible to assign the path by which the visitor arrived at a website. Google Analytics also assigns the proper campaign names to Google Ads traffic when connected. However, this is created via the gclid assignment and not via the UTM parameters.
However, if you want to have readable campaign names and ad group names in Matomo, this is a rocky road.
Manually setting campaign name and ad group name for UTM parameters in Google Ads
A nice UTM parameter structure would be for example:
utm_source=google&utm_medium=cpc&utm_campaign={Kampagnenname}&utm_content={ad group name}&utm_term=
This would also be a good way to evaluate the ad structure in Matomo. It would be nice. Unfortunately, Google Ads does not provide corresponding variables for campaign name and ad group name. Click here for the variable list on Google
The solution is the detour via user-defined ValueTrack parameters, in which we enter names accordingly.
Example:
campaign = My%20Campaign%20Name
adgroup = My%20Adgroup%20Name
It’s also really easy, because we have to encode special characters and spaces only URL. With spaces the links would not work. So it’s not something you can do quickly on the side while creating a campaign. So as soon as there are minor campaign changes to the ad groups and someone doesn’t think about it, everything breaks again.
It’s running again! (If you’re frustrated now, read on. Everything will be fine).
Best practices for using UTM parameters
To ensure your UTM parameters deliver the desired results, you should follow these best practices:
Consistent naming: Use consistent naming conventions for campaigns to facilitate analysis of your data. For example, all campaigns running on Facebook should use the medium “social” and the source “facebook.”
No special characters: Avoid special characters and spaces in parameters, as these can cause tracking issues. Use hyphens or underscores instead.
Clarity and precision: Keep your naming as clear and precise as possible, e.g., utm_campaign=sale_2025 instead of utm_campaign=action.
Regularly review: Check your URL parameters regularly to ensure they still align with your current campaigns and strategies.
These best practices will help you optimize the quality of your tracking data and manage your marketing efforts more effectively.
Anyone who wants to set UTM parameters for other networks in addition to Google Ads should definitely check out the blog article about it.
Advantages of automating UTM parameters in Google Ads
Automating UTM parameters offers numerous benefits for marketers who want to manage their campaigns more efficiently. A key advantage is time savings: Manually adding UTM parameters to each campaign in Google Ads can quickly become tedious, especially with a large number of ads. Automation standardizes this process and significantly speeds it up.
Automation also minimizes the risk of errors. Typos or inconsistent parameters can affect the accuracy of data analysis. A cleanly automated workflow ensures that tracking URLs are always correct and consistent. This allows marketers to collect reliable data and make informed decisions for their future campaigns. Automation thus also contributes to better transparency and increased efficiency.
Automatically set UTM parameters in Google Ads with script
The script is a MCC script, which does the job automatically via a defined label and a defined tracking parameter suffix. In all the MCC’s Google Ads account that have the corresponding label.
What the script does:
- Campaigns with the satis “Activated” and “Paused” are selected.
In campaigns, the custom parameter “campaign ={Kampagnenname} ” set
In ad groups, the custom parameter “adgroup = ” is set.
The campaign suffix is set to the value in CONFIG.LABEL_NAME. (This should of course contain the two user-defined parameters accordingly.
Installation instructions:
- Copy and authorize script
- Set variables: CONFIG.LABEL_NAME enter the name of the label, which must be set for the accounts. In CONFIG.SUFFIX the (previously tested!) parameter Link must be added.
- The script can run hourly. Thus, it also eliminates problems with latest campaigns.
- Of course, you should test the links thoroughly with the URL tester.
Step-by-step guide to implementing the script
Implementing a script to automate UTM parameters in Google Ads is easier than many people think. Here’s a quick guide:
Create a script: Log in to your Google Ads account and navigate to “Tools and Settings” → “Scripts.” Click the “+” button to create a new script.
Insert script: Copy the script from this article and paste it into the editor.
Configure parameters: Adapt the variables in the script to your campaign structure. Define which parameters should be set automatically, such as utm_source, utm_medium, or utm_campaign.
Test the script: Before activating, you should test the script to ensure it works correctly. Use the test feature in Google Ads to do so.
Schedule script: Specify how frequently the script should run, for example, daily, to ensure new campaigns are automatically provided with the correct parameters.
These steps make it easy to integrate the script into your campaign management and reap the benefits of automation.
Common mistakes and how to avoid them
Errors can also occur when using UTM parameters. The most common ones include:
Forgotten URL encoding : Parameters like spaces or special characters can cause errors if they aren’t encoded correctly. Use tools like a URL builder to ensure your links are formatted correctly.
Inconsistent parameters : Spelling differences, such as “Google” and “google,” can cause data fragmentation in Analytics. Stick to a consistent schema.
Too many parameters : Sometimes too many parameters are added, which unnecessarily bloats the URL and makes it difficult to understand. Limit yourself to the most important information.
Irregular use : If only a portion of your campaigns are tagged with UTM parameters, data gaps arise. Automation helps prevent this.
Following these points will minimize errors and ensure that your tracking data is complete and reliable.
// UTM Setter for Google Ads // This Script aims to set UTM Parameters for Google Ads automatically. The key benefit is the transport of the camaign name which is not automatically provided by value Track Parameters. // German: https://webmasterei-prange.de/utm-parameter-kampagnennamen-automatisiert-in-google-ads // English: https://webmasterei-prange.de/en/utm-parameter-in-google-ads/ var CONFIG = { LABEL_NAME: 'Add UTM Parameter', SUFFIX: 'utm_source=google&utm_medium=cpc&utm_campaign={_campaign}|{campaignid}&utm_term={feeditemid}{keyword}&utm_content={_adgroup}|{adgroupid}&utm_id={campaignid}' } function main() { const accountSelector = AdsManagerApp.accounts() .withCondition("LabelNames CONTAINS '" + CONFIG.LABEL_NAME + "'") .withLimit(50); accountSelector.executeInParallel('processAccount', 'allFinished'); } function processAccount() { campaignParametersUpdate() adgroupParametersUpdate(); pmaxParametersUpdate(); } function pmaxParametersUpdate(){ var performanceMaxCampaignSelector = AdsApp .performanceMaxCampaigns() .withCondition("campaign.status IN ('ENABLED','PAUSED')"); var performanceMaxCampaignIterator = performanceMaxCampaignSelector.get(); while (performanceMaxCampaignIterator.hasNext()) { var performanceMaxCampaign = performanceMaxCampaignIterator.next(); var name = performanceMaxCampaign.getName(); name = encodeURI(name); var params = performanceMaxCampaign.urls().getCustomParameters() //Logger.log(params.campaign + ' = ' + name) if(params.campaign != name){ params.campaign = name; performanceMaxCampaign.urls().setCustomParameters(params) } if(performanceMaxCampaign.urls().getFinalUrlSuffix() != CONFIG.SUFFIX){ performanceMaxCampaign.urls().setFinalUrlSuffix(CONFIG.SUFFIX) } } } function campaignParametersUpdate(account){ var campaignSelector = AdsApp .campaigns() .withCondition("campaign.status IN ('ENABLED','PAUSED')") var campaignIterator = campaignSelector.get(); while (campaignIterator.hasNext()) { var campaign = campaignIterator.next(); var name = campaign.getName(); name = encodeURI(name) //Logger.log(name) var params = campaign.urls().getCustomParameters() //Logger.log(params.campaign + ' = ' + name) if(params.campaign != name){ params.campaign = name; campaign.urls().setCustomParameters(params) } if(campaign.urls().getFinalUrlSuffix() != CONFIG.SUFFIX){ campaign.urls().setFinalUrlSuffix(CONFIG.SUFFIX) } } } function adgroupParametersUpdate(){ var adGroupSelector = AdsApp .adGroups() .withCondition("campaign.status IN ('ENABLED','PAUSED')") var adGroupIterator = adGroupSelector.get(); while (adGroupIterator.hasNext()) { var adgroup = adGroupIterator.next(); var name = adgroup.getName(); name = encodeURI(name) //Logger.log(name) var params = adgroup.urls().getCustomParameters() if(params.adgroup != name){ params.adgroup = name; //Logger.log(params) adgroup.urls().setCustomParameters(params) } } } function updateAccountsInParallel() { // You can use this approach when you have a large amount of processing // to do in each of your client accounts. // Select the accounts to be processed. You can process up to 50 accounts. const accountSelector = AdsManagerApp.accounts() .withCondition("LabelNames CONTAINS 'High spend accounts'") .withLimit(50); // Process the account in parallel. The 'processAccount' function will // be called in the context of each account in the selector. The 'allFinished' function // will be called in this script once processing is complete, and is optional. accountSelector.executeInParallel('processAccount', 'allFinished'); } /** * Process one account at a time. This method is called by the executeInParallel * method call in updateAccountsInParallel function for every account that * IT processes. * * @return {Number} the number of campaigns paused by this method. */ /** * Post-process the results from processAccount. This method will be called * once all the accounts have been processed by the executeInParallel method * call. * * @param {Array.<ExecutionResult>} results An array of ExecutionResult objects, * one for each account that was processed by the executeInParallel method. */ function allFinished(results) { for (const result of results) { /*console.log(`Customer ID: ${result.getCustomerId}; ` + `status = ${result.getStatus}.`); */ // Check the execution status. This can be one of ERROR, OK, or TIMEOUT. if (result.getStatus() == 'ERROR') { console.log(`-- Failed with error: '${result.getError()}'.`); } else if (result.getStatus() == 'OK') { // This is the value you returned from processAccount method. If you // used JSON.stringify(value) in processAccount, you can use // JSON.parse(text) to reconstruct the JavaScript object. const retval = result.getReturnValue(); console.log(`--Processed ${retval} campaigns.`); } else { // Handle timeouts here. } } }
Conclusion
Making UTM parameters in Google Ads campaigns human-readable is a challenge when done manually. If you configure a Google Ads script for this purpose, it will do the job reliably.