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
Log in to your Google Ads account.
Confirm you are in the correct account where tracking should be applied.
Step 2: Navigate to Scripts in Google Ads
Click Tools and Settings.
Select Bulk actions.
Click Scripts.
If you do not see Tools and Settings, switch Google Ads to Advanced view.
Step 3: Create a New Script
Click the + (plus) button to add a new script.
Delete any pre-filled code in the editor.
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
Click Save.
Authorize the script when prompted.
Return to the Scripts list.
Rename the script to something identifiable (example:
UTM Precautionary Tracking).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_CONTAINSand_ADGROUP_CONTAINSempty 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.

