Featured post
javascript - Convert Google Analytics cookies to Local/Session Storage -
update http://jsfiddle.net/musicisair/rsktp/embedded/result/
google analytics sets 4 cookies sent requests domain (and ofset subdomains). can tell no server uses them directly; they're sent __utm.gif
query param.
now, google analytics reads, writes , acts on values , need available ga tracking script.
so, wondering if possible to:
- rewrite
__utm*
cookies local storage afterga.js
has written them - delete them after
ga.js
has run - rewrite cookies local storage cookie form right before
ga.js
reads them - start over
or, monkey patch ga.js
use local storage before begins cookie read/write part.
obviously if going far out of way remove __utm*
cookies we'll want use async variant of analytics.
i'm guessing down vote because didn't ask question. doh!
my questions are:
can done described above?
if so, why hasn't been done?
i have default html/css/js
boilerplate template passes yslow, pagespeed, , chrome's audit near perfect scores. i'm looking way squeeze remaining cookie bytes google analytics in browsers support local storage.
use this:
(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','//www.google-analytics.com/analytics.js','ga'); if(window.localstorage) { ga('create', 'ua-98765432-1', 'www.example.com', { 'storage': 'none' , 'clientid': window.localstorage.getitem('ga_clientid') }); ga(function(tracker) { window.localstorage.setitem('ga_clientid', tracker.get('clientid')); }); } else { ga('create', 'ua-98765432-1', 'www.example.com'); } ga('send', 'pageview');
first, check if localstorage
supported. if supported 'storage': 'none'
option disable cookies. can set clientid localstorage. if empty, google analytics generate new 1 us. save new (or existing) clientid in localstorage after tracker loads.
if localstorage
not supported, use regular analytics method. after initialization send pageview via ga('send', 'pageview')
.
also, check out plunk: http://plnkr.co/mwh6xwgk00u3cfotzepk
- Get link
- X
- Other Apps
Comments
Post a Comment