Upcoming LunaMetrics Seminars
Seattle, Nov 3-7 New York City, Nov 17-21 Washington DC, Dec 1-5 Los Angeles - Anaheim, Dec 8-12

Campaign Tracking with a Dynamic Source

Where-do-you-come-fromDo 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:


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:


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:

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!

Jim Gianoglio

About Jim Gianoglio

Jim Gianoglio is our Digital Analytics Engineer. He works with implementation, analysis of Google Analytics, and spearheads the LunaMetrics Google Analytics seminars across the country. Want to see him in action? He'll be heading our Google Analytics training in Los Angeles. Before succumbing to the siren song of analytics, he led the SEO campaigns of Fortune 500 companies in the insurance, retail and CPG industries. Things you didn’t know about Jim: he runs marathons, photographs weddings and has done voiceovers for TV commercials.


6 Responses to “Campaign Tracking with a Dynamic Source”

Dan Wilkerson says:

Hey Jim,

Great stuff! Really clever way to use GTM to enhance campaign parameters (and you know how I feel about campaign parameters).

For added uniformity, you can dynamically create a new < a > element and set it’s href equal to the referrer; then you can use URL APIs to just pull the hostname and path for the corresponding utm_source & utm_content parameters, e.g.:

var a = document.createElement(‘a’);
a.href = document.referrer;
source = a.hostname;
content = a.pathname;

Since this only will happen when there are campaign parameters in the URL, it should be no issue.

If you really want to get fancy, you can add the following to strip out a ‘www.’ from the beginning:

source = a.hostname.replace(/^www\./, ”);

One other suggestion; I’m sure some sites will push users from https:, and if the receiving site is insecure, the referrer will be ”. That would cause the utm_source to be set to ”, and I’m not sure what would happen next. Maybe do ‘utm_source=dynamic-press-release’ and populate ‘press-release’ if the referrer is blank, or add utm_default=press-release for the same purposes?

Really smart idea! Definitely going to implement on our end :) this will solve a lot of attribution headaches.


Damion says:

Neat tricks, especially the GTM solution!

It might be worth mentioning utm.io — it’s a URL shortener that automatically switches the utm_source.

@Damion – That’s an interesting service (utm.io). I hadn’t seen it before, thanks for sharing!

@Dan –

I like the way you think. Good catch on the secure site missing the document.referrer. I’ll update the code to handle that case.

Jeffrey Cody says:

What happens if there are no URL parameters. Will the NULL values in these areas

ga(‘set’, {
‘campaignSource': source,
‘campaignMedium': medium,
‘campaignName': campaign,
‘campaignContent': content

zero out an organic or referred (or other) value?

I have an issue with event : sending an event tag
seems to break the tracked visit, creating a new
referral visit.

Should I also populate those campaign “fields to set” in GA event tags sent by GTM ?

Leave a Reply