Campaign Tracking With A Dynamic Source

June 11, 2014
By Jim Gianoglio

Where-do-you-come-from

Do you want to track your press releases or distributed content (widgets, infographics, embedded content, etc.)? I’m going to show you a much better way to do that with campaign tracking in Google Analytics.

I was recently asked a question by an attendee to our Google Analytics training in Los Angeles about using campaign tracking in Google Analytics:

We distribute press releases that get distributed and posted on various websites.  I want to be able to track any traffic generated by those pickups as part of a campaign, but also know from which sites the traffic is coming.  What happens is I simply leave utm_source out?

Coincidentally, less than 30 minutes after that email, another attendee emailed with the exact same question, worded slightly differently:

The question I have is can I track the referral path if the source and medium is not set as “referral”? I have distributed a link with same source and medium to all the affiliates and was wondering if I could track the traffic to see which affiliate is directing more traffic to a certain page.

I also had a client ask about this exact same thing less than a month ago, so I figured it would make for a great blog post.

The Problem

Wouldn’t it be great if we could just leave off the utm_source and GA would just dynamically fill that in with the referring site? Unfortunately, it doesn’t behave that way. If you don’t include utm_source, GA ignores all other utm parameters.

The Solution

The good news is that we can work around this limitation. I’ll show you two ways to do this, the first way if you’re using Google Tag Manager, and the second way if you’re not. The basic idea is the same either way, the only thing that changes is the implementation.

The examples in both solutions assume that you are using Universal Analytics. If you’re still using Classic GA, follow these instructions to upgrade.

3 steps to dynamic source attribution

Here are the three basic steps you need to follow to “hard code” your medium and campaign, but have the source dynamically filled in:

1. Tag your link with your utm campaign parameters. For the source, use utm_source=dynamic.

2. When a user clicks on the link and arrives on your site, capture the value of the utm campaign parameters.

3. When the value of utm_source is “dynamic” replace it with the URL of the referring site.

Below are the full instructions for each of these three steps if you’re using Google Tag Manager, followed by instructions if you’re not using GTM.

Google Tag Manager

Step 1 – Tag your links

Tag your link with your utm campaign parameters. For the source, use utm_source=dynamic. For example, a link to your site within a press release would go to:

http://www.yoursite.com/page.html?utm_source=dynamic&utm_medium=press-release&utm_campaign=new-product

Step 2 – Capture the campaign parameters

When the user arrives on your site, the URL will include your campaign parameters. We need to capture the value of those parameters. This is easy to do in GTM with a simple macro:

Google Tag Manager macro to capture utm_source

Following the example pictured above, create macros for utm_source, utm_medium, utm_campaign and utm_content.

Step 3 – Replace the source

Create a macro to change the source from “dynamic” to whatever the referring site is. This is simple to do with a Lookup Table macro, like below:

source macro2

This macro looks at the value of your utm_source macro, and if that value equals dynamic, it sets the value of the source macro to the referring URL. Otherwise, it defaults to just using whatever value was in the utm_source macro to begin with. This is necessary so you don’t accidentally overwrite any campaigns where the source is not meant to be dynamic.

Now that you have macros that capture the values of the campaign parameters, plus your Lookup Table macro to do a switcharoo for your dynamic source, you can plug them in to your Google Analytics pageview tag. To do this, go into the More Settings > Fields to Set of your pageview tag. Add campaignSource, campaignMedium, campaignName and campaignContent as field names, with the macros you just created as the values.

source settings

Now save your tag, create a version, publish your container and go party! You’re done. That’s it. No more to see here.

What do you mean you don’t use Google Tag Manager?!

OK, for those of you unlucky souls not taking advantage of an amazingly powerful (and free!) tagging solution like Google Tag Manager, you can still use this workaround. You’ll just have to get your hands a little dirtier. The concepts are the same – you still need to grab the parameter values and use them to set your source information. But instead of doing that through the convenience of GTM, you’ll be placing the code directly on your site.

Step 1 – Tag your links

Tag your link with your utm campaign parameters. For the source, use utm_source=dynamic. For example, a link to your site within a press release would go to:

http://www.yoursite.com/page.html?utm_source=dynamic&utm_medium=press-release&utm_campaign=new-product

Step 2 – Capture the campaign parameters

You’ll need to include the following JavaScript before your Google Analytics code:


Step 3 – Replace the source

Now, in your Google Analytics code, you’ll need to modify it from the standard tracking code to include the traffic source information:

Standard Tracking Snippet:

ga('create', 'UA-XXXXXXX-YY', 'auto');
ga('send', 'pageview');

Modify to include traffic source info:

ga('create', 'UA-XXXXXXX-YY', 'auto');
ga('set', {
	'campaignSource': source,
	'campaignMedium': medium,
	'campaignName': campaign,
	'campaignContent': content
});
ga('send', 'pageview');

tl;dr Now that you can track your press releases and other distributed content with more granularity, will you? How will you use this extra information? Let us know down below!