GuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In
Hey! These docs are for version 2.1, which is no longer officially supported. Click here for the latest version, 3.1!

Set up Google Analytics

With Google Analytics, you can use events to track Optimizely test activations and build segments for each variation in a test. 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 and Google Analytics results, remember to apply a date filter in Google Analytics to correspond with the dates your Optimizely 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'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 test. Google recommends against reusing indices, so the number of available indicies is the upper limit of Optimizely tests that you can use custom dimensions to track and segment.

Android

If you're 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're 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/lib/utils/enums');

/**
 * Assume the Google Analytics is loaded.
 * Add an ACTIVATE notification listener.
 */
const activateId = optly.notificationCenter.addNotificationListener(
  optimizelyEnums.NOTIFICATION_TYPES.ACTIVATE,
  function onActivate(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're getting started, read the Google instructions for your platform:

🚧

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

#import <Google/Analytics.h>

NSInteger activateNotificationId = [optimizely.notificationCenter addActivateNotificationListener:^(OPTLYExperiment *experiment, NSString *userId, NSDictionary<NSString *,NSString *> *attributes, OPTLYVariation *variation, NSDictionary<NSString *,NSString *> *event) {
    // Google Analytics tracker
    id<GAITracker> tracker = [GAI sharedInstance].defaultTracker;

    NSString *action
        = [NSString stringWithFormat:@"Experiment - %@",
            experiment.experimentKey];
    NSString *label
        = [NSString stringWithFormat:@"Variation - %@",
            variation.variationKey];
    [tracker send:[[GAIDictionaryBuilder
        createEventWithCategory:@"Optimizely"
                         action:action
                          label:label
                          value:nil] build]];
}];
// Add an activate notification listener
let activateNotificationId = optimizely?.notificationCenter?.addActivateNotificationListener({ (experiment, userId, attributes, variation, logEvent) in
    // Google Analytics tracker
    let tracker : GAITracker? = GAI.sharedInstance().defaultTracker

    let action : String = "Experiment - " + experiment.experimentKey
    let label : String = "Variation - " + variation.variationKey

    // Build and send an Event
    let builder = GAIDictionaryBuilder.createEvent(
        withCategory: "Optimizely",
        action: action,
        label: label,
        value: nil).build()
    tracker?.send(builder as [NSObject : AnyObject]!)
})

Unsupported platforms

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

  • Node
  • PHP
  • Python
  • Ruby

Did this page help you?