From f90ea024e962e65e479f321fa39cbe64c178e0ec Mon Sep 17 00:00:00 2001 From: severak Date: Thu, 16 Jan 2020 00:20:03 +0100 Subject: [PATCH] working counter script --- README.md | 4 ++-- index.html | 2 +- millions_v1.js | 63 ++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ee4b352..11c2949 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# milions +# Millions -non-tracking web counter/analytics inspired by plausible.io and Simple analytics \ No newline at end of file +a non-tracking web counter/analytics inspired by plausible.io and Simple analytics \ No newline at end of file diff --git a/index.html b/index.html index 5f82e3a..3ad1eed 100644 --- a/index.html +++ b/index.html @@ -10,6 +10,6 @@

ping

- + \ No newline at end of file diff --git a/millions_v1.js b/millions_v1.js index 8902ff4..02c6f75 100644 --- a/millions_v1.js +++ b/millions_v1.js @@ -1,14 +1,59 @@ -/* Milions non-tracking web counter/analytics */ -(function(){ +/* Millions - a non-tracking web counter/analytics */ +(function(counterHost){ + if (!window) return; + + if ("doNotTrack" in navigator && navigator["doNotTrack"] === "1") return; + // inspired by: // https://github.com/simpleanalytics/scripts/blob/master/src/default.js // https://docs.simpleanalytics.com/what-we-collect // https://docs.simpleanalytics.com/uniques - // https://christianheilmann.com/2015/12/25/detecting-adblock-without-an-extra-http-overhead/ - - console.log('href', document.location.href); - console.log('ref', document.referrer); - console.log('DNT', "doNotTrack" in navigator && navigator["doNotTrack"] === "1"); - console.log('innerWidth', window.innerWidth); - console.log('UA', navigator.userAgent); + + var getLocation = function(href) { + var match = href.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/); + return match && { + href: href, + protocol: match[1], + host: match[2], + hostname: match[3], + port: match[4], + pathname: match[5], + search: match[6], + hash: match[7] + } || { href: href }; + } + var parseQuery = function(queryString) { + var query = {}; + var pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&'); + for (var i = 0; i < pairs.length; i++) { + var pair = pairs[i].split('='); + query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || ''); + } + return query; + } + var selfUrl = getLocation(document.location.href); + // commented out for now // if (selfUrl=='localhost') return; + var refUrl = getLocation(document.referrer); + + var bonzUrl = selfUrl.protocol + '//' + counterHost + '/one.php?for=' + selfUrl.hostname + '&path=' + encodeURIComponent(selfUrl.pathname) + '&width=' + window.innerWidth; + + if (refUrl.href=='' || (refUrl.hostname && selfUrl.hostname != refUrl.hostname)) { + bonzUrl += '&unique=1'; + } + + var Q = parseQuery(selfUrl.search); + + if (Q.utm_source || Q.ref || Q.pk_campaign) { + bonzUrl += '&ref=' + encodeURIComponent(Q.utm_source || Q.ref || Q.pk_campaign); + } else if (Q.fbclid) { + bonzUrl += '&ref=facebook'; + } else if (refUrl.hostname && selfUrl.hostname != refUrl.hostname) { + bonzUrl += '&ref=' + encodeURIComponent(refUrl.hostname + refUrl.pathname); + } + + console.log(refUrl.hostname); + var req = new XMLHttpRequest(); + req.open("GET", bonzUrl, true); + req.send(null); + // we don't care about response yet })('localhost'); \ No newline at end of file