Skip to content

Instantly share code, notes, and snippets.

@LeCoupa
Last active May 11, 2021 21:32
  • Star 20 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save LeCoupa/3824ff531c29077ae130 to your computer and use it in GitHub Desktop.
Optimizely Javascript API Cheatsheet --> https://github.com/LeCoupa/awesome-cheatsheets
// Optimizely JavaScript API
// http://developers.optimizely.com/javascript/
// To opt a visitor out of Optimizely tracking
// http://www.example.com/page.html?optimizely_opt_out=true
// 1. API Function Calls
// http://developers.optimizely.com/javascript/#api-function-calls-2
optimizely.push(["activate", experimentId]); // The given experiment is run if the visitor meets the experiment's targeting conditions.
optimizely.push(["activateSiteCatalyst", {"sVariable": mySVar}]); // Integrate with SiteCatalyst.
optimizely.push(['addToAudience', audienceId]); // Add a visitor to an audience.
optimizely.push(['removeFromAudience', audienceId]); // Remove a visitor from an audience.
optimizely.push(['removeFromAllAudiences']); // Remove a visitor from all audiences.
optimizely.push(["bucketVisitor", experimentId, variationId]); // Assign a visitor to a specific variation.
optimizely.push(["customTag", tagKey, tagValue]); // You can use custom tags to target based on information you have about a visitor.
optimizely.push(["delayPageviewTracking", 1000]); // Delay pageview tracking by a specified number of milliseconds.
optimizely.push(['setDimensionValue', dimensionId, 'value']); // Set a dimension value for a visitor.
optimizely.push(["disable"]); // Disable Optimizely entirely.
optimizely.push(["disable", "tracking"]); // Disable just Optimizely tracking calls from being made.
// You can also do this by using the query parameter `optimizely_log=true`
optimizely.push(["log"]); // Tell Optimizely to output its log to the browser's console log.
optimizely.push(["setUserId", myHashedEmail]); // Set a unique (logged-in) identifier to be used by Optimizely for bucketing and tracking.
optimizely.push(["optOut"]); // Opt a visitor out of Optimizely tracking.
optimizely.push(["optOutThirdPartyCookies"]); // Disable Optimizely's 3rd party cookies.
optimizely.push(["setCookieExpiration", 365]); // Specify the number of days before the Optimizely visitor cookies will be set to expire.
optimizely.push(["setCookieDomain", "www.example.com"]); // Instruct Optimizely to set its cookies on a specific subdomain instead of the default domain.
optimizely.push(["bindTrackElement", "selector", "eventName"]); // Track clicks on elements matching "selector" with the event "eventName".
optimizely.push(["trackEvent", "eventName"]); // Track custom events in Optimizely. The event "eventName" will be tracked and associated with the current visitor.
optimizely.push(["trackEvent", "eventName", {"revenue": valueInCents}]); // Track revenue-generating events. The event "eventName" will be tracked and associated with a revenue of valueInCents.
// 2. The Data Object
// http://developers.optimizely.com/javascript/#the-data-object
optimizely.data // Contains read-only information about the current user and the running experiments.
optimizely.data.customTags // Contains all the key-value pairs you've sent to Optimizely using the custom tag function.
optimizely.data.experiments // An object with information about all the experiments for this project file.
optimizely.data.experiments[experimentId].code // This contains the experiment's global JavaScript code, if any.
optimizely.data.experiments[experimentId].manual // This is a boolean indicating whether the experiment is a manual experiment or not.
optimizely.data.experiments[experimentId].name // This is the experiment's name.
optimizely.data.experiments[experimentId].section_ids // This contains the section ids for the experiment, if it is a multivariate experiment.
optimizely.data.experiments[experimentId].variation_ids // This is an array of variation ids for the variations of this experiment.
optimizely.data.sections // This object contains information about all the project file's sections, indexed by their section id.
optimizely.data.sections[variationId].name // This contains the section's name.
optimizely.data.sections[variationId].variation_ids // This is an array containing the variation ids for this section.
optimizely.data.state // This object contains information about the current state of Optimizely, such as the active variations and the visitor's bucket map.
optimizely.data.state.activeExperiments // This is an array of experiment ids for all the active experiments.
optimizely.data.state.variationMap // This is a hash table whose keys are the experiment ids of experiments running for the visitor.
optimizely.data.state.variationIdsMap // This is a hash table whose keys are the experiment ids of experiments running for the visitor.
optimizely.data.state.variationNamesMap // This is a hash table whose keys are the experiment ids of experiments running for the visitor.
optimizely.data.variations // This is an object with information about all the project file's variations, indexed by their variation id.
optimizely.data.variations[variationId].name // This contains the variation's name.
optimizely.data.variations[variationId].code // This contains the variation's JavaScript code.
optimizely.data.audiences // This is an object with audienceId as the key, and a boolean representing whether the visitor is in the audience (true if the visitor is in the audience) as the value.
optimizely.data.visitor // This contains helpful information about the visitor to Optimizely.
optimizely.data.visitor.browser // This is a string containing information about the browser type that the visitor is using.
optimizely.data.visitor.dimensions // An object with dimensionId as the key, and a string for the dimension value (if any) as the value.
optimizely.data.visitor.referrer // This is a string listing the visitor's referring URL, if any. This is functionally equivalent to document.referer.
optimizely.data.visitor.os // This is a string listing the visitor's operating system.
optimizely.data.thirdParty // This object contains information about the visitor sourced from third-party integrations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment