Introduction
Google Analytics 4 (GA4) is an analytics service that enables you to measure traffic and engagement across your websites and applications. GA4 has the flexibility to measure many different kinds of data, delivering a strong analytics experience that is designed for the future. It will replace Google Universal Analytics (UA) starting in 2023.
For information on setting up an integration between Google Universal Analytics (UA) and Optimizely Web Experimentation, see the following articles:
If you use Google Analytics to monitor your project’s traffic, you can view the results of your Optimizely experiments alongside the rest of the data that Google Analytics collects.
You can set up the integration between Optimizely Web Experimentation and Google Analytics 4 (GA4) both with and without Google Tag Manager (GTM). This article covers setting up the integration without using GTM. For instructions on setting up the GA4 integration using GTM, see this article.
Table of Contents
Setting up GA4 in Optimizely Web Experimentation
Step 1. Add script in Optimizely Web Experimentation
Complete the following steps to add the script in Optimizely Web Experimentation:
- Go to Settings > Integrations.
- Expand the Create Analytics Integration.. drop-down list and select Using JSON.
- Copy the following code and paste it in the JSON Code field:
{
"plugin_type": "analytics_integration",
"name": "Custom GA4 integration",
"form_schema": [],
"description": "",
"options": {
"track_layer_decision": "var state = window['optimizely'].get('state');\nvar campaignObject = state.getDecisionObject({'campaignId':campaignId});\n\nvar utils = window[\"optimizely\"].get(\"utils\");\nutils.waitUntil(function() {\n return typeof(gtag) === 'function';\n}).then(function() {\n gtag(\"event\", \"optimizely-decision\", {\n Experiment: campaignObject.experiment,\n Variation: campaignObject.variation\n });\n});"
}
} - Click Create Extension.
Step 2. Install GA4 on your website
Install Google Analytics 4 (GA4) directly on your website. For help with this, complete the following steps in Google Analytics to find their data tagging instructions:
- Open Google Analytics.
- Go to Admin > Data Streams and open your website’s Web stream details page.
- Go to Tagging Instructions > Global site tag (gtag.js) to find the instructions for adding GA4 to your website.