Skip to main content

How to Set Up the Google Ads Precautionary Tracking Script

This article explains how to install and schedule a Google Ads script that applies a fallback tracking template to preserve attribution data when UTM templates fail.

Updated over 2 months ago

What the Precautionary Tracking Script Does

The Google Ads Precautionary Tracking Script:

  • Applies a tracking template to eligible ad groups

  • Ensures UTM parameters are consistently appended

  • Runs automatically on a schedule

  • Prevents attribution loss if primary tracking templates fail

The only required customization is updating the landing page URL inside the script.

Step 1: Access Your Google Ads Account

  1. Log in to your Google Ads account.

  2. Confirm you are in the correct account where tracking should be applied.

Step 2: Navigate to Scripts in Google Ads

  1. Click Tools and Settings.

  2. Select Bulk actions.

  3. Click Scripts.

If you do not see Tools and Settings, switch Google Ads to Advanced view.

Step 3: Create a New Script

  1. Click the + (plus) button to add a new script.

  2. Delete any pre-filled code in the editor.

  3. Paste the full script provided below into the editor.

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 _CAMPAIGN_CONTAINS = "";     var _ADGROUP_CONTAINS = "";     var STATUS = "ENABLED";      if (TrackingTemplate.search(/{AdGroupName}|{CampaignName}|{AdName}/g) == -1) {         Logger.log("Enter at least one of the {CampaignName} or {AdGroupName} or {AdName} parameter in the tracking template");         return;     }      if (TrackingTemplate.search("{AdGroupName}") > 0) {         var adgroupIterator = { hasNext: function() { return false; } };          if (_ADGROUP_CONTAINS == "" && _CAMPAIGN_CONTAINS == "") {             adgroupIterator = AdsApp.adGroups().withCondition("Status = " + STATUS).get();         } else if (_ADGROUP_CONTAINS == "" && _CAMPAIGN_CONTAINS !== "") {             adgroupIterator = AdsApp.adGroups().withCondition("CampaignName contains '" + _CAMPAIGN_CONTAINS + "'").withCondition("Status = " + STATUS).get();         } else if (_ADGROUP_CONTAINS !== "" && _CAMPAIGN_CONTAINS !== "") {             adgroupIterator = AdsApp.adGroups().withCondition("CampaignName contains '" + _CAMPAIGN_CONTAINS + "'").withCondition("Name contains '" + _ADGROUP_CONTAINS + "'").withCondition("Status = " + STATUS).get();         } else if (_ADGROUP_CONTAINS !== "" && _CAMPAIGN_CONTAINS == "") {             adgroupIterator = AdsApp.adGroups().withCondition("Name contains '" + _ADGROUP_CONTAINS + "'").withCondition("Status = " + STATUS).get();         }          if (!adgroupIterator.hasNext()) {             Logger.log("No Campaigns/Adgroups matched with this condition");             return;         }          while (adgroupIterator.hasNext()) {             var adgroup = adgroupIterator.next();             var adgrouptemplate = TrackingTemplate.replace(/{AdGroupName}/g, adgroup.getName().replace(/\s/g, '%20'));              if (TrackingTemplate.search("{CampaignName}") > 0) {                 adgrouptemplate = adgrouptemplate.replace(/{CampaignName}/g, adgroup.getCampaign().getName().replace(/\s/g, '%20'));             }              if (TrackingTemplate.search("{AdName}") > 0) {                 var adsIterator = adgroup.ads().get();                 while (adsIterator.hasNext()) {                     var ad = adsIterator.next();                     var headline = ad.getId();                     adgrouptemplate = adgrouptemplate.replace(/{AdName}/g, headline);                 }             }              adgroup.urls().setTrackingTemplate(adgrouptemplate);         }     } }

Step 4: Update the Tracking Template URL

Inside the script, locate the TrackingTemplate variable.

Update the base landing page URL to match your website’s URL structure.

Only modify the URL portion. Do not remove UTM parameters unless intentional.

Step 5: Save, Authorize, and Schedule the Script

  1. Click Save.

  2. Authorize the script when prompted.

  3. Return to the Scripts list.

  4. Rename the script to something identifiable (example: UTM Precautionary Tracking).

  5. Set the Frequency to Hourly.

How Script Frequency Works

When set to Hourly, Google runs the script automatically on its servers.

You do not need to keep Google Ads open for the script to function.

Common Issues and Fixes

Script shows no changes applied

  • Confirm campaigns and ad groups are enabled.

  • Confirm the tracking template contains at least one required placeholder.

No campaigns or ad groups matched

  • Leave _CAMPAIGN_CONTAINS and _ADGROUP_CONTAINS empty to target all enabled ad groups.

Tracking data looks overwritten

  • Multiple scripts modifying tracking templates can override each other.

FAQ

Will this script affect other Google Ads scripts?

Yes. Scripts that modify tracking templates or UTM parameters can override one another.

Why does the script require authorization?

Authorization is required because the script makes changes within your Google Ads account. Always use Preview before enabling.

How do I change the landing page URL used for tracking?

Edit the TrackingTemplate variable and replace the base URL with your website’s landing page URL.

What happens if no campaigns or ad groups match?

The script logs a message stating no matches were found and makes no changes.

Do I need to keep the Google Ads tab open?

No. Once saved and scheduled, the script runs automatically on Google’s servers.

Did this answer your question?