Skip to content

Instantly share code, notes, and snippets.

@thatsjonsense
Created September 6, 2016 20:31
Show Gist options
  • Save thatsjonsense/298c516825ae170c90740917ecd036d7 to your computer and use it in GitHub Desktop.
Save thatsjonsense/298c516825ae170c90740917ecd036d7 to your computer and use it in GitHub Desktop.
Reactivate Optimizely on push state
if (!history.onpushstate) {
(function(history){
var pushState = history.pushState;
history.pushState = function(state) {
if (typeof history.onpushstate == "function") {
history.onpushstate({state: state});
}
return pushState.apply(history, arguments);
}
})(window.history);
var currentUrl = document.location.hostname + document.location.pathname
onPotentialRouteChange = function () {
window.setTimeout(function() {
var newUrl = document.location.hostname + document.location.pathname;
if (newUrl !== currentUrl) {
onRouteChange();
}
currentUrl = newUrl;
}, 10);
}
history.onpushstate = onPotentialRouteChange;
window.onpopstate = onPotentialRouteChange;
onRouteChange = function() {
optimizely.push({
type: 'activate',
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment