---
title: A/B Testing with Layer0 Split Testing and Google Optimize
description: Set up A/B testing with Layer0 Edge Split Testing and a Google Optimize server-side experiment, then deploy your Next.js app to Layer0.
url: https://rishi.app/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework
author: Rishi Raj Jain
published_at: 2021-08-12T19:16:36.626Z
updated_at: 2026-11-15T05:40:02.225Z
---

# A/B Testing with Layer0 Split Testing and Google Optimize

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](https://github.com/rishi-raj-jain/nextjs-layer0-google-server-side-experiment-2) to get the entire setup.

## Set up Google Analytics (Universal Analytics)

Follow Google's official guide for creating [Analytics for a website (Universal Analytics)](https://support.google.com/analytics/answer/10269537?hl=en)

## 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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-1.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-2.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-3.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-4.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-5.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-6.webp)

7\. Make a note of the **Experiment ID**.

![Google Optimize Measurement and objectives section with the Experiment ID highlighted (step 11)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-7.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-8.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-9.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-10.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-11.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-12.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-13.webp)

14\. Click on **Start** to initiate experiment run.

![Google Optimize experience marked ready to start, with the Start button highlighted (step 19)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-14.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-15.webp)

## Set up Layer0

1.  Follow [this guide](https://docs.layer0.co/guides/next) 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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-16.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-17.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-18.webp)

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

![Layer0 Split Testing section with the Add Rule button highlighted (step 2)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-19.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-20.webp)

4\. Click on **ACTIVATE**

![Layer0 Split Testing with the 50/50 rule saved and the Activate button highlighted (step 4)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-21.webp)

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)](https://rishi.app/assets/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework/step-22.webp)

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:
```bash
npm install js-cookie
```
and edit \_app.js as follows:
```js
// File: _app.js

import { useEffect } from 'react'
import Cookies from 'js-cookie'

const GA_TRACKING_ID= '' // Your GA TRACKING ID here
const experimentID= ''  // Your experiment ID here

function 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 cookie
function 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

- [GitHub Repo](https://github.com/rishi-raj-jain/nextjs-layer0-google-server-side-experiment-2)
- [Google Optimize Docs](https://developers.google.com/optimize/devguides/experiments)
- [Layer0 Split Testing Docs](https://docs.layer0.co/guides/split_testing)
- [Layer0's Next.js Docs](https://docs.layer0.co/guides/next)

---

This is the Markdown version of https://rishi.app/blog/a-b-testing-using-layer0-edge-split-testing-with-google-optimize-server-side-framework. Every page on this site is listed with a summary at https://rishi.app/sitemap.md.
