Dynamically Capture Referral Traffic With Google Tag Manager
Image courtesy of David Orban

09/04/2020 Update: I have been meaning to update this post for, like, ever because Google Tag Manager updates have broken this method of dynamically capturing source traffic data.

Thankfully, I no longer have to update this post because Shelbe Spurlock of OM Marketing has done it for me. Thank you, Shelbe! Get the details in her post or watch her video tutorial:

This is an update to Jim Gianoglio’s LunaMetrics post about Campaign Tracking with a Dynamic Source. Mr. Gianoglio’s article explained how to use UTM URL tagging coupled with a clever implementation of Google Tag Manager (GTM) to dynamically capture source URLs in Google Analytics for mass campaigns.

The example he cited as a use case is tracking links from a press release that has been distributed through PR Newswire, for instance. Hundreds of sites could’ve published the release, so this would be a way to efficiently identify referrals from those sites in Analytics.

The same principle can apply to banner campaigns that have been placed through a network, where you don’t necessarily know all the sites on which the ad will run.

Google has updated Tag Manager since Mr. Gianolgio’s article was published and, as Google often does, it has changed its terminology, making his article a little difficult to translate for those who are relatively new to GTM.

I will essentially update Jim’s excellent article below to reflect the current state of Google Tag Manager. This guide assumes you have both Google’s Universal Analytics and Tag Manager installed and a fundamental understanding of how each works.

The Problem With URL-Tagging & Mass Referral Tracking

This image shows my template for the e-Strategy Content Marketing Blog that I apply to URLs for sharing on social channels via Hootsuite.

Screenshot: UTM Parameters Using Hootsuite

This tagging configuration helps me recognize traffic to this blog that was a result of my own social sharing efforts. Based on these tags, the URL to this post looks like this:

“https://e-strategyblog.com/dynamically-capture-source-referrals-using-google-tag-manager?utm_source=Hootsuite&utm_medium=Social%20Media&utm_campaign=eStrategy%20Blog&
utm_content=How%20To%20Dynamically%20Capture%20Source%20Referrals%20Using%20Google%20Tag%20Manager”

The tag that we will make dynamic is highlighted in red.

As of now, all I know is that the traffic that comes to my blog with a utm_source parameter value of Hootsuite is traffic that originated from my social sharing efforts.

But what if someone copied that very same tagged link and used it to link from their blog or included it as a link in a newsletter? It would be much harder for me to figure out where that traffic came from.

As a public relations professional, when my agency places a press release with tagged URLs on a PR Newswire or Cision distribution service, a source parameter is likely to be hardcoded as PR Newswire or Cision.

It would be much better for that source parameter to be dynamically capable of capturing the source URL of the traffic.

How To Tag URLs To Dynamically Capture Source Referrals

To do this, we are going to change our utm_source parameter to dynamic. Thus, my Hootsuite tagging template would look like this:

Screenshot: UTM Parameters Using Hootsuite With Dynamic Source

As you can see, all the tags for the URL to this page remain the same except utm_source, which changes to dynamic:

“https://e-strategyblog.com/dynamically-capture-source-referrals-using-google-tag-manager?utm_source=dynamic&utm_medium=Social%20Media&utm_campaign=eStrategy%20Blog&
utm_content=How%20To%20Dynamically%20Capture%20Source%20Referrals%20Using%20Google%20Tag%20Manager”

How To Configure Google Tag Manager To Dynamically Capture The utm_source Value

Now that we have our tagging protocol in place, we will take advantage of some of the unique capabilities of Google Tag Manager to replace the word dynamic in our tagged URL with the referring site.

1) Create Variables For UTM Parameters

Since the visitors to your site will be arriving via a URL that has UTM parameters, we can use Google Tag Manager to sniff out the values of those parameters that are in the URL string. We will need to create a GTM variable for each UTM Parameter, four in all:

  1. utm_source,
  2. utm_medium,
  3. utm_campaign, and
  4. utm_content.

To do this, go to the Variables section of your GTM account and create a new variable titled utm_source.

Choose URL as your Variable Type, Query as your Component Type, and type in utm_source as your Query Key. The configuration should look like this.

Screenshot: GTM utm_source Variable Configuration

Save this variable and then follow the same steps to create variables for the three remaining UTM parameters. When you are done, you should have four variables titled:

  1. utm_source,
  2. utm_medium,
  3. utm_campaign, and
  4. utm_content.

