Created
September 6, 2016 20:31
-
-
Save thatsjonsense/298c516825ae170c90740917ecd036d7 to your computer and use it in GitHub Desktop.
Reactivate Optimizely on push state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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