Set up Google Analytics 4 (GA4)

This topic describes the enrich Google Analytics 4 with Optimizely data.

πŸ“˜

Google Universal Analytics (UA) and Google Analytics 4 (GA4)

Starting July 1, 2023, GA4 will replace standard Google Universal Analytics (UA), which will no longer process data. For more information, see Introducing Google Analytics 4 (GA4).

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.

Setting up GA4 in Full Stack

You can use a custom notification listener to set up GA4 in Optimizely Full Stack. Notification listeners allow you to programmatically observe and act on various events within the Full Stack SDKs.

For information on how to set up a notification listener in Optimizely, please select the corresponding SDK to view the developer documentation:

πŸ“˜

Example code

We will use the JavaScript (Browser) SDK in the following examples.

Example onDecision listener:

import { enums } from '@optimizely/optimizely-sdk';
// import your third-party analytics integration here
///////////////////////////////////////////
// SET UP DECISION NOTIFICATION LISTENER //
///////////////////////////////////////////

const onDecision = ({ type, userId, attributes, decisionInfo }) => {
  // Add a DECISION Notification Listener for type FLAG
  if (type === 'flag') {
    // Access information about feature, for example, key and enabled status
    console.log(decisionInfo['flagKey']);
    console.log(decisionInfo['enabled']);
    console.log(decisionInfo['decisionEventDispatched']);
    // Send data to analytics provider here
  }
}

const notificationId = optimizelyClient.notificationCenter.addNotificationListener(enums.NOTIFICATION_TYPES.DECISION, onDecision);

πŸ“˜

Note

The notification listener returns the decision. It is possible to update what is returned by editing the onDecision notification listener. Review the Decision notifications listeners developer documentation for more information.

Example of a targeted delivery decisionInfo object:

Example of an experiment decisionInfo object:

You can distinguish between a targeted rollout or an experiment based on decisionEventDispatched. An experiment will have this value set to true. A targeted delivery will return false as there is no dispatched event for rollouts. View the decision event dispatched parameter developer documentation for more information.

Integrating with or without Google Tag Manager

You can set up the integration between Optimizely Full Stack and Google Analytics 4 (GA4) with or without Google Tag Manager (GTM). We highly recommend using GTM to track the data and variables sent by Optimizely, as it makes timing issues much more manageable.

GTM allows you to push items to GTM’s data layer even while the integration end object is unavailable, such as the GA object. Using GTM is especially useful if you run a back-end experiment and pass that information to your front-end to send that information to Google Analytics.

Alternatively, setting up GA4 without GTM requires manually tracking the events and variables sent by Optimizely and verifying that they are sent after GA is initialized.

You can skip directly to each section using the links below:

❗

️ Warning

Implementing your Optimizely Web Experimentation snippet through Google Tag Manager is NOT recommended.

Instead, it would be best to host the Optimizely snippet as far up in the <head> tag as possible. For information on Optimizely Web, please view the documetnation.

For information on setting up GA4 with Optimizely Web, please review the following documentation:

With Google Tag Manager:

Step 1. Push Optimizely data to Google Tag Manager

In your local code, push Optimizely data to the dataLayer collection of GTM. In our JavaScript example, you could implement the following code:

window.dataLayer = window.dataLayer || [];
dataLayer.push({
  'event': 'optimizely-decision',
  'optimizely-flagKey': decisionInfo.flagKey,
  'optimizely-ruleKey': decisionInfo.ruleKey,
  'optimizely-variationKey': decisionInfo.variationKey,
});

Step 2. Create User-defined Variables in Google Tag Manager

  1. Go to Workspace > Variables > New.

  1. Update the variable's name field from "Untitled Variable" to Optimizely - flagKey.

  1. Click Variable Configuration and select Data Layer Variable.

  1. In the Data Layer Variable Name field, enter optimizely-flagKey.

  1. Click Save.
  2. Repeat steps 1 through 5 to create a ruleKey and variationKey data layer variable.
VariableVariable NameData Layer Variable Name
ruleKeyOptimzely - ruleKeyoptimizely-ruleKey
variationKeyOptimizely - variationKeyoptimizely-variationKey

When completed, you should have the following user-defined data layer variables:

Step 3. Set up the configuration tag in GTM

In Google Tag Manager, complete the following steps to set up a Google Analytics GA4 Configuration tag:

  1. Go to Workspace > Tags > New.

  1. Update the name field from "Untitled Tag" to Google Analytics GA4 Configuration.
  2. Click Tag Configuration and select Google Analytics: GA4 Configuration.
    Enter your Measurement ID. For information on where to find your Measurement ID, refer to the GA4 documentation.

  1. Click Triggering and select Initialization - All Pages.
  2. Click Save.

Step 4. Set up the event tag in GTM

In GTM, complete the following steps to set up a Google Analytics GA4 Event tag:

  1. Go to Workspace > Tags > New.

  1. Update the name field from "Untitled Tag" to Optimizely - GA4 Event.
  2. Click Tag Configuration and select Google Analytics: GA4 Event.

πŸ“˜

Optimizely Web Experimentation and Optimizely Full Stack

If you plan on creating both a Web experiment and a Full Stack experiment, you may want to differentiate between your Full Stack project and Web project.

As a best practice, name your event: optimizely-decision-fs for Full Stack and optimizely-decision-web for Web.

For instructions on setting up Web Experimentation and GA4, please review the corresponding documentation:

  1. Enter optimizely-decision-fs in the Event Name field.
  2. Select Google Analytics GA4 Configuration in the Configuration Tag drop-down menu.
  3. Expand Event Parameters
    a) Select Add Row
    b) Enter Flag in the Parameter Name field.
    c) In the Value field, select the Choose a Variable icon and select Optimizely - flagKey, which you created in Step 2.
    d) Repeat steps a-c for Rule and Variation, selecting the corresponding data layer variable for each.

When complete, your Tag Configuration should look like the following:

  1. Click Triggering and then click the New Trigger, + icon to create the following trigger:
    a) Update the trigger name from "Untitled Trigger" to Optimizely - GA4 Trigger

b) Click Trigger Configuration, scroll down, and select Custom Event.

c) In the Event name field, enter optimizely-decision-fs. Click Save.

d) Then click Save again on the overall tag setup page.

Step 5. Submit and Publish GA Workspace

Now submit and publish your workspace changes by going to Workspace > Submit > Publish (adding any details you would like to the Submission Configuration).

You will then be able to see results in the events being populated in Google Analytics:

With their sent values:

This can be verified by viewing the Realtime overview in the Reports tab of GA4.

Without using Google Tag Manager:

You can directly push events to GA4 without using GTM, but tracking the events and variables sent by Optimizely is significantly more complicated.

To send Optimizely data to GA4, you will use GA4’s Send events methods. For more information, refer to GA4’s complete documentation on their Send events methods:

For the JavaScript example in this article, you would use the GA4 Send events call. You would add the following sample call into your code base.

gtag("event", "optimizely-decision", {
  Flag: decisionInfo.flagKey,
  Rule: decisionInfo.ruleKey,
  Variation: decisionInfo.variationKey
});

Within GA4, you can name your variables whatever you would like. You can remove or include any variables. But for a Flag rule, we recommend:

  • flagKey
  • ruleKey
  • variationKey

❗

️ Warning

If you use the above code example, ensure that gtag is defined and that you have GA4 initialized and ready to interpret variables returned by Optimizely.

Conclusion

After successfully setting up and pushing events from Full Stack to GA4, the next step is to create segments in Google Analytics. Segments are a subset of your analytics data. Review the Create segments for Google Analytics 4 documentation for complete instructions.