@carter-rmn/cpix-js 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENCE ADDED
@@ -0,0 +1,32 @@
1
+ Proprietary License
2
+
3
+ This software is licensed under the terms of this Proprietary License (the "License"). By using this software, you agree to comply with the terms of this License.
4
+
5
+ 1. Grant of License
6
+ This software is licensed, not sold, to you by Carter Analytics for use only under the terms of this License. Carter Analytics retains ownership of the software itself and reserves all rights not expressly granted to you.
7
+
8
+ 2. Permitted Use
9
+ You are granted a non-exclusive, non-transferable, limited license to use the software solely for your personal or internal business purposes, provided you have an active subscription to Carter Analytics's paid supporting service.
10
+
11
+ 3. Restrictions
12
+ You may not:
13
+ a. Redistribute, sell, lease, sublicense, or otherwise transfer the software or any portion thereof.
14
+ b. Modify, adapt, translate, reverse engineer, decompile, or disassemble the software.
15
+ c. Remove or alter any proprietary notices or labels on the software.
16
+
17
+ 4. Intellectual Property
18
+ All intellectual property rights in and to the software are and shall remain the exclusive property of Carter Analytics.
19
+
20
+ 5. Disclaimer of Warranties
21
+ The software is provided "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose.
22
+
23
+ 6. Limitation of Liability
24
+ In no event shall Carter Analytics be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or any other pecuniary loss) arising out of the use of or inability to use this software, even if Carter Analytics has been advised of the possibility of such damages.
25
+
26
+ 7. Termination
27
+ This License is effective until terminated. Your rights under this License will terminate automatically without notice from Carter Analytics if you fail to comply with any term(s) of this License. Upon termination, you shall cease all use of the software and destroy all copies, full or partial, of the software.
28
+
29
+ 8. Governing Law
30
+ This License shall be governed by and construed in accordance with the laws of [Your Jurisdiction].
31
+
32
+ By using this software, you acknowledge that you have read this License, understand it, and agree to be bound by its terms and conditions.
@@ -6,6 +6,8 @@ export declare const CONFIG: {
6
6
  QUEUE_INITIAL_DELAY: number;
7
7
  version: string;
8
8
  package: string;
9
+ USER_PROPERTIES: {};
10
+ SESSION_EXPIRY_DAYS: number;
9
11
  };
