{"id":4971,"date":"2022-02-16T11:33:05","date_gmt":"2022-02-16T08:33:05","guid":{"rendered":"https:\/\/trackingchef.com\/?p=4971"},"modified":"2022-08-30T15:43:24","modified_gmt":"2022-08-30T12:43:24","slug":"google-ads-dynamic-tracking-template","status":"publish","type":"post","link":"https:\/\/trackingchef.com\/google-ads\/google-ads-dynamic-tracking-template\/","title":{"rendered":"Dynamically passing Google Ads Campaign & Ad Group names to the tracking template"},"content":{"rendered":"\n

When working with Google Ads, most advertisers usually default to using the Auto-tagging<\/a> feature which automatically appends the gclid<\/em> parameter to the destination URL (e.g. domain.com\/landing-page?gclid=12345abcde). This feature enables the tracking mechanisms used by Google Ads and Google Analytics that can match a specific visitor and their consecutive actions with the ad clicked.<\/p>\n\n\n\n

In layman’s terms, the click ID (i.e. gclid) pairs the anonymous visitor with the ad clicked.<\/p>\n\n\n\n

Why use UTM’s instead of only using GCLID?<\/h2>\n\n\n\n

If you’re using Google Analytics as your primary reporting platform, this makes perfect sense. But in some cases, you might still require the UTM tracking. Let’s consider these examples:<\/p>\n\n\n\n

If you’re using Google Analytics as your primary reporting platform, this makes perfect sense. But in some cases, you might still require the UTM tracking. Let’s consider these examples:<\/p>\n\n\n\n

  1. Additional analytics platform<\/strong> – If you’re using a tool other than Google Analytics (or even in parallel to), you will want it to report accurately on the campaigns, as it won’t fetch the same Google Ads data.<\/li>
  2. Marketing Automation platforms<\/strong> – While some platforms are able to fetch the Google Ads data (e.g. Hubspot), most platforms will struggle with this, and require the UTM’s to appear explicitly in the URL.<\/li>
  3. CRM Platforms<\/strong> – Some CRM platforms, e.g. Salesforce, are disconnected from the page level tracking, so to enable the data capture of the user’s source, campaign data is sent using hidden fields in forms. This too requires the UTM’s to be present.<\/li>
  4. BI Platforms<\/strong> – Similar to the above, these too don’t always have this integration and require the UTM’s to be present.<\/li><\/ol>\n\n\n\n

    Doesn’t Google Ads have a solution?<\/h2>\n\n\n\n

    The intuitive solution to this should have been using Google Ad’s ValueTrack parameters<\/a>. These parameters can be automatically appended to all landing page URLs, injecting static (e.g. utm_source=google) or dynamic (e.g. utm_keyword={keyword}) content to the URL.<\/p>\n\n\n\n

    However, Google Ads allows only for the Campaign ID<\/strong> and Ad Group ID<\/strong> to be used as parameters for ValueTrack.<\/p>\n\n\n

    \n
    \"\"<\/figure><\/div>\n\n\n

    Though this might make sense, as Campaign\/Ad Group ID are static, whereas the name can change, this is far from being a human-friendly way of analyzing your data.<\/p>\n\n\n\n

    The solution: Google Ads Script<\/h2>\n\n\n\n

    Google Ads Scripts are a great solution for automating complex tasks in your account. (see other examples here<\/a>). These code snippets can interact with other Google tools, e.g. Google Sheets, and even with external tools such as writing\/reading from a SQL server.<\/p>\n\n\n\n

    This is usually a tool for technical savvy marketers, so we tried to keep this as simple as possible.<\/p>\n\n\n\n

    The code<\/h2>\n\n\n\n
    function  main() {\n\tvar SuffixTemplate=\"utm_source=google&utm_medium=cpc&utm_campaign={CampaignName}&utm_content={AdGroupName}&utm_ad={creative}&utm_term={keyword}&matchtype={matchtype}&device={device}&GeoLoc={loc_physical_ms}&placement={placement}&network={network}&campaign_id={campaignid}&adset_id={adgroupid}&ad_id={creative}\";\n\n\tvar _CAMPAIGN_CONTAINS=\"\";               \/\/Filter by Campaign name   \n\tvar _ADGROUP_CONTAINS=\"\";               \/\/Filter by Adgroup name \n\tvar STATUS=\"ENABLED\";                    \/\/ENABLED, PAUSED\n\n\tif (SuffixTemplate.search(\/{AdGroupName}|{CampaignName}\/g)==-1) {\n\t\tLogger.log(\"Enter at least one of the {CampaignName} or {AdGroupName} parameter in the tracking template\");\n\t\treturn\n\t}   \n\tif (SuffixTemplate.search(\"{CampaignName}\")>0&&SuffixTemplate.search(\"{AdGroupName}\")==-1) {\n\t\tvar campaignIterator=_CAMPAIGN_CONTAINS==\"\"?AdsApp.campaigns().withCondition(\"Status = \"+STATUS).get():AdsApp.campaigns().withCondition(\"Name contains '\"+_CAMPAIGN_CONTAINS+\"'\").withCondition(\"Status = \"+STATUS).get();\n\t\tif (!campaignIterator.hasNext()) {\n\t\t\tLogger.log(\"No Campaigns matched with this condition\");\n\t\t\treturn\n\t\t}\n\t\twhile (campaignIterator.hasNext()) {  \n\t\t\tvar campaign=campaignIterator.next(); \n\t\t\tvar campaigntemplate=SuffixTemplate.replace(\/{CampaignName}\/g,campaign.getName().replace(\/\\s\/g,'%20'))\n\t\t\tcampaign.urls().setFinalUrlSuffix(campaigntemplate);\n\t\t\tLogger.log(campaign.getName()+\" => \"+campaigntemplate)  \n\t\t}\n\t}\n\tif (SuffixTemplate.search(\"{AdGroupName}\")>0) {\n\t\tvar adgroupIterator = {hasNext:function(){return false}}  \n\t\tif (_ADGROUP_CONTAINS==\"\"&&_CAMPAIGN_CONTAINS==\"\") {\n\t\t\tadgroupIterator=AdsApp.adGroups().withCondition(\"Status = \"+STATUS).get();\n\t\t} else if (_ADGROUP_CONTAINS==\"\"&&_CAMPAIGN_CONTAINS!==\"\") {\n\t\t\tadgroupIterator=AdsApp.adGroups().withCondition(\"CampaignName contains '\"+_CAMPAIGN_CONTAINS+\"'\").withCondition(\"Status = \"+STATUS).get();\n\t\t} else if (_ADGROUP_CONTAINS!==\"\"&&_CAMPAIGN_CONTAINS!==\"\") {\n\t\t\tadgroupIterator=AdsApp.adGroups().withCondition(\"CampaignName contains '\"+_CAMPAIGN_CONTAINS+\"'\").withCondition(\"Name contains '\"+_ADGROUP_CONTAINS+\"'\").withCondition(\"Status = \"+STATUS).get();\n\t\t} else if (_ADGROUP_CONTAINS!==\"\"&&_CAMPAIGN_CONTAINS==\"\") {\n\t\t\tadgroupIterator=AdsApp.adGroups().withCondition(\"Name contains '\"+_ADGROUP_CONTAINS+\"'\").withCondition(\"Status = \"+STATUS).get();\n\t\t}\n\n\t\tif (!adgroupIterator.hasNext()) {\n\t\t\tLogger.log(\"No Campaigns\/Adgroups matched with this condition\");return\n\t\t}\n\t\twhile (adgroupIterator.hasNext()) {   \n\t\t    var adgroup=adgroupIterator.next();\n\t\t\tvar adgrouptemplate=SuffixTemplate.replace(\/{AdGroupName}\/g,adgroup.getName().replace(\/\\s\/g,'%20'))\n\t\t\tif (SuffixTemplate.search(\"{CampaignName}\")>0) {\n\t\t\t\tadgrouptemplate=adgrouptemplate.replace(\/{CampaignName}\/g,adgroup.getCampaign().getName().replace(\/\\s\/g,'%20'))\n\t\t\t}\n\t\t\tadgroup.urls().setFinalUrlSuffix(adgrouptemplate);\n\t\t\tLogger.log(adgroup.getCampaign().getName()+\" => \"+adgroup.getName()+\" => \"+adgrouptemplate)\n  \t\t}\n\t}\n}\n<\/code><\/pre>\n\n\n\n

    What it does<\/h2>\n\n\n\n

    This script adds two dynamic UTM’s to your tracking template:
    CampaignName and AdGroupName<\/p>\n\n\n\n

    TrackingTemplate<\/h3>\n\n\n\n

    You can edit this variable to add\/remove data that will appear in the destination URL the users are sent to.<\/p>\n\n\n\n

    CAMPAIGN_CONTAINS<\/h3>\n\n\n\n

    This variable can be used to filter in only specific campaigns by their name. For example, if all your brand campaigns have a standard naming convention, e.g. Brand_Jaspers_UK_Exact, you can simply adjust the variable to _CAMPAIGN_CONTAINS = “Brand”<\/em>.<\/p>\n\n\n\n

    To apply the script to all campaigns in the account, simply leave this variable empty.<\/p>\n\n\n\n

    ADGROUP_CONTAINS<\/h3>\n\n\n\n

    Similarly, this variable can be used to filter in only specific ad groups by their name. For example, if all your exact match ad groups have a standard naming convention, e.g. Brand_Jaspers_UK_Exact, you can simply adjust the variable to _ADGROUP_CONTAINS<\/em> = “Exact”<\/em>.<\/p>\n\n\n\n

    To apply the script to all ad groups in the account, simply leave this variable empty.<\/p>\n\n\n\n

    Important<\/strong>
    Make sure you remove all existing tracking templates from your account (on all levels). This script will only run on campaigns that are active.<\/p><\/blockquote>\n\n\n\n

    Installing the script<\/h2>\n\n\n\n

    After making the relevant adjustments to the script, you can go ahead and install it on your account.<\/p>\n\n\n

    \n
    \"\"<\/figure><\/div>\n\n\n

    Navigate to Tools -> Bulk Actions -> Scripts. Create a new script by clicking the blue plus icon.<\/p>\n\n\n\n

    Give the script a memorable name, e.g. Auto Campaign and Ad Group UTM’s<\/em>.<\/p>\n\n\n

    \n
    \"\"<\/figure><\/div>\n\n\n

    Paste in the script’s code and hit Save.<\/p>\n\n\n\n

    Google Ads will then ask for your permission to add the script (there’s no fishy code here, pinky swear!). There’s a minor chance it will show an error after confirming the installation, that’s just a bug.<\/p>\n\n\n\n

    You can then click Run and then Preview.<\/p>\n\n\n

    \n
    \"\"<\/figure><\/div>\n\n\n

    The code will now run, and its outputs will show in the bottom window. This effectively adds the tracking template to the campaigns and ad groups set in the script (if no filters were set, then to all the account).<\/p>\n\n\n\n

    Back on the main Scripts page, you can set the frequency at which the script will run (as you want new campaigns\/ad groups to use it as well).<\/p>\n\n\n

    \n
    \"\"<\/figure><\/div>\n\n\n

    The frequency can be set as you see fit. If this account has many changes happening daily, you can set it to update on an hourly basis. This will update both new and existing campaigns (if their name changes).<\/p>\n\n\n\n

    Final thoughts<\/h2>\n\n\n\n

    This script and post were both contributed by the awesome team at Terrific Digital<\/a>, which are sailing the high seas of digital marketing with a holistic approach and a bottle of rum.<\/p>\n","protected":false},"excerpt":{"rendered":"

    When working with Google Ads, most advertisers usually default to using the Auto-tagging feature which automatically appends the gclid parameter to the destination URL (e.g. domain.com\/landing-page?gclid=12345abcde). This feature enables the tracking mechanisms used by Google Ads and Google Analytics that can match a specific visitor and their consecutive actions with the ad clicked. In layman’s […]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4971"}],"collection":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/comments?post=4971"}],"version-history":[{"count":4,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4971\/revisions"}],"predecessor-version":[{"id":5282,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4971\/revisions\/5282"}],"wp:attachment":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/media?parent=4971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/categories?post=4971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/tags?post=4971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}