Use localStorage for Google Analytics tracking when available
See original GitHub issueTL;DR:
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
ga('create','UA-XXXXX-X',{'storage': 'none','clientId':localStorage.getItem('gaClientId')});
ga(function(t){localStorage.setItem('gaClientId',t.get('clientId'));});
ga('send','pageview');
Google Analytics Docs: https://developers.google.com/analytics/devguides/collection/analyticsjs/domains#disableCookies
We could use Modernizer.localstorage
to check for localStorage
support and fallback to cookies if its not available. Though I’m not sure if we want to lock in Modernizr as a dependency.
Why? Because Google doesn’t need to send their cookie to your server for every single request to your domain (or theirs, for that matter).
Issue Analytics
- State:
- Created 10 years ago
- Comments:30 (18 by maintainers)
Top Results From Across the Web
Use localStorage For Client ID Persistence In Google Analytics
When a Google Analytics tracker is created, check if Client ID is persisted in localStorage . If this is the case, check if...
Read more >Use localStorage in GTM -
Let's take a look at how to do that using Google Tag Manager. Read values from localStorage. As this functionality is not available...
Read more >Convert Google Analytics cookies to Local/Session Storage
+1 for the well described case · I'm wondering, local storage cookies is bound to one browser session right? · The data in...
Read more >How to use Google Tag Manager and Google Analytics ...
Connect your user behavior with technical insights without using cookies to improve your customer experience.
Read more >How I removed google analytics and still have good data to ...
I added a little script into my website. Instead of cookies it uses local storage. local storage can not be used to track...
Read more >
Top Related Medium Post
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Update:
It is not against TOS to use localStorage to store the ClientID; it is now officially supported by Google: https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#using_localstorage_to_store_the_client_id
Note: if you have to support (extremely) old browsers (like iOS5 and FF4) their example snippet may fail (see: https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js).
Hmmm, I don’t think this is true. There are opt-out features that GA provides (e.g. Chrome extensions) that don’t rely on the implementor using cookies. I think the point of this section of the TOS is that you can’t create a mechanism by which someone who’s using an official “do not track” extension will still be tracked.
I can look into it further and I’ll update this thread is my assumptions turn out to be false.