From 1ce8ed008f127eea2d0d67b6331daeda09a242d9 Mon Sep 17 00:00:00 2001 From: Daniel Regeci <536331+ovx@users.noreply.github.com> Date: Sat, 12 Oct 2024 14:29:44 +0700 Subject: [PATCH] 0.1.1 --- dist_bundle/tracker.js | 2 +- package.json | 2 +- src/main.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dist_bundle/tracker.js b/dist_bundle/tracker.js index 6ed0e30..93be63f 100644 --- a/dist_bundle/tracker.js +++ b/dist_bundle/tracker.js @@ -1 +1 @@ -(function(n,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(n=typeof globalThis<"u"?globalThis:n||self,s(n.AltchaAnalyticsTracker={}))})(this,function(n){"use strict";var W=Object.defineProperty;var I=n=>{throw TypeError(n)};var $=(n,s,r)=>s in n?W(n,s,{enumerable:!0,configurable:!0,writable:!0,value:r}):n[s]=r;var a=(n,s,r)=>$(n,typeof s!="symbol"?s+"":s,r),R=(n,s,r)=>s.has(n)||I("Cannot "+r);var o=(n,s,r)=>(R(n,s,"read from private field"),r?r.call(n):s.get(n)),l=(n,s,r)=>s.has(n)?I("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(n):s.set(n,r),g=(n,s,r,A)=>(R(n,s,"write to private field"),A?A.call(n,r):s.set(n,r),r),V=(n,s,r)=>(R(n,s,"access private method"),r);var m,f,b,y,k,x,w,P,E,L,T,S,N,p,v,C,X,D,_;class s{constructor(i,t={disable:!1}){a(this,"lastEvent",null);this.tracker=i,this.options=t}destroy(){}getExitReason(i=!1){}isLastEventRecent(i=1e4,t=this.lastEvent){return!!t&&performance.now()-t.timeStamp=0&&this.offsetX>=0&&this.offsetX<150?void 0:""}}onMouseEnter(){this.isMouseOut=!1,this.offsetX=-1,this.offsetY=-1}onMouseLeave(t){this.isMouseOut=!0,this.offsetX=t.clientX,this.offsetY=t.clientY}}w=new WeakMap,P=new WeakMap;class j extends s{constructor(t,e){super(t);l(this,E,null);l(this,L,this.onPopState.bind(this));l(this,T,this.onPushState.bind(this));a(this,"lastPopStateEvent",null);const h=g(this,E,history.pushState);history.pushState=(O,q,G)=>{o(this,T).call(this),h==null||h.apply(history,[O,q,G])},addEventListener("popstate",o(this,L))}destroy(){o(this,E)&&(history.pushState=o(this,E)),removeEventListener("popstate",o(this,L))}onPopState(t){this.lastPopStateEvent=t,this.tracker.trackPageview()}onPushState(){this.tracker.trackPageview()}}E=new WeakMap,L=new WeakMap,T=new WeakMap;class F extends s{constructor(t,e){super(t);l(this,S,this.onVisibilityChange.bind(this));l(this,N);l(this,p,null);a(this,"visibilityState",document.visibilityState);g(this,N,e.hiddenTimeout||4e3),addEventListener("visibilitychange",o(this,S))}destroy(){removeEventListener("visibilitychange",o(this,S))}getExitReason(t=!1){if(t&&this.visibilityState==="hidden"&&this.lastEvent&&performance.now()-this.lastEvent.timeStamp>=1e3)return""}onTimeout(){document.visibilityState==="hidden"&&this.tracker.trackPageview({},!0)}onVisibilityChange(t){this.lastEvent=t,this.visibilityState=document.visibilityState,this.tracker.isMobile&&(o(this,p)&&clearTimeout(o(this,p)),document.visibilityState==="hidden"&&g(this,p,setTimeout(()=>{this.onTimeout()},o(this,N))))}}S=new WeakMap,N=new WeakMap,p=new WeakMap;const c=class c{constructor(i){l(this,C);l(this,v,this.onPageHide.bind(this));a(this,"isMobile",/Mobi|Android|iPhone|iPad|iPod|Opera Mini/i.test(navigator.userAgent||""));a(this,"events",[]);a(this,"extensions",{});a(this,"globalName",null);a(this,"lastPageLoadAt",performance.now());a(this,"lastPageview",null);a(this,"returningVisitor",null);a(this,"trackedEvents",0);a(this,"trackedPageviews",0);if(this.options=i,!i.projectId)throw new Error("Parameter projectId required.");V(this,C,X).call(this),this.isDNTEnabled&&this.options.respectDnt===!0?this.log("DoNotTrack enabled."):(this.loadExtensions(),addEventListener("pagehide",o(this,v)),addEventListener("beforeunload",o(this,v)))}get apiUrl(){return this.options.apiUrl||c.DEFAULT_API_URL}get isDNTEnabled(){return"doNotTrack"in navigator&&navigator.doNotTrack==="1"||"globalPrivacyControl"in navigator&&navigator.globalPrivacyControl===!0}destroy(){this.flushEvents();for(const i in this.extensions)this.extensions[i].destroy();this.extensions={},removeEventListener("pagehide",o(this,v)),removeEventListener("beforeunload",o(this,v)),this.globalName&&delete globalThis[this.globalName]}flushEvents(){const i=this.events.splice(0);i.length&&this.sendBeacon(i)}getBeaconPayload(i){return{events:i,projectId:this.options.projectId,time:Date.now(),uniqueId:this.options.uniqueId}}getExitReason(i=!1){for(const t in this.extensions){const e=this.extensions[t].getExitReason(i);if(e!==void 0)return this.log("exit reason:",{ext:t,result:e}),e}}getExtension(i){return this.extensions[i]}getHostname(){return location.hostname}getOrigin(){return location.origin}getView(){return this.sanitizeUrl(location.href)}getPageviewOptions(i=!1){const t=this.getExitReason(i),e=this.getReferrer(),h=this.getOrigin();return{appVersion:this.options.appVersion,exit:t!==void 0,outbound:t,duration:Math.max(0,Math.floor(performance.now()-this.lastPageLoadAt)),referrer:e&&new URL(e,h).origin===h?"":e,returning:this.returningVisitor===null?void 0:this.returningVisitor,view:this.getView()}}getReferrer(){return document.referrer}hasExtension(i){return!!this.getExtension(i)}loadExtensions(){var i;for(const t in c.EXTENSIONS){let e=((i=this.options)==null?void 0:i[t])!==void 0?this.options[t]:{};typeof e=="boolean"?e={disable:!e}:e.disable=e.disable===void 0?!c.DEFAULT_EXTENSIONS.includes(t):e.disable,e.disable!==!0&&(this.extensions[t]=new c.EXTENSIONS[t](this,e))}}log(...i){this.options.debug&&console.log("[ALTCHA Tracker]",...i)}sanitizeUrl(i){var t;if(i=new URL(i),(t=this.options.allowSearchParams)!=null&&t.length&&i.hostname===this.getHostname())for(const[e]of i.searchParams)this.options.allowSearchParams.includes(e)||i.searchParams.delete(e);else i.search="";return this.hasExtension("hash")||(i.hash=""),i.toString()}sendBeacon(i){if("sendBeacon"in navigator)return navigator.sendBeacon(this.apiUrl,JSON.stringify(this.getBeaconPayload(i)))}trackEvent(i={},t=!1){const e={timestamp:Date.now(),...i};return this.events.push(e),this.trackedEvents+=1,this.log("trackEvent",e),t&&this.flushEvents(),!0}trackPageview(i={},t=!1){const e=this.getPageviewOptions(t);return this.events.length&&e.duration<100&&this.events[this.events.length-1].view===e.view?(this.log("duplicate pageview",e),!1):(this.log("trackPageview",e),this.trackEvent({...e,...i},t),this.trackedPageviews+=1,this.lastPageLoadAt=performance.now(),this.lastPageview=e.view,e.exit&&(this.trackedPageviews=0),!0)}onPageHide(){this.lastPageview!==this.getView()&&this.trackPageview({},!0)}};v=new WeakMap,C=new WeakSet,X=function(){if(this.globalName=this.options.globalName===void 0?c.DEAFAUL_GLOBAL_NAME:this.options.globalName||null,this.globalName){if(globalThis[this.globalName])throw new Error("Another instance of the Tracker is already present in globalThis. Set globalName:null to disable global reference.");globalThis[this.globalName]=this}},a(c,"EXTENSIONS",{click:r,cookie:A,hash:H,keyboard:B,mouse:K,pushstate:j,visibility:F}),a(c,"DEFAULT_API_URL","https://eu.altcha.org/api/v1/event"),a(c,"DEFAULT_EXTENSIONS",["click","keyboard","mouse","pushstate","visibility"]),a(c,"DEAFAUL_GLOBAL_NAME","altchaTracker");let M=c;const u=document.currentScript,U=(_=(D=u==null?void 0:u.getAttribute("src"))==null?void 0:D.match(/([^\.]+)\.altcha\.org/))==null?void 0:_[1],z=((u==null?void 0:u.getAttributeNames())||[]).reduce((d,i)=>{if(i.startsWith("data-")){const t=i.slice(5).replace(/\-([a-z])/g,(h,O)=>O.toUpperCase());let e=u==null?void 0:u.getAttribute(i);e&&(e==="true"?e=!0:e==="false"&&(e=!1),d[t]=e)}return d},{}),Y=new M({apiUrl:U?`https://${U}.altcha.org/api/v1/event`:void 0,...z});n.tracker=Y,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}); +(function(n,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(n=typeof globalThis<"u"?globalThis:n||self,s(n.AltchaAnalyticsTracker={}))})(this,function(n){"use strict";var W=Object.defineProperty;var I=n=>{throw TypeError(n)};var $=(n,s,r)=>s in n?W(n,s,{enumerable:!0,configurable:!0,writable:!0,value:r}):n[s]=r;var a=(n,s,r)=>$(n,typeof s!="symbol"?s+"":s,r),R=(n,s,r)=>s.has(n)||I("Cannot "+r);var o=(n,s,r)=>(R(n,s,"read from private field"),r?r.call(n):s.get(n)),l=(n,s,r)=>s.has(n)?I("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(n):s.set(n,r),g=(n,s,r,A)=>(R(n,s,"write to private field"),A?A.call(n,r):s.set(n,r),r),V=(n,s,r)=>(R(n,s,"access private method"),r);var m,f,b,y,k,x,w,P,E,L,T,S,N,p,v,C,X,D,_;class s{constructor(i,t={disable:!1}){a(this,"lastEvent",null);this.tracker=i,this.options=t}destroy(){}getExitReason(i=!1){}isLastEventRecent(i=1e4,t=this.lastEvent){return!!t&&performance.now()-t.timeStamp=0&&this.offsetX>=0&&this.offsetX<150?void 0:""}}onMouseEnter(){this.isMouseOut=!1,this.offsetX=-1,this.offsetY=-1}onMouseLeave(t){this.isMouseOut=!0,this.offsetX=t.clientX,this.offsetY=t.clientY}}w=new WeakMap,P=new WeakMap;class j extends s{constructor(t,e){super(t);l(this,E,null);l(this,L,this.onPopState.bind(this));l(this,T,this.onPushState.bind(this));a(this,"lastPopStateEvent",null);const h=g(this,E,history.pushState);history.pushState=(O,q,G)=>{o(this,T).call(this),h==null||h.apply(history,[O,q,G])},addEventListener("popstate",o(this,L))}destroy(){o(this,E)&&(history.pushState=o(this,E)),removeEventListener("popstate",o(this,L))}onPopState(t){this.lastPopStateEvent=t,this.tracker.trackPageview()}onPushState(){this.tracker.trackPageview()}}E=new WeakMap,L=new WeakMap,T=new WeakMap;class F extends s{constructor(t,e){super(t);l(this,S,this.onVisibilityChange.bind(this));l(this,N);l(this,p,null);a(this,"visibilityState",document.visibilityState);g(this,N,e.hiddenTimeout||4e3),addEventListener("visibilitychange",o(this,S))}destroy(){removeEventListener("visibilitychange",o(this,S))}getExitReason(t=!1){if(t&&this.visibilityState==="hidden"&&this.lastEvent&&performance.now()-this.lastEvent.timeStamp>=1e3)return""}onTimeout(){document.visibilityState==="hidden"&&this.tracker.trackPageview({},!0)}onVisibilityChange(t){this.lastEvent=t,this.visibilityState=document.visibilityState,this.tracker.isMobile&&(o(this,p)&&clearTimeout(o(this,p)),document.visibilityState==="hidden"&&g(this,p,setTimeout(()=>{this.onTimeout()},o(this,N))))}}S=new WeakMap,N=new WeakMap,p=new WeakMap;const c=class c{constructor(i){l(this,C);l(this,v,this.onPageHide.bind(this));a(this,"isMobile",/Mobi|Android|iPhone|iPad|iPod|Opera Mini/i.test(navigator.userAgent||""));a(this,"events",[]);a(this,"extensions",{});a(this,"globalName",null);a(this,"lastPageLoadAt",performance.now());a(this,"lastPageview",null);a(this,"returningVisitor",null);a(this,"trackedEvents",0);a(this,"trackedPageviews",0);if(this.options=i,!i.projectId)throw new Error("Parameter projectId required.");V(this,C,X).call(this),this.isDNTEnabled&&this.options.respectDnt===!0?this.log("DoNotTrack enabled."):(this.loadExtensions(),addEventListener("pagehide",o(this,v)),addEventListener("beforeunload",o(this,v)))}get apiUrl(){return this.options.apiUrl||c.DEFAULT_API_URL}get isDNTEnabled(){return"doNotTrack"in navigator&&navigator.doNotTrack==="1"||"globalPrivacyControl"in navigator&&navigator.globalPrivacyControl===!0}destroy(){this.flushEvents();for(const i in this.extensions)this.extensions[i].destroy();this.extensions={},removeEventListener("pagehide",o(this,v)),removeEventListener("beforeunload",o(this,v)),this.globalName&&delete globalThis[this.globalName]}flushEvents(){const i=this.events.splice(0);i.length&&this.sendBeacon(i)}getBeaconPayload(i){return{events:i,projectId:this.options.projectId,time:Date.now(),uniqueId:this.options.uniqueId}}getExitReason(i=!1){for(const t in this.extensions){const e=this.extensions[t].getExitReason(i);if(e!==void 0)return this.log("exit reason:",{ext:t,result:e}),e}}getExtension(i){return this.extensions[i]}getHostname(){return location.hostname}getOrigin(){return location.origin}getView(){return this.sanitizeUrl(location.href)}getPageviewOptions(i=!1){const t=this.getExitReason(i),e=this.getReferrer(),h=this.getOrigin();return{appVersion:this.options.appVersion,exit:t!==void 0,outbound:t,duration:Math.max(0,Math.floor(performance.now()-this.lastPageLoadAt)),referrer:e&&new URL(e,h).origin===h?"":e,returning:this.returningVisitor===null?void 0:this.returningVisitor,view:this.getView()}}getReferrer(){return document.referrer}hasExtension(i){return!!this.getExtension(i)}loadExtensions(){var i;for(const t in c.EXTENSIONS){let e=((i=this.options)==null?void 0:i[t])!==void 0?this.options[t]:{};typeof e=="boolean"?e={disable:!e}:e.disable=e.disable===void 0?!c.DEFAULT_EXTENSIONS.includes(t):e.disable,e.disable!==!0&&(this.extensions[t]=new c.EXTENSIONS[t](this,e))}}log(...i){this.options.debug&&console.log("[ALTCHA Tracker]",...i)}sanitizeUrl(i){var t;if(i=new URL(i),(t=this.options.allowSearchParams)!=null&&t.length&&i.hostname===this.getHostname())for(const[e]of i.searchParams)this.options.allowSearchParams.includes(e)||i.searchParams.delete(e);else i.search="";return this.hasExtension("hash")||(i.hash=""),i.toString()}sendBeacon(i){if("sendBeacon"in navigator)return navigator.sendBeacon(this.apiUrl,JSON.stringify(this.getBeaconPayload(i)))}trackEvent(i={},t=!1){const e={timestamp:Date.now(),...i};return this.events.push(e),this.trackedEvents+=1,this.log("trackEvent",e),t&&this.flushEvents(),!0}trackPageview(i={},t=!1){const e=this.getPageviewOptions(t);return this.events.length&&e.duration<100&&this.events[this.events.length-1].view===e.view?(this.log("duplicate pageview",e),!1):(this.log("trackPageview",e),this.trackEvent({...e,...i},t),this.trackedPageviews+=1,this.lastPageLoadAt=performance.now(),this.lastPageview=e.view,e.exit&&(this.trackedPageviews=0),!0)}onPageHide(){this.lastPageview!==this.getView()&&this.trackPageview({},!0)}};v=new WeakMap,C=new WeakSet,X=function(){if(this.globalName=this.options.globalName===void 0?c.DEAFAUL_GLOBAL_NAME:this.options.globalName||null,this.globalName){if(globalThis[this.globalName])throw new Error("Another instance of the Tracker is already present in globalThis. Set globalName:null to disable global reference.");globalThis[this.globalName]=this}},a(c,"EXTENSIONS",{click:r,cookie:A,hash:H,keyboard:B,mouse:K,pushstate:j,visibility:F}),a(c,"DEFAULT_API_URL","https://eu.altcha.org/api/v1/event"),a(c,"DEFAULT_EXTENSIONS",["click","keyboard","mouse","pushstate","visibility"]),a(c,"DEAFAUL_GLOBAL_NAME","altchaTracker");let M=c;const u=document.currentScript,U=(_=(D=u==null?void 0:u.getAttribute("src"))==null?void 0:D.match(/([\w\-]+)\.altcha\.org/))==null?void 0:_[1],z=((u==null?void 0:u.getAttributeNames())||[]).reduce((d,i)=>{if(i.startsWith("data-")){const t=i.slice(5).replace(/\-([a-z])/g,(h,O)=>O.toUpperCase());let e=u==null?void 0:u.getAttribute(i);e&&(e==="true"?e=!0:e==="false"&&(e=!1),d[t]=e)}return d},{}),Y=new M({apiUrl:U?`https://${U}.altcha.org/api/v1/event`:void 0,...z});n.tracker=Y,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}); diff --git a/package.json b/package.json index d433507..5dde06f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@altcha/tracker", "description": "JavaScript client for ALTCHA Analytics.", - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "author": { "name": "Daniel Regeci", diff --git a/src/main.ts b/src/main.ts index 4d48d5f..198742b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import { Tracker } from './tracker'; import type { ITrackerOptions } from './types'; const currentScript = document.currentScript; -const region = currentScript?.getAttribute('src')?.match(/([^\.]+)\.altcha\.org/)?.[1]; +const region = currentScript?.getAttribute('src')?.match(/([\w\-]+)\.altcha\.org/)?.[1]; const attrs = currentScript?.getAttributeNames() || []; const options = attrs.reduce((acc, attr) => { if (attr.startsWith('data-')) {