August 12, 2021 4 min read 940 words

A/B Testing with Layer0 Split Testing and Google Optimize

Rishi Raj Jain
Rishi Raj Jain @rishi_raj_jain_

This guide describes how to configure A/B Testing with Layer0 and Google Optimize Server Side Experiment & deploy your Next SPA to Layer0. Clone the repo nextjs-layer0-google-server-side-experiment-2 (opens in a new tab) to get the entire setup.

Set up Google Analytics (Universal Analytics)

Follow Google's official guide for creating Analytics for a website (Universal Analytics) (opens in a new tab)

Set up Google Optimize

1. To get started, login to your google optimize, and click on Create account.

Google Optimize with the Create account button highlighted (step 1)

2. Input an account name, and click on Next

Google Optimize Create account form with an account name entered (step 2) and the Next button highlighted (step 3)

3. Input a Container name, and click on Create

Google Optimize Add container form with a container name entered (step 4) and the Create button highlighted (step 5)

4. To start creating an experience, Input an experience name, the url of website to A/B Test upon, and click on Create (By default A/B test is selected as the experience type)

Google Optimize Create experience form with the experience name and editor page URL entered and A/B test selected (steps 6 to 8)

5. Scroll down, and click on Link to Analytics to link the created Google Analytics property.

Google Optimize Measurement and objectives section with the Link to Analytics button highlighted (step 9)

6. Select the Google Analytics property you want to link with, and click on Link

Google Optimize Link a property dialog with a Google Analytics property and view selected and the Link button highlighted (step 10)

7. Make a note of the Experiment ID.

Google Optimize Measurement and objectives section with the Experiment ID highlighted (step 11)

8. Before we run an server-side experiment, we need to create a variant, so click on Add variant

Google Optimize Targeting and variants section with the Add variant button highlighted (step 12)

9. Input the desired Variant name (can be anything as the data reported is not matched with the Variant names, but their indices [which by default starts with 0])

Google Optimize Add variant dialog with Variant A entered (step 13) and the Done button highlighted (step 14)

10. After you've add the variant, scroll down and delete the URL rule, pre-populated by Google for you.

Google Optimize Page targeting with the pre-filled URL matches rule and its delete button highlighted (step 15)

11. Click on Add URL rule to start creating your own experiment rule

Google Optimize Page targeting with no rules left and the Add URL rule button highlighted (step 16)

12. Select equals as the matching URL rule, and SERVER_SIDE as the value for it. Click on Add to save the changes.

Google Optimize URL rule set to URL equals SERVER_SIDE, with the Add button highlighted (step 17)

13. Select the primary objective before you can run the server-side experiment.

Google Optimize Objectives section with the Add experiment objective button highlighted (step 18)

14. Click on Start to initiate experiment run.

Google Optimize experience marked ready to start, with the Start button highlighted (step 19)

15. Ignore the Installation not checked, as we're posting data to Google Analytics by custom snippet as mentioned in the blog below.

Google Optimize Start experiment dialog warning that the installation was not checked, with the Start button highlighted (step 20)

Set up Layer0

  1. Follow this guide (opens in a new tab) for deploying a Next.js project with Layer0.

  2. Now, we want to create two deployment environments, and configure A/B split test with Layer0 Console. So, let's start by creating two environments,  default (initiated by default) and preview. To create an environment, click on New Environment

Layer0 Console Environments tab with the New Environment button highlighted (step 1)

  1. Type in your name environment name, and click on CREATE

Layer0 Create a new Environment dialog with the name preview entered (step 2) and the Create button highlighted (step 3)

NOTE: After you're done creating the preview environment, deploy the A/B testing versions (one on default, and the other on preview environment) of your website to Layer0.

Configuring A/B Testing with Layer0

This section will walk you through the steps of configuring A/B testing with Layer0.

1. To do so, open the desired environment (here default) to A/B Test on, and click on EDIT

Layer0 Console configuration of the default environment with the Edit button highlighted (step 1)

2. Scroll down to Split Testing, and click on Add RULE

Layer0 Split Testing section with the Add Rule button highlighted (step 2)

3. Click on ADD DESTINATION and assign the desired percentages for your A/B test to both the environments (current and preview). Optionally, you may add further criterion for configuring the A/B test. Then, click on APPLY to save the changes.

Layer0 Add Rule dialog sending 50% of traffic to the current environment and 50% to the preview environment (step 3), with Apply highlighted (step 4)

4. Click on ACTIVATE

Layer0 Split Testing with the 50/50 rule saved and the Activate button highlighted (step 4)

5. Click on YES, REDPLOY MY SITE NOW to deploy your changes!

Layer0 Activate version dialog with the Yes, redeploy my site now button highlighted (step 5)

Now you're all set up with A/B Testing on your website. Open the web url assigned to the Layer0 environment on different clients to experience the A/B testing.

Set up Next.js

This section will walk you through how to configure your Next.js _app.js to report A/B Testing metrics to Google Analytics (which reflects automatically in Google Optimize)

Set up Next.js to Report A/B Testing Metrics to Google Analytics

To report the A/B testing metrics, we make use of layer0_destination cookie sent by Layer0 while serving your app. (Make sure that you've successfully configured A/B Testing on Layer0). These steps assume that you've two environments: default (initiated by default) and preview.

Run the following command inside your project:

npm install js-cookie

and edit _app.js as follows:

// File: _app.jsimport { useEffect } from 'react'import Cookies from 'js-cookie'const GA_TRACKING_ID= '' // Your GA TRACKING ID hereconst experimentID= ''  // Your experiment ID herefunction addAnalytics(i, s, o, g, r, a, m) {    i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)}// Get variant based on layer0_destination cookiefunction getVariant(destination) {    // Create your own environments' map here    let variantMap = {    default: 0, // variant codes are indexed from 0 and so on, 0 being for the original    __layer0__env__preview: 1, // first variant (an environment named `preview`)    }    return variantMap[destination]}function MyApp({ Component, pageProps }) {    useEffect(() => {        // Get the variant index        let variant = getVariant(Cookies.get('layer0_destination'))        // Report it to Google Analytics        addAnalytics(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga')        ga('create', GA_TRACKING_ID, 'auto')        ga('set', 'exp', `${experimentID}.${variant}`)        ga('send', 'pageview')    }, []);    return <Component {...pageProps} />}

Conclusion

In this tutorial, we learned how to initiate A/B Testing with Google Optimize, configure A/B testing on Layer0, and integrate the analytics with google optimize, Next.js and Layer0 to start serving and visualizing the server-side experiment.

References

Thanks for reading

If this was useful, sharing it on X helps a lot. Spotted a typo or want to push back? DM me.

More posts

2 posts