Integrating Optimizely with Google Analytics using Google Tag Manager

by Ryan Jordan

posted on: November 25th 2015

Google Tag Manager (GTM) is a powerful tool for anyone working, or interested, in Conversion Rate Optimisation. Granted, it has hundreds of benefits for other areas of Ecommerce and Marketing, but today I want to specifically focus on how you can use it within Optimisation to link together Optimizely and Google Analytics.

As a side note, if you're not already using Google Tag Manager and have just stumbled across this post, I'd highly recommend checking it out - mainly because you need to have some understanding of GTM for this implementation, but also because it can provide a wealth of data around on-site user behaviours outside of the standard Google Analytics set up. For me, the main advantage it provides is an understanding of events, tell me where and when users click (or interact) with certain elements on a site, and what effect that has on Conversion Rate. These can usually be a bit of a pain to implement in the code directly, but with Tag Manager, a minute is all it takes to get this data populating into Analytics.

But anyway, event tracking via Google Tag Manager isn't the focus of this post today. If you want to learn more about this I'd point you in the direction of Simo's blog - it's amazing, and has more than enough information to get you started.

Today I'm going to cover integrating Optimizely with Google Analytics using Google Tag Manager...

Before I start I should mention that in order to set up Optimizely / Google Analytics integration through Tag Manager, your Google Analytics code also needs to be fired using Tag Manager.

So, why integrate the two?

There are so many reasons why you should do this, the main one being the wealth of additional data you're going to get about your AB tests. Without this data, you're running tests without understanding what other metrics are being influenced on your site (and you or your client is probably reporting on these metrics). In addition to this, I'm guessing the majority of people will be using the 'Ecommerce Conversion Rate' and other metrics within Google Analytics as their site benchmark, so integrating these platforms with Google Analytics will give you accurate uplift figure on each test. Not only that, but this will also give you a number of different ways to segment your AB test data. Just think of the learnings...

Integrating Optimizely with Google Analytics using Google Tag Manager

To get started, you first need to create Custom Dimensions in Google Analytics. You do this in the Admin -> Property -> Custom Definitions -> Custom Dimensions section. Name the dimension whatever you'd like (I usually go for the name of the AB test I'm running), set the scope to 'session' and check the active box. Hit create, then click Done (you don't need any of the snippets they provide), and that's it, you've created a custom dimension. For every test you have running you'll have to create a new Custom Dimension (to keep the data separate). Google Analytics lets you create up to 20 Custom Dimensions, so you should have more than enough. When reach the 20 limit you can just rename some of the Dimensions you're no longer using...

Ok, so now you've got the Custom Dimensions defined in Google Analytics, you need to create a variable (for each of these Custom Dimension) in Google Tag Manager. To do this, head to 'Variables' and click 'New' within GTM. Name the variable whatever you choose (it can be the same as the dimension in GA), select 'Custom JavaScript' and paste the following code in to the JavaScript box:

function () {
if(window.optimizely) {
var o = optimizely;
if(o.activeExperiments && o.allExperiments) {
for(var i=0;i<o.activeExperiments.length;i++) {
var experimentId = o.activeExperiments[i];
var experiment = o.allExperiments[experimentId];
if(experiment && experiment.universal_analytics) {
var slot = experiment.universal_analytics.slot;
if(slot == 1) {
if(o.variationIdsMap && o.variationNamesMap) {
return o.variationIdsMap[experimentId] + ': ' + o.variationNamesMap[experimentId];
return undefined;

You just need to change one thing here... Where it says 'if(slot == 1)' change the '1' to match the index number of your custom dimension. (Each Custom Dimension will have an index number within Google Analytics (go back to Admin -> Property -> Custom Definitions -> Custom Dimensions to find out what they are)).

Ok so you've now got your custom dimension and variable sorted (that's the hard part out of the way). Now let's set this variable to fire through Google Tag Manager. Open your Google Analytics tag, under step 3 (configure tag), click the more settings, then 'Custom Dimensions' and add a new Custom Dimension. The index needs to be the index number from Google Analytics e.g. 1 and the Dimension Value needs to be the corresponding Variable you have just created in Tag Manager (with the same index number defined in the JavaScript). Next open the 'Advanced Configuration' and check 'Set Tracker Name' (you don't need to put anything in the box). Now save the changes to your Google Analytics Tag.

Do the above for every Variable and Custom Dimension you've created.

Preview the tag and make sure that the Analytics tag is still working as it should be, and once you're happy it is fire the tag.

Now for the easy part (it's also the last bit...). Head to Optimizely and log in, then click on the integrations tab and make sure Google Analytics is turned on (Classic or Universal depending on what you use). Next, open the experiment editor of an experiment you want to integrate with GA. Click options -> Integrations and then under Google Analytics set the Custom Dimension number to the corresponding Index number in GA.

Save the experiment, and you've just integrated Optimizely and Google Analytics.

To create the report you just need to head to Customization (in GA), create a new custom report, define the metrics you want to view in your test and finally in the Dimensions drill down, click Custom Dimensions and select the dimension you have defined as your test.

All done. Enjoy!

Let me know if you have any questions 🙂

avatar for author

Ryan Jordan

Integrating Optimizely with Google Analytics using…

by Ryan Jordan Time to read: 4 min