Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Set up Google Universal Analytics

This topic describes how to enrich Google Universal Analytics with Optimizely Feature Experimentation test information.

❗️

Warning

This article is about Google Universal Analytics properties, which will stop processing data on July 1, 2023.

View the updated documentation on implementing Optimizely Feature Experimentation and Google Analytics 4.

With Google Analytics, you can use events to track Optimizely Feature Experimentation test activations and build segments for each variation in an experiment. For more details on alternative solutions, read on.

The example code in each section below shows how to add an activate listener closure to the notification center. When the callback is called, the code creates and sends an event containing the experiment key and variation key.

To set up Google Analytics:

  1. Read the setup instructions and information about non-interaction events for your platform and study the example code.
  2. Build segments for each variation of your test. In the segment-builder user interface, look under Advanced and add a new condition. Select to filter users by Event Action and Event Label, enter a name for the segment, and save.
  3. Build a report of metrics that you are interested in.

Compare results

When comparing Optimizely Feature Experimentation and Google Analytics results, remember to apply a date filter in Google Analytics to correspond with the dates your Optimizely Feature Experimentation test was running.

Alternative solution

Google offers custom dimensions as another way to include non-standard data in your reports. Custom dimensions are the replacement for custom variables from older versions of Google Analytics.

Optimizely Feature Experimentation's suggested integration does not use custom dimensions because free Google Analytics accounts are limited to 20 indices (or slots), and 360 accounts are limited to 200 indices. Because a custom dimension cannot store a value that exceeds 150 bytes, you would need to dedicate an index for each Optimizely Feature Experimentation test. Google recommends against reusing indices, so the number of available indices is the upper limit of Optimizely Feature Experimentation tests that you can use custom dimensions to track and segment.

Android

If you are getting started, read the Google Analytics Android Setup instructions.

Read more about non-interaction events and support for them in the Google Analytics Android SDK.

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.optimizely.ab.config.Experiment;
import com.optimizely.ab.config.Variation;
import com.optimizely.ab.notification.ActivateNotificationListener;
import com.optimizely.ab.notification.NotificationCenter;
import com.optimizely.ab.notification.TrackNotificationListener;

// Add a Activate listener
int notificationId = optimizely.notificationCenter.addNotificationListener(NotificationCenter.NotificationType.Activate, new ActivateNotificationListener() {
   @Override
   public void onActivate(@Nonnull Experiment experiment, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Variation variation, @Nonnull LogEvent event) {
    String experimentKey = experiment.getKey();
    String variationKey = variation.getKey();

    // Google Analytics tracker
    SampleApp application = (SampleApp) getApplication();
    mTracker = application.getDefaultTracker();

    // Build and send a non-interaction Event
    mTracker.send(new HitBuilders.EventBuilder()
        .setCategory("Optimizely")
        .setAction("Experiment - " + experimentKey)
        .setLabel("Variation - " + variationKey)
        .setNonInteraction(true)
        .build());
  }
});

JavaScript

If you are getting started, read the Google Analytics JavaScript setup instructions.

Read more about non-interaction events and support for them in the Google Analytics JavaScript SDK.

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

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

/**
 * Assume the Google Analytics is loaded.
 * Add an ACTIVATE notification listener.
 */
const notificationId = optimizely.notificationCenter.addNotificationListener(
  optimizelyEnums.NOTIFICATION_TYPES.ACTIVATE,
  (activateObject) => {
    const action = 'Experiment - ' + activateObject.experiment['key'];
    const label = 'Variation - ' + activateObject.variation['key'];
    ga('send', 'event', 'Optimizely', action, label, {
      nonInteraction: true,
    });
  },
);

Objective-C and Swift

If you are getting started, read the Google instructions for your platform:

🚧

Important

The Google Analytics iOS SDK does not support non-interaction events. This may affect your bounce rate.

Unsupported platforms

Optimizely Feature Experimentation does not have a suggested solution for integrating Google Analytics with our SDKs for these platforms:

  • Agent
  • Go
  • Node
  • PHP
  • Python
  • Ruby