Skip to content

Instantly share code, notes, and snippets.

@austinginder
Last active May 2, 2022 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austinginder/b7690cdeca1a109d9edf58f07ea1f146 to your computer and use it in GitHub Desktop.
Save austinginder/b7690cdeca1a109d9edf58f07ea1f146 to your computer and use it in GitHub Desktop.
Rewritten tracker.js for Fathom Lite
/*
Rewritten tracker.js template for self hosted Fathom with bundled trackerUrl details.
Use this as a template and included directly with each website you plan on tracking.
Add the following script tag to embed.
<script src="/wp-content/mu-plugins/tracker.js" data-site="ABCDEFG" defer></script>
See unminified version here: https://gist.github.com/austinginder/50c244e64c701e1a5a1ecd8f3719273f
*/
window.fathom_lite=function(){var e={siteId:"",trackerUrl:"//your-tracker-domain.tld/collect"};function t(){return{isNewVisitor:!0,isNewSession:!0,pagesViewed:[],previousPageviewId:"",lastSeen:+new Date}}function n(){let e=new Date;e.setMinutes(e.getMinutes()-30);let n=function(e){for(var t=document.cookie?document.cookie.split("; "):[],n=0;n<t.length;n++){var i=t[n].split("=");if(decodeURIComponent(i[0])===e){var r=i.slice(1).join("=");return decodeURIComponent(r)}}return""}("_fathom_lite");if(!n)return t();try{n=JSON.parse(n)}catch(e){return console.error(e),t()}return n.lastSeen<+e&&(n.isNewSession=!0),n}return e.siteId=document.currentScript.getAttribute("data-site")||e.siteId,setTimeout(function(){window.fathom_lite.trackPageview()}),{trackPageview:function(t){if(t=t||{},"doNotTrack"in navigator&&"1"===navigator.doNotTrack)return;if("visibilityState"in document&&"prerender"===document.visibilityState)return;if(null===document.body)return void document.addEventListener("DOMContentLoaded",()=>{trackPageview(t)});let i=window.location;if(""===i.host)return;let r=document.querySelector('link[rel="canonical"][href]');if(r){let e=document.createElement("a");e.href=r.href,i=e}let o=t.path||i.pathname+i.search;o||(o="/");let a=t.hostname||i.protocol+"//"+i.hostname,d=t.referrer||"";document.referrer.indexOf(a)<0&&(d=document.referrer);var s,c,u,l=n(),p={id:(s=20,c="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",Array(s).join().split(",").map(()=>c.charAt(Math.floor(Math.random()*c.length))).join("")),pid:l.previousPageviewId||"",p:o,h:a,r:d,u:-1==l.pagesViewed.indexOf(o)?1:0,nv:l.isNewVisitor?1:0,ns:l.isNewSession?1:0,sid:e.siteId},m=e.trackerUrl,f=document.createElement("img");f.setAttribute("alt",""),f.setAttribute("aria-hidden","true"),f.src=m+(u=p,"?"+Object.keys(u).map(function(e){return encodeURIComponent(e)+"="+encodeURIComponent(u[e])}).join("&")),f.addEventListener("load",function(){var e=new Date,t=new Date(e.getFullYear(),e.getMonth(),e.getDate(),24,0,0);-1==l.pagesViewed.indexOf(o)&&l.pagesViewed.push(o),l.previousPageviewId=p.id,l.isNewVisitor=!1,l.isNewSession=!1,l.lastSeen=+new Date,function(e,t,n){var i=(e=encodeURIComponent(e))+"="+(t=encodeURIComponent(String(t)));n.path&&(i+=";path="+n.path),n.expires&&(i+=";expires="+n.expires.toUTCString()),document.cookie=i}("_fathom_lite",JSON.stringify(l),{expires:t,path:"/"}),document.body.removeChild(f)}),document.body.appendChild(f)}}}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment