Useful for preventing PII from being tracked.
Usage:
- In GA4 config tag set
page_location={{Page URL (PII Stripped)}}. - In UA's GA Settings, set
page={{UA Pageview URL (PII Stripped)}} andlocation={{Page URL (PII Stripped)}}.
Useful for preventing PII from being tracked.
Usage:
page_location={{Page URL (PII Stripped)}}.page={{UA Pageview URL (PII Stripped)}} and location={{Page URL (PII Stripped)}}.| /** | |
| * @function sanitizeUrl | |
| * @param {string} url - Input URL to be sanitized. | |
| * @return {string} URL with parameters modified according to predefined allow/block -list. | |
| */ | |
| function(){ | |
| var CONFIG = { | |
| // Uses allowlist if true, otherwise uses blocklist. | |
| useAllowlist: false, | |
| // List of parameter names to be blocked (used if useAllowlist=false). | |
| blocklist: 'name,email,phone,address'.split(','), | |
| // List of parameter names to be allowed (used if useAllowlist=true). | |
| allowlist: 'utm_campaign,utm_content,utm_medium,utm_source,utm_term,utm_creative_format,utm_marketing_tactic,gbraid,wbraid,gclid,dclid'.split(','), | |
| // Forces blocked parameters to this value. | |
| // If not provided, blocked parameters will be dropped entirely. | |
| //replaceString:'[REDACTED]' | |
| }; | |
| /** | |
| * @function screenQueryParams | |
| * @author <https://gist.github.com/smhmic/fca71a9914a755cde241498c8a11d254> | |
| * @param {string} url - Input string containing a query string to be screened (i.e. URL). | |
| * @param {string[]} paramList - List of parameter names to be blocked or allowed (depending on isAllowList). | |
| * @param {boolean} isAllowList - If true, parameters will blocked unless specified in paramList. | |
| * @param {undefined|string} [replaceString] - If falsy, blocked parameters will be dropped entirely, otherwise this will be the value of blocked params. | |
| * @return {string} Modified url with parameters screened as indicated. | |
| * | |
| * @example | |
| * screenQueryParams( '?a=1&b=2&c=3', ['a','c'] ); //= ?b=2 | |
| * screenQueryParams( '?a=1&b=2&c=3', ['a','c'], true ); //= ?a=1&c=3 | |
| * screenQueryParams( '/example#?a=1', ['a'] ); //= /example# | |
| * screenQueryParams( '/example#a=1', ['a'] ); //= /example#a=1 | |
| * screenQueryParams( location.href, ['email'], false, '[PII]' ); // Example blocklist | |
| * screenQueryParams( location.href, ['type','tag'], true, '*' ); // Example allowlist | |
| */ | |
| var screenQueryParams = function( url, paramList, isAllowList, replaceString ){ | |
| if( !url || !url.replace ) return url; | |
| paramList = paramList || []; | |
| if( paramList.split ) paramList = paramList.split(/\s*(,|\n)\s*/g); | |
| return url.replace( /((\?)|&)([^#&=]+)(?:=([^#&]*))?/g, function(fullMatch,delim,qmark,key,m3){ | |
| if( isAllowList ? paramList.indexOf(key)+1 : !(paramList.indexOf(key)+1) ) | |
| return fullMatch; | |
| else return replaceString ? delim+key+'='+replaceString : qmark||''; | |
| // Trim trailing "&"s, and remove "?" if all params removed. | |
| }).replace(/\?&*$|(\?)&+/,'$1'); // .replace(/&+$/,'').replace(/^\?$/,''); | |
| } | |
| return function sanitizeUrl( url ){ | |
| return screenQueryParams( url, CONFIG.useAllowlist ? CONFIG.allowlist : CONFIG.blocklist, CONFIG.useAllowlist, CONFIG.replaceString ); | |
| } | |
| } |
| function(){ | |
| return {{fn.sanitizeUrl}}( location.href ); | |
| } |
| function(){ | |
| return {{fn.sanitizeUrl}}( | |
| location.pathname + location.search //+ location.hash //<-- Uncomment to include anchor in pageview URL | |
| ); | |
| } |
Could improve perf by moving regex out of loop using method shown in this solution - https://stackoverflow.com/a/37949642/445295
Done!
Could improve perf by moving regex out of loop using method shown in this solution - https://stackoverflow.com/a/37949642/445295