Skip to content

Instantly share code, notes, and snippets.

@Nils-van-Kleef
Last active September 7, 2016 11:19
Show Gist options
  • Save Nils-van-Kleef/0f0bf941b6dca0e42841eef3afb95ba3 to your computer and use it in GitHub Desktop.
Save Nils-van-Kleef/0f0bf941b6dca0e42841eef3afb95ba3 to your computer and use it in GitHub Desktop.
Set dimension for new and returning visitors in Project JS
/**
* This code will help assign all your visitors to either a new or returning dimension.
* This helps attribute conversion events to new/returning visitors.
* I've written this code to replace the new/returning audience conditions enabled for segmentation that many customers use.
*
* Instructions:
* Set up a new dimension called new/returning visitors as per this URL: https://help.optimizely.com/Target_Your_Visitors/Dimensions%3A_Capture_visitor_data_through_the_API
* Dimension Name: "New / returning visitor"
* Dimension Description: "New or returning visitor to your site. Helps attribute conversions to new or returning visitors."
* Dimension API Name: "new_returning_visitor"
* Remember the ID of your newly created dimension, we'll need it later.
* This code needs to run before experiments, on all your pages, e.g. through Project JavaScript to correctly set the dimension for your visitors
* Change the following:
* Replace DIMENSION_ID with the ID of your newly created dimension, see above.
*/
var dimension_ID = DIMENSION_ID; // REPLACE DIMENSION_ID with the ID of your newly created dimension, see instructions above
if (sessionStorage.getItem('optimizely_data$$first_session') && window['optimizely'].data.visitor.dimensions[dimension_ID] === undefined) {
optimizely.push(['setDimensionValue', 'new_returning_visitor', 'new']);
} else if (!sessionStorage.getItem('optimizely_data$$first_session') && (window['optimizely'].data.visitor.dimensions[dimension_ID] === undefined || window['optimizely'].data.visitor.dimensions[dimension_ID] === "new")) {
optimizely.push(['setDimensionValue', 'new_returning_visitor', 'returning']);
}
console.log(window['optimizely'].data.visitor.dimensions[dimension_ID]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment