Setting Up Google Ad Precautionary Tracking Script in Stack
Enhance your Google Ads tracking with Stack by setting up a precautionary script. This script ensures that if your UTM template fails to capture attribution data, your clicks are still tracked effectively. Here's how you can set it up:
Steps to Set Up the Script
Step 1: Access Your Google Ad Account
- Log in to your Google Ad account.
Step 2: Navigate to Scripts
-
Click on Tools and Settings.
-
Select Bulk Actions.
-
Click on Scripts.
Note: If you can't find this option, switch to the advanced view to access more tools.
Step 3: Add the Script
-
Remove any predefined code.
-
Insert 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 headline = getAdHeadline(ad);
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);
}
}
}
function getAdHeadline(ad) {
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);
return headline;
}Remember to update the URL in the script to match your website's tracking page.
Step 4: Finalize Script Setup
- Change the script's name to something descriptive.
- Set the script's frequency to Hourly to ensure regular updates.
Frequently Asked Questions
Will this script affect other scripts?
Yes, scripts can override each other, especially if they modify UTM parameters.
Why is authorization required?
Authorization is necessary because the script interacts with your Google Ad account. Always preview the script to ensure most campaigns are successful and the landing pages are found.