Skip to content

Instantly share code, notes, and snippets.

@sperand-io
Created November 5, 2018 19:48
Show Gist options
  • Save sperand-io/0c1c4dccc9a5c9ace9dd5c99232a8ef6 to your computer and use it in GitHub Desktop.
Save sperand-io/0c1c4dccc9a5c9ace9dd5c99232a8ef6 to your computer and use it in GitHub Desktop.
Override errant referrers
// on page with the payment/login button that they leave from
var link = document.getElementById('paymentOrLoginLinkElementID')
var href = link.getAttribute('href');
link.addEventListener('click', function(event){
// before progressing to the link, save the referrer
event.preventDefault();
sessionStorage.setItem('ctxReferrer', document.referrer);
window.location.href = href;
});
// on whatever page the payment provider redirects back to
// ..snippet..
analytics.load('writekey');
analytics.ready(function() {
// see if there's a stored referrer
var referrer = sessionStorage.getItem('ctxReferrer');
if (referrer) {
// if so, send "fixed" page call
analytics.page({}, { page: { referrer: referrer }});
sessionStorage.removeItem('ctxReferrer');
} else {
analytics.page();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment