Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has anyone been successful in implementing google optimize in a SPA React App?

I am fairly new to the Google Analytics Suite and am currently trying to integrate Google Optimize for A/B testing in a react app.

I have gotten to the point where the variant changes display on the desired web page, but only in Preview mode, and am stumped as to how to proceed to viewing the variant changes on a live site.

So far:

  • google optimize scripts are installed in app

    <style>.async-hide { opacity: 0 !important} </style> <script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date; h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')}; (a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c; })(window,document.documentElement,'async-hide','dataLayer',4000, {'GTM-XXXXXXX':true});</script>

  • ga tracking is setup using ReactGA

  • GTM is setup and is firing tags to track
  • firing google optimize experiments on custom activation event and using dataLayer to push new events

    const initializeReactGa = () => { console.log('called to initialize react ga'); ReactGA.initialize(config.gaTag) ReactGA.ga('require', config.optimizeTag); ReactGA.pageview(window.location.pathname + window.location.search); }

    const googleOptimizeLoader = () => { console.log('called google optimize loader'); if(window.dataLayer) { window.dataLayer.push({'event': 'optimize.activate'}) } }

The problem

  • unable to see variant changes in live site (without preview running)

Any help is appreciated.

TIA

Nayyir

[SOLVED]

Was able to determine the issues were not with the setup. The issue was that google optimize's cookie management was unable to set the required cookie for the expirement on localhost:3000, but rather a custom domain pointing to localhost, similar to XXXX.local:3000.

like image 910
rh0delta Avatar asked Jun 05 '18 19:06

rh0delta


People also ask

How do I optimize Google for react?

Our two variants. Back in your React app, add the script tag you got to your index. html file in between the <head> tags. After you've added the <script> tag to your React app, go back to the Google Optimize dashboard and select a Primary Objective.

What can you do with Google optimize?

Optimize allows you to test variants of web pages and see how they perform against an objective that you specify. Optimize monitors the results of your experiment and tells you which variant is the leader.


1 Answers

This might be what you are looking for

How to Add Google Optimize A/B Testing to Your React App in 10 Lines of Code

like image 140
HGG-Dev Avatar answered Sep 19 '22 08:09

HGG-Dev