Set up Google Analytics
This topic describes how you can enrich Google analytics with Optimizely test information.
Note
See Optimizely's Third-Party Add-Ons & Platform Integration Terms.
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:
- Read the setup instructions and information about non-interaction events for your platform and study the example code.
- 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.
- 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 indices is the upper limit of Optimizely 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/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 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 does not have a suggested solution for integrating Google Analytics with our SDKs for these platforms:
- Agent
- Go
- Node
- PHP
- Python
- Ruby
Updated 4 months ago