Skip to content

Instantly share code, notes, and snippets.

@Nils-van-Kleef
Nils-van-Kleef / customClickGoalEvent.js
Last active May 25, 2016 07:13
Custom click goal event that doesn't fire when visitors scroll on mobile/tablet devices
/**
* OPTIMIZELY CUSTOM CLICK EVENTS FRAMEWORK
* customClickEvent.js
* Version 1.0
*
* @install: You can add the following code to your Experiment JavaScript or Project JavaScript
* Replace the SELECTOR with the jQuery selectors of the element(s) you're looking to track
* Replace CUSTOMEVENTGOAL with the name of your custom event goal (eventName)
*
* @explanation: This code fires a custom event goal for Optimizely. For mobile visitors, it
@Nils-van-Kleef
Nils-van-Kleef / redirect-replace-string.js
Last active September 12, 2016 13:48
Redirect - Replace string. This JavaScript will redirect a visitor to the same URL with a string replaced, preserving the rest of the URL.
/* _optimizely_redirect=http://www.example.com */
var toReplace = "/[TO_REPLACE]";
var newString = "/[NEW_STRING]";
var _optly = {redir: document.createElement("a")};
_optly.redir = {protocol: "https:" == document.location.protocol ? "https://" : "http://",
domain: window.location.hostname,
first: window.location.href.split(toReplace)[0],
second: window.location.href.split(toReplace)[1],
query: window.location.search
};
@Nils-van-Kleef
Nils-van-Kleef / dimension-new-returning-visitor.js
Last active September 7, 2016 11:19
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"
@Nils-van-Kleef
Nils-van-Kleef / redirect-remove-query-parameter-angular.js
Created June 28, 2016 14:16
Redirect - Remove query parameter for angular site. This JavaScript will redirect a visitor to the same URL with a query parameter removed, preserving the rest of the URL. For non-angular, change the line `window.location ..` to `window.location.replace(_optly.redir.href)`
/* _optimizely_redirect=http://www.example.com */
/* remove Query Parameter from url */
_optly = {redir: document.createElement("a")};
_optly.redir = {protocol: "https:" == document.location.protocol ? "https://" : "http://",
domain: window.location.hostname,
pathname: window.location.pathname
};
queryToRemove = "TO_REPLACE";
_optly_cur_loc = window.location.href;
@Nils-van-Kleef
Nils-van-Kleef / getCustomDimensionsForActiveExperiments.js
Last active September 7, 2016 11:19
JS that gets Google Universal Analytics custom dimension slots that are in use by active (running) experiments
/**
* JS that gets Google Universal Analytics custom dimension slots that are in use by active (running) experiments.
* Execute in console on website with correct Optimizely snippet
*/
console.log("All Google Universal Analytics Custom Dimensions in use by active experiments:");
for (var experiment in optimizely.allExperiments) {
if (optimizely.allExperiments[experiment].enabled == true) {
if (typeof optimizely.allExperiments[experiment].universal_analytics == 'object') {
console.log("Experiment: " + experiment + ". Custom Dimension used: " + optimizely.allExperiments[experiment].universal_analytics.slot);
}
@Nils-van-Kleef
Nils-van-Kleef / redirect-keep-substring.js
Last active September 7, 2016 11:20
Redirect - Keep string. This JavaScript will redirect a visitor to a different URL but keeps everything up until a defined part of the URL.
/* _optimizely_redirect=http://www.example.com */
var toKeep = "/[TO_KEEP]";
var addition = "/[NEW_ADDITION]";
var _optly = {redir: document.createElement("a")};
_optly.redir = {protocol: "https:" == document.location.protocol ? "https://" : "http://",
domain: window.location.hostname,
first: window.location.href.split(toKeep)[0],
query: window.location.search
};
_optly.redir.href = _optly.redir.first + toKeep + addition + _optly.redir.query;
@Nils-van-Kleef
Nils-van-Kleef / redirect-to-https-www.js
Created December 16, 2016 14:50
Redirect: Strips href up to "www.", then Prepends https://www. and then executes redirect
/* _optimizely_redirect=http://www.example.com */
var toPrepend = "https://www.";
var hrefPostWWW = window.location.href.split("//")[1].replace("www.","");
var newHref = toPrepend.concat(hrefPostWWW);
window.location.replace(newHref);
@Nils-van-Kleef
Nils-van-Kleef / redirect-append-qp.js
Last active January 31, 2017 13:43
Redirect: adds query-parameter at the end
/* _optimizely_redirect=http://www.example.com */
var qp_addition = "newSearchWeb=true";
if (window.location.href.indexOf(qp_addition) < 0) {
var _optly = {redir: document.createElement("a")};
_optly.redir = {protocol: "https:" == document.location.protocol ? "https://" : "http://",
domain: window.location.hostname,
entire_url: window.location.href,
query: window.location.search
};
if (_optly.redir.query.length == 0) {
@Nils-van-Kleef
Nils-van-Kleef / waitForOriginSync-optimizelyEndUserId-cookie.js
Created March 13, 2017 15:51
waitForOriginSync, but only if the optimizelyEndUserId cookie doesn't exist yet
@Nils-van-Kleef
Nils-van-Kleef / econda-optimizely-integration.js
Created November 27, 2017 15:59
Integration of Econda with Optimizely. Use in Custom Analytics Integrations (or Project JavaScript).
// Pre conditions :
// 1: Econda-Tracking-Script is loaded before
(function(){
var decObject = window.optimizely.get("state").getDecisionObject({campaignId: campaignId});
if( typeof window.emos3 !== 'undefined'){
window.emos3.send({ type: 'event', abtest: [ [decObject.experiment , decObject.variation] ] });
} else if (typeof(window.emosPropertiesEvent)== "function"){
window.emosPropertiesEvent({ type: 'event', abtest: [ [decObject.experiment , decObject.variation] ] });
} else {
console.log("Problem: econda A/B Test Tracking is activated but tracking library is not yet loaded. Could not send data to econda.");