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 are not sure about the tracking part. Namely:
– What do we need to implement to our existing events (such as Purchase, Add To Cart, etc.?)
– Why do we need the new event “ExperimentViewed”?

Google Analytics 4 & Growthbook user
#ask-questionsSo, 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.
Get in touch
- +31 53 234 0444
- [email protected]
You will have a meeting with our CRO expert Rudger de Groot