Getting 'Traffic Source' in universal analytics

12,590 views
Skip to first unread message

Kaushik Sahoo

unread,
Nov 6, 2013, 6:53:16 AM11/6/13
to google-analyti...@googlegroups.com

How to get Traffic source data in universal analytics?

I'm trying the following code in firebug console:-

ga(function(tracker) {
  var src = tracker.get('campaignSource');
  console.log(src);
});

But it always logs 'undefined'.

The field 'campaignMedium' also returns 'undefined'.

But I get correct values for some fields like 'clientId' or 'screenResolution' or 'language'.

Is there some other process to get these infromation?

P.S: I'm using https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#trafficsources for reference of field names.

Frank B.

unread,
Nov 6, 2013, 5:25:30 PM11/6/13
to google-analyti...@googlegroups.com
Hi Kaushik, same question here.

With ga.js I always extracted traffic source/campaign data from the __umtz cookie but no clue how to get this information with analytics.js.

Tried out the get method, too, same results... did take a look at the analytics.js code but as my JavaScript skills are very, very basic I couldn't figure anything out.

Nick Mihailovski

unread,
Nov 6, 2013, 5:43:47 PM11/6/13
to Frank B., google-analyti...@googlegroups.com
Hi,

All the session data is calculated on the server with Universal Analytics.

Currently there is no way to get this in the analytics.js tracking code.
-Nick


--
You received this message because you are subscribed to the Google Groups "google-analytics-analyticsjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-ana...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Frank B.

unread,
Nov 6, 2013, 5:57:26 PM11/6/13
to google-analyti...@googlegroups.com
Hi Nick,

thanks for clearing up. Any chance this will be possible in the near future?

We for example have implemented a url/cookie logic which we use to control when/if we include affiliate tracking codes on our order success pages and in this logic we partially rely on the campaign data, too.

So as long as this "feature" is missing we will not be able to fully switch to analytics.js.

Regards, Frank

Nick Mihailovski

unread,
Nov 6, 2013, 6:01:44 PM11/6/13
to Frank B., google-analyti...@googlegroups.com
Universal Analytics will only be doing campaign server side.

With analytics.js, you can explicitly set the campaign, so somebody could write some JS code to parse referrals / domains / cookies to determine the campaign and send that data to GA.


--

Kaushik Sahoo

unread,
Nov 7, 2013, 12:05:01 AM11/7/13
to google-analyti...@googlegroups.com, Frank B.
Hi Nick,

Thanks for the information.

 JS code to parse referrals / domains / cookies

What do we parse? How do we get these information?

- Kaushik 

To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-analyticsjs+unsub...@googlegroups.com.

Sebastian Breidert

unread,
Nov 7, 2013, 3:53:18 AM11/7/13
to google-analyti...@googlegroups.com, Frank B.

Hi Nick,

I guess we all understand that this is handled server side and are aware that we can set campaign data via URL tagging or using fields in analytics.js code (vs. setting campaignParams with ga.js).

But going with Frank’s example:

If someone lands on a page with campaign tagging, e.g. domain.tld/example.html?utm_source=source&utm_medium=medium we can't extract the previous traffic/campaign data (as it currently is possible with __utmz cookie), can we?

So in order to work with a similar solution as mentioned above we would have to set our "own" traffic/campaign cookie that probably will not be 100% compliant with the data in Analytics (which would be ok for such purposes if set up properly).

Simple example:

  • gclid was in URL --> custom cookie with source=google, medium=cpc
  • campaign tagging in URL --> write campaign data into cookie
  • referrer is google domain without gclid --> custom cookie with source=google, medium=organic
  • referrer is bing without campaign tagging in URL --> source=bing, medium=organic
  • No referrer, no URL tagging, no active session (separate cookie/s) --> source=direct
  • Referring domain is not specified otherwise, no campaign tagging in URL --> source=reffering-domain, medium=referral
Or am I missing something?

Thanks! Sebastian

Nick Mihailovski

unread,
Nov 7, 2013, 12:02:13 PM11/7/13
to Sebastian Breidert, google-analyti...@googlegroups.com, Frank B.
Sebastian,

You are 100% correct.

Per your question, you can not extract the previous traffic / campaign data as you currently can in ga.js

-Nick


--
You received this message because you are subscribed to the Google Groups "google-analytics-analyticsjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-ana...@googlegroups.com.

Amin Shawki

unread,
Jan 9, 2014, 12:06:06 AM1/9/14
to google-analyti...@googlegroups.com, Sebastian Breidert, Frank B.
Hi Nick and Sebastian,

