How to set up Google Ad Precautionary Tracking Script
This article covers the steps required to set up the Google Ad Precautionary Tracking Script, a fail-safe mechanism designed to ensure accurate attribution data when the UTM template fails. This script tracks all clicks on a specific link and sends the data to Google Analytics. The only customization needed is updating the URL for the tracking page to match the one used by your website.
Steps to Set Up Google Ad Precautionary Tracking Script
Step 1: Visit Your Google Ad Account
- Log in to your Google Ad account.
Step 2: Navigate to Scripts in Google Ads
- Click on Tools and Settings.
- Select Bulk Actions.
- Click on Scripts.
Please Note: If you cannot find this option, switch to advanced view to see the Tools and Settings option.
Step 3: Add the Script
- Remove any predefined code in the script editor.
- Copy and paste the following script:
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 adType = ad.getType();
var headline = "";
if (ad.getHeadline()) {
headline = ad.getHeadline();
} else if(ad.isType().expandedTextAd()) {
headline = ad.asType().expandedTextAd().getHeadlinePart1();
} else if(ad.isType().gmailImageAd()) {
headline = ad.asType().gmailImageAd().getName();
} else if(ad.isType().gmailMultiProductAd()) {
headline = ad.asType().gmailMultiProductAd().getHeadline();
} else if(ad.isType().gmailSinglePromotionAd()) {
headline = ad.asType().gmailSinglePromotionAd().getHeadline();
} else if(ad.isType().html5Ad()) {
headline = ad.asType().html5Ad().getName();
} else if(ad.isType().imageAd()) {
headline = ad.asType().imageAd().getName();
} else if(ad.isType().responsiveDisplayAd()) {
headline = ad.asType().responsiveDisplayAd().getShortHeadline();
} 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
- After closing the script editor, rename the script to something recognizable.
- Change the frequency from the list to Hourly.
FAQs
Question: If I have another script, will it affect it?
Answer: Yes, scripts can override each other as they affect UTM parameters.
Question: Why do I need to authorize it?
Answer: Authorization is necessary because the script is implemented on your Google Ad Account. It is essential to check the preview before running it to ensure it shows most campaigns as successful with the landing page found.
Question: How do I update the URL in the tracking template?
Answer: You need to update the TrackingTemplate variable in the script to match the URL structure used by your website. The URL should be adjusted to include the appropriate UTM parameters that correspond to your campaign, ad group, and other relevant details. This is essential for the script to track clicks correctly.
Question: What happens if the script doesn’t find a campaign or ad group?
Answer: If no campaigns or ad groups match the conditions, the script logs a message saying “No Campaigns/Adgroups matched with this condition” and does not apply any tracking templates.
Question: Do I need to keep the Google Ads tab open for the script to run?
Answer: No. Once you have authorized and saved the script with an hourly frequency, it runs on Google’s servers automatically. You can close the tab and the script will continue to function.
Question: Can I use this script if I already use a third-party tracking software?
Answer: Use caution. Many third-party tracking tools provide their own scripts. If you run both, they may compete to overwrite the same UTM parameters, leading to "Flapping" (where the URL changes back and forth every hour). If you use a third-party tool, check their documentation before adding a manual precautionary script.