Insights

Google Analytics 4 integration with Growthbook

Foto van Rudger de Groot
Rudger de Groot
Founder & CRO

Share

Due to the popularity of GA4 there are a lot of questions on the Growthbook Slack channel #ask-questions about integrating Growthbook with Google Analytics 4 just like this one:

We’re currently implementing on https://www.example.com/. We are not sure about the tracking part. Namely: – What do we need to implement in our existing events (such as Purchase, Add To Cart, etc.)? – Why do we need the new event “ExperimentViewed”?

Google Analytics 4 & Growthbook User

#ask-questions

So, the answer to this question is pretty straightforward.

To the first question. No you don’t need to setup any additional events for like purchase and add to cart specifically for Growthbook.

To the second question, the Experiment Viewed is needed to record on a visitor level which experiment and variation a visitor has seen. We only need to store this info in GA4 and BigQuery. Then, in GA4, you can see what the Growthbook visitors did on a dimension level for your existing events.

Connect GA4 with BQ

The first step is to connect Google Analytics 4 with Google BigQuery. Then, you can connect Growthbook to BigQuery, which is required if you want to use the experiment analysis features from Growthbook. Another reason is that when you store your data in Google BigQuery you have all the raw data so you won’t run into sampling issues that you have in Google Analytics online interface when you analyse your data there. The only reason to use the GA online interface is if you want to check if your data is coming in correctly. Anyway back to the main story.

Growthbook push to datalayer

When a user is assigned to a variation in an experiment, Growthbook will call the trackingCallback function. So we use the key from the feature, for example, AB-Test-1 as an Experiment Id. and the variations as the value for the Variation Id, i.e. 0 for Control or 1 for Variation 1.

{   “feature-1”: {
“rules”: [      
{        
// Use “my-experiment” as the key instead of “feature-1”
        “key”: “my-experiment”,
        “name”: “My Experiment”,
        “variations”: [“A”, “B”]
      }
    ]
  }
}

The GrowthBook SDK implementation will then send an event to the DataLayer that looks like the following:

{
  “event”: “experiment_viewed”,
  “experiment_id”: “…”,
  “variation_id”: “…”
}

Now with the experiment information on the datalayer, it’s easy to get this data to GA4.

We assist companies
scaling experimentation

Our GrowthBook expert

Rudger de Groot

Founder & CEO

Contact form

Discover how we’ve helped leading brands transform their digital experiences through experimentation and innovative GrowthBook solutions.

Direct contact

Nijverheidstraat 11-1E
7511 JM Enschede
the Nederlands

+31 53 234 0444