question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use localStorage for Google Analytics tracking when available

See original GitHub issue

TL;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');

The Source: http://stackoverflow.com/questions/4502128/convert-google-analytics-cookies-to-local-session-storage/19207035?noredirect=1#19207035

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:closed
  • Created 10 years ago
  • Comments:30 (18 by maintainers)

github_iconTop GitHub Comments

3reactions
davidmurdochcommented, Apr 21, 2016

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).

0reactions
philipwaltoncommented, Jun 4, 2015

Using HTTP State Management mechanisms" (read: localStorage) to propagate cookie state is a circumvention of our privacy safeguards. Doing so violates the Google Analytics Terms of Service

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found