Rich-text Reply

Variant ID/Name within custom variable

lorenzonannini 06-20-16

Variant ID/Name within custom variable

Hi all,


I am trying to pass my Optimizely Campaign and Variation IDs into a custom variable that I will then be able to read from my Google Analytics tag and fire within it. Unfortunately, the way our Optimizely code is integrated is not standard and therefore I can't make use of the out of the box Optimizely/Google Analytics integration (tried it and it doesn't work).


What I would like to obtain is a variable with my:

  • Campaign/Test ID (or name)
  • Variant ID (or name)

something like: 

var abtest = campaign_id + ' - ' + variant_id;

Is there a way that I can refer the campaign id and the variant ID from within Optimizely?


Many thanks

CouchPsycho 06-20-16

Re: Variant ID/Name within custom variable


interesting issue. How about setting them within the code-editor? If you try to access a variable from without your experiment like i think you won't receive correct data. Just imagine, you have more than one experiment running. I haven't found anything within the documentation, so maybe a pro can help out.

But take into account, that -if done manually- you can set values which are non-cryptic, which will allow your analytics team to understand the values they see more easily. IDs are nice as long as you can easily translate them....

"the essence of the creative act is to see the familiar as strange." (anonymous)
robertchan 06-21-16

Re: Variant ID/Name within custom variable

Hi @lorenzonannini, yes, you can get the information you requested and more using Optimizely's data object. For the numbers you wanted, I wrote a little work around. You can use what I wrote as a start:


if(document.URL.indexOf('optimizely_x') > -1){
	var experimentID = '', oIdx = document.URL.indexOf('optimizely_x');
	var idSt = oIdx+12, idEnd = oIdx+22, idIdx = oIdx+23;
	for(var i=idSt; i<idEnd; i++){ experimentID += document.URL[i]; }
	experimentID = parseInt(experimentID)
	console.log('This is experiment ID ' + typeof experimentID + ' ' + experimentID + ' running on variant index number ' + document.URL[parseInt(idIdx)]);
} else {
	console.log('Experiment ID not found. Please manually input.');

	experimentTitle =

	console.log('Currently on Optimizely revision number ' + optimizely.revision);
	console.log('The experiment name is: ' + experimentTitle);

//Requested abtest variable
var abtest = experimentID + ' - ' + document.URL[parseInt(idIdx)];
console.log('abtest = ' + abtest);

It's very important to note that this code will only work with the preview URL under the assumption that optimizely_x is found within the URL, or using a force preview link (e.g. You'll likely want to tweak the code to exactly what you're looking for, but I thought this might be a good start.

Robert Chan

Experimentation Hero

Re: Variant ID/Name within custom variable

Hi @robertchan


The approach sounds good, but I am specifically try to make it work in a production environment (i.e. without the query string parameters in the URL). Is there any variable defined/definable from Optimizely that I can set with the experiment and variation ID?


Many thanks

robertchan 06-22-16

Re: Variant ID/Name within custom variable

Hi @lorenzonannini, you can have a look at the Optimizely data object. Have a look here:


Assuming you're able to use ECMAScript 5 in production, you can do the following for what you're looking for:


//Returns (as defined by Optimizely) a hash table whose keys are the experiment ids of experiments running for the visitor
const vMap = window['optimizely'].data.state.variationMap;

//Returns the experiment ID
const eID = parseInt(Object.keys(vMap));

//Returns the variant index
const vIdx = vMap[eID];
Robert Chan

Experimentation Hero