Skip to content

Instantly share code, notes, and snippets.

@robisatthefunction
Last active July 17, 2020 16:23
Show Gist options
  • Save robisatthefunction/2757ed6e3589059018e9d49e7ba2d72d to your computer and use it in GitHub Desktop.
Save robisatthefunction/2757ed6e3589059018e9d49e7ba2d72d to your computer and use it in GitHub Desktop.
(function(_pushState) {
var pushState = history.pushState;
history.pushState = function(state) {
if (typeof history.onpushstate == "function") {
history.onpushstate({
state: state
});
}
window.dispatchEvent(new Event('pushState'));
window.dispatchEvent(new Event('locationchange'));
return pushState.apply(history, arguments);
};
})(window._pushState);
(function(_replaceState) {
var replaceState = history.replaceState;
history.replaceState = function(state) {
window.dispatchEvent(new Event('pushState'));
window.dispatchEvent(new Event('locationchange'));
return replaceState.apply(history, arguments);
};
})(window._replaceState);
window.addEventListener('popstate', function() {
window.dispatchEvent(new Event('locationchange'));
});
function setPlanAttribute() {
var utils = window.optimizely.get('utils');
utils.waitForElement('.plan-selection-group').then(function(element) {
utils.waitForElement("body").then(function(el){
el.addEventListener("click",function(event){
var target = event.target;
if(target.matches("input") && target.getAttribute("id").indexOf("PlanRadio")>-1){
window.plan = target.getAttribute("id").split("P")[0];
}
});
});
var url = window.location.href;
var urlSplit = url.split("/");
var plan = urlSplit[urlSplit.length - 1].charAt(0) == "M";
window.plan = plan ? "monthly" : "yearly";
window.addEventListener('locationchange',function(e) {
if(window.location.href.indexOf("https://secure.web.plus.espn.com/billing/purchase/") > -1){
window["optimizely"].push({
type: "user",
attributes: {
"plan": window.plan
}
});
}});
});
}
window["optimizely"] = window["optimizely"] || [];
window["optimizely"].push({
"type": "addListener",
"filter": {
"type": "lifecycle",
"name": "initialized"
},
"handler": setPlanAttribute
});
window.addEventListener('locationchange',function(e) {
if(window.location.href.indexOf("account?returnUrl") > -1) {
setPlanAttribute();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment