How to Set Up Google Ad Precautionary Tracking Script

Jan//Apprezo
Written by Jan//ApprezoLast updated 1 year ago

How to Set Up Google Ad Precautionary Tracking Script

Overview: The Google Ad Precautionary Tracking Script is a safety mechanism designed to ensure accurate attribution data, even when the UTM template fails. This script tracks all clicks on specific links and sends the data to Google Analytics. The only customization needed is to update the URL in the script to match your website's tracking page.

Steps to Set Up the Google Ad Precautionary Tracking Script

Step 1: Log in to Your Google Ad Account

  • Start by logging into your Google Ad account.

Step 2: Navigate to Scripts in Google Ads

  1. Click on Tools and Settings in the top menu.

  2. Select Bulk Actions.

  3. Click on Scripts.

    • Note: If you can't find this option, switch to advanced view to access the Tools and Settings option.

Step 3: Add the Script

  1. Open the script editor by clicking on + Script.

  2. Remove any predefined code in the editor.

  3. Copy and paste the following script:

javascript

Copy code

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 CAMPAIGNCONTAINS = "";

    var ADGROUPCONTAINS = "";

    var STATUS = "ENABLED";


    if (TrackingTemplate.search(/{AdGroupName}|{CampaignName}|{AdName}/g) == -1) {

        Logger.log("Enter at least one of the {CampaignName}, {AdGroupName}, or {AdName} parameters in the tracking template");

        return;

    }


    if (TrackingTemplate.search("{AdGroupName}") > 0) {

        var adgroupIterator = AdsApp.adGroups().withCondition("Status = " + STATUS).get();


        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 = "";


                    if (ad.getHeadline()) {

                        headline = ad.getHeadline();

                    } else if (ad.isType().expandedTextAd()) {

                        headline = ad.asType().expandedTextAd().getHeadlinePart1();

                    } else if (ad.isType().responsiveSearchAd()) {

                        var headlines = ad.asType().responsiveSearchAd().getHeadlines();

                        if (headlines && headlines[0].text) {

                            headline = headlines[0].text;

                        }

                    }


                    Logger.log("Headline text: " + headline);

                    if (headline) {

                        adgrouptemplate = adgrouptemplate.replace(/{AdName}/g, headline.replace(/\s/g, '%20'));

                    } else {

                        adgrouptemplate = adgrouptemplate.replace(/{AdName}/g, ad.getId());

                    }

                }

            }


            adgroup.urls().setTrackingTemplate(adgrouptemplate);

            Logger.log(adgroup.getCampaign().getName() + " => " + adgroup.getName() + " => " + adgrouptemplate);

        }

    }

}


Step 4: Finalize the Script

  1. After pasting the script, close the script editor.

  2. Rename the script to something easily recognizable, like "Google Ad Precautionary Tracking Script".

  3. Set the script's frequency to Hourly to ensure it runs regularly and keeps your tracking updated.

Frequently Asked Questions

  1. If I have another script running, will this affect it?

    • Yes, scripts can override each other if they are affecting the same UTM parameters. Be cautious of conflicts between multiple scripts.

  2. Why do I need to authorize the script?

    • Authorization is necessary because the script will be executed within your Google Ad account, affecting your campaigns. Always preview the script before running it to ensure it functions as expected.

This setup ensures that your Google Ads account tracks clicks accurately, even if your primary tracking template encounters issues.


Did this article help you solve your issue?

Apprezo Helpdesk - Help Center

© 2026 Apprezo Helpdesk - Help Center. All rights reserved.