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, I check if localStorage
is supported. If it is supported then the 'storage': 'none'
option will disable cookies. Now we can set the clientId from localStorage. If it is empty, Google Analytics will generate a new one for us. We save the new (or existing) clientid in localStorage after the tracker loads.
If localStorage
is not supported, I just use the regular analytics method. After the initialization I send a pageView via ga('send', 'pageView')
.
Also, check out this plunk: http://plnkr.co/MwH6xwGK00u3CFOTzepK
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…