I have a similar issue/question.  What if we used the decorate link method, could we use the _ga cookie to "force" the source information to be read by the page?  

Our goal with one of our clients is to pass original source information all the way through an offsite Facebook log in authentication.  The problem is a page in the log in process is missing universal analytics (facebook authentication page), thus needing campaign parameters manually passed all the way.

For example, here is an issue we are currently facing:

2. User navigates to subdomain.site.com via a "sign in" button in the header
3. User clicks "log in with facebook" button
4. User is sent to facebook.com authentication page to sign on
5. After successfully authenticating/logging on, user is redirected back to subdomain.site.com, but source information in Universal analytics is overwritten to "Social/Facebook" or "Referral/Facebook"

We decided to test adding the decorate link method to the "sign in" button on site.com and pass the _ga cookie through to subdomain.site.com, then again to the facebook log in page, and finally to back to subdomain.site.com, so the new flow is:

2. User navigates to subdomain.site.com via a "sign in" button in the header which has decorate link method associated with click, lands on subdomain.site.com?_ga=1.1234567.1234567
3. User clicks "log in with facebook" button
4. User goes to facebook.com authentication page to sign on
5. After successfully authenticating/logging on, user is redirected back to subdomain.site.com?_ga=1.1234567.1234567, but source information in Universal analytics is still overwritten to "Social/Facebook" or "Referral/Facebook"

To my understanding, I thought Universal Analytics would read this _ga cookie to obtain the original source information set on site.com...?

Some ideas we have I'd like your thoughts on:
1. Set referral exclusion rule to "facebook.com", and ensure any/all campaigns on facebook are manually tagged with utm parameters.  Thus forcing UA to ignore these log-ins as referrals and use the _ga cookie source info instead?
2. Change the _ga cookie expiration setting to "0" to make it session based (this would be through GTM).

Any other ideas what we can do to make this possible?

Thanks in advance!
-Amin
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-analyticsjs+unsub...@googlegroups.com.

Samy

unread,
Jan 9, 2014, 1:37:58 AM1/9/14
to Amin Shawki, google-analyti...@googlegroups.com, Sebastian Breidert, Frank B.
Hi Amin

What I could get here, is as there's a new referrer, it overwrites and starts a new session.

Universal analytics still behaves a bit differently than other veteran analytics packages, even though server side.
Referrals are processed differently than in in previous versions of Google Analytics.
By default, all referrals trigger a new session in UA.

Decorate link method just visitors across domains by overwriting user id.
What you could do is, force the referrer now to be a custom page on the same domain - using
ga('set', 'referrer', 'http://subdomain.site.com/facebooksigninreturn');
This should not trigger a new session or overwrite the previous referrer.

2. User navigates to subdomain.site.com via a "sign in" button in the header
3. User clicks "log in with facebook" button
Save a trigger in a session storage variable (than cookie).
4. User is sent to facebook.com authentication page to sign on
5. After successfully authenticating/logging on, user is redirected back to subdomain.site.com,
Check fro trigger in session storage and respectively set the referrer as the custom page explained above.

Hope this helps.

Thanks
Samrat


To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-ana...@googlegroups.com.

Frank B.

unread,
Jan 9, 2014, 7:21:08 AM1/9/14
to google-analyti...@googlegroups.com, Amin Shawki, Sebastian Breidert, Frank B.
Hi Nick, hi all,

is it or will it be possible in the future to read visitor-level custom dimensions similar to _getVisitorCustomVar() in ga.js?

Thanks, Frank


Samy

unread,
Jan 9, 2014, 8:23:22 AM1/9/14
to Frank B., google-analyti...@googlegroups.com, Amin Shawki, Sebastian Breidert
Hi Frank

ga.js works on the concept of client side processing - everything including session id is saved as cookies on client machine.

For analytics.js, it's done on server side. It's just sent to google in the hit, it's set on the page and server side processing takes care of the rest.

Hence the _getVisitorCustomVar(), wouldn't work anymore as there's no cookie to read from.
However, you could use DOM localStorage for the same functionality.

Thanks
Samrat D'souza


Stuart Williams

unread,
May 23, 2014, 9:48:54 AM5/23/14
to google-analyti...@googlegroups.com, Frank B., Amin Shawki, Sebastian Breidert
Sorry to open this back up but it's very topical for me right now.

We were pulling the campaign information out of the _utmz cookie to add to all our forms in order to attribute them in internal reports. I recently discovered when I switch to UA that this will no longer work.

Is the consensus that we ought to be creating a custom cookie to extract this data from the URL and updating it each time?