Here’s what it will look like in the Variables section of your GTM account:

Screenshot: GTM Variables For UTM Parameters

2) Replace Dynamic UTM Source Parameter With Referring URL

Next, we need to create a Variable that will change the value of utm_source from dynamic to whatever happens to be the referring URL. We will do this using a Lookup Table, which sniffs for a utm_source value of dynamic and then replaces that with whatever the referring URL is on the fly.

To do this, go to the Variables section of Google Tag Manager and create a new variable titled source. Choose a Variable Type of Lookup Table (under the Utilities section) and for Input Value, choose the utm_source variable you created in the previous step.

Under the Lookup Table section, click the Add Row button and type in dynamic into the Input field and choose Referrer using the LEGO-like brick icon for the Output field.

Finally, check the Set Default Value box and choose utm_source using the brick icon. This keeps you from accidentally overwriting campaigns where your source is not set as dynamic.

Here’s what the configuration for your source Lookup Table variable will look like in Google Tag Manager:

Screenshot: GTM Lookup Table Variable For Source UTM

3) Configure Pageviews Tag To Sniff For UTM Parameters

Now that you’ve created variables for all the UTM parameters and you’ve create a Lookup Table to replace the source value with the referring site’s URL, you’ll need to configure your Pageviews tag to look for those parameters.

To do this, go to the Tags section of your GTM account and edit your Pageviews tag. Click on More Settings and then Fields to Set. From here, click on the Add Field button:

Screenshot: GTM Pageviews Tag Fields To Set

In the first Field Name, type campaignSource and for its Value, choose the source Lookup Table Variable you created earlier.

Add another field and type in campaignMedium for the Field Name and choose the utm_medium Variable you created.

Add another field and type in campaignName for the Field Name and choose the utm_campaign Variable you created.

Add another field and type in campaignContent for the Field Name and choose the utm_content Variable you created.

Here’s what the Fields to Set section of your Pageviews tag should look like when you’re done:

Screenshot: GTM Pageviews Tag Fields To Set Dynamic Source Tracking

Save your Pageviews tag, preview and test.

Test With Dynamically Tagged URLs

After you’ve saved your work, switch to Preview mode in Google Tag Manager and test. You can do this by tagging a URL to a page or post on the site for which you are configuring GTM using the utm_source value of dynamic, as we discussed earlier.

Then you’ll need access to another domain on which to place a link to your tagged URL. Click on that link and then examine your Pageviews tag in GTM Preview mode.

In this example, I’ve created the following tagged link to the Tiny Foods Video Trend post on this blog, which looks like this:

“https://e-strategyblog.com/2017/01/tiny-food-video-trend/?utm_source=dynamic&utm_medium=Social%20Media&utm_campaign=eStrategy%20Blog&
utm_content=Tiny%20Food%20Video%20Trend”

I placed a link to that post on a different domain I own, Videolicious.tv, so I can test whether my configuration is working and actually capturing referring URLs. Using the GTM Preview mode, I can see that my Pageviews tag is indeed firing:

Screenshot: GTM Pageviews Tag Firing

And when I click on the Pageviews tag to examine its properties, I can see that in the Fields to Set, the campaignSource includes the actual referring URL from Videolicious.tv, not the word dynamic:

Screenshot: GTM Pageviews Tag Properties

Having verified that your dynamic source tracking configuration works, you can publish it live and wait for the referral data to come in to Google Analytics.

Finding Referral Source Data In Google Analytics

You’ll find the referral source data from your dynamic UTM source campaigns in Google Analytics under All Campaigns within the Acquisition reports section.

Choose Source as the Primary Dimension, and you’ll get the referral URLs. This screenshot shows the example I used above for the Tiny Foods Video Trend post traffic from a link from Videolicious.tv with a dynamically-tagged URL:

Screenshot: GTM Dynamic UTM Source Tracking Google Analytics Reports

And this is what it looks like when your tag is not working. In row 2, you’ll see the word dynamic, which indicates that the Google Tag Manager configuration was not correct because it was not replacing dynamic with the referral URL.

Rows 5 and 6 are examples of how it looked after I’d implemented the tag properly.

Screenshot: GTM Dynamic Source Tracking Google Analytics Wrong

So there it is. Now you should be able to implement dynamic UTM source tracking and get better and richer data as a result.

Good luck!