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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…