From 9e60765713bba4bc985a6d3b8a64072504e82ce0 Mon Sep 17 00:00:00 2001 From: Adriaan van Rossum Date: Thu, 14 Nov 2019 14:42:47 +0700 Subject: [PATCH] Add unique page views to our scripts https://github.com/simpleanalytics/roadmap/issues/12 --- dist/default.js | 2 +- dist/external.js | 2 +- minify.js | 27 ++++++++++++++++++++++----- package-lock.json | 5 +++++ package.json | 4 +++- src/default.js | 46 ++++++++++++++++++++++++++++++---------------- src/external.js | 33 ++++++++++++++++++++++++++------- 7 files changed, 88 insertions(+), 31 deletions(-) diff --git a/dist/default.js b/dist/default.js index e33117c..97e343b 100644 --- a/dist/default.js +++ b/dist/default.js @@ -1,3 +1,3 @@ /* Simple Analytics - Privacy friend analytics (docs.simpleanalytics.com/script) */ -!function(s){if(s){var r,n,e="https://",c=e+"api.simpleanalytics.io/post",t=s.console;try{function h(e){t&&t.warn&&t.warn("Simple Analytics: "+e)}var u,l=s.navigator,p=s.location,d=s.document,f=l.userAgent,a=s.dispatchEvent,m="Not sending requests ",i=d.querySelector('script[src="https://cdn.simpleanalytics.io/hello.js"]'),v=i?i.getAttribute("data-mode"):null;if(-1"); \ No newline at end of file +!function(s,e){if(s){function c(e){t&&t.warn&&t.warn("Simple Analytics: "+e)}var n,a,p=s.navigator,l=s.location,u=l.hostname,h=s.document,t=s.console,f=s.performance,g="//"+e,i="https://"+e;try{function r(e,t){return e&&e.getAttribute("data-"+t)}var m,v=p.userAgent,o=s.dispatchEvent,d="Not sending requests ",y=h.querySelector('script[src$="'+g+'/app.js"]'),w=r(y,"mode"),E="true"===r(y,"skip-dnt"),T=r(y,"sa-global")||"sa";if("localhost"===u)return c(d+"from localhost");if(/(bot|spider|crawl)/i.test(v))return c(d+"because user agent is a robot");function b(e){var t=l.search.match(new RegExp("[?&]("+e+")=([^?&]+)","gi")),n=t?t.map(function(e){return e.split("=")[1]}):[];if(n&&n[0])return n[0]}function R(e){var t=l.protocol+"//"+u+l.pathname;if("hash"===w&&l.hash&&(t+=l.hash.split("?")[0]),m!==t){if(m=t,!E&&"doNotTrack"in p&&"1"===p.doNotTrack)return c(d+"when doNotTrack is enabled");var n={url:t};v&&(n.ua=v),S&&(n.urlReferrer=S),x&&!e&&(n.referrer=x),s.innerWidth&&(n.width=s.innerWidth);try{var a=f&&f.getEntriesByType&&f.getEntriesByType("navigation")[0]&&f.getEntriesByType("navigation")[0].type?-1<["reload","back_forward"].indexOf(f.getEntriesByType("navigation")[0].type):null,r="boolean"==typeof a?a:f&&f.navigation&&f.navigation.type&&-1<[f.navigation.TYPE_RELOAD,f.navigation.TYPE_BACK_FORWARD].indexOf(f.navigation.type);n.unique=!e&&!r&&(h.referrer&&h.referrer.split("/")[2]!==l.hostname)}catch(o){}try{n.timezone=Intl.DateTimeFormat().resolvedOptions().timeZone}catch(o){}var i=new XMLHttpRequest;i.open("POST",g+"/api",!0),i.setRequestHeader("Content-Type","text/plain; charset=UTF-8"),i.send(JSON.stringify(n))}}var S=b("utm_source|source|ref"),q=b("utm_campaign|campaign"),x=h.referrer.replace(/^https?:\/\/((m|l|w{2,3}([0-9]+)?)\.)?([^?#]+)(.*)$/,"$4").replace(/^([^/]+)\/$/,"$1")||null,A=s.history;if((A?A.pushState:null)&&Event&&o){A.pushState=(a=A[n="pushState"],function(){var e=a.apply(this,arguments),t=new Event(n);return t.arguments=arguments,o(t),e}),s.addEventListener("pushState",function(){R(!0)})}"hash"===w&&"onhashchange"in s&&(s.onhashchange=R),R();var O=/\.(.+)/.exec(e)[1];if(u!==O&&!new RegExp("."+O+"$","i").test(u))return c("Events via this script only work on "+O+" domains");function k(){_=!0;var r=h.createElement("iframe");r.setAttribute("src",g+"/iframe.html"),r.style.display="none",r.onload=function(){var t=r.contentWindow,n=S||x;try{if($)for(var e=0;e<$.length;e++)t.postMessage({event:$[e][0],ref:n,campaign:q},i)}catch(a){}s[T]=function(e){t.postMessage({event:e,ref:n,campaign:q},i)}},h.body.appendChild(r)}var $=s[T]&&s[T].q?s[T].q:[],_=!1;s[T]=function(){_||k(),$.push([].slice.call(arguments))},!_&&$.length&&k()}catch(N){c(N.message);var B=g+"/image.gif";N.message&&(B=B+"?error="+encodeURIComponent(N.message)),(new Image).src=B}}}(window,""); \ No newline at end of file diff --git a/minify.js b/minify.js index 094427c..94b314f 100644 --- a/minify.js +++ b/minify.js @@ -1,30 +1,35 @@ const UglifyJS = require('uglify-js') const fs = require('fs') +const esprima = require('esprima') const green = '\x1b[32m%s\x1b[0m' const yellow = '\x1b[33m%s\x1b[0m' -// const red = '\x1b[31m%s\x1b[0m' +const red = '\x1b[31m%s\x1b[0m' const files = { default: { + type: 'js', original: `${__dirname}/src/default.js`, minified: `${__dirname}/dist/default.js`, prepend: ['/* Simple Analytics - Privacy friend analytics (docs.simpleanalytics.com/script) */', ''], append: [] }, embed: { + type: 'js', original: `${__dirname}/src/embed.js`, minified: `${__dirname}/dist/embed.js`, prepend: ['/* Simple Analytics - Privacy friend analytics (docs.simpleanalytics.com/embed-graph-on-your-site) */', ''], append: [] }, external: { + type: 'js', original: `${__dirname}/src/external.js`, minified: `${__dirname}/dist/external.js`, prepend: ['/* Simple Analytics - Privacy friend analytics (docs.simpleanalytics.com/script) */', ''], append: [] }, iframe: { + type: 'html', original: `${__dirname}/src/iframe.js`, minified: `${__dirname}/dist/iframe.html`, prepend: ['', 'SA', '