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:

Standard Tracking Snippet:

Modify to include traffic source info:

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 is a Manager for the Analytics & Insight department. He works with implementation, analysis and training of Google Analytics and Google Tag Manager. Before focusing on 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’s biked from Pittsburgh to Washington DC in 41 hours, roasts coffee beans and has done voiceovers for TV commercials.

  • Dan Wilkerson

    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

    Neat tricks, especially the GTM solution!

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

  • @Damion – That’s an interesting service ( 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.

  • 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 ?

  • udi

    This method only works 75% of the time for me..

    i am seeing 25% source = not set. any insight?

  • @udi –

    If the referrer is blank (which happens if a user goes from https to http) then the {{referrer}} macro has a value of ” (i.e. an empty string). That may be causing (not set) to show up for source.

    If you have a medium and campaign, but the source is (not set), this is likely the cause. If you have (not set) for source, medium, and campaign, then it may be something else at play.

  • Hi, Jim –
    Is it possible to do this using a view in Google Analytics?
    for example, can you create an advanced filter that scans the URL for “utm_source=dynamic” and replaces it with “utm_source={{referrer}}” or something like that?

    If this is possible, then it would avoid rewriting the URL for all your views, and would rewrite it just for the view in question.


  • Owais

    Were Can I see the report in GA. Also how can I access value field data from Fields to set as I have use the following method but data is not visible in GA

  • Alexis Favis

    I know this is an older post, but I’m wondering if these macros are still applicable. Additionally, would a macro like this work if the URL was put through a URL shortener? Could this potentially pick up Instagram referral traffic that would normally come through as (direct/none)? Thanks in advance for a response, if this post is still be monitored.

    • Jim Gianoglio

      Hi Alexis –

      For tracking traffic from Instagram, you don’t need to use the method in this post. You simply need to include campaign parameters in the link you share on Instagram (or the link that gets shortened). Since you are dealing only with Instagram, your source will not be changing, so forget about the whole dynamic source stuff.

      For example, if the link you’re sharing is:

      You would just need to add the standard campaign parameters:

      Even if you use a URL shortener on the above link, that doesn’t affect anything. As long as the user gets to the final URL on your site with the UTM campaign parameters in tact.

      • Alexis Favis

        Thanks for the reply Jim! Getting more familiar with GTM everyday, so excited to try it out.

    • Jim Gianoglio

      Even though the post is older and uses V1 of GTM, it still works like a charm. The only difference is that instead of creating “macros” you’ll need to create “variables” in GTM. Simple name change, that’s all.

      As far as using link shorteners, that doesn’t change anything. As long as the link you’re shortening includes the campaign parameters (utm_source, utm_medium, etc.) GA will see those and grab those values.

      If you’re talking about referral traffic from Instagram in general (not links that you share, but links that other people share) then this will not help you. Campaign parameters only help you if you are the one sharing the links.

  • Dimitris Skountis

    I came across this post and although everything looks and works just fine, and on the GTM Debugger I am getting for the source variable the referrer as I should, on Google analytics I am still getting the ‘dynamic’ value as the source. Thanks in advance for the response, great article btw

    • Jim Gianoglio

      I’m a little confused – if you’re getting the referrer as the value of the {{source}} variable, that IS the dynamic value. Can you give an example? What comes through for the value of the {{source}} variable and what shows up in GA for the Source dimension?

      • Dimitris Skountis

        Hi Jim, it seems that it didn’t work in debugging mode. I published it and it works just fine. I have another question though: Is ot possible to bring instead of the full referrer, just the main source name, e.g I mean instead of bringing in GA, to bring just Thanks in advance for your response!

        • Jim Gianoglio

          Great – I’m glad you got it to work!

          To get just the referrer hostname without the page path, you’ll just need to create a new user-defined variable in GTM. You can name it something like {{Referrer Hostname}}.

          Choose “URL” as the type. Under the Configure Variable settings, select “Host Name” as the Component Type. Click on “More Settings” and for the URL Source, select {{Referrer}}.

          Now you have a variable that records just the hostname portion of the referrer.

          Hope that helps!

  • Darya Tokareva

    Hi Jim,

    Thank you very much for this great post! It helped me alot, but I think I’m having a double-counting problem on Google Analytics.

    I used Google Tag Manager option to set up the tracking. Right now when I follow the link with UTM parameters, Google Analytics seems to count this interaction as 2 visits – 1st with ‘dynamic’ value as the source and 2nd (the right one) with ‘referrer hostname’.

    Would you give me some advice on how to fix this problem?

    Thank you

    • Jim Gianoglio

      Hi Darya,

      Do you have multiple Google Analytics Pageview tags set up in GTM? Or is it possible that you have Google Analytics hard-coded on the site?

      If you can share a link to the site, I can take a look.


Contact Us.

Follow Us



We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187