Skip to content

Instantly share code, notes, and snippets.

@jamesspittal
Last active June 4, 2024 10:50
Show Gist options
  • Save jamesspittal/2735d5f8310f8fd50fd52a548ee60cc9 to your computer and use it in GitHub Desktop.
Save jamesspittal/2735d5f8310f8fd50fd52a548ee60cc9 to your computer and use it in GitHub Desktop.
JS API calls for Optimizely X
// Top-level functions
window.optimizely.get('state');
window.optimizely.get('data');
window.optimizely.get('visitor'); // For custom attributes and such
window.optimizely.get('behavior'); // For behavior queries
// Common examples
window.optimizely.get('state').getActiveExperimentIds(); // Returns an array of active experiment IDs on a page.
window.optimizely.get('data').revision; // Returns the revision that the snippet on the page is on. Useful in determining whether you are looking at the most updated changes made within a campaign.
window.optimizely.get('state').getVariationMap(); // Returns an object of experiment IDs and the corresponding variation ID a user has been bucketed into. Equivalent of classic Optimizely Testing's optimizelyBuckets cookie (which is no longer available within Optimizely X Web).
// Get campaign states and get page states
window.optimizely.get('state').getCampaignStates({isActive: true}); // Get active campaigns.
window.optimizely.get('state').getPageStates({isActive: true}); // Get active pages.
// Useful visitor data
window.optimizely.get('visitor').first_session; // Returns if it is first session (true or false)
window.optimizely.get('visitor').source_type; // Returns "direct"
window.optimizely.get('visitor').device; // Returns "desktop" or "mobile"
// Only accessible on some plans
window.optimizely.get('visitor').location.city;
// Useful behaviour queries (only work if you have access to the behaviour query/Optimizely X Personalization)
window.optimizely.get('behavior').getEvents(); // Get all events
window.optimizely.get('behavior').query({"version": "0.2", "filter": [ { "field": ["type"], "value": "custom" }]}); // Get all custom events
window.optimizely.get('behavior').query({"version": "0.2", "filter": [ { "field": ["type"], "value": "click" }]}); // Get all click events
window.optimizely.get('behavior').query({"version": "0.2", "filter": [ { "field": ["type"], "value": "pageview" }]}); // Get all pageview events
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment