Testing AB using Local Storage and Google Analytics

On my e-commerce site, I run an AB test for sitewide. After the visitor lands, I assign them the local storage key / value:

function isLocalStorageNameSupported() { var testKey = 'test', storage = window.localStorage; try { storage.setItem(testKey, '1'); storage.removeItem(testKey); return true; } catch (error) { return false; } } $(function() { if(isLocalStorageNameSupported()){ var version = Cookies.get("version"); if (version == null) { if (Math.random() >= 0.5){ Cookies.set("version", "A"); var version = "A" }else{ Cookies.set("version", "B"); var version = "B" } ga('create', 'UA-XXXXXXXX-1', 'auto'); ga('send', { hitType: 'event', eventCategory: 'Pricing Experiment', eventAction: 'New Unique Visit', eventLabel: version, eventValue: 0 }); } } }); 

After the visitor checks, I check which version they are in and sends another Google Analytics event.

My conversion events are displayed very well. But I get about 25% of the New Unique Visit events. Google Analytics shows 12,000 site visits, but I only have 3,000 of my user events.

What part of my code causes this mismatch and how can I fire the event in all visits?

+8
javascript google-analytics
source share
1 answer

This is a bit of a shot in the dark since I ran your code and the event will fire every time (I assume your Cookies object / function is working), but I wonder, 12,000 numbers might be wrong ...

Is the snippet here exactly that your site is working? Where do you submit your GA page view? I ask because I see the event right after your create function. Another potential reporting problem that I see is that the event is interactive, which means that it will affect the bounce rate if you also submit a pageview (which I assume).

What specific metric do you mean when you say 12,000 visits? Sessions?

Sending events before viewing pages will also give you a bunch of (not set) Landing Page sizes. This means that the event will not be associated with any landing page, and when viewing the page it will also increase your user counter ... Therefore, if you see more users than sessions that indicate that this is a problem.

Some things to try:

  • Submit a pageview before the event.
  • Set the event as an event with no interaction using {nonInteraction: true} .
  • Another thing I would like to do is to put the event only in the create conditional and the page should always be viewed regardless of the localstorage condition.
  • Edit: you also expect the DOM to be ready for create . I would move it to the <head> tag so that the event is the only one waiting for the document to be ready.

 <head> <script> //Your preferred method of loading analytics.js here... ga('create', 'UA-XXXXXXXX-1', 'auto'); ga('send', 'pageview'); </script> </head> 

 function isLocalStorageNameSupported() { var testKey = 'test', storage = window.localStorage; try { storage.setItem(testKey, '1'); storage.removeItem(testKey); return true; } catch (error) { return false; } } $(function() { if(isLocalStorageNameSupported()){ var version = Cookies.get("version"); if (version == null) { if (Math.random() >= 0.5){ Cookies.set("version", "A"); var version = "A" }else{ Cookies.set("version", "B"); var version = "B" } ga('send', 'event', 'Pricing Experiment', 'New Unique Visit', version, 0, {nonInteraction: true}); } } }); 

See how it brings your metrics closer together, and then consider an alternative way to track data:

Perhaps the event here is not the best solution. What you do sounds like a great candidate for using a user-limited user size.

I would recommend creating a custom parameter called “Price Experiment” with a user scope (because with GA Experiments the user will always see the same variations in successive sessions), and then after your create function replace the event with this: ga('set', 'dimension1', version); (remember to replace dimension1 with your actual number.

Finally, submit the page view after setting the dimension (custom sizes use hit types to transfer to GA).

 <head> <script> //Your preferred method of loading analytics.js here... ga('create', 'UA-XXXXXXXX-1', 'auto'); if(isLocalStorageNameSupported()){ var version = Cookies.get("version"); if (version == null) { if (Math.random() >= 0.5){ Cookies.set("version", "A"); var version = "A" }else{ Cookies.set("version", "B"); var version = "B" } ga('set', 'dimension1', version); } } ga('send', 'pageview'); function isLocalStorageNameSupported() { var testKey = 'test', storage = window.localStorage; try { storage.setItem(testKey, '1'); storage.removeItem(testKey); return true; } catch (error) { return false; } } </script> </head> 

Now, when you look at some other report, you can apply the Secondary Dimension of the “Evaluation Experiment” and you can show which version they saw.

Thus, you can get contextual data without entering into the types of strokes that change reporting indicators.

Again, I apologize if this does not completely cover your problem - I take the fragment that you provided literally and fill in any gaps as possible.

+4
source share

All Articles