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.
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
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'})
}
}
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
.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With