Thanks

Amin Shawki

unread,
May 23, 2014, 11:39:13 AM5/23/14
to Stuart Williams, google-analyti...@googlegroups.com, Frank B., Sebastian Breidert
Yes, I think custom cookie or local storage is the best general approach right now.  It makes debugging with GA debugger/tag assistant tools tricky but you can always check out the cookies in developer tools as you navigate through the site to make sure they are still there.

-Amin
--
Amin Shawki
Digital Marketing and Analytics Manager
Google Analytics Certified Individual
Google Adwords Certified Individual
  



Michael Freeman

unread,
Jun 17, 2014, 1:49:53 AM6/17/14
to google-analyti...@googlegroups.com
Nick,
How will GA support developers who want to better align the data in GA with their CRM systems? By pulling out the "cookie method" and not providing an API to query that data, it is not helping with the idea of developing closed loop reporting.

I suggest that come sort of method be provided in the API where we could query the current campaign data from the server so that it may be passed along with leads/shopping carts, etc... so we can see what campaigns are really driving sales (this is the only way in a non ecommerce environment).

Thanks,
Michael

Martin Sarsale

unread,
Jul 10, 2014, 4:58:47 PM7/10/14
to google-analyti...@googlegroups.com, frank.b...@googlemail.com
I found the following workaround:

Just link the old/traditional GA script and configure it to work on 'local mode' (no logging calls are made to google servers).

Cons:
  • you're loading 15kb of JS just for parsing a URL and setting a few cookies

I guess someone with *lots* of patience could de-minify the ga.js code and leave only the URL parsing and strip all the rest of the code.
I tried to find an unminimized version of ga.js with no luck....



On Wednesday, November 6, 2013 7:43:47 PM UTC-3, nm wrote:
Hi,

All the session data is calculated on the server with Universal Analytics.

Currently there is no way to get this in the analytics.js tracking code.
-Nick
On Wed, Nov 6, 2013 at 2:25 PM, Frank B. <frank.b...@googlemail.com> wrote:
Hi Kaushik, same question here.

With ga.js I always extracted traffic source/campaign data from the __umtz cookie but no clue how to get this information with analytics.js.

Tried out the get method, too, same results... did take a look at the analytics.js code but as my JavaScript skills are very, very basic I couldn't figure anything out.

--
You received this message because you are subscribed to the Google Groups "google-analytics-analyticsjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-analyticsjs+unsub...@googlegroups.com.

Goldy Das

unread,
Jul 11, 2014, 4:01:25 PM7/11/14
to Stuart Williams, google-analyti...@googlegroups.com, Frank B., Amin Shawki, Sebastian Breidert
 
Hi All,

 I am targeting 2 keywords for one url, these two keywords are automatic invisible from google search ranking result until & unless I did fetch as Google in Google webmaster tool,  these two keywords became visible in search result only when I manually index it in google webmaster tool, after sometime of fetching as google, in google webmaster tool. what can be the reason behind this, can you explain me. I tried a lot, only a hope from you all.

Miguel Martín Goula

unread,
Aug 1, 2014, 7:55:28 AM8/1/14
to google-analyti...@googlegroups.com
I've just pubished a small library to help on this. It gets the campaign parameters and store them in a base64 cookie. It's not a final solution but it does the workaround.

Sheji Ho

unread,
Sep 3, 2014, 4:42:32 AM9/3/14
to google-analyti...@googlegroups.com, frank.b...@googlemail.com
Hi Martin, do you mind sharing how you implemented this with the old GA script and configured for local mode?  I'm having a hard time wrapping my head around this...

Martin Sarsale

unread,
Sep 3, 2014, 9:05:14 AM9/3/14
to Sheji Ho, google-analyti...@googlegroups.com, frank.b...@googlemail.com
```html
<script>    
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', "UA-33973-1"]);
                    _gaq.push(['_setDomainName', "x.com.ar"]);
                                     _gaq.push(['_setAllowAnchor', true]);
    _gaq.push(['_setLocalGifPath', "http://s1.static.x.com.ar/images/ga_track.gif"]);
    _gaq.push(['_setLocalServerMode']);

    _gaq.push(['_trackPageview']);

    (function() {var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                   ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
                   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);}
                )();
</script>
```


--
You received this message because you are subscribed to a topic in the Google Groups "google-analytics-analyticsjs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-analytics-analyticsjs/NtwQFQAZ2Oo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-analytics-ana...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Martin Sarsale
http://www.properati.com
Reply all
Reply to author
Forward
0 new messages