Skip to content

Instantly share code, notes, and snippets.

@Kugelschieber
Created March 18, 2022 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kugelschieber/b30a0eb9e17925d3c12eea25d69563f3 to your computer and use it in GitHub Desktop.
Save Kugelschieber/b30a0eb9e17925d3c12eea25d69563f3 to your computer and use it in GitHub Desktop.
(function() {
"use strict";
window.pirsch = (name, options) => {
console.log(`Pirsch event: ${name}${options ? " "+JSON.stringify(options) : ""}`);
return Promise.resolve(null);
};
if(navigator.doNotTrack === "1" || localStorage.getItem("disable_pirsch")) {
return;
}
const script = document.querySelector("#pirscheventsjs");
const endpoint = script.getAttribute("data-endpoint") || "https://api.pirsch.io/event";
const identification_code = script.getAttribute("data-code");
const dev = script.getAttribute("data-dev");
if(!dev && (/^localhost(.*)$|^127(\.[0-9]{1,3}){3}$/is.test(location.hostname) || location.protocol === "file:")) {
console.warn("You're running Pirsch on localhost. Events will be ignored.");
return;
}
window.pirsch = function(name, options) {
if(typeof name !== "string" || !name) {
return Promise.reject("The event name for Pirsch is invalid (must be a non-empty string)! Usage: pirsch('event name', {duration: 42, meta: {key: 'value'}})");
}
return new Promise((resolve, reject) => {
const meta = options && options.meta ? options.meta : {};
for(let key in meta) {
if(meta.hasOwnProperty(key)) {
meta[key] = String(meta[key]);
}
}
const req = new XMLHttpRequest();
req.open("POST", endpoint);
req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
req.onload = () => {
if(req.status >= 200 && req.status < 300) {
resolve(req.response);
} else {
reject(req.statusText);
}
};
req.onerror = () => reject(req.statusText);
req.send(JSON.stringify({
identification_code,
url: location.href.substr(0, 1800),
title: document.title,
referrer: document.referrer,
screen_width: screen.width,
screen_height: screen.height,
event_name: name,
event_duration: options && options.duration && typeof options.duration === "number" ? options.duration : 0,
event_meta: meta
}));
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment