The tracker script, line by line
Your visitors load one small file from PureMetrix: under 3 KB compressed (2.7 KB with every feature). Below is the complete core of that file, readable, so you can check for yourself what runs on your site.
What it does
cfg()reads thedata-*attributes of the script tag: domain, public key, optional event endpoint (data-api, used by the proxy), and switches for localhost, manual mode, 404 pages, consent mode, leaving out query strings, global properties, debug output and the download file types. Website code can add functions (a filter, computed properties) viawindow.puremetrixConfig.ign(),bot(),loc(),why()stop all sending if you excluded this browser, if the browser is automated, if the page runs on localhost, or while consent mode is still waiting. With debug output on, the reason appears in the browser console.body()builds the request: event name, domain, key, page URL, referrer – plus engagement time, scroll depth, revenue or custom properties when they apply.strip()removes query parameters (exceptutm_…andref) when you ask for it.send()first passes the request to your optionalbeforeSendfilter (which can change or drop it), then posts it withsendBeacon(orfetchas fallback). No cookies are set or read.consent()starts or stops sending when your cookie banner reports the visitor's choice. While waiting, nothing is sent or kept.pageview()andbindSpa()count page loads and URL changes in single-page apps.tick(),flush(),bindEngagement()measure time only while the tab is visible and focused, and send it when the visitor leaves.
The file also contains optional extensions (outbound links, downloads, forms, scroll milestones, hash routes, CSS-class events). Each one stays inactive until you switch it on in the site settings, which adds the matching data-* attribute to your snippet.
Source
This is core.js before minification. The build step only compresses it and bundles the optional extensions.
var K='puremetrix_ignore';
function tru(v){return v==='true'||v==='1'||v===''}
export function scriptEl(){try{return document.currentScript||document.querySelector('script[data-domain][data-key],#puremetrix-tracker')}catch(e){return null}}
function G(){try{return window.puremetrixConfig||{}}catch(e){return{}}}
/** Read config from data-* attributes of the script tag. Functions (beforeSend, props) come from window.puremetrixConfig. */
export function cfg(){var el=scriptEl();if(!el)return null;function A(n){return el.getAttribute('data-'+n)}var a=A('api')||'',x=A('download-extensions'),p=A('props'),g=A('debug'),c;
if(!a)try{a=new URL(el.getAttribute('src')).origin+'/api/event'}catch(e){a='/px/event'}if(a[0]==='/')a=location.origin+a;
c={domain:A('domain')||'',key:A('key')||'',api:a,il:tru(A('include-localhost')),manual:tru(A('manual')),is404:tru(A('404')),consent:A('consent')==='pending',xs:tru(A('exclude-search')),bs:A('before-send'),dl:x?x.split(/[\s,]+/):null,debug:g==null?null:tru(g)};
if(p)try{c.props=JSON.parse(p)}catch(e){}return c}
function ign(){try{if(G().ignore)return true;return localStorage.getItem(K)==='1'}catch(e){return !!G().ignore}}
function bot(){try{return!!(navigator.webdriver||window._phantom||window.__nightmare||window.Cypress)}catch(e){return false}}
function dnt(){try{if(navigator.doNotTrack==='1'||navigator.globalPrivacyControl===true)return true}catch(e){}return false}
function loc(){try{var h=location.hostname;return h==='localhost'||h==='127.0.0.1'||h==='[::1]'||location.protocol==='file:'}catch(e){return false}}
function on(){var v=document.visibilityState==='visible';try{return v&&document.hasFocus()}catch(e){return v}}
function dbg(c){var v=c&&c.debug;if(v==null)v=G().debug;return v==null?loc():!!v}
function log(c,m){if(dbg(c))console.info('[puremetrix] '+m)}
/** Keep utm_* and ref, drop every other query parameter (excludeSearch). */
function strip(u){try{var x=new URL(u),d=[];x.searchParams.forEach(function(v,k){if(!/^(utm_|ref$)/.test(k))d.push(k)});d.forEach(function(k){x.searchParams.delete(k)});return x.href}catch(e){if(typeof u!=='string')return u;var q=u.indexOf('?');if(q<0)return u;var base=u.slice(0,q),rest=u.slice(q+1),kept=[];rest.split('&').forEach(function(p){if(!p)return;var k=decodeURIComponent(p.split('=')[0]||'');if(/^(utm_|ref$)/.test(k))kept.push(p)});return kept.length?base+'?'+kept.join('&'):base}}
export function createTracker(c){var ms=0,since=0,last='',mx=0,smx=0,sms=0,onPv,pend=!!c.consent||G().consent==='pending';
function body(n,o){o=o||{};var u=o.u||location.href,g=n!=='engagement'&&(c.props||G().props),b={n:n,d:c.domain,k:c.key,u:c.xs?strip(u):u,r:o.r!=null?o.r:document.referrer||''};if(typeof g==='function')try{g=g(n)}catch(e){g=0}if(g||o.props)b.p=Object.assign({},g,o.props);if(o.revenue)b.revenue=o.revenue;if(o.interactive===false)b.i=false;if(o.engagement_ms!=null)b.e=o.engagement_ms;if(o.scroll_pct!=null)b.sd=o.scroll_pct;return b}
function why(){return ign()?'opt-out':bot()?'bot':!c.il&&loc()?'localhost':pend?'no consent':''}
function send(b,cb){var n=b.n,w=why(),f=c.beforeSend||(c.bs&&window[c.bs])||G().beforeSend;if(!w&&typeof f==='function'){try{b=f(b)}catch(e){b=null}if(!b)w='beforeSend'}
if(w){log(c,'skip '+n+': '+w);if(cb)cb({ignored:w});return}log(c,b.n+' '+b.u);var j=JSON.stringify(b);
if(!cb)try{if(navigator.sendBeacon){var bl=new Blob([j],{type:'text/plain'});if(navigator.sendBeacon(c.api,bl))return}}catch(e){}
try{fetch(c.api,{method:'POST',body:j,keepalive:true}).then(function(r){if(cb)cb({status:r.status})},function(e){if(cb)cb({error:e})})}catch(e){if(cb)cb({error:e})}}
function track(n,o){if(n)send(body(n,o),o&&o.callback)}
function tick(){if(since&&on())ms+=Date.now()-since;since=on()?Date.now():0}
function flush(u){tick();var d=ms-sms;if(mx>smx||d>=3e3){var o={u:u||location.href};if(d>0)o.engagement_ms=d;if(mx>smx)o.scroll_pct=mx;track('engagement',o);smx=mx;sms=ms}}
function pageview(u,o,force){o=o||{};u=u||location.href;if(u===last&&!force)return;flush(last||u);last=u;mx=0;smx=0;sms=0;ms=0;since=on()?Date.now():0;track('pageview',{u:u,r:o.r,props:o.props,callback:o.callback});if(onPv)try{onPv(u)}catch(e){}}
function nav(){pageview(location.href)}
function vis(){if(on()){if(!since)since=Date.now()}else{tick();flush()}}
function consent(g){var was=pend;pend=!g;if(g&&was){ms=sms=mx=smx=0;since=on()?Date.now():0;if(!c.manual)pageview(location.href,0,1)}}
return{c:c,track:track,pageview:pageview,consent:consent,noteScroll:function(p){if(p>mx)mx=p},flush:flush,onPageview:function(fn){onPv=fn},
cmd:function(n,o){if(n==='consent'&&typeof o==='string')consent(o==='granted');else if(n==='pageview')pageview(o&&o.u,o,c.manual);else track(n,o)},
bindSpa:function(){var p=history.pushState,r=history.replaceState;history.pushState=function(){var o=p.apply(this,arguments);nav();return o};history.replaceState=function(){var loc=location.pathname+location.search;var o=r.apply(this,arguments);if(location.pathname+location.search!==loc)nav();return o};addEventListener('popstate',nav)},
bindEngagement:function(){since=on()?Date.now():0;document.addEventListener('visibilitychange',vis);addEventListener('pagehide',function(){flush()})}};}
function nop(){try{var n=function(){};n.q=[];window.puremetrix=n}catch(e){}}
export function boot(f){if(window.__puremetrix_loaded)return null;return start(bot()||dnt()?{dnt:1}:cfg(),f)}
/** Start tracking with a ready config object (script tag via boot(), npm package bundled mode directly). */
export function start(c,f){if(window.__puremetrix_loaded)return null;var r=!c?'no tag':c.dnt||dnt()?'dnt':!c.domain||!c.key?'no key':!c.il&&loc()?'localhost':'';
if(r){log(c,'off: '+r);nop();return null}
window.__puremetrix_loaded=1;var t=createTracker(c);var q=window.puremetrix&&window.puremetrix.q?window.puremetrix.q:[];window.puremetrix=function(n,o){t.cmd(n,o)};window.puremetrix.q=[];for(var i=0;i<q.length;i++)window.puremetrix.apply(null,q[i]);f=f||{};if(f.pageview!==false&&!c.manual)t.pageview();if(c.is404||f['404'])t.track('404',{u:location.href});if(f.spa!==false&&!c.manual)t.bindSpa();if(f.engagement!==false)t.bindEngagement();return t} Where the file comes from
- Default:
https://collect.puremetrix.org/js/script.js - With the proxy: from your own domain, for example
/px/json WordPress.
What happens with the data on our side is described in How we measure.