Configuring GrowthBook to work with Google Analytics - Universal Analytics (UA)
Also please be aware that GA UA is being deprecated, and will not collect new data after **July 1, 2023**.
1. Connect GrowthBook to GA
GrowthBook uses one of the custom dimensions that you define in GA to pass in with your gtag call. The value
you pass for this dimension is in the format of [experiment key][delimiter][variation id]
. In this example
we’ll use dimension 1
, and a colon as a delimiter.
You set the custom dimension you’re going to use as well as the divider string when you connect GrowthBook to your GA data source:
2. Implement the the GrowthBook code
Add the gtag to the GrowthBook tracking callback. The exact implementation will depend on the SDK you're using, but should look similar to the javascript example shown here:
// Create a GrowthBook instance
const growthbook = new GrowthBook({
trackingCallback: (experiment, result) => {
if ("gtag" in window) {
window.gtag("event", "experiment_viewed", {
event_category: "experiment",
event_label: result.variationId,
event_action: experiment.key,
dimension1: experiment.key + ":" + result.variationId,
});
} else {
console.log("no gtag");
}
},
});
You can also map the dimension name passed in google, by defining it in gtag config:
gtag("config", "UA-1234...", {
custom_map: { dimension1: "experiment" },
});
When you define a custom map, the last line in the tracking callback will be mapped from dimension1 to the name you chose, in this case ‘experiment.’ Therefore, if you do this, the last line of your gtag call would become:
'experiment': experiment.key+":"+result.variationId
3. Add Dimension in Google Analytics
Within GA you need to map the custom dimension:
The name here is for your internal use, and it can be whatever you like. At the end, you should see a dimension like this:
With this done, you can add metrics within GrowthBook, and start implementing an experiment.
Keep in mind that it takes a day for event data to show up in GA and therefore will take a day to show up in GrowthBook as well.