Get the referrer, paid/natural and keywords for the current visitor with Google Analytics

12,781

Solution 1

If you're still using ga.js (the legacy version of Google Analytics tracking code), you can use the below code to generate the values you want within the browser, by reading browser cookies. (Most people will have migrated to analytics.js, which does not store the campaign information in the __utmz cookie.)

I assume you have a function called readCookie(); I tend to use the one from QuirksMode

For referral, medium, and campaign information:

var utmz = readCookie('__utmz'); //using a cookie reading function
var vals = (function() {
        var pairs = utmz.split('.').slice(4).join('.').split('|');
        var ga = {};
        for (var i = 0; i < pairs.length; i++) {
            var temp = pairs[i].split('=');
                ga[temp[0]] = temp[1];
        }
        return ga;
    })();

//vals.utmcmd: medium (organic, referral, direct, etc)
//vals.utmcsr: source (google, facebook.com, etc)
//vals.utmcct: content (index.html, etc)
//vals.utmccn: campaign 
//vals.utmctr: term (search term)
//vals.utmgclid: adwords-only (value is irrelevant, but means its AdWords autotagged traffic, but it implies that medium=cpc, even though it'll be set to `(none)` or `(not%20set)`

For pageview count and visit count:

var pageviews = readCookie('__utmz').split('.')[1];
var visits = readCookie('__utma').split('.').pop() //returns number of visits

Obviously, if (+visits)===1, then its a first time visitor. (Remember: values from cookies will be strings, so you'll need to cast them to numbers to safely do numeric comparisons, even though JS is loosely typed.

Solution 2

You should be able to get it all from the cookies set by Google Analytics. They are stored as first party cookies so JavaScript running on a page will be able to read them. The number of visits can be obtained from the last part of the __utma cookie and the referrer can be taken from __utmz. The source is the utmcsr bit of __utmz while the medium comes from utmcmd and the keyword is utmctr.

Solution 3

Apparently, this doesn't work anymore. Since 2013 where use enabled SSL, all keywords are stripped out from referer URL.

From now on the only option to get some statistic (not per user) data is to enable Search Console.

Share:
12,781
Evgenii
Author by

Evgenii

I was born in Russia and have been working as a programmer since 1998. In 2012 my wife and I immigrated to Australia where I was working as a web and iOS developer. Currently I am doing a career change by studying astronomy in Monash University. I want to learn how the Universe works.

Updated on June 05, 2022

Comments

  • Evgenii
    Evgenii almost 2 years

    Is it possible to get the following information about the current visitor using Google Analytics API with JavaScript?

    • Referrer site ('Source' in GA)
    • Paid or natural ('Medium' in GA)
    • Keyword
    • First time/returning
    • Number of visits

    If it's not possible with Google Analytics API is there any other easy way to do it (apart from parsing HTTP Referer, storing the visits statistics in DB etc.)?

  • Evgenii
    Evgenii about 13 years
    WOW! That's pure magic. I did not know about that. Thank you so much.
  • jrosell
    jrosell over 12 years
    I wonder if it is possible to get pages visited for this user.
  • Yahel
    Yahel about 10 years
    @ByronSingh No, UA doesn't store campaign information in the cookie. But there's a whole API process to extract campaign information from a Universal Analytics cookie. But that's a separate question.
  • dubesor
    dubesor almost 9 years
    @Yahel would love to hear more about that - are there any docs on that process somewhere? I'm looking to extract new vs returning info, or source/campaign info.
  • SSH This
    SSH This almost 7 years
    Thanks for this info, is there any alternatives?
  • Dawid Adach
    Dawid Adach over 6 years
    As per today - not really. Google is hashing link, therefore, referrer won't give you valuable information.
  • Yahel
    Yahel about 6 years
    @DipuR yes, this only applies to ga.js, not analytics.js. I've added a note to reflect that.
  • Dipu R
    Dipu R about 6 years
    @Yahel , sorry I overlooked