Skip to content

Instantly share code, notes, and snippets.

@tjtate
Last active March 13, 2018 13:58
Show Gist options
  • Save tjtate/8c7132816ca59cb3a47803c0ef24b51f to your computer and use it in GitHub Desktop.
Save tjtate/8c7132816ca59cb3a47803c0ef24b51f to your computer and use it in GitHub Desktop.
PII mask in GTM
/*
REMOVE EMAIL PII FROM URL
For best use: run this after scripts that need to leverage the email in the query string
and before you start loading marketing pixels.
*/
//get url parameter from url
function getUrlParam(e) {e=e.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");var n=new RegExp("[\\?&]"+e+"=([^&#]*)"),r="";if(typeof t==="undefined"){r=n.exec(location.search)}else{r=n.exec(t)}return r==null?null:decodeURIComponent(r[1].replace(/\+/g," "))}
//you can optionally save the email address for use later in a global variable or as part of a global object if you want.
var user_email = null;
//this checks if email is a query string
if (getUrlParam('email') !== null) {
//get the original url decode it, save out the email address
var original_url = window.location.href,
decoded_url = decodeURIComponent(original_url),
original_url_email = decoded_url.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi),
new_url = decoded_url.replace(original_url_email[0], '[email protected]');
//swap out the URL with the redacted url using replaceState to avoid page refresh
window.history.replaceState({},"", new_url);
//assign the original email to the global scope variable for use in other internal scripts
user_email = original_url_email[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment