How to Use Google Analytics to Track a User’s Original Traffic Source

A long-term bane for web marketers and analysts is that Google Analytics defaults all reports to last click attribution modeling. That is, for multisession users, the most recent traffic source gets credit for current interaction and goal completions. For example, I may find out about a website via a paid traffic source by clicking on an ad. However, I am not yet ready to convert. At a later time, I change my mind, and return to the website via an organic search and subsequently generate a conversion. Under this scenario, the organic search source receives credit for the conversion in Google Analytics because it is the most recent driver.

Google has provided some relief for this in recent years by incorporating its model comparison tool, which allows for “first click” attribution modeling. I am pleased to say that the accuracy of this tool is ever increasing (if still imperfect) to the point of reliable usability. But the model comparison tool applies solely to conversions and typically uses 30- to 90-day windows. What if you wanted to follow all user interaction over an indefinite period of time by their first traffic source?

One answer to this lies in the previous Google Analytics tracking script. What’s old is new again when “Classic” Google Analytics can provide us with a solution hack.

The previous iteration of Google Analytics tracking code is commonly called Classic (or Asynchronous tracking code). The current version, Universal Analytics, is superior in every way, except one—and this applies to developers. Universal Analytics stores user cookie information on the server side, which means it cannot be accessed directly by JavaScript. Classic Analytics stored the cookie client side. The advantage of client side storage is that scripts can read the cookie information, including source & medium.

    Here is the process for implementation:

  1. 1. Set up a dummy tracking script to log source/medium.

    analytics tracking

    We need to set up a new analytics tracking script for Classic Analytics. This will run concurrent with your existing Universal Analytics/Tag Manager tracking script. Because they operate on different libraries, you do not have to worry about the scripts conflicting. Important: Set your UA code to a dummy script, preferably your existing analytics property ID with false extension. For example, if you run your website on UA-1XXXXXXX-1, set this tracking script for UA-1XXXXXXX-25.

    The key here is that we do not want to actually track anything with the dummy Classic Analytics script; we only want to generate the Google cookie associated with it. This is critical, as using your actual Google Analytics code will lead to double counting.

  2. 2. Configure cookie.

    analytics tracking

    This is the most code-intensive step. Here, we will read values from the Google Analytics cookie, store those values in our own cookie, and/or ignore everything if that cookie already exists.

    Step 1: Get source & medium from the Google Analytics cookie. You can use the attached file/script:

    //Attach file get-source-med.txt

    Step 2: Check if our own cookie already exists. If the cookie already exists, do nothing. If it does not, create it with the source & medium values.

  3. 3. Configure Google Analytics.

    analytics tracking

    Within your Google Analytics admin, create a custom dimension for original source & medium. As this will stay with the user throughout their entire existence, these will be user-level dimensions.

  4. 4. Assign Cookie Value to GA Dimension.

    Use a universal analytics call to assign the key value pair present in the cookie to the custom dimension we created in Step 3. In this case, replace UA-1XXXXXXXX-1 with your actual Google Analytics code.

    <script>

    (function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){

    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)

    })(window,document,’script’,’https://www.google-analytics.com/analytics.js’,’ga’);

    ga(‘create’, ‘UA-1XXXXXXX-1’, ‘auto’);

    ga(‘set’,’dimension1′,cookie_val); //set to whatever dimension your selected in Step 3

    ga(‘send’, ‘event’, “cookie”, “set”,{‘nonInteraction’: 1});

    </script>

    The end result is that we can now best measure return user traffic by their original traffic source to better gauge the lifetime value/engagement of marketing efforts.

Multiple roads lead to Rome, and there are other means to accomplish this—e.g., log the document.referrer in a first-party cookie on the user’s first visit. But the above offers the advantage of using the same syntax and naming methodologies as you would see in your standard Google Analytics reports. In the end, you can now use the First Traffic Source as a secondary dimension, in custom reports, or as a primary dimension in custom dashboards/data studio. Take your users back to the beginning.

Note: This process assumes that you are running your website on Universal Analytics code. Do NOT run this if you are currently on Classic Analytics Tracking code. In that case, this would cause issues with your existing tracking. Under that scenario, please upgrade to Universal Analytics and then come back to this post. You really should; it’s nearly 2019.

Keep reading in Analytics