Skip to content

Instantly share code, notes, and snippets.

@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-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 / 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-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 / 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