10
12
  export declare const COOKIE: {
11
13
  SESSION: string;
package/dist/cpix.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var v=Object.defineProperty;var E=(u,e,t)=>e in u?v(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var o=(u,e,t)=>(E(u,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S="@carter-rmn/cpix-js",x="1.0.2",b="module",A=["dist"],P="dist/cpix.umd.cjs",T="dist/cpix.js",U="dist/cpix.d.ts",_={".":{import:"./dist/cpix.js",require:"./dist/cpix.umd.cjs"}},C={dev:"vite",build:"tsc && vite build",preview:"vite preview"},D={picocolors:"^1.0.0","rollup-plugin-visualizer":"^5.12.0",typescript:"^5.2.2",vite:"^5.2.0","vite-plugin-progress":"^0.0.7"},I={"vite-plugin-dts":"^3.8.1"},m={name:S,version:x,type:b,files:A,main:P,module:T,types:U,exports:_,scripts:C,devDependencies:D,dependencies:I},l={DEBUG:!1,TRACKER_FUNC_NAME:"cpix",API_URL:"",QUEUE_MAX_RETRIES:3,QUEUE_INITIAL_DELAY:1e4,version:m.version,package:m.name},c={SESSION:"cpix_session",META_PARAMETERS:"cpix_meta_parameters",ACCESS_TOKEN:"cpix_access_token",UTM:"cpix_utm"},k=["utm_source","utm_medium","utm_term","utm_content","utm_campaign"],a=function(){const u=()=>l.DEBUG;return{info:function(...e){console.log(...e)},debug:function(...e){u()&&console.log(...e)},error:function(...e){console.error(...e)}}}();class N extends EventTarget{constructor(t={maxRetries:3,initialDelay:1e4,consumerHandler:i=>(a.debug("Consuming event:",i),Promise.resolve())}){super();o(this,"dataQueue");o(this,"retryQueue");o(this,"maxRetries");o(this,"initialDelay");o(this,"publishEvent");o(this,"retryEvent");o(this,"handler");this.dataQueue=[],this.retryQueue=[],this.maxRetries=t.maxRetries,this.initialDelay=t.initialDelay,this.handler=t.consumerHandler,this.publishEvent=new Event("eventPublished"),this.retryEvent=new Event("retryMessage"),this.consume(),this.retry()}publish(t){a.debug("publishing event");const i={data:t,id:`${Date.now()+Math.random()}`,retryCount:0,retryDelay:this.initialDelay};this.dataQueue.push(i),this.dispatchEvent(this.publishEvent)}consume(){a.debug("Started Consumer Queue"),this.addEventListener("eventPublished",async()=>{for(;this.dataQueue.length>0;){const t=this.dataQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(s){a.error("Failed to publish event:",s),this.scheduleRetry(t)}}})}scheduleRetry(t){if(t.retryCount>=this.maxRetries){a.error("Max retries reached for message:",t);return}setTimeout(()=>{this.retryQueue.push({...t,retryCount:t.retryCount+1,retryDelay:t.retryDelay*2}),this.dispatchEvent(this.retryEvent)},t.retryDelay)}acknowledge(t){this.dataQueue=this.dataQueue.filter(i=>i.id!==t)}retry(){a.debug("Started Retry Queue"),this.addEventListener("retryMessage",async()=>{for(;this.retryQueue.length>0;){const t=this.retryQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(s){a.error("Failed to publish event:",s),this.scheduleRetry(t)}}})}}class h{static getBrowserInfo(){var s,n,p,f,g;const e=navigator.userAgent||"";let t,i;return/chrome/i.test(e)?(t="Chrome",i=(s=e.match(/chrome\/(\d+)/i))==null?void 0:s[1]):/firefox/i.test(e)?(t="Firefox",i=(n=e.match(/firefox\/(\d+)/i))==null?void 0:n[1]):/safari/i.test(e)?(t="Safari",i=(p=e.match(/version\/(\d+)/i))==null?void 0:p[1]):/edge/i.test(e)?(t="Edge",i=(f=e.match(/edge\/(\d+)/i))==null?void 0:f[1]):/trident/i.test(e)?(t="Internet Explorer",i=(g=e.match(/rv:(\d+)/i))==null?void 0:g[1]):(t="Unknown",i="Unknown"),`${t} ${i}`}static getDeviceCategory(){const e=navigator.userAgent;return/mobile/i.test(e)?"Mobile":/tablet/i.test(e)?"Tablet":"Desktop"}static getDevicePlatform(){const e=navigator.userAgent;return/android/i.test(e)?"Android":/iphone|ipad|ipod/i.test(e)?"iOS":/windows phone/i.test(e)?"Windows Phone":/mac|Macintosh/i.test(e)?"Mac":/windows|Microsoft/i.test(e)?"Windows":/linux/i.test(e)?"Linux":"Unknown"}static getDeviceManufacturer(){const e=navigator.userAgent;let t;return/iphone|ipad|ipod|mac|Macintosh/i.test(e)?t="Apple":/samsung/i.test(e)?t="Samsung":/google/i.test(e)?t="Google":/huawei/i.test(e)?t="Huawei":/xiaomi/i.test(e)?t="Xiaomi":/oneplus/i.test(e)?t="OnePlus":/dell/i.test(e)?t="Dell":/lenovo/i.test(e)?t="Lenovo":/acer/i.test(e)?t="Acer":/asus/i.test(e)?t="Asus":/toshiba/i.test(e)?t="Toshiba":t="Unknown",t}static async getDeviceNetworkParameters(){try{const e=await fetch(`${l.API_URL}/api/geolocation`),{data:t}=await e.json();return{city:t.city||"Unknown",region:t.region||"Unknown",country:t.country||"Unknown",timezone:t.timezone||"Unknown",loc:t.loc||"Unknown",ip_address:t.ip_address||"Unknown"}}catch(e){return a.error("Error retrieving device network parameters:",e),{city:"Unknown",region:"Unknown",country:"Unknown",timezone:"Unknown",loc:"Unknown",ip_address:"Unknown"}}}static async aquireAccessToken(e){try{return(await(await fetch(`${l.API_URL}/api/authenticate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:e,metadata:{sdk:l.package,version:l.version,hostname:window.location.hostname}})})).json()).accessToken}catch(t){throw a.error("Error acquiring access token:",t),new Error("[CPIX] Authentication error")}}}class d{static isPresent(e){return typeof e<"u"&&e!==null&&e!==""}static now(){return 1*new Date().getTime()}static guid(){return m.version+"-xxxxxxxx-".replace(/[x]/g,function(e){const t=Math.random()*36|0;return(e=="x"?t:t&3|8).toString(36)})+(1*new Date().getTime()).toString(36)}static optionalData(e){return d.isPresent(e)===!1?"":typeof e=="object"?d.optionalData(JSON.stringify(e)):typeof e=="function"?d.optionalData(e()):String(e)}static sleep(e){return new Promise(t=>setTimeout(t,e))}}class w{static getParametersByName(e,t){t||(t=window.location.href),e=e.replace(/[[\]]/g,"\\$&");const i=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)","gi"),s=[];let n;for(;(n=i.exec(t))!==null;)n[2]&&s.push(decodeURIComponent(n[2].replace(/\+/g," ")));return s.length>0?s:null}static externalHost(e){var t;return e.hostname!=location.hostname&&((t=e==null?void 0:e.protocol)==null?void 0:t.indexOf("http"))===0}}class r{static prefix(){return`__${l.TRACKER_FUNC_NAME}__`}static get(e){const t=`${r.prefix()}${e}`,i=document.cookie.split("; ").find(s=>s.startsWith(`${t}=`));return i?i.split("=")[1]:void 0}static set(e,t,i){const s=`${r.prefix()}${e}`,n=new Date;n.setTime(n.getTime()+i*60*1e3),document.cookie=`${s}=${t}; expires=${n.toUTCString()}; path=/`}static delete(e){const t=`${r.prefix()}${e}`;document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`}static clear(){document.cookie.split("; ").filter(e=>e.startsWith(r.prefix())).forEach(e=>{const t=e.split("=")[0];document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`})}static exists(e){return!!r.get(e)}static setUtms(){const e=k;let t=!1;for(let i=0;i<e.length;i++){const s=e[i];if(d.isPresent(w.getParametersByName(s,window.location.href))){t=!0;break}}if(t){let i;const s=[];for(let n=0;n<e.length;n++){const p=e[n];i=w.getParametersByName(p,window.location.href),s.push({[p]:i})}r.set(c.UTM,JSON.stringify(s),14*24*60)}}static getUtms(){const e=r.get(c.UTM);return d.isPresent(e)?JSON.parse(e):{}}static createSession(){r.exists(c.SESSION)||r.set(c.SESSION,d.guid(),14*24*60),r.setUtms()}}class R{constructor(){o(this,"instances",[]);o(this,"initialized",!1);o(this,"accessToken","");o(this,"metaParameters",{});o(this,"queue");if(!window)throw new Error("[CPIX] CarterAnalytics SDK can only be used in a browser environment");this.queue=new N({maxRetries:l.QUEUE_MAX_RETRIES,initialDelay:l.QUEUE_INITIAL_DELAY,consumerHandler:this.publishEventToServer.bind(this)})}async initialize(e){if(!e||e.length===0)throw new Error("[CPIX] At least one instance configuration is required for initialization");if(this.instances=e,l.DEBUG=this.instances[0].options.debug===!0,l.API_URL=this.instances[0].options.tracker_server_url,r.exists(c.SESSION)||r.createSession(),r.exists(c.META_PARAMETERS)||await this.generateMetaParameters().then(t=>{this.metaParameters=t,r.set("metaParameters",JSON.stringify(t),60*24)}),r.setUtms(),this.metaParameters=JSON.parse(r.get("metaParameters")),this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has already been initialized");if(!this.metaParameters)throw new Error("[CPIX] Meta parameters have not been generated");r.exists(c.ACCESS_TOKEN)||(this.accessToken=await h.aquireAccessToken(this.instances[0].client_id),r.set(c.ACCESS_TOKEN,this.accessToken,60*4)),this.initialized=!0,a.debug("CPIX Initialized"),a.debug("Carter Analytics SDK initialized with options:",this.instances),this.publish({event:"init",...this.metaParameters}),this.publish({event:"page_view",event_properties:{title:document.title,page:window.location.pathname,url:window.location.href},...this.metaParameters})}publish(e){if(!this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has not been initialized. Please initialize before publishing events.");const t={...e,...this.metaParameters};t.utm_params=r.getUtms(),document.referrer&&(t.referrer=encodeURIComponent(document.referrer)),a.debug("Publishing event:",t),this.queue.publish(t),e.event==="logout"&&(r.delete(c.SESSION),r.createSession(),this.generateMetaParameters().then(i=>{this.metaParameters=i,r.set(c.META_PARAMETERS,JSON.stringify(this.metaParameters),60*24)}))}async generateMetaParameters(){if(!window)throw new Error("[CPIX] Meta parameters can only be generated in a browser environment");const e=await h.getDeviceNetworkParameters();return{client_id:this.instances[0].client_id,session:r.get(c.SESSION),location:{city:e.city,region:e.region,loc:e.loc,timezone:e.timezone,country:e.country},device:{category:h.getDeviceCategory(),brand:h.getDeviceManufacturer(),ip_address:e.ip_address,platform:h.getDevicePlatform()},referrer:encodeURIComponent(document.referrer)}}async publishEventToServer(e){try{e.timestamp=Date.now(),e.event_properties={url:window.location.href,...e.event_properties};const t=await fetch(`${l.API_URL}/api/event`,{method:"POST",body:JSON.stringify({event_data:e}),headers:{"Content-Type":"application/json",Authorization:`Bearer ${r.get(c.ACCESS_TOKEN)}`}});if(!t.ok)throw t.status===401&&(this.accessToken=await h.aquireAccessToken(this.instances[0].client_id),r.set(c.ACCESS_TOKEN,this.accessToken,60*4)),new Error("[CPIX] Failed to publish event");a.debug("Event published:",t.ok)}catch(t){throw a.error("Failed to publish event:",t),new Error("[CPIX] Failed to publish event")}}}const y=new R;typeof window<"u"&&(window.cpix=y);exports.CarterAnalytics=y;
1
+ "use strict";var E=Object.defineProperty;var S=(l,e,t)=>e in l?E(l,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):l[e]=t;var o=(l,e,t)=>(S(l,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v="@carter-rmn/cpix-js",P="1.0.3",_="module",x=["dist"],b="dist/cpix.umd.cjs",A="dist/cpix.js",T="dist/cpix.d.ts",U={".":{import:"./dist/cpix.js",require:"./dist/cpix.umd.cjs"}},I={dev:"vite",build:"tsc && vite build",preview:"vite preview"},R={picocolors:"^1.0.0","rollup-plugin-visualizer":"^5.12.0",typescript:"^5.2.2",vite:"^5.2.0","vite-plugin-progress":"^0.0.7"},D={"vite-plugin-dts":"^3.8.1"},m={name:v,version:P,type:_,files:x,main:b,module:A,types:T,exports:U,scripts:I,devDependencies:R,dependencies:D},n={DEBUG:!1,TRACKER_FUNC_NAME:"cpix",API_URL:"https://etg20oxcge.execute-api.ca-central-1.amazonaws.com/dev-lambda",QUEUE_MAX_RETRIES:3,QUEUE_INITIAL_DELAY:1e4,version:m.version,package:m.name,USER_PROPERTIES:{},SESSION_EXPIRY_DAYS:14},u={SESSION:"cpix_session",META_PARAMETERS:"cpix_meta_parameters",ACCESS_TOKEN:"cpix_access_token",UTM:"cpix_utm"},C=["utm_source","utm_medium","utm_term","utm_content","utm_campaign"],c=function(){const l=()=>n.DEBUG;return{info:function(...e){console.log(...e)},debug:function(...e){l()&&console.log(...e)},error:function(...e){console.error(...e)}}}();class N extends EventTarget{constructor(t={maxRetries:3,initialDelay:1e4,consumerHandler:i=>(c.debug("Consuming event:",i),Promise.resolve())}){super();o(this,"dataQueue");o(this,"retryQueue");o(this,"maxRetries");o(this,"initialDelay");o(this,"publishEvent");o(this,"retryEvent");o(this,"handler");this.dataQueue=[],this.retryQueue=[],this.maxRetries=t.maxRetries,this.initialDelay=t.initialDelay,this.handler=t.consumerHandler,this.publishEvent=new Event("eventPublished"),this.retryEvent=new Event("retryMessage"),this.consume(),this.retry()}publish(t){c.debug("publishing event");const i={data:t,id:`${Date.now()+Math.random()}`,retryCount:0,retryDelay:this.initialDelay};this.dataQueue.push(i),this.dispatchEvent(this.publishEvent)}consume(){c.debug("Started Consumer Queue"),this.addEventListener("eventPublished",async()=>{for(;this.dataQueue.length>0;){const t=this.dataQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(r){c.error("Failed to publish event:",r),this.scheduleRetry(t)}}})}scheduleRetry(t){if(t.retryCount>=this.maxRetries){c.error("Max retries reached for message:",t);return}setTimeout(()=>{this.retryQueue.push({...t,retryCount:t.retryCount+1,retryDelay:t.retryDelay*2}),this.dispatchEvent(this.retryEvent)},t.retryDelay)}acknowledge(t){this.dataQueue=this.dataQueue.filter(i=>i.id!==t)}retry(){c.debug("Started Retry Queue"),this.addEventListener("retryMessage",async()=>{for(;this.retryQueue.length>0;){const t=this.retryQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(r){c.error("Failed to publish event:",r),this.scheduleRetry(t)}}})}}class d{static getBrowserInfo(){var r,a,p,f,g;const e=navigator.userAgent||"";let t,i;return/chrome/i.test(e)?(t="Chrome",i=(r=e.match(/chrome\/(\d+)/i))==null?void 0:r[1]):/firefox/i.test(e)?(t="Firefox",i=(a=e.match(/firefox\/(\d+)/i))==null?void 0:a[1]):/safari/i.test(e)?(t="Safari",i=(p=e.match(/version\/(\d+)/i))==null?void 0:p[1]):/edge/i.test(e)?(t="Edge",i=(f=e.match(/edge\/(\d+)/i))==null?void 0:f[1]):/trident/i.test(e)?(t="Internet Explorer",i=(g=e.match(/rv:(\d+)/i))==null?void 0:g[1]):(t="Unknown",i="Unknown"),`${t} ${i}`}static getDeviceCategory(){const e=navigator.userAgent;return/mobile/i.test(e)?"Mobile":/tablet/i.test(e)?"Tablet":"Desktop"}static getDevicePlatform(){const e=navigator.userAgent;return/android/i.test(e)?"Android":/iphone|ipad|ipod/i.test(e)?"iOS":/windows phone/i.test(e)?"Windows Phone":/mac|Macintosh/i.test(e)?"Mac":/windows|Microsoft/i.test(e)?"Windows":/linux/i.test(e)?"Linux":"Unknown"}static getDeviceManufacturer(){const e=navigator.userAgent;let t;return/iphone|ipad|ipod|mac|Macintosh/i.test(e)?t="Apple":/samsung/i.test(e)?t="Samsung":/google/i.test(e)?t="Google":/huawei/i.test(e)?t="Huawei":/xiaomi/i.test(e)?t="Xiaomi":/oneplus/i.test(e)?t="OnePlus":/dell/i.test(e)?t="Dell":/lenovo/i.test(e)?t="Lenovo":/acer/i.test(e)?t="Acer":/asus/i.test(e)?t="Asus":/toshiba/i.test(e)?t="Toshiba":t="Unknown",t}static async getDeviceNetworkParameters(){try{const e=await fetch(`${n.API_URL}/api/geolocation`),{data:t}=await e.json();return{city:t.city||"Unknown",region:t.region||"Unknown",country:t.country||"Unknown",timezone:t.timezone||"Unknown",loc:t.loc||"Unknown",ip_address:t.ip_address||"Unknown"}}catch(e){return c.error("Error retrieving device network parameters:",e),{city:"Unknown",region:"Unknown",country:"Unknown",timezone:"Unknown",loc:"Unknown",ip_address:"Unknown"}}}static async aquireAccessToken(e){try{return(await(await fetch(`${n.API_URL}/api/authenticate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:e,metadata:{sdk:n.package,version:n.version,hostname:window.location.hostname}})})).json()).accessToken}catch(t){throw c.error("Error acquiring access token:",t),new Error("[CPIX] Authentication error")}}}class h{static isPresent(e){return typeof e<"u"&&e!==null&&e!==""}static now(){return 1*new Date().getTime()}static guid(){return m.version+"-xxxxxxxx-".replace(/[x]/g,function(e){const t=Math.random()*36|0;return(e=="x"?t:t&3|8).toString(36)})+(1*new Date().getTime()).toString(36)}static optionalData(e){return h.isPresent(e)===!1?"":typeof e=="object"?h.optionalData(JSON.stringify(e)):typeof e=="function"?h.optionalData(e()):String(e)}static sleep(e){return new Promise(t=>setTimeout(t,e))}}class w{static getParametersByName(e,t){t||(t=window.location.href),e=e.replace(/[[\]]/g,"\\$&");const i=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)","gi"),r=[];let a;for(;(a=i.exec(t))!==null;)a[2]&&r.push(decodeURIComponent(a[2].replace(/\+/g," ")));return r.length>0?r:null}static externalHost(e){var t;return e.hostname!=location.hostname&&((t=e==null?void 0:e.protocol)==null?void 0:t.indexOf("http"))===0}}class s{static prefix(){return`__${n.TRACKER_FUNC_NAME}__`}static get(e){const t=`${s.prefix()}${e}`,i=document.cookie.split("; ").find(r=>r.startsWith(`${t}=`));return i?i.split("=")[1]:void 0}static set(e,t,i){const r=`${s.prefix()}${e}`,a=new Date;a.setTime(a.getTime()+i*60*1e3),document.cookie=`${r}=${t}; expires=${a.toUTCString()}; path=/`}static delete(e){const t=`${s.prefix()}${e}`;document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`}static clear(){document.cookie.split("; ").filter(e=>e.startsWith(s.prefix())).forEach(e=>{const t=e.split("=")[0];document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`})}static exists(e){return!!s.get(e)}static setUtms(){const e=C;let t=!1;for(let i=0;i<e.length;i++){const r=e[i];if(h.isPresent(w.getParametersByName(r,window.location.href))){t=!0;break}}if(t){let i;const r=[];for(let a=0;a<e.length;a++){const p=e[a];i=w.getParametersByName(p,window.location.href),r.push({[p]:i})}s.set(u.UTM,JSON.stringify(r),n.SESSION_EXPIRY_DAYS*24*60)}}static getUtms(){const e=s.get(u.UTM);return h.isPresent(e)?JSON.parse(e):{}}static createSession(){s.exists(u.SESSION)||s.set(u.SESSION,h.guid(),n.SESSION_EXPIRY_DAYS*24*60),s.setUtms()}}class k{constructor(){o(this,"instances",[]);o(this,"initialized",!1);o(this,"sessionCreated",!1);o(this,"accessToken","");o(this,"metaParameters",{});o(this,"queue");if(!window)throw new Error("[CPIX] CarterAnalytics SDK can only be used in a browser environment");this.queue=new N({maxRetries:n.QUEUE_MAX_RETRIES,initialDelay:n.QUEUE_INITIAL_DELAY,consumerHandler:this.publishEventToServer.bind(this)})}async initialize(e){if(!e||e.length===0)throw new Error("[CPIX] At least one instance configuration is required for initialization");if(this.instances=e,n.DEBUG=this.instances[0].options.debug===!0,n.API_URL=this.instances[0].options.tracker_server_url,n.USER_PROPERTIES=this.instances[0].options.user_properties||{},n.SESSION_EXPIRY_DAYS=this.instances[0].options.session_expiry_days||14,s.exists(u.SESSION)||(s.createSession(),this.sessionCreated=!0),s.exists(u.META_PARAMETERS)||await this.generateMetaParameters().then(t=>{this.metaParameters=t,s.set("metaParameters",JSON.stringify(t),60*24)}),s.setUtms(),this.metaParameters=JSON.parse(s.get("metaParameters")),this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has already been initialized");if(!this.metaParameters)throw new Error("[CPIX] Meta parameters have not been generated");s.exists(u.ACCESS_TOKEN)||(this.accessToken=await d.aquireAccessToken(this.instances[0].client_id),s.set(u.ACCESS_TOKEN,this.accessToken,60*4)),this.initialized=!0,c.debug("CPIX Initialized"),c.debug("Carter Analytics SDK initialized with options:",this.instances),this.sessionCreated&&this.publish({event:"init",user_properties:n.USER_PROPERTIES,...this.metaParameters}),this.attachHistoryListener()}attachHistoryListener(){const e=history.pushState,t=history.replaceState,i=()=>{this.publish({event:"page_view",event_properties:{title:document.title,page:window.location.pathname,url:window.location.href},user_properties:n.USER_PROPERTIES,...this.metaParameters})};history.pushState=(...r)=>{e.apply(history,r),i()},history.replaceState=(...r)=>{t.apply(history,r),i()},window.addEventListener("popstate",i),i()}publish(e){if(!this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has not been initialized. Please initialize before publishing events.");const t={...e,...this.metaParameters};t.utm_params=s.getUtms(),document.referrer&&(t.referrer=encodeURIComponent(document.referrer)),c.debug("Publishing event:",t),this.queue.publish(t),e.event==="logout"&&(s.delete(u.SESSION),s.createSession(),this.generateMetaParameters().then(i=>{this.metaParameters=i,s.set(u.META_PARAMETERS,JSON.stringify(this.metaParameters),60*24)}))}async generateMetaParameters(){if(!window)throw new Error("[CPIX] Meta parameters can only be generated in a browser environment");const e=await d.getDeviceNetworkParameters();return{client_id:this.instances[0].client_id,session:s.get(u.SESSION),location:{city:e.city,region:e.region,loc:e.loc,timezone:e.timezone,country:e.country},device:{category:d.getDeviceCategory(),brand:d.getDeviceManufacturer(),ip_address:e.ip_address,platform:d.getDevicePlatform()},referrer:encodeURIComponent(document.referrer)}}async publishEventToServer(e){try{e.timestamp=Date.now(),e.event_properties={url:window.location.href,...e.event_properties};const t=await fetch(`${n.API_URL}/api/event`,{method:"POST",body:JSON.stringify({event_data:e}),headers:{"Content-Type":"application/json",Authorization:`Bearer ${s.get(u.ACCESS_TOKEN)}`}});if(!t.ok)throw t.status===401&&(this.accessToken=await d.aquireAccessToken(this.instances[0].client_id),s.set(u.ACCESS_TOKEN,this.accessToken,60*4)),new Error("[CPIX] Failed to publish event");c.debug("Event published:",t.ok)}catch(t){throw c.error("Failed to publish event:",t),new Error("[CPIX] Failed to publish event")}}}const y=new k;typeof window<"u"&&(window.cpix=y);exports.CarterAnalytics=y;
package/dist/cpix.iife.js CHANGED
@@ -1 +1 @@
1
- var tracker=function(l){"use strict";var P=Object.defineProperty;var T=(l,d,p)=>d in l?P(l,d,{enumerable:!0,configurable:!0,writable:!0,value:p}):l[d]=p;var c=(l,d,p)=>(T(l,typeof d!="symbol"?d+"":d,p),p);const w={name:"@carter-rmn/cpix-js",version:"1.0.2",type:"module",files:["dist"],main:"dist/cpix.umd.cjs",module:"dist/cpix.js",types:"dist/cpix.d.ts",exports:{".":{import:"./dist/cpix.js",require:"./dist/cpix.umd.cjs"}},scripts:{dev:"vite",build:"tsc && vite build",preview:"vite preview"},devDependencies:{picocolors:"^1.0.0","rollup-plugin-visualizer":"^5.12.0",typescript:"^5.2.2",vite:"^5.2.0","vite-plugin-progress":"^0.0.7"},dependencies:{"vite-plugin-dts":"^3.8.1"}},u={DEBUG:!1,TRACKER_FUNC_NAME:"cpix",API_URL:"",QUEUE_MAX_RETRIES:3,QUEUE_INITIAL_DELAY:1e4,version:w.version,package:w.name},o={SESSION:"cpix_session",META_PARAMETERS:"cpix_meta_parameters",ACCESS_TOKEN:"cpix_access_token",UTM:"cpix_utm"},x=["utm_source","utm_medium","utm_term","utm_content","utm_campaign"],s=function(){const f=()=>u.DEBUG;return{info:function(...e){console.log(...e)},debug:function(...e){f()&&console.log(...e)},error:function(...e){console.error(...e)}}}();class b extends EventTarget{constructor(t={maxRetries:3,initialDelay:1e4,consumerHandler:i=>(s.debug("Consuming event:",i),Promise.resolve())}){super();c(this,"dataQueue");c(this,"retryQueue");c(this,"maxRetries");c(this,"initialDelay");c(this,"publishEvent");c(this,"retryEvent");c(this,"handler");this.dataQueue=[],this.retryQueue=[],this.maxRetries=t.maxRetries,this.initialDelay=t.initialDelay,this.handler=t.consumerHandler,this.publishEvent=new Event("eventPublished"),this.retryEvent=new Event("retryMessage"),this.consume(),this.retry()}publish(t){s.debug("publishing event");const i={data:t,id:`${Date.now()+Math.random()}`,retryCount:0,retryDelay:this.initialDelay};this.dataQueue.push(i),this.dispatchEvent(this.publishEvent)}consume(){s.debug("Started Consumer Queue"),this.addEventListener("eventPublished",async()=>{for(;this.dataQueue.length>0;){const t=this.dataQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(r){s.error("Failed to publish event:",r),this.scheduleRetry(t)}}})}scheduleRetry(t){if(t.retryCount>=this.maxRetries){s.error("Max retries reached for message:",t);return}setTimeout(()=>{this.retryQueue.push({...t,retryCount:t.retryCount+1,retryDelay:t.retryDelay*2}),this.dispatchEvent(this.retryEvent)},t.retryDelay)}acknowledge(t){this.dataQueue=this.dataQueue.filter(i=>i.id!==t)}retry(){s.debug("Started Retry Queue"),this.addEventListener("retryMessage",async()=>{for(;this.retryQueue.length>0;){const t=this.retryQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(r){s.error("Failed to publish event:",r),this.scheduleRetry(t)}}})}}class m{static getBrowserInfo(){var r,a,g,E,S;const e=navigator.userAgent||"";let t,i;return/chrome/i.test(e)?(t="Chrome",i=(r=e.match(/chrome\/(\d+)/i))==null?void 0:r[1]):/firefox/i.test(e)?(t="Firefox",i=(a=e.match(/firefox\/(\d+)/i))==null?void 0:a[1]):/safari/i.test(e)?(t="Safari",i=(g=e.match(/version\/(\d+)/i))==null?void 0:g[1]):/edge/i.test(e)?(t="Edge",i=(E=e.match(/edge\/(\d+)/i))==null?void 0:E[1]):/trident/i.test(e)?(t="Internet Explorer",i=(S=e.match(/rv:(\d+)/i))==null?void 0:S[1]):(t="Unknown",i="Unknown"),`${t} ${i}`}static getDeviceCategory(){const e=navigator.userAgent;return/mobile/i.test(e)?"Mobile":/tablet/i.test(e)?"Tablet":"Desktop"}static getDevicePlatform(){const e=navigator.userAgent;return/android/i.test(e)?"Android":/iphone|ipad|ipod/i.test(e)?"iOS":/windows phone/i.test(e)?"Windows Phone":/mac|Macintosh/i.test(e)?"Mac":/windows|Microsoft/i.test(e)?"Windows":/linux/i.test(e)?"Linux":"Unknown"}static getDeviceManufacturer(){const e=navigator.userAgent;let t;return/iphone|ipad|ipod|mac|Macintosh/i.test(e)?t="Apple":/samsung/i.test(e)?t="Samsung":/google/i.test(e)?t="Google":/huawei/i.test(e)?t="Huawei":/xiaomi/i.test(e)?t="Xiaomi":/oneplus/i.test(e)?t="OnePlus":/dell/i.test(e)?t="Dell":/lenovo/i.test(e)?t="Lenovo":/acer/i.test(e)?t="Acer":/asus/i.test(e)?t="Asus":/toshiba/i.test(e)?t="Toshiba":t="Unknown",t}static async getDeviceNetworkParameters(){try{const e=await fetch(`${u.API_URL}/api/geolocation`),{data:t}=await e.json();return{city:t.city||"Unknown",region:t.region||"Unknown",country:t.country||"Unknown",timezone:t.timezone||"Unknown",loc:t.loc||"Unknown",ip_address:t.ip_address||"Unknown"}}catch(e){return s.error("Error retrieving device network parameters:",e),{city:"Unknown",region:"Unknown",country:"Unknown",timezone:"Unknown",loc:"Unknown",ip_address:"Unknown"}}}static async aquireAccessToken(e){try{return(await(await fetch(`${u.API_URL}/api/authenticate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:e,metadata:{sdk:u.package,version:u.version,hostname:window.location.hostname}})})).json()).accessToken}catch(t){throw s.error("Error acquiring access token:",t),new Error("[CPIX] Authentication error")}}}class h{static isPresent(e){return typeof e<"u"&&e!==null&&e!==""}static now(){return 1*new Date().getTime()}static guid(){return w.version+"-xxxxxxxx-".replace(/[x]/g,function(e){const t=Math.random()*36|0;return(e=="x"?t:t&3|8).toString(36)})+(1*new Date().getTime()).toString(36)}static optionalData(e){return h.isPresent(e)===!1?"":typeof e=="object"?h.optionalData(JSON.stringify(e)):typeof e=="function"?h.optionalData(e()):String(e)}static sleep(e){return new Promise(t=>setTimeout(t,e))}}class y{static getParametersByName(e,t){t||(t=window.location.href),e=e.replace(/[[\]]/g,"\\$&");const i=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)","gi"),r=[];let a;for(;(a=i.exec(t))!==null;)a[2]&&r.push(decodeURIComponent(a[2].replace(/\+/g," ")));return r.length>0?r:null}static externalHost(e){var t;return e.hostname!=location.hostname&&((t=e==null?void 0:e.protocol)==null?void 0:t.indexOf("http"))===0}}class n{static prefix(){return`__${u.TRACKER_FUNC_NAME}__`}static get(e){const t=`${n.prefix()}${e}`,i=document.cookie.split("; ").find(r=>r.startsWith(`${t}=`));return i?i.split("=")[1]:void 0}static set(e,t,i){const r=`${n.prefix()}${e}`,a=new Date;a.setTime(a.getTime()+i*60*1e3),document.cookie=`${r}=${t}; expires=${a.toUTCString()}; path=/`}static delete(e){const t=`${n.prefix()}${e}`;document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`}static clear(){document.cookie.split("; ").filter(e=>e.startsWith(n.prefix())).forEach(e=>{const t=e.split("=")[0];document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`})}static exists(e){return!!n.get(e)}static setUtms(){const e=x;let t=!1;for(let i=0;i<e.length;i++){const r=e[i];if(h.isPresent(y.getParametersByName(r,window.location.href))){t=!0;break}}if(t){let i;const r=[];for(let a=0;a<e.length;a++){const g=e[a];i=y.getParametersByName(g,window.location.href),r.push({[g]:i})}n.set(o.UTM,JSON.stringify(r),20160)}}static getUtms(){const e=n.get(o.UTM);return h.isPresent(e)?JSON.parse(e):{}}static createSession(){n.exists(o.SESSION)||n.set(o.SESSION,h.guid(),20160),n.setUtms()}}class A{constructor(){c(this,"instances",[]);c(this,"initialized",!1);c(this,"accessToken","");c(this,"metaParameters",{});c(this,"queue");if(!window)throw new Error("[CPIX] CarterAnalytics SDK can only be used in a browser environment");this.queue=new b({maxRetries:u.QUEUE_MAX_RETRIES,initialDelay:u.QUEUE_INITIAL_DELAY,consumerHandler:this.publishEventToServer.bind(this)})}async initialize(e){if(!e||e.length===0)throw new Error("[CPIX] At least one instance configuration is required for initialization");if(this.instances=e,u.DEBUG=this.instances[0].options.debug===!0,u.API_URL=this.instances[0].options.tracker_server_url,n.exists(o.SESSION)||n.createSession(),n.exists(o.META_PARAMETERS)||await this.generateMetaParameters().then(t=>{this.metaParameters=t,n.set("metaParameters",JSON.stringify(t),1440)}),n.setUtms(),this.metaParameters=JSON.parse(n.get("metaParameters")),this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has already been initialized");if(!this.metaParameters)throw new Error("[CPIX] Meta parameters have not been generated");n.exists(o.ACCESS_TOKEN)||(this.accessToken=await m.aquireAccessToken(this.instances[0].client_id),n.set(o.ACCESS_TOKEN,this.accessToken,240)),this.initialized=!0,s.debug("CPIX Initialized"),s.debug("Carter Analytics SDK initialized with options:",this.instances),this.publish({event:"init",...this.metaParameters}),this.publish({event:"page_view",event_properties:{title:document.title,page:window.location.pathname,url:window.location.href},...this.metaParameters})}publish(e){if(!this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has not been initialized. Please initialize before publishing events.");const t={...e,...this.metaParameters};t.utm_params=n.getUtms(),document.referrer&&(t.referrer=encodeURIComponent(document.referrer)),s.debug("Publishing event:",t),this.queue.publish(t),e.event==="logout"&&(n.delete(o.SESSION),n.createSession(),this.generateMetaParameters().then(i=>{this.metaParameters=i,n.set(o.META_PARAMETERS,JSON.stringify(this.metaParameters),1440)}))}async generateMetaParameters(){if(!window)throw new Error("[CPIX] Meta parameters can only be generated in a browser environment");const e=await m.getDeviceNetworkParameters();return{client_id:this.instances[0].client_id,session:n.get(o.SESSION),location:{city:e.city,region:e.region,loc:e.loc,timezone:e.timezone,country:e.country},device:{category:m.getDeviceCategory(),brand:m.getDeviceManufacturer(),ip_address:e.ip_address,platform:m.getDevicePlatform()},referrer:encodeURIComponent(document.referrer)}}async publishEventToServer(e){try{e.timestamp=Date.now(),e.event_properties={url:window.location.href,...e.event_properties};const t=await fetch(`${u.API_URL}/api/event`,{method:"POST",body:JSON.stringify({event_data:e}),headers:{"Content-Type":"application/json",Authorization:`Bearer ${n.get(o.ACCESS_TOKEN)}`}});if(!t.ok)throw t.status===401&&(this.accessToken=await m.aquireAccessToken(this.instances[0].client_id),n.set(o.ACCESS_TOKEN,this.accessToken,240)),new Error("[CPIX] Failed to publish event");s.debug("Event published:",t.ok)}catch(t){throw s.error("Failed to publish event:",t),new Error("[CPIX] Failed to publish event")}}}const v=new A;return typeof window<"u"&&(window.cpix=v),l.CarterAnalytics=v,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"}),l}({});
1
+ var tracker=function(l){"use strict";var b=Object.defineProperty;var A=(l,d,p)=>d in l?b(l,d,{enumerable:!0,configurable:!0,writable:!0,value:p}):l[d]=p;var c=(l,d,p)=>(A(l,typeof d!="symbol"?d+"":d,p),p);const w={name:"@carter-rmn/cpix-js",version:"1.0.3",type:"module",files:["dist"],main:"dist/cpix.umd.cjs",module:"dist/cpix.js",types:"dist/cpix.d.ts",exports:{".":{import:"./dist/cpix.js",require:"./dist/cpix.umd.cjs"}},scripts:{dev:"vite",build:"tsc && vite build",preview:"vite preview"},devDependencies:{picocolors:"^1.0.0","rollup-plugin-visualizer":"^5.12.0",typescript:"^5.2.2",vite:"^5.2.0","vite-plugin-progress":"^0.0.7"},dependencies:{"vite-plugin-dts":"^3.8.1"}},n={DEBUG:!1,TRACKER_FUNC_NAME:"cpix",API_URL:"https://etg20oxcge.execute-api.ca-central-1.amazonaws.com/dev-lambda",QUEUE_MAX_RETRIES:3,QUEUE_INITIAL_DELAY:1e4,version:w.version,package:w.name,USER_PROPERTIES:{},SESSION_EXPIRY_DAYS:14},u={SESSION:"cpix_session",META_PARAMETERS:"cpix_meta_parameters",ACCESS_TOKEN:"cpix_access_token",UTM:"cpix_utm"},P=["utm_source","utm_medium","utm_term","utm_content","utm_campaign"],a=function(){const f=()=>n.DEBUG;return{info:function(...e){console.log(...e)},debug:function(...e){f()&&console.log(...e)},error:function(...e){console.error(...e)}}}();class _ extends EventTarget{constructor(t={maxRetries:3,initialDelay:1e4,consumerHandler:i=>(a.debug("Consuming event:",i),Promise.resolve())}){super();c(this,"dataQueue");c(this,"retryQueue");c(this,"maxRetries");c(this,"initialDelay");c(this,"publishEvent");c(this,"retryEvent");c(this,"handler");this.dataQueue=[],this.retryQueue=[],this.maxRetries=t.maxRetries,this.initialDelay=t.initialDelay,this.handler=t.consumerHandler,this.publishEvent=new Event("eventPublished"),this.retryEvent=new Event("retryMessage"),this.consume(),this.retry()}publish(t){a.debug("publishing event");const i={data:t,id:`${Date.now()+Math.random()}`,retryCount:0,retryDelay:this.initialDelay};this.dataQueue.push(i),this.dispatchEvent(this.publishEvent)}consume(){a.debug("Started Consumer Queue"),this.addEventListener("eventPublished",async()=>{for(;this.dataQueue.length>0;){const t=this.dataQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(r){a.error("Failed to publish event:",r),this.scheduleRetry(t)}}})}scheduleRetry(t){if(t.retryCount>=this.maxRetries){a.error("Max retries reached for message:",t);return}setTimeout(()=>{this.retryQueue.push({...t,retryCount:t.retryCount+1,retryDelay:t.retryDelay*2}),this.dispatchEvent(this.retryEvent)},t.retryDelay)}acknowledge(t){this.dataQueue=this.dataQueue.filter(i=>i.id!==t)}retry(){a.debug("Started Retry Queue"),this.addEventListener("retryMessage",async()=>{for(;this.retryQueue.length>0;){const t=this.retryQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(r){a.error("Failed to publish event:",r),this.scheduleRetry(t)}}})}}class m{static getBrowserInfo(){var r,o,g,S,v;const e=navigator.userAgent||"";let t,i;return/chrome/i.test(e)?(t="Chrome",i=(r=e.match(/chrome\/(\d+)/i))==null?void 0:r[1]):/firefox/i.test(e)?(t="Firefox",i=(o=e.match(/firefox\/(\d+)/i))==null?void 0:o[1]):/safari/i.test(e)?(t="Safari",i=(g=e.match(/version\/(\d+)/i))==null?void 0:g[1]):/edge/i.test(e)?(t="Edge",i=(S=e.match(/edge\/(\d+)/i))==null?void 0:S[1]):/trident/i.test(e)?(t="Internet Explorer",i=(v=e.match(/rv:(\d+)/i))==null?void 0:v[1]):(t="Unknown",i="Unknown"),`${t} ${i}`}static getDeviceCategory(){const e=navigator.userAgent;return/mobile/i.test(e)?"Mobile":/tablet/i.test(e)?"Tablet":"Desktop"}static getDevicePlatform(){const e=navigator.userAgent;return/android/i.test(e)?"Android":/iphone|ipad|ipod/i.test(e)?"iOS":/windows phone/i.test(e)?"Windows Phone":/mac|Macintosh/i.test(e)?"Mac":/windows|Microsoft/i.test(e)?"Windows":/linux/i.test(e)?"Linux":"Unknown"}static getDeviceManufacturer(){const e=navigator.userAgent;let t;return/iphone|ipad|ipod|mac|Macintosh/i.test(e)?t="Apple":/samsung/i.test(e)?t="Samsung":/google/i.test(e)?t="Google":/huawei/i.test(e)?t="Huawei":/xiaomi/i.test(e)?t="Xiaomi":/oneplus/i.test(e)?t="OnePlus":/dell/i.test(e)?t="Dell":/lenovo/i.test(e)?t="Lenovo":/acer/i.test(e)?t="Acer":/asus/i.test(e)?t="Asus":/toshiba/i.test(e)?t="Toshiba":t="Unknown",t}static async getDeviceNetworkParameters(){try{const e=await fetch(`${n.API_URL}/api/geolocation`),{data:t}=await e.json();return{city:t.city||"Unknown",region:t.region||"Unknown",country:t.country||"Unknown",timezone:t.timezone||"Unknown",loc:t.loc||"Unknown",ip_address:t.ip_address||"Unknown"}}catch(e){return a.error("Error retrieving device network parameters:",e),{city:"Unknown",region:"Unknown",country:"Unknown",timezone:"Unknown",loc:"Unknown",ip_address:"Unknown"}}}static async aquireAccessToken(e){try{return(await(await fetch(`${n.API_URL}/api/authenticate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:e,metadata:{sdk:n.package,version:n.version,hostname:window.location.hostname}})})).json()).accessToken}catch(t){throw a.error("Error acquiring access token:",t),new Error("[CPIX] Authentication error")}}}class h{static isPresent(e){return typeof e<"u"&&e!==null&&e!==""}static now(){return 1*new Date().getTime()}static guid(){return w.version+"-xxxxxxxx-".replace(/[x]/g,function(e){const t=Math.random()*36|0;return(e=="x"?t:t&3|8).toString(36)})+(1*new Date().getTime()).toString(36)}static optionalData(e){return h.isPresent(e)===!1?"":typeof e=="object"?h.optionalData(JSON.stringify(e)):typeof e=="function"?h.optionalData(e()):String(e)}static sleep(e){return new Promise(t=>setTimeout(t,e))}}class y{static getParametersByName(e,t){t||(t=window.location.href),e=e.replace(/[[\]]/g,"\\$&");const i=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)","gi"),r=[];let o;for(;(o=i.exec(t))!==null;)o[2]&&r.push(decodeURIComponent(o[2].replace(/\+/g," ")));return r.length>0?r:null}static externalHost(e){var t;return e.hostname!=location.hostname&&((t=e==null?void 0:e.protocol)==null?void 0:t.indexOf("http"))===0}}class s{static prefix(){return`__${n.TRACKER_FUNC_NAME}__`}static get(e){const t=`${s.prefix()}${e}`,i=document.cookie.split("; ").find(r=>r.startsWith(`${t}=`));return i?i.split("=")[1]:void 0}static set(e,t,i){const r=`${s.prefix()}${e}`,o=new Date;o.setTime(o.getTime()+i*60*1e3),document.cookie=`${r}=${t}; expires=${o.toUTCString()}; path=/`}static delete(e){const t=`${s.prefix()}${e}`;document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`}static clear(){document.cookie.split("; ").filter(e=>e.startsWith(s.prefix())).forEach(e=>{const t=e.split("=")[0];document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`})}static exists(e){return!!s.get(e)}static setUtms(){const e=P;let t=!1;for(let i=0;i<e.length;i++){const r=e[i];if(h.isPresent(y.getParametersByName(r,window.location.href))){t=!0;break}}if(t){let i;const r=[];for(let o=0;o<e.length;o++){const g=e[o];i=y.getParametersByName(g,window.location.href),r.push({[g]:i})}s.set(u.UTM,JSON.stringify(r),n.SESSION_EXPIRY_DAYS*24*60)}}static getUtms(){const e=s.get(u.UTM);return h.isPresent(e)?JSON.parse(e):{}}static createSession(){s.exists(u.SESSION)||s.set(u.SESSION,h.guid(),n.SESSION_EXPIRY_DAYS*24*60),s.setUtms()}}class x{constructor(){c(this,"instances",[]);c(this,"initialized",!1);c(this,"sessionCreated",!1);c(this,"accessToken","");c(this,"metaParameters",{});c(this,"queue");if(!window)throw new Error("[CPIX] CarterAnalytics SDK can only be used in a browser environment");this.queue=new _({maxRetries:n.QUEUE_MAX_RETRIES,initialDelay:n.QUEUE_INITIAL_DELAY,consumerHandler:this.publishEventToServer.bind(this)})}async initialize(e){if(!e||e.length===0)throw new Error("[CPIX] At least one instance configuration is required for initialization");if(this.instances=e,n.DEBUG=this.instances[0].options.debug===!0,n.API_URL=this.instances[0].options.tracker_server_url,n.USER_PROPERTIES=this.instances[0].options.user_properties||{},n.SESSION_EXPIRY_DAYS=this.instances[0].options.session_expiry_days||14,s.exists(u.SESSION)||(s.createSession(),this.sessionCreated=!0),s.exists(u.META_PARAMETERS)||await this.generateMetaParameters().then(t=>{this.metaParameters=t,s.set("metaParameters",JSON.stringify(t),1440)}),s.setUtms(),this.metaParameters=JSON.parse(s.get("metaParameters")),this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has already been initialized");if(!this.metaParameters)throw new Error("[CPIX] Meta parameters have not been generated");s.exists(u.ACCESS_TOKEN)||(this.accessToken=await m.aquireAccessToken(this.instances[0].client_id),s.set(u.ACCESS_TOKEN,this.accessToken,240)),this.initialized=!0,a.debug("CPIX Initialized"),a.debug("Carter Analytics SDK initialized with options:",this.instances),this.sessionCreated&&this.publish({event:"init",user_properties:n.USER_PROPERTIES,...this.metaParameters}),this.attachHistoryListener()}attachHistoryListener(){const e=history.pushState,t=history.replaceState,i=()=>{this.publish({event:"page_view",event_properties:{title:document.title,page:window.location.pathname,url:window.location.href},user_properties:n.USER_PROPERTIES,...this.metaParameters})};history.pushState=(...r)=>{e.apply(history,r),i()},history.replaceState=(...r)=>{t.apply(history,r),i()},window.addEventListener("popstate",i),i()}publish(e){if(!this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has not been initialized. Please initialize before publishing events.");const t={...e,...this.metaParameters};t.utm_params=s.getUtms(),document.referrer&&(t.referrer=encodeURIComponent(document.referrer)),a.debug("Publishing event:",t),this.queue.publish(t),e.event==="logout"&&(s.delete(u.SESSION),s.createSession(),this.generateMetaParameters().then(i=>{this.metaParameters=i,s.set(u.META_PARAMETERS,JSON.stringify(this.metaParameters),1440)}))}async generateMetaParameters(){if(!window)throw new Error("[CPIX] Meta parameters can only be generated in a browser environment");const e=await m.getDeviceNetworkParameters();return{client_id:this.instances[0].client_id,session:s.get(u.SESSION),location:{city:e.city,region:e.region,loc:e.loc,timezone:e.timezone,country:e.country},device:{category:m.getDeviceCategory(),brand:m.getDeviceManufacturer(),ip_address:e.ip_address,platform:m.getDevicePlatform()},referrer:encodeURIComponent(document.referrer)}}async publishEventToServer(e){try{e.timestamp=Date.now(),e.event_properties={url:window.location.href,...e.event_properties};const t=await fetch(`${n.API_URL}/api/event`,{method:"POST",body:JSON.stringify({event_data:e}),headers:{"Content-Type":"application/json",Authorization:`Bearer ${s.get(u.ACCESS_TOKEN)}`}});if(!t.ok)throw t.status===401&&(this.accessToken=await m.aquireAccessToken(this.instances[0].client_id),s.set(u.ACCESS_TOKEN,this.accessToken,240)),new Error("[CPIX] Failed to publish event");a.debug("Event published:",t.ok)}catch(t){throw a.error("Failed to publish event:",t),new Error("[CPIX] Failed to publish event")}}}const E=new x;return typeof window<"u"&&(window.cpix=E),l.CarterAnalytics=E,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"}),l}({});
package/dist/cpix.js CHANGED
@@ -1,75 +1,77 @@
1
1
  var y = Object.defineProperty;
2
- var v = (u, e, t) => e in u ? y(u, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : u[e] = t;
3
- var o = (u, e, t) => (v(u, typeof e != "symbol" ? e + "" : e, t), t);
4
- const E = "@carter-rmn/cpix-js", S = "1.0.2", x = "module", b = [
2
+ var E = (l, e, t) => e in l ? y(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t;
3
+ var o = (l, e, t) => (E(l, typeof e != "symbol" ? e + "" : e, t), t);
4
+ const S = "@carter-rmn/cpix-js", v = "1.0.3", P = "module", _ = [
5
5
  "dist"
6
- ], A = "dist/cpix.umd.cjs", P = "dist/cpix.js", T = "dist/cpix.d.ts", U = {
6
+ ], x = "dist/cpix.umd.cjs", A = "dist/cpix.js", b = "dist/cpix.d.ts", T = {
7
7
  ".": {
8
8
  import: "./dist/cpix.js",
9
9
  require: "./dist/cpix.umd.cjs"
10
10
  }
11
- }, _ = {
11
+ }, U = {
12
12
  dev: "vite",
13
13
  build: "tsc && vite build",
14
14
  preview: "vite preview"
15
- }, C = {
15
+ }, I = {
16
16
  picocolors: "^1.0.0",
17
17
  "rollup-plugin-visualizer": "^5.12.0",
18
18
  typescript: "^5.2.2",
19
19
  vite: "^5.2.0",
20
20
  "vite-plugin-progress": "^0.0.7"
21
- }, D = {
21
+ }, R = {
22
22
  "vite-plugin-dts": "^3.8.1"
23
23
  }, m = {
24
- name: E,
25
- version: S,
26
- type: x,
27
- files: b,
28
- main: A,
29
- module: P,
30
- types: T,
31
- exports: U,
32
- scripts: _,
33
- devDependencies: C,
34
- dependencies: D
35
- }, l = {
24
+ name: S,
25
+ version: v,
26
+ type: P,
27
+ files: _,
28
+ main: x,
29
+ module: A,
30
+ types: b,
31
+ exports: T,
32
+ scripts: U,
33
+ devDependencies: I,
34
+ dependencies: R
35
+ }, n = {
36
36
  DEBUG: !1,
37
37
  TRACKER_FUNC_NAME: "cpix",
38
- API_URL: "",
38
+ API_URL: "https://etg20oxcge.execute-api.ca-central-1.amazonaws.com/dev-lambda",
39
39
  QUEUE_MAX_RETRIES: 3,
40
40
  QUEUE_INITIAL_DELAY: 1e4,
41
41
  version: m.version,
42
- package: m.name
43
- }, c = {
42
+ package: m.name,
43
+ USER_PROPERTIES: {},
44
+ SESSION_EXPIRY_DAYS: 14
45
+ }, u = {
44
46
  SESSION: "cpix_session",
45
47
  META_PARAMETERS: "cpix_meta_parameters",
46
48
  ACCESS_TOKEN: "cpix_access_token",
47
49
  UTM: "cpix_utm"
48
- }, I = [
50
+ }, D = [
49
51
  "utm_source",
50
52
  "utm_medium",
51
53
  "utm_term",
52
54
  "utm_content",
53
55
  "utm_campaign"
54
- ], a = /* @__PURE__ */ function() {
55
- const u = () => l.DEBUG;
56
+ ], c = /* @__PURE__ */ function() {
57
+ const l = () => n.DEBUG;
56
58
  return {
57
59
  info: function(...e) {
58
60
  console.log(...e);
59
61
  },
60
62
  debug: function(...e) {
61
- u() && console.log(...e);
63
+ l() && console.log(...e);
62
64
  },
63
65
  error: function(...e) {
64
66
  console.error(...e);
65
67
  }
66
68
  };
67
69
  }();
68
- class k extends EventTarget {
70
+ class C extends EventTarget {
69
71
  constructor(t = {
70
72
  maxRetries: 3,
71
73
  initialDelay: 1e4,
72
- consumerHandler: (i) => (a.debug("Consuming event:", i), Promise.resolve())
74
+ consumerHandler: (i) => (c.debug("Consuming event:", i), Promise.resolve())
73
75
  }) {
74
76
  super();
75
77
  o(this, "dataQueue");
@@ -82,7 +84,7 @@ class k extends EventTarget {
82
84
  this.dataQueue = [], this.retryQueue = [], this.maxRetries = t.maxRetries, this.initialDelay = t.initialDelay, this.handler = t.consumerHandler, this.publishEvent = new Event("eventPublished"), this.retryEvent = new Event("retryMessage"), this.consume(), this.retry();
83
85
  }
84
86
  publish(t) {
85
- a.debug("publishing event");
87
+ c.debug("publishing event");
86
88
  const i = {
87
89
  data: t,
88
90
  id: `${Date.now() + Math.random()}`,
@@ -92,20 +94,20 @@ class k extends EventTarget {
92
94
  this.dataQueue.push(i), this.dispatchEvent(this.publishEvent);
93
95
  }
94
96
  consume() {
95
- a.debug("Started Consumer Queue"), this.addEventListener("eventPublished", async () => {
97
+ c.debug("Started Consumer Queue"), this.addEventListener("eventPublished", async () => {
96
98
  for (; this.dataQueue.length > 0; ) {
97
99
  const t = this.dataQueue.shift(), { data: i = {} } = t;
98
100
  try {
99
101
  await this.handler(i), this.acknowledge(t == null ? void 0 : t.id);
100
102
  } catch (r) {
101
- a.error("Failed to publish event:", r), this.scheduleRetry(t);
103
+ c.error("Failed to publish event:", r), this.scheduleRetry(t);
102
104
  }
103
105
  }
104
106
  });
105
107
  }
106
108
  scheduleRetry(t) {
107
109
  if (t.retryCount >= this.maxRetries) {
108
- a.error("Max retries reached for message:", t);
110
+ c.error("Max retries reached for message:", t);
109
111
  return;
110
112
  }
111
113
  setTimeout(() => {
@@ -120,24 +122,24 @@ class k extends EventTarget {
120
122
  this.dataQueue = this.dataQueue.filter((i) => i.id !== t);
121
123
  }
122
124
  retry() {
123
- a.debug("Started Retry Queue"), this.addEventListener("retryMessage", async () => {
125
+ c.debug("Started Retry Queue"), this.addEventListener("retryMessage", async () => {
124
126
  for (; this.retryQueue.length > 0; ) {
125
127
  const t = this.retryQueue.shift(), { data: i = {} } = t;
126
128
  try {
127
129
  await this.handler(i), this.acknowledge(t == null ? void 0 : t.id);
128
130
  } catch (r) {
129
- a.error("Failed to publish event:", r), this.scheduleRetry(t);
131
+ c.error("Failed to publish event:", r), this.scheduleRetry(t);
130
132
  }
131
133
  }
132
134
  });
133
135
  }
134
136
  }
135
- class h {
137
+ class d {
136
138
  static getBrowserInfo() {
137
- var r, n, p, f, g;
139
+ var r, a, p, f, g;
138
140
  const e = navigator.userAgent || "";
139
141
  let t, i;
140
- return /chrome/i.test(e) ? (t = "Chrome", i = (r = e.match(/chrome\/(\d+)/i)) == null ? void 0 : r[1]) : /firefox/i.test(e) ? (t = "Firefox", i = (n = e.match(/firefox\/(\d+)/i)) == null ? void 0 : n[1]) : /safari/i.test(e) ? (t = "Safari", i = (p = e.match(/version\/(\d+)/i)) == null ? void 0 : p[1]) : /edge/i.test(e) ? (t = "Edge", i = (f = e.match(/edge\/(\d+)/i)) == null ? void 0 : f[1]) : /trident/i.test(e) ? (t = "Internet Explorer", i = (g = e.match(/rv:(\d+)/i)) == null ? void 0 : g[1]) : (t = "Unknown", i = "Unknown"), `${t} ${i}`;
142
+ return /chrome/i.test(e) ? (t = "Chrome", i = (r = e.match(/chrome\/(\d+)/i)) == null ? void 0 : r[1]) : /firefox/i.test(e) ? (t = "Firefox", i = (a = e.match(/firefox\/(\d+)/i)) == null ? void 0 : a[1]) : /safari/i.test(e) ? (t = "Safari", i = (p = e.match(/version\/(\d+)/i)) == null ? void 0 : p[1]) : /edge/i.test(e) ? (t = "Edge", i = (f = e.match(/edge\/(\d+)/i)) == null ? void 0 : f[1]) : /trident/i.test(e) ? (t = "Internet Explorer", i = (g = e.match(/rv:(\d+)/i)) == null ? void 0 : g[1]) : (t = "Unknown", i = "Unknown"), `${t} ${i}`;
141
143
  }
142
144
  static getDeviceCategory() {
143
145
  const e = navigator.userAgent;
@@ -154,7 +156,7 @@ class h {
154
156
  }
155
157
  static async getDeviceNetworkParameters() {
156
158
  try {
157
- const e = await fetch(`${l.API_URL}/api/geolocation`), { data: t } = await e.json();
159
+ const e = await fetch(`${n.API_URL}/api/geolocation`), { data: t } = await e.json();
158
160
  return {
159
161
  city: t.city || "Unknown",
160
162
  region: t.region || "Unknown",
@@ -164,7 +166,7 @@ class h {
164
166
  ip_address: t.ip_address || "Unknown"
165
167
  };
166
168
  } catch (e) {
167
- return a.error("Error retrieving device network parameters:", e), {
169
+ return c.error("Error retrieving device network parameters:", e), {
168
170
  city: "Unknown",
169
171
  region: "Unknown",
170
172
  country: "Unknown",
@@ -176,7 +178,7 @@ class h {
176
178
  }
177
179
  static async aquireAccessToken(e) {
178
180
  try {
179
- return (await (await fetch(`${l.API_URL}/api/authenticate`, {
181
+ return (await (await fetch(`${n.API_URL}/api/authenticate`, {
180
182
  method: "POST",
181
183
  headers: {
182
184
  "Content-Type": "application/json"
@@ -184,18 +186,18 @@ class h {
184
186
  body: JSON.stringify({
185
187
  accountId: e,
186
188
  metadata: {
187
- sdk: l.package,
188
- version: l.version,
189
+ sdk: n.package,
190
+ version: n.version,
189
191
  hostname: window.location.hostname
190
192
  }
191
193
  })
192
194
  })).json()).accessToken;
193
195
  } catch (t) {
194
- throw a.error("Error acquiring access token:", t), new Error("[CPIX] Authentication error");
196
+ throw c.error("Error acquiring access token:", t), new Error("[CPIX] Authentication error");
195
197
  }
196
198
  }
197
199
  }
198
- class d {
200
+ class h {
199
201
  static isPresent(e) {
200
202
  return typeof e < "u" && e !== null && e !== "";
201
203
  }
@@ -210,7 +212,7 @@ class d {
210
212
  }
211
213
  // reduces all optional data down to a string
212
214
  static optionalData(e) {
213
- return d.isPresent(e) === !1 ? "" : typeof e == "object" ? d.optionalData(JSON.stringify(e)) : typeof e == "function" ? d.optionalData(e()) : String(e);
215
+ return h.isPresent(e) === !1 ? "" : typeof e == "object" ? h.optionalData(JSON.stringify(e)) : typeof e == "function" ? h.optionalData(e()) : String(e);
214
216
  }
215
217
  static sleep(e) {
216
218
  return new Promise((t) => setTimeout(t, e));
@@ -220,9 +222,9 @@ class w {
220
222
  static getParametersByName(e, t) {
221
223
  t || (t = window.location.href), e = e.replace(/[[\]]/g, "\\$&");
222
224
  const i = new RegExp("[?&]" + e + "(=([^&#]*)|&|#|$)", "gi"), r = [];
223
- let n;
224
- for (; (n = i.exec(t)) !== null; )
225
- n[2] && r.push(decodeURIComponent(n[2].replace(/\+/g, " ")));
225
+ let a;
226
+ for (; (a = i.exec(t)) !== null; )
227
+ a[2] && r.push(decodeURIComponent(a[2].replace(/\+/g, " ")));
226
228
  return r.length > 0 ? r : null;
227
229
  }
228
230
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -233,15 +235,15 @@ class w {
233
235
  }
234
236
  class s {
235
237
  static prefix() {
236
- return `__${l.TRACKER_FUNC_NAME}__`;
238
+ return `__${n.TRACKER_FUNC_NAME}__`;
237
239
  }
238
240
  static get(e) {
239
241
  const t = `${s.prefix()}${e}`, i = document.cookie.split("; ").find((r) => r.startsWith(`${t}=`));
240
242
  return i ? i.split("=")[1] : void 0;
241
243
  }
242
244
  static set(e, t, i) {
243
- const r = `${s.prefix()}${e}`, n = /* @__PURE__ */ new Date();
244
- n.setTime(n.getTime() + i * 60 * 1e3), document.cookie = `${r}=${t}; expires=${n.toUTCString()}; path=/`;
245
+ const r = `${s.prefix()}${e}`, a = /* @__PURE__ */ new Date();
246
+ a.setTime(a.getTime() + i * 60 * 1e3), document.cookie = `${r}=${t}; expires=${a.toUTCString()}; path=/`;
245
247
  }
246
248
  static delete(e) {
247
249
  const t = `${s.prefix()}${e}`;
@@ -257,11 +259,11 @@ class s {
257
259
  return !!s.get(e);
258
260
  }
259
261
  static setUtms() {
260
- const e = I;
262
+ const e = D;
261
263
  let t = !1;
262
264
  for (let i = 0; i < e.length; i++) {
263
265
  const r = e[i];
264
- if (d.isPresent(w.getParametersByName(r, window.location.href))) {
266
+ if (h.isPresent(w.getParametersByName(r, window.location.href))) {
265
267
  t = !0;
266
268
  break;
267
269
  }
@@ -269,27 +271,36 @@ class s {
269
271
  if (t) {
270
272
  let i;
271
273
  const r = [];
272
- for (let n = 0; n < e.length; n++) {
273
- const p = e[n];
274
+ for (let a = 0; a < e.length; a++) {
275
+ const p = e[a];
274
276
  i = w.getParametersByName(p, window.location.href), r.push({
275
277
  [p]: i
276
278
  });
277
279
  }
278
- s.set(c.UTM, JSON.stringify(r), 14 * 24 * 60);
280
+ s.set(
281
+ u.UTM,
282
+ JSON.stringify(r),
283
+ n.SESSION_EXPIRY_DAYS * 24 * 60
284
+ );
279
285
  }
280
286
  }
281
287
  static getUtms() {
282
- const e = s.get(c.UTM);
283
- return d.isPresent(e) ? JSON.parse(e) : {};
288
+ const e = s.get(u.UTM);
289
+ return h.isPresent(e) ? JSON.parse(e) : {};
284
290
  }
285
291
  static createSession() {
286
- s.exists(c.SESSION) || s.set(c.SESSION, d.guid(), 14 * 24 * 60), s.setUtms();
292
+ s.exists(u.SESSION) || s.set(
293
+ u.SESSION,
294
+ h.guid(),
295
+ n.SESSION_EXPIRY_DAYS * 24 * 60
296
+ ), s.setUtms();
287
297
  }
288
298
  }
289
299
  class N {
290
300
  constructor() {
291
301
  o(this, "instances", []);
292
302
  o(this, "initialized", !1);
303
+ o(this, "sessionCreated", !1);
293
304
  o(this, "accessToken", "");
294
305
  o(this, "metaParameters", {});
295
306
  o(this, "queue");
@@ -297,9 +308,9 @@ class N {
297
308
  throw new Error(
298
309
  "[CPIX] CarterAnalytics SDK can only be used in a browser environment"
299
310
  );
300
- this.queue = new k({
301
- maxRetries: l.QUEUE_MAX_RETRIES,
302
- initialDelay: l.QUEUE_INITIAL_DELAY,
311
+ this.queue = new C({
312
+ maxRetries: n.QUEUE_MAX_RETRIES,
313
+ initialDelay: n.QUEUE_INITIAL_DELAY,
303
314
  consumerHandler: this.publishEventToServer.bind(this)
304
315
  });
305
316
  }
@@ -312,7 +323,7 @@ class N {
312
323
  throw new Error(
313
324
  "[CPIX] At least one instance configuration is required for initialization"
314
325
  );
315
- if (this.instances = e, l.DEBUG = this.instances[0].options.debug === !0, l.API_URL = this.instances[0].options.tracker_server_url, s.exists(c.SESSION) || s.createSession(), s.exists(c.META_PARAMETERS) || await this.generateMetaParameters().then((t) => {
326
+ if (this.instances = e, n.DEBUG = this.instances[0].options.debug === !0, n.API_URL = this.instances[0].options.tracker_server_url, n.USER_PROPERTIES = this.instances[0].options.user_properties || {}, n.SESSION_EXPIRY_DAYS = this.instances[0].options.session_expiry_days || 14, s.exists(u.SESSION) || (s.createSession(), this.sessionCreated = !0), s.exists(u.META_PARAMETERS) || await this.generateMetaParameters().then((t) => {
316
327
  this.metaParameters = t, s.set("metaParameters", JSON.stringify(t), 60 * 24);
317
328
  }), s.setUtms(), this.metaParameters = JSON.parse(s.get("metaParameters")), this.initialized)
318
329
  throw new Error(
@@ -320,23 +331,38 @@ class N {
320
331
  );
321
332
  if (!this.metaParameters)
322
333
  throw new Error("[CPIX] Meta parameters have not been generated");
323
- s.exists(c.ACCESS_TOKEN) || (this.accessToken = await h.aquireAccessToken(
334
+ s.exists(u.ACCESS_TOKEN) || (this.accessToken = await d.aquireAccessToken(
324
335
  this.instances[0].client_id
325
- ), s.set(c.ACCESS_TOKEN, this.accessToken, 60 * 4)), this.initialized = !0, a.debug("CPIX Initialized"), a.debug(
336
+ ), s.set(u.ACCESS_TOKEN, this.accessToken, 60 * 4)), this.initialized = !0, c.debug("CPIX Initialized"), c.debug(
326
337
  "Carter Analytics SDK initialized with options:",
327
338
  this.instances
328
- ), this.publish({
339
+ ), this.sessionCreated && this.publish({
329
340
  event: "init",
341
+ user_properties: n.USER_PROPERTIES,
330
342
  ...this.metaParameters
331
- }), this.publish({
332
- event: "page_view",
333
- event_properties: {
334
- title: document.title,
335
- page: window.location.pathname,
336
- url: window.location.href
337
- },
338
- ...this.metaParameters
339
- });
343
+ }), this.attachHistoryListener();
344
+ }
345
+ /**
346
+ * Attaches a history listener to publish a page_view event whenever the page changes.
347
+ */
348
+ attachHistoryListener() {
349
+ const e = history.pushState, t = history.replaceState, i = () => {
350
+ this.publish({
351
+ event: "page_view",
352
+ event_properties: {
353
+ title: document.title,
354
+ page: window.location.pathname,
355
+ url: window.location.href
356
+ },
357
+ user_properties: n.USER_PROPERTIES,
358
+ ...this.metaParameters
359
+ });
360
+ };
361
+ history.pushState = (...r) => {
362
+ e.apply(history, r), i();
363
+ }, history.replaceState = (...r) => {
364
+ t.apply(history, r), i();
365
+ }, window.addEventListener("popstate", i), i();
340
366
  }
341
367
  /**
342
368
  * Publishes an event to the analytics queue.
@@ -351,9 +377,9 @@ class N {
351
377
  ...e,
352
378
  ...this.metaParameters
353
379
  };
354
- t.utm_params = s.getUtms(), document.referrer && (t.referrer = encodeURIComponent(document.referrer)), a.debug("Publishing event:", t), this.queue.publish(t), e.event === "logout" && (s.delete(c.SESSION), s.createSession(), this.generateMetaParameters().then((i) => {
380
+ t.utm_params = s.getUtms(), document.referrer && (t.referrer = encodeURIComponent(document.referrer)), c.debug("Publishing event:", t), this.queue.publish(t), e.event === "logout" && (s.delete(u.SESSION), s.createSession(), this.generateMetaParameters().then((i) => {
355
381
  this.metaParameters = i, s.set(
356
- c.META_PARAMETERS,
382
+ u.META_PARAMETERS,
357
383
  JSON.stringify(this.metaParameters),
358
384
  60 * 24
359
385
  );
@@ -367,10 +393,10 @@ class N {
367
393
  throw new Error(
368
394
  "[CPIX] Meta parameters can only be generated in a browser environment"
369
395
  );
370
- const e = await h.getDeviceNetworkParameters();
396
+ const e = await d.getDeviceNetworkParameters();
371
397
  return {
372
398
  client_id: this.instances[0].client_id,
373
- session: s.get(c.SESSION),
399
+ session: s.get(u.SESSION),
374
400
  location: {
375
401
  city: e.city,
376
402
  region: e.region,
@@ -379,10 +405,10 @@ class N {
379
405
  country: e.country
380
406
  },
381
407
  device: {
382
- category: h.getDeviceCategory(),
383
- brand: h.getDeviceManufacturer(),
408
+ category: d.getDeviceCategory(),
409
+ brand: d.getDeviceManufacturer(),
384
410
  ip_address: e.ip_address,
385
- platform: h.getDevicePlatform()
411
+ platform: d.getDevicePlatform()
386
412
  },
387
413
  referrer: encodeURIComponent(document.referrer)
388
414
  };
@@ -397,26 +423,26 @@ class N {
397
423
  url: window.location.href,
398
424
  ...e.event_properties
399
425
  };
400
- const t = await fetch(`${l.API_URL}/api/event`, {
426
+ const t = await fetch(`${n.API_URL}/api/event`, {
401
427
  method: "POST",
402
428
  body: JSON.stringify({ event_data: e }),
403
429
  headers: {
404
430
  "Content-Type": "application/json",
405
- Authorization: `Bearer ${s.get(c.ACCESS_TOKEN)}`
431
+ Authorization: `Bearer ${s.get(u.ACCESS_TOKEN)}`
406
432
  }
407
433
  });
408
434
  if (!t.ok)
409
- throw t.status === 401 && (this.accessToken = await h.aquireAccessToken(
435
+ throw t.status === 401 && (this.accessToken = await d.aquireAccessToken(
410
436
  this.instances[0].client_id
411
- ), s.set(c.ACCESS_TOKEN, this.accessToken, 60 * 4)), new Error("[CPIX] Failed to publish event");
412
- a.debug("Event published:", t.ok);
437
+ ), s.set(u.ACCESS_TOKEN, this.accessToken, 60 * 4)), new Error("[CPIX] Failed to publish event");
438
+ c.debug("Event published:", t.ok);
413
439
  } catch (t) {
414
- throw a.error("Failed to publish event:", t), new Error("[CPIX] Failed to publish event");
440
+ throw c.error("Failed to publish event:", t), new Error("[CPIX] Failed to publish event");
415
441
  }
416
442
  }
417
443
  }
418
- const R = new N();
419
- typeof window < "u" && (window.cpix = R);
444
+ const k = new N();
445
+ typeof window < "u" && (window.cpix = k);
420
446
  export {
421
- R as CarterAnalytics
447
+ k as CarterAnalytics
422
448
  };
package/dist/cpix.umd.cjs CHANGED
@@ -1 +1 @@
1
- (function(u,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(u=typeof globalThis<"u"?globalThis:u||self,l(u.tracker={}))})(this,function(u){"use strict";var P=Object.defineProperty;var T=(u,l,p)=>l in u?P(u,l,{enumerable:!0,configurable:!0,writable:!0,value:p}):u[l]=p;var c=(u,l,p)=>(T(u,typeof l!="symbol"?l+"":l,p),p);const w={name:"@carter-rmn/cpix-js",version:"1.0.2",type:"module",files:["dist"],main:"dist/cpix.umd.cjs",module:"dist/cpix.js",types:"dist/cpix.d.ts",exports:{".":{import:"./dist/cpix.js",require:"./dist/cpix.umd.cjs"}},scripts:{dev:"vite",build:"tsc && vite build",preview:"vite preview"},devDependencies:{picocolors:"^1.0.0","rollup-plugin-visualizer":"^5.12.0",typescript:"^5.2.2",vite:"^5.2.0","vite-plugin-progress":"^0.0.7"},dependencies:{"vite-plugin-dts":"^3.8.1"}},d={DEBUG:!1,TRACKER_FUNC_NAME:"cpix",API_URL:"",QUEUE_MAX_RETRIES:3,QUEUE_INITIAL_DELAY:1e4,version:w.version,package:w.name},o={SESSION:"cpix_session",META_PARAMETERS:"cpix_meta_parameters",ACCESS_TOKEN:"cpix_access_token",UTM:"cpix_utm"},x=["utm_source","utm_medium","utm_term","utm_content","utm_campaign"],r=function(){const f=()=>d.DEBUG;return{info:function(...e){console.log(...e)},debug:function(...e){f()&&console.log(...e)},error:function(...e){console.error(...e)}}}();class b extends EventTarget{constructor(t={maxRetries:3,initialDelay:1e4,consumerHandler:i=>(r.debug("Consuming event:",i),Promise.resolve())}){super();c(this,"dataQueue");c(this,"retryQueue");c(this,"maxRetries");c(this,"initialDelay");c(this,"publishEvent");c(this,"retryEvent");c(this,"handler");this.dataQueue=[],this.retryQueue=[],this.maxRetries=t.maxRetries,this.initialDelay=t.initialDelay,this.handler=t.consumerHandler,this.publishEvent=new Event("eventPublished"),this.retryEvent=new Event("retryMessage"),this.consume(),this.retry()}publish(t){r.debug("publishing event");const i={data:t,id:`${Date.now()+Math.random()}`,retryCount:0,retryDelay:this.initialDelay};this.dataQueue.push(i),this.dispatchEvent(this.publishEvent)}consume(){r.debug("Started Consumer Queue"),this.addEventListener("eventPublished",async()=>{for(;this.dataQueue.length>0;){const t=this.dataQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(s){r.error("Failed to publish event:",s),this.scheduleRetry(t)}}})}scheduleRetry(t){if(t.retryCount>=this.maxRetries){r.error("Max retries reached for message:",t);return}setTimeout(()=>{this.retryQueue.push({...t,retryCount:t.retryCount+1,retryDelay:t.retryDelay*2}),this.dispatchEvent(this.retryEvent)},t.retryDelay)}acknowledge(t){this.dataQueue=this.dataQueue.filter(i=>i.id!==t)}retry(){r.debug("Started Retry Queue"),this.addEventListener("retryMessage",async()=>{for(;this.retryQueue.length>0;){const t=this.retryQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(s){r.error("Failed to publish event:",s),this.scheduleRetry(t)}}})}}class m{static getBrowserInfo(){var s,a,g,E,S;const e=navigator.userAgent||"";let t,i;return/chrome/i.test(e)?(t="Chrome",i=(s=e.match(/chrome\/(\d+)/i))==null?void 0:s[1]):/firefox/i.test(e)?(t="Firefox",i=(a=e.match(/firefox\/(\d+)/i))==null?void 0:a[1]):/safari/i.test(e)?(t="Safari",i=(g=e.match(/version\/(\d+)/i))==null?void 0:g[1]):/edge/i.test(e)?(t="Edge",i=(E=e.match(/edge\/(\d+)/i))==null?void 0:E[1]):/trident/i.test(e)?(t="Internet Explorer",i=(S=e.match(/rv:(\d+)/i))==null?void 0:S[1]):(t="Unknown",i="Unknown"),`${t} ${i}`}static getDeviceCategory(){const e=navigator.userAgent;return/mobile/i.test(e)?"Mobile":/tablet/i.test(e)?"Tablet":"Desktop"}static getDevicePlatform(){const e=navigator.userAgent;return/android/i.test(e)?"Android":/iphone|ipad|ipod/i.test(e)?"iOS":/windows phone/i.test(e)?"Windows Phone":/mac|Macintosh/i.test(e)?"Mac":/windows|Microsoft/i.test(e)?"Windows":/linux/i.test(e)?"Linux":"Unknown"}static getDeviceManufacturer(){const e=navigator.userAgent;let t;return/iphone|ipad|ipod|mac|Macintosh/i.test(e)?t="Apple":/samsung/i.test(e)?t="Samsung":/google/i.test(e)?t="Google":/huawei/i.test(e)?t="Huawei":/xiaomi/i.test(e)?t="Xiaomi":/oneplus/i.test(e)?t="OnePlus":/dell/i.test(e)?t="Dell":/lenovo/i.test(e)?t="Lenovo":/acer/i.test(e)?t="Acer":/asus/i.test(e)?t="Asus":/toshiba/i.test(e)?t="Toshiba":t="Unknown",t}static async getDeviceNetworkParameters(){try{const e=await fetch(`${d.API_URL}/api/geolocation`),{data:t}=await e.json();return{city:t.city||"Unknown",region:t.region||"Unknown",country:t.country||"Unknown",timezone:t.timezone||"Unknown",loc:t.loc||"Unknown",ip_address:t.ip_address||"Unknown"}}catch(e){return r.error("Error retrieving device network parameters:",e),{city:"Unknown",region:"Unknown",country:"Unknown",timezone:"Unknown",loc:"Unknown",ip_address:"Unknown"}}}static async aquireAccessToken(e){try{return(await(await fetch(`${d.API_URL}/api/authenticate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:e,metadata:{sdk:d.package,version:d.version,hostname:window.location.hostname}})})).json()).accessToken}catch(t){throw r.error("Error acquiring access token:",t),new Error("[CPIX] Authentication error")}}}class h{static isPresent(e){return typeof e<"u"&&e!==null&&e!==""}static now(){return 1*new Date().getTime()}static guid(){return w.version+"-xxxxxxxx-".replace(/[x]/g,function(e){const t=Math.random()*36|0;return(e=="x"?t:t&3|8).toString(36)})+(1*new Date().getTime()).toString(36)}static optionalData(e){return h.isPresent(e)===!1?"":typeof e=="object"?h.optionalData(JSON.stringify(e)):typeof e=="function"?h.optionalData(e()):String(e)}static sleep(e){return new Promise(t=>setTimeout(t,e))}}class y{static getParametersByName(e,t){t||(t=window.location.href),e=e.replace(/[[\]]/g,"\\$&");const i=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)","gi"),s=[];let a;for(;(a=i.exec(t))!==null;)a[2]&&s.push(decodeURIComponent(a[2].replace(/\+/g," ")));return s.length>0?s:null}static externalHost(e){var t;return e.hostname!=location.hostname&&((t=e==null?void 0:e.protocol)==null?void 0:t.indexOf("http"))===0}}class n{static prefix(){return`__${d.TRACKER_FUNC_NAME}__`}static get(e){const t=`${n.prefix()}${e}`,i=document.cookie.split("; ").find(s=>s.startsWith(`${t}=`));return i?i.split("=")[1]:void 0}static set(e,t,i){const s=`${n.prefix()}${e}`,a=new Date;a.setTime(a.getTime()+i*60*1e3),document.cookie=`${s}=${t}; expires=${a.toUTCString()}; path=/`}static delete(e){const t=`${n.prefix()}${e}`;document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`}static clear(){document.cookie.split("; ").filter(e=>e.startsWith(n.prefix())).forEach(e=>{const t=e.split("=")[0];document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`})}static exists(e){return!!n.get(e)}static setUtms(){const e=x;let t=!1;for(let i=0;i<e.length;i++){const s=e[i];if(h.isPresent(y.getParametersByName(s,window.location.href))){t=!0;break}}if(t){let i;const s=[];for(let a=0;a<e.length;a++){const g=e[a];i=y.getParametersByName(g,window.location.href),s.push({[g]:i})}n.set(o.UTM,JSON.stringify(s),14*24*60)}}static getUtms(){const e=n.get(o.UTM);return h.isPresent(e)?JSON.parse(e):{}}static createSession(){n.exists(o.SESSION)||n.set(o.SESSION,h.guid(),14*24*60),n.setUtms()}}class A{constructor(){c(this,"instances",[]);c(this,"initialized",!1);c(this,"accessToken","");c(this,"metaParameters",{});c(this,"queue");if(!window)throw new Error("[CPIX] CarterAnalytics SDK can only be used in a browser environment");this.queue=new b({maxRetries:d.QUEUE_MAX_RETRIES,initialDelay:d.QUEUE_INITIAL_DELAY,consumerHandler:this.publishEventToServer.bind(this)})}async initialize(e){if(!e||e.length===0)throw new Error("[CPIX] At least one instance configuration is required for initialization");if(this.instances=e,d.DEBUG=this.instances[0].options.debug===!0,d.API_URL=this.instances[0].options.tracker_server_url,n.exists(o.SESSION)||n.createSession(),n.exists(o.META_PARAMETERS)||await this.generateMetaParameters().then(t=>{this.metaParameters=t,n.set("metaParameters",JSON.stringify(t),60*24)}),n.setUtms(),this.metaParameters=JSON.parse(n.get("metaParameters")),this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has already been initialized");if(!this.metaParameters)throw new Error("[CPIX] Meta parameters have not been generated");n.exists(o.ACCESS_TOKEN)||(this.accessToken=await m.aquireAccessToken(this.instances[0].client_id),n.set(o.ACCESS_TOKEN,this.accessToken,60*4)),this.initialized=!0,r.debug("CPIX Initialized"),r.debug("Carter Analytics SDK initialized with options:",this.instances),this.publish({event:"init",...this.metaParameters}),this.publish({event:"page_view",event_properties:{title:document.title,page:window.location.pathname,url:window.location.href},...this.metaParameters})}publish(e){if(!this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has not been initialized. Please initialize before publishing events.");const t={...e,...this.metaParameters};t.utm_params=n.getUtms(),document.referrer&&(t.referrer=encodeURIComponent(document.referrer)),r.debug("Publishing event:",t),this.queue.publish(t),e.event==="logout"&&(n.delete(o.SESSION),n.createSession(),this.generateMetaParameters().then(i=>{this.metaParameters=i,n.set(o.META_PARAMETERS,JSON.stringify(this.metaParameters),60*24)}))}async generateMetaParameters(){if(!window)throw new Error("[CPIX] Meta parameters can only be generated in a browser environment");const e=await m.getDeviceNetworkParameters();return{client_id:this.instances[0].client_id,session:n.get(o.SESSION),location:{city:e.city,region:e.region,loc:e.loc,timezone:e.timezone,country:e.country},device:{category:m.getDeviceCategory(),brand:m.getDeviceManufacturer(),ip_address:e.ip_address,platform:m.getDevicePlatform()},referrer:encodeURIComponent(document.referrer)}}async publishEventToServer(e){try{e.timestamp=Date.now(),e.event_properties={url:window.location.href,...e.event_properties};const t=await fetch(`${d.API_URL}/api/event`,{method:"POST",body:JSON.stringify({event_data:e}),headers:{"Content-Type":"application/json",Authorization:`Bearer ${n.get(o.ACCESS_TOKEN)}`}});if(!t.ok)throw t.status===401&&(this.accessToken=await m.aquireAccessToken(this.instances[0].client_id),n.set(o.ACCESS_TOKEN,this.accessToken,60*4)),new Error("[CPIX] Failed to publish event");r.debug("Event published:",t.ok)}catch(t){throw r.error("Failed to publish event:",t),new Error("[CPIX] Failed to publish event")}}}const v=new A;typeof window<"u"&&(window.cpix=v),u.CarterAnalytics=v,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
1
+ (function(l,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(l=typeof globalThis<"u"?globalThis:l||self,d(l.tracker={}))})(this,function(l){"use strict";var b=Object.defineProperty;var A=(l,d,p)=>d in l?b(l,d,{enumerable:!0,configurable:!0,writable:!0,value:p}):l[d]=p;var c=(l,d,p)=>(A(l,typeof d!="symbol"?d+"":d,p),p);const w={name:"@carter-rmn/cpix-js",version:"1.0.3",type:"module",files:["dist"],main:"dist/cpix.umd.cjs",module:"dist/cpix.js",types:"dist/cpix.d.ts",exports:{".":{import:"./dist/cpix.js",require:"./dist/cpix.umd.cjs"}},scripts:{dev:"vite",build:"tsc && vite build",preview:"vite preview"},devDependencies:{picocolors:"^1.0.0","rollup-plugin-visualizer":"^5.12.0",typescript:"^5.2.2",vite:"^5.2.0","vite-plugin-progress":"^0.0.7"},dependencies:{"vite-plugin-dts":"^3.8.1"}},r={DEBUG:!1,TRACKER_FUNC_NAME:"cpix",API_URL:"https://etg20oxcge.execute-api.ca-central-1.amazonaws.com/dev-lambda",QUEUE_MAX_RETRIES:3,QUEUE_INITIAL_DELAY:1e4,version:w.version,package:w.name,USER_PROPERTIES:{},SESSION_EXPIRY_DAYS:14},u={SESSION:"cpix_session",META_PARAMETERS:"cpix_meta_parameters",ACCESS_TOKEN:"cpix_access_token",UTM:"cpix_utm"},P=["utm_source","utm_medium","utm_term","utm_content","utm_campaign"],a=function(){const f=()=>r.DEBUG;return{info:function(...e){console.log(...e)},debug:function(...e){f()&&console.log(...e)},error:function(...e){console.error(...e)}}}();class x extends EventTarget{constructor(t={maxRetries:3,initialDelay:1e4,consumerHandler:i=>(a.debug("Consuming event:",i),Promise.resolve())}){super();c(this,"dataQueue");c(this,"retryQueue");c(this,"maxRetries");c(this,"initialDelay");c(this,"publishEvent");c(this,"retryEvent");c(this,"handler");this.dataQueue=[],this.retryQueue=[],this.maxRetries=t.maxRetries,this.initialDelay=t.initialDelay,this.handler=t.consumerHandler,this.publishEvent=new Event("eventPublished"),this.retryEvent=new Event("retryMessage"),this.consume(),this.retry()}publish(t){a.debug("publishing event");const i={data:t,id:`${Date.now()+Math.random()}`,retryCount:0,retryDelay:this.initialDelay};this.dataQueue.push(i),this.dispatchEvent(this.publishEvent)}consume(){a.debug("Started Consumer Queue"),this.addEventListener("eventPublished",async()=>{for(;this.dataQueue.length>0;){const t=this.dataQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(n){a.error("Failed to publish event:",n),this.scheduleRetry(t)}}})}scheduleRetry(t){if(t.retryCount>=this.maxRetries){a.error("Max retries reached for message:",t);return}setTimeout(()=>{this.retryQueue.push({...t,retryCount:t.retryCount+1,retryDelay:t.retryDelay*2}),this.dispatchEvent(this.retryEvent)},t.retryDelay)}acknowledge(t){this.dataQueue=this.dataQueue.filter(i=>i.id!==t)}retry(){a.debug("Started Retry Queue"),this.addEventListener("retryMessage",async()=>{for(;this.retryQueue.length>0;){const t=this.retryQueue.shift(),{data:i={}}=t;try{await this.handler(i),this.acknowledge(t==null?void 0:t.id)}catch(n){a.error("Failed to publish event:",n),this.scheduleRetry(t)}}})}}class m{static getBrowserInfo(){var n,o,g,S,v;const e=navigator.userAgent||"";let t,i;return/chrome/i.test(e)?(t="Chrome",i=(n=e.match(/chrome\/(\d+)/i))==null?void 0:n[1]):/firefox/i.test(e)?(t="Firefox",i=(o=e.match(/firefox\/(\d+)/i))==null?void 0:o[1]):/safari/i.test(e)?(t="Safari",i=(g=e.match(/version\/(\d+)/i))==null?void 0:g[1]):/edge/i.test(e)?(t="Edge",i=(S=e.match(/edge\/(\d+)/i))==null?void 0:S[1]):/trident/i.test(e)?(t="Internet Explorer",i=(v=e.match(/rv:(\d+)/i))==null?void 0:v[1]):(t="Unknown",i="Unknown"),`${t} ${i}`}static getDeviceCategory(){const e=navigator.userAgent;return/mobile/i.test(e)?"Mobile":/tablet/i.test(e)?"Tablet":"Desktop"}static getDevicePlatform(){const e=navigator.userAgent;return/android/i.test(e)?"Android":/iphone|ipad|ipod/i.test(e)?"iOS":/windows phone/i.test(e)?"Windows Phone":/mac|Macintosh/i.test(e)?"Mac":/windows|Microsoft/i.test(e)?"Windows":/linux/i.test(e)?"Linux":"Unknown"}static getDeviceManufacturer(){const e=navigator.userAgent;let t;return/iphone|ipad|ipod|mac|Macintosh/i.test(e)?t="Apple":/samsung/i.test(e)?t="Samsung":/google/i.test(e)?t="Google":/huawei/i.test(e)?t="Huawei":/xiaomi/i.test(e)?t="Xiaomi":/oneplus/i.test(e)?t="OnePlus":/dell/i.test(e)?t="Dell":/lenovo/i.test(e)?t="Lenovo":/acer/i.test(e)?t="Acer":/asus/i.test(e)?t="Asus":/toshiba/i.test(e)?t="Toshiba":t="Unknown",t}static async getDeviceNetworkParameters(){try{const e=await fetch(`${r.API_URL}/api/geolocation`),{data:t}=await e.json();return{city:t.city||"Unknown",region:t.region||"Unknown",country:t.country||"Unknown",timezone:t.timezone||"Unknown",loc:t.loc||"Unknown",ip_address:t.ip_address||"Unknown"}}catch(e){return a.error("Error retrieving device network parameters:",e),{city:"Unknown",region:"Unknown",country:"Unknown",timezone:"Unknown",loc:"Unknown",ip_address:"Unknown"}}}static async aquireAccessToken(e){try{return(await(await fetch(`${r.API_URL}/api/authenticate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:e,metadata:{sdk:r.package,version:r.version,hostname:window.location.hostname}})})).json()).accessToken}catch(t){throw a.error("Error acquiring access token:",t),new Error("[CPIX] Authentication error")}}}class h{static isPresent(e){return typeof e<"u"&&e!==null&&e!==""}static now(){return 1*new Date().getTime()}static guid(){return w.version+"-xxxxxxxx-".replace(/[x]/g,function(e){const t=Math.random()*36|0;return(e=="x"?t:t&3|8).toString(36)})+(1*new Date().getTime()).toString(36)}static optionalData(e){return h.isPresent(e)===!1?"":typeof e=="object"?h.optionalData(JSON.stringify(e)):typeof e=="function"?h.optionalData(e()):String(e)}static sleep(e){return new Promise(t=>setTimeout(t,e))}}class y{static getParametersByName(e,t){t||(t=window.location.href),e=e.replace(/[[\]]/g,"\\$&");const i=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)","gi"),n=[];let o;for(;(o=i.exec(t))!==null;)o[2]&&n.push(decodeURIComponent(o[2].replace(/\+/g," ")));return n.length>0?n:null}static externalHost(e){var t;return e.hostname!=location.hostname&&((t=e==null?void 0:e.protocol)==null?void 0:t.indexOf("http"))===0}}class s{static prefix(){return`__${r.TRACKER_FUNC_NAME}__`}static get(e){const t=`${s.prefix()}${e}`,i=document.cookie.split("; ").find(n=>n.startsWith(`${t}=`));return i?i.split("=")[1]:void 0}static set(e,t,i){const n=`${s.prefix()}${e}`,o=new Date;o.setTime(o.getTime()+i*60*1e3),document.cookie=`${n}=${t}; expires=${o.toUTCString()}; path=/`}static delete(e){const t=`${s.prefix()}${e}`;document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`}static clear(){document.cookie.split("; ").filter(e=>e.startsWith(s.prefix())).forEach(e=>{const t=e.split("=")[0];document.cookie=`${t}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`})}static exists(e){return!!s.get(e)}static setUtms(){const e=P;let t=!1;for(let i=0;i<e.length;i++){const n=e[i];if(h.isPresent(y.getParametersByName(n,window.location.href))){t=!0;break}}if(t){let i;const n=[];for(let o=0;o<e.length;o++){const g=e[o];i=y.getParametersByName(g,window.location.href),n.push({[g]:i})}s.set(u.UTM,JSON.stringify(n),r.SESSION_EXPIRY_DAYS*24*60)}}static getUtms(){const e=s.get(u.UTM);return h.isPresent(e)?JSON.parse(e):{}}static createSession(){s.exists(u.SESSION)||s.set(u.SESSION,h.guid(),r.SESSION_EXPIRY_DAYS*24*60),s.setUtms()}}class _{constructor(){c(this,"instances",[]);c(this,"initialized",!1);c(this,"sessionCreated",!1);c(this,"accessToken","");c(this,"metaParameters",{});c(this,"queue");if(!window)throw new Error("[CPIX] CarterAnalytics SDK can only be used in a browser environment");this.queue=new x({maxRetries:r.QUEUE_MAX_RETRIES,initialDelay:r.QUEUE_INITIAL_DELAY,consumerHandler:this.publishEventToServer.bind(this)})}async initialize(e){if(!e||e.length===0)throw new Error("[CPIX] At least one instance configuration is required for initialization");if(this.instances=e,r.DEBUG=this.instances[0].options.debug===!0,r.API_URL=this.instances[0].options.tracker_server_url,r.USER_PROPERTIES=this.instances[0].options.user_properties||{},r.SESSION_EXPIRY_DAYS=this.instances[0].options.session_expiry_days||14,s.exists(u.SESSION)||(s.createSession(),this.sessionCreated=!0),s.exists(u.META_PARAMETERS)||await this.generateMetaParameters().then(t=>{this.metaParameters=t,s.set("metaParameters",JSON.stringify(t),60*24)}),s.setUtms(),this.metaParameters=JSON.parse(s.get("metaParameters")),this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has already been initialized");if(!this.metaParameters)throw new Error("[CPIX] Meta parameters have not been generated");s.exists(u.ACCESS_TOKEN)||(this.accessToken=await m.aquireAccessToken(this.instances[0].client_id),s.set(u.ACCESS_TOKEN,this.accessToken,60*4)),this.initialized=!0,a.debug("CPIX Initialized"),a.debug("Carter Analytics SDK initialized with options:",this.instances),this.sessionCreated&&this.publish({event:"init",user_properties:r.USER_PROPERTIES,...this.metaParameters}),this.attachHistoryListener()}attachHistoryListener(){const e=history.pushState,t=history.replaceState,i=()=>{this.publish({event:"page_view",event_properties:{title:document.title,page:window.location.pathname,url:window.location.href},user_properties:r.USER_PROPERTIES,...this.metaParameters})};history.pushState=(...n)=>{e.apply(history,n),i()},history.replaceState=(...n)=>{t.apply(history,n),i()},window.addEventListener("popstate",i),i()}publish(e){if(!this.initialized)throw new Error("[CPIX] CarterAnalytics SDK has not been initialized. Please initialize before publishing events.");const t={...e,...this.metaParameters};t.utm_params=s.getUtms(),document.referrer&&(t.referrer=encodeURIComponent(document.referrer)),a.debug("Publishing event:",t),this.queue.publish(t),e.event==="logout"&&(s.delete(u.SESSION),s.createSession(),this.generateMetaParameters().then(i=>{this.metaParameters=i,s.set(u.META_PARAMETERS,JSON.stringify(this.metaParameters),60*24)}))}async generateMetaParameters(){if(!window)throw new Error("[CPIX] Meta parameters can only be generated in a browser environment");const e=await m.getDeviceNetworkParameters();return{client_id:this.instances[0].client_id,session:s.get(u.SESSION),location:{city:e.city,region:e.region,loc:e.loc,timezone:e.timezone,country:e.country},device:{category:m.getDeviceCategory(),brand:m.getDeviceManufacturer(),ip_address:e.ip_address,platform:m.getDevicePlatform()},referrer:encodeURIComponent(document.referrer)}}async publishEventToServer(e){try{e.timestamp=Date.now(),e.event_properties={url:window.location.href,...e.event_properties};const t=await fetch(`${r.API_URL}/api/event`,{method:"POST",body:JSON.stringify({event_data:e}),headers:{"Content-Type":"application/json",Authorization:`Bearer ${s.get(u.ACCESS_TOKEN)}`}});if(!t.ok)throw t.status===401&&(this.accessToken=await m.aquireAccessToken(this.instances[0].client_id),s.set(u.ACCESS_TOKEN,this.accessToken,60*4)),new Error("[CPIX] Failed to publish event");a.debug("Event published:",t.ok)}catch(t){throw a.error("Failed to publish event:",t),new Error("[CPIX] Failed to publish event")}}}const E=new _;typeof window<"u"&&(window.cpix=E),l.CarterAnalytics=E,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
@@ -6,6 +6,7 @@ import { Event, Init } from '../types/common.types';
6
6
  declare class CarterAnalytics {
7
7
  private instances;
8
8
  private initialized;
9
+ private sessionCreated;
9
10
  private accessToken;
10
11
  private metaParameters;
11
12
  private queue;
@@ -15,6 +16,10 @@ declare class CarterAnalytics {
15
16
  * @param options - The configuration options for initializing the SDK.
16
17
  */
17
18
  initialize(options: Init[]): Promise<void>;
19
+ /**
20
+ * Attaches a history listener to publish a page_view event whenever the page changes.
21
+ */
22
+ private attachHistoryListener;
18
23
  /**
19
24
  * Publishes an event to the analytics queue.
20
25
  * @param event - The event to be published.
@@ -4,6 +4,8 @@
4
4
  export interface InitOptions {
5
5
  debug?: boolean;
6
6
  tracker_server_url: string;
7
+ session_expiry_days?: number;
8
+ user_properties?: UserProperties;
7
9
  }
8
10
  /**
9
11
  * Represents the initialization options for the application.
@@ -16,7 +18,7 @@ export interface Init {
16
18
  /**
17
19
  * Additional options for the application.
18
20
  */
19
- options: Record<string, unknown>;
21
+ options: InitOptions;
20
22
  }
21
23
  /**
22
24
  * Represents the event properties.
@@ -31,7 +33,7 @@ export interface UserProperties {
31
33
  /**
32
34
  * The user ID.
33
35
  */
34
- user_id: string;
36
+ user_id?: string;
35
37
  [key: string]: unknown;
36
38
  }
37
39
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carter-rmn/cpix-js",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -14,11 +14,6 @@
14
14
  "require": "./dist/cpix.umd.cjs"
15
15
  }
16
16
  },
17
- "scripts": {
18
- "dev": "vite",
19
- "build": "tsc && vite build",
20
- "preview": "vite preview"
21
- },
22
17
  "devDependencies": {
23
18
  "picocolors": "^1.0.0",
24
19
  "rollup-plugin-visualizer": "^5.12.0",
@@ -28,5 +23,10 @@
28
23
  },
29
24
  "dependencies": {
30
25
  "vite-plugin-dts": "^3.8.1"
26
+ },
27
+ "scripts": {
28
+ "dev": "vite",
29
+ "build": "tsc && vite build",
30
+ "preview": "vite preview"
31
31
  }
32
- }
32
+ }