Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Configure Google Analytics 4 (GA4)

Use Google Analytics 4 (GA4) to capture and analyze traffic data across your websites and applications.

📘

Note

This is a third-party integration and is not an Optimizely subprocessor. See Optimizely's Third-Party Add-Ons & Platform Integration Terms.


📘

Note

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 Feature Experimentation

You can use a custom notification listener to configure GA4 in Optimizely Feature Experimentation. Notification listeners let you programmatically observe and act on various events within the Optimizely Feature Experimentation SDKs.

For information on how to configure a notification listener in Optimizely Feature Experimentation, select the corresponding SDK to view the developer documentation:

📘

Note

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

Example onDecision listener:

const optimizelySdk = require('@optimizely/optimizely-sdk');
const optimizelyEnums = require('@optimizely/optimizely-sdk').enums;  

const optimizely = optimizelySdk.createInstance({
  sdkKey: '<YOUR_SDK_KEY>',
});

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']);
    if (decisionInfo['decisionEventDispatched']) {
      // Send data to analytics provider here
    }
  }
}

const notificationId = optimizely.notificationCenter.addNotificationListener(
  optimizelyEnums.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 congigure the integration between Optimizely Feature Experimentation and Google Analytics 4 (GA4) with or without Google Tag Manager (GTM). You should use GTM to track the data and variables sent by Optimizely Feature Experimentation, as it makes timing issues much more manageable.

GTM lets you 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 following:

With Google Tag Manager:

Step 1. Push Optimizely data to Google Tag Manager

In your local code, push Optimizely Feature Experimentation 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.
create user-defined variables
  1. Update the variable's name field from "Untitled Variable" to Optimizely - flagKey.
update name field
  1. Click Variable Configuration and select Data Layer Variable.
select data layer variable
  1. In the Data Layer Variable Name field, enter optimizely-flagKey.
name data layer variable
  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:

user defined 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.
create new tag
  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.
select Google Analytics GA4 configuration tag
  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.
create new event tag
  1. Update the name field from "Untitled Tag" to Optimizely - GA4 Event.
  2. Click Tag Configuration and select Google Analytics: GA4 Event.
add Google Analytics: GA4 Event tag configuration

📘

Note

If you plan on creating both an Optimizely Web Experimentation experiment and an Optimizely Feature Experimentation experiment, you may want to differentiate between your projects.

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

For instructions on setting up Optimizely 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:

example tag configuration
  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
create new trigger

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

select Custom Event trigger

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

name Event field

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

save the overall tag setup

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:

example google results

With their sent values:

example 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 Feature Experimentation is significantly more complicated.

To send Optimizely Feature Experimentation 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 Feature Experimentation.

Conclusion

After successfully setting up and pushing events from Optimizely Feature Experimentation 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.