Last active
January 27, 2017 19:23
-
-
Save optimizely-christine/4c1619e525e7cd207325be735b7d0469 to your computer and use it in GitHub Desktop.
Vets First // LiveRamp Project JS Script
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
//redirect URL mapping - replace this with the mapping from LiveRamp | |
var redirectUrls = { | |
"56252426" : "http://sfveterinaryhousecalls.vetsfirstchoice.com", | |
"12345444" : "http://bluecrosspet.vetsfirstchoice.com", | |
}; | |
//Optimizely Redirect Handler | |
function dataHandler( LiveRampData ) { | |
if(LiveRampData.length > -1){ | |
LiveRampData.forEach(function(id) { | |
var storeUrl = redirectUrls[id]; | |
if (storeUrl){ | |
var crd = queryStringsRedirectHandler("redirect"); | |
if (crd){ | |
storeUrl += storeUrl.match(/\/$/) ? crd : "/" + crd; | |
} else { | |
storeUrl += window.location.search; | |
} | |
window.location.href = storeUrl; | |
} | |
}); | |
} | |
} | |
function queryStringsRedirectHandler(name) { | |
var queryString = window.location.search.substring(1); | |
var nqs = ""; | |
if (queryString) { | |
var prefix= encodeURIComponent(name)+'='; | |
var pars= queryString.split(/[&;]/g); | |
for (var i= pars.length; i-- > 0;) { | |
if (pars[i].lastIndexOf(prefix, 0) !== -1) { | |
var rp = pars[i].split("="); | |
nqs += rp.length == 2 ? rp[1] : ""; | |
pars.splice(i, 1); | |
} | |
} | |
nqs += pars.length > 0 ? "?" + pars.join("&") : ""; | |
} | |
return nqs; | |
} | |
//LiveRamp Handlers | |
var pm_id = "456499"; | |
var pm_handler = dataHandler; | |
var debug = false; | |
function printDebug(msg) { | |
if (debug === true) { | |
var elt = document.getElementById("debug"); | |
elt.innerHTML = elt.innerHTML + "<br/>" + msg; | |
} | |
} | |
function setupMessageHandler(handler) { | |
var eventHandler = function(event) { | |
handler(eval("(" + event.data + ")")); | |
}; | |
if(window.addEventListener) { | |
window.addEventListener("message", eventHandler, false); | |
} else { | |
window.attachEvent("onmessage", eventHandler); | |
} | |
} | |
function setupDataFeed(id) { | |
var f = document.createElement('script'); f.type = 'text/javascript'; | |
f.src = "//rrc.rlcdn.com/" + id + ".js?redirect=1"; | |
document.head.insertBefore(f, document.head.firstChild); | |
} | |
function main() { | |
$('html').hide(); | |
setupMessageHandler(pm_handler); | |
setupDataFeed(pm_id); | |
$('html').delay(1000).fadeIn(400); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment