Created
September 17, 2022 10:40
-
-
Save Kugelschieber/53d1949ef460d013c24fd4b28c7aadc9 to your computer and use it in GitHub Desktop.
A custom pirsch.js without the requirement for a script tag in the header section.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use strict"; | |
// The identification code for your website from the settings page. | |
// Required. | |
const identificationCode = ""; | |
// A comma-separated list of path patterns (regular expressions) to include. | |
// Optional. | |
const includePaths = ""; | |
// A comma-separated list of path patterns (regular expressions) to exclude. | |
// Optional. | |
const excludePaths = ""; | |
// A comma-separated of domains to send page views to. This can be used to create roll-up views. | |
// Optional. | |
const domainList = ""; | |
if (navigator.doNotTrack === "1" || localStorage.getItem("disable_pirsch")) { | |
return; | |
} | |
if ((/^localhost(.*)$|^127(\.[0-9]{1,3}){3}$/is.test(location.hostname) || location.protocol === "file:")) { | |
console.warn("Pirsch ignores hits on localhost. You can enable it by adding the data-dev attribute."); | |
return; | |
} | |
try { | |
const paths = includePaths ? includePaths.split(",") : []; | |
if (paths.length) { | |
let found = false; | |
for (let i = 0; i < paths.length; i++) { | |
if (new RegExp(paths[i]).test(location.pathname)) { | |
found = true; | |
break; | |
} | |
} | |
if (!found) { | |
return; | |
} | |
} | |
} catch (e) { | |
console.error(e); | |
} | |
try { | |
const paths = excludePaths ? excludePaths.split(",") : []; | |
for (let i = 0; i < paths.length; i++) { | |
if (new RegExp(paths[i]).test(location.pathname)) { | |
return; | |
} | |
} | |
} catch (e) { | |
console.error(e); | |
} | |
const domains = domainList ? domainList.split(",") || [] : []; | |
function hit() { | |
sendHit(); | |
for (let i = 0; i < domains.length; i++) { | |
sendHit(domains[i]); | |
} | |
} | |
function sendHit(hostname) { | |
if (!hostname) { | |
hostname = location.href; | |
} else { | |
hostname = location.href.replace(location.hostname, hostname); | |
} | |
const url = "https://api.pirsch.io/hit" + | |
"?nc=" + new Date().getTime() + | |
"&code=" + identificationCode + | |
"&url=" + encodeURIComponent(hostname.substring(0, 1800)) + | |
"&t=" + encodeURIComponent(document.title) + | |
"&ref=" + encodeURIComponent(document.referrer) + | |
"&w=" + screen.width + | |
"&h=" + screen.height; | |
const req = new XMLHttpRequest(); | |
req.open("GET", url); | |
req.send(); | |
} | |
if (history.pushState) { | |
const pushState = history["pushState"]; | |
history.pushState = function () { | |
pushState.apply(this, arguments); | |
hit(); | |
} | |
window.addEventListener("popstate", hit); | |
} | |
if (!document.body) { | |
window.addEventListener("DOMContentLoaded", hit); | |
} else { | |
hit(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment