Skip to content

Advertising · 1 min read

Set Up a Backup Ad Tracking Script for Google Ads

What the script does

If your Google Ads tracking template doesn't apply correctly, clicks arrive without UTM parameters and your attribution data has gaps. This script adds a backup: it reapplies a tracking template to every enabled ad group on a schedule, using campaign and ad group names.

Before you start

  • You need admin access to your Google Ads account.
  • Know your landing page URL and the UTM parameters you want to use.
  • You'll need to preview and authorize the script before it runs.

Add the script

  1. Sign in to Google Ads and go to Tools → Bulk Actions → Scripts.
  2. Click + to create a new script.
  3. Remove the placeholder code and paste:
`` function main() { var TrackingTemplate = "{lpurl}?utm_source=adwords&utm_medium={AdName}&utm_campaign={CampaignName}&utm_content={AdGroupName}&utm_keyword={keyword}&utm_matchtype={matchtype}&campaign_id={campaignid}&ad_group_id={adgroupid}&ad_id={creative}"; var STATUS = "ENABLED"; if (TrackingTemplate.search(/{AdGroupName}|{CampaignName}|{AdName}/g) == -1) { Logger.log("Enter at least one required parameter"); return; } var adgroupIterator = AdsApp.adGroups().withCondition("Status = " + STATUS).get(); while (adgroupIterator.hasNext()) { var adgroup = adgroupIterator.next(); var template = TrackingTemplate .replace(/{AdGroupName}/g, adgroup.getName().replace(/\s/g, '%20')) .replace(/{CampaignName}/g, adgroup.getCampaign().getName().replace(/\s/g, '%20')); adgroup.urls().setTrackingTemplate(template); } } ``
  1. Click Authorize and follow the prompts.
  2. Click Preview and check the log for errors.

Name and schedule it

  1. Name it something clear, such as "Tracking Backup Script".
  2. Set the frequency to Hourly.
  3. Save.

Check it's working

Click one of your ads (or use Google's ad preview tool) and confirm the UTM parameters appear in the landing page URL.

Did this answer your question?

No, I need help

More in Advertising