@hifilabs/pixel 0.1.1 → 0.1.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/dist/index.esm.js CHANGED
@@ -371,7 +371,7 @@ function useBalanceIdentify() {
371
371
  }
372
372
 
373
373
  // src/react/GTMProvider.tsx
374
- import React3, { useEffect as useEffect4 } from "react";
374
+ import React3 from "react";
375
375
  import Script from "next/script";
376
376
 
377
377
  // src/react/useGTMConsent.ts
@@ -471,27 +471,29 @@ var DEFAULT_GTM_CONSENT = {
471
471
  function GTMProvider({ gtmId, children, debug = false }) {
472
472
  const resolvedGtmId = gtmId || process.env.NEXT_PUBLIC_GTM_ID;
473
473
  useGTMConsent({ debug });
474
- useEffect4(() => {
475
- if (typeof window === "undefined" || !resolvedGtmId)
476
- return;
477
- window.dataLayer = window.dataLayer || [];
478
- if (typeof window.gtag !== "function") {
479
- window.gtag = function gtag(...args) {
480
- window.dataLayer.push(args);
481
- };
482
- }
483
- window.gtag("consent", "default", DEFAULT_GTM_CONSENT);
484
- if (debug) {
485
- console.log("[GTMProvider] Initialized with default consent:", DEFAULT_GTM_CONSENT);
486
- }
487
- }, [resolvedGtmId, debug]);
488
474
  if (!resolvedGtmId) {
489
475
  if (debug) {
490
476
  console.log("[GTMProvider] No GTM ID configured, skipping GTM initialization");
491
477
  }
492
478
  return /* @__PURE__ */ React3.createElement(React3.Fragment, null, children);
493
479
  }
480
+ const consentJson = JSON.stringify(DEFAULT_GTM_CONSENT);
481
+ const consentScript = `
482
+ window.dataLayer = window.dataLayer || [];
483
+ function gtag(){dataLayer.push(arguments);}
484
+ window.gtag = gtag;
485
+ gtag('consent', 'default', ${consentJson});
486
+ gtag('set', 'wait_for_update', 500);
487
+ ${debug ? "console.log('[GTM] Default consent initialized (denied)');" : ""}
488
+ `;
494
489
  return /* @__PURE__ */ React3.createElement(React3.Fragment, null, /* @__PURE__ */ React3.createElement(
490
+ Script,
491
+ {
492
+ id: "gtm-consent-default",
493
+ strategy: "beforeInteractive",
494
+ dangerouslySetInnerHTML: { __html: consentScript }
495
+ }
496
+ ), /* @__PURE__ */ React3.createElement(
495
497
  Script,
496
498
  {
497
499
  id: "gtm-script",
package/dist/index.js CHANGED
@@ -136,6 +136,18 @@ var BalancePixel = (() => {
136
136
  const debug = currentScript?.dataset.debug === "true";
137
137
  const heartbeatInterval = parseInt(currentScript?.dataset.heartbeatInterval || "30000", 10);
138
138
  const heartbeatEnabled = currentScript?.dataset.heartbeat !== "false";
139
+ const explicitSource = currentScript?.dataset.source;
140
+ function detectTrackingSource() {
141
+ if (explicitSource)
142
+ return explicitSource;
143
+ const hasDataLayer = typeof window.dataLayer !== "undefined" && Array.isArray(window.dataLayer);
144
+ const hasGtag = typeof window.gtag === "function";
145
+ if (hasDataLayer && hasGtag) {
146
+ return "gtm";
147
+ }
148
+ return "pixel";
149
+ }
150
+ let trackingSource = "pixel";
139
151
  if (!artistId) {
140
152
  console.error("[BALANCE Pixel] Error: data-artist-id attribute is required");
141
153
  return;
@@ -356,6 +368,8 @@ var BalancePixel = (() => {
356
368
  device_type: deviceInfo.device_type,
357
369
  browser: deviceInfo.browser,
358
370
  os: deviceInfo.os,
371
+ // Tracking source (gtm, pixel, etc.) - enables filtering in artistHQ
372
+ tracking_source: trackingSource,
359
373
  ...partial,
360
374
  ...attribution
361
375
  };
@@ -549,6 +563,8 @@ var BalancePixel = (() => {
549
563
  enqueueEvent(event);
550
564
  }
551
565
  function init() {
566
+ trackingSource = detectTrackingSource();
567
+ log("Tracking source detected:", trackingSource);
552
568
  loadConsent();
553
569
  if (consent?.analytics === true) {
554
570
  currentStorageTier = "local";
@@ -578,6 +594,7 @@ var BalancePixel = (() => {
578
594
  fanIdHash,
579
595
  consent,
580
596
  storageTier: currentStorageTier,
597
+ trackingSource,
581
598
  useEmulator,
582
599
  endpoint: API_ENDPOINT
583
600
  });
package/dist/index.min.js CHANGED
@@ -1 +1 @@
1
- var BalancePixel=(()=>{var Se=Object.create;var k=Object.defineProperty;var Ie=Object.getOwnPropertyDescriptor;var _e=Object.getOwnPropertyNames;var Ae=Object.getPrototypeOf,Pe=Object.prototype.hasOwnProperty;var Q=(i=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(i,{get:(r,s)=>(typeof require<"u"?require:r)[s]}):i)(function(i){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+i+'" is not supported')});var Ee=(i,r)=>{for(var s in r)k(i,s,{get:r[s],enumerable:!0})},X=(i,r,s,c)=>{if(r&&typeof r=="object"||typeof r=="function")for(let d of _e(r))!Pe.call(i,d)&&d!==s&&k(i,d,{get:()=>r[d],enumerable:!(c=Ie(r,d))||c.enumerable});return i};var ke=(i,r,s)=>(s=i!=null?Se(Ae(i)):{},X(r||!i||!i.__esModule?k(s,"default",{value:i,enumerable:!0}):s,i)),Ce=i=>X(k({},"__esModule",{value:!0}),i);var je={};Ee(je,{BalanceAnalytics:()=>Z,getAttribution:()=>Le,getConsent:()=>He,getFanIdHash:()=>Be,getSessionId:()=>Ue,hasConsent:()=>ze,identify:()=>Oe,page:()=>Ne,purchase:()=>Re,setConsent:()=>Fe,track:()=>De});var u=ke(Q("react")),C=Q("next/navigation");function Te(){let i=(0,C.usePathname)(),r=(0,C.useSearchParams)(),s=(0,u.useRef)(!0),c=(0,u.useRef)(null);return(0,u.useEffect)(()=>{if(typeof window>"u"||!window.balance)return;let d=i+(r?.toString()||"");if(c.current===d)return;if(s.current&&(s.current=!1,window._balanceInitialPageviewFired)){c.current=d,console.log("[BalanceAnalytics] Skipping initial pageview (script already fired)");return}let v=window.location.href,_=document.title;c.current=d,window.balance.page({url:v,title:_})},[i,r]),null}function Z(){return u.default.createElement(u.Suspense,{fallback:null},u.default.createElement(Te,null))}(function(){function i(e){let t="desktop";/ipad|tablet|android(?!.*mobile)/i.test(e)?t="tablet":/mobile|iphone|android.*mobile|blackberry|iemobile/i.test(e)&&(t="mobile");let n="Unknown";/edg/i.test(e)?n="Edge":/opr|opera/i.test(e)?n="Opera":/firefox/i.test(e)?n="Firefox":/chrome/i.test(e)?n="Chrome":/safari/i.test(e)&&(n="Safari");let o="Unknown";return/iphone|ipad/i.test(e)?o="iOS":/android/i.test(e)?o="Android":/windows/i.test(e)?o="Windows":/mac os/i.test(e)?o="macOS":/linux/i.test(e)?o="Linux":/cros/i.test(e)&&(o="ChromeOS"),{device_type:t,browser:n,os:o}}let r=null;function s(){if(!r)try{r=i(navigator.userAgent)}catch{r={device_type:"desktop",browser:"Unknown",os:"Unknown"}}return r}let c=document.currentScript,d=c?.dataset.artistId,v=c?.dataset.projectId,_=c?.dataset.emulator==="true",ee=c?.dataset.debug==="true",T=parseInt(c?.dataset.heartbeatInterval||"30000",10),H=c?.dataset.heartbeat!=="false";if(!d){console.error("[BALANCE Pixel] Error: data-artist-id attribute is required");return}let z="session_id",D="session_timestamp",j="attribution",M="fan_id_hash",K="balance_consent",te=60*60*1e3,O="balance_",N=_?"http://localhost:5001/artist-os-distro/us-central1/pixelEndpoint":"https://us-central1-artist-os-distro.cloudfunctions.net/pixelEndpoint",A=null,f=null,l=null,m={},g=[],R=null,p="session",w=null,ne=0,U=0,b=0,x=!0,ie=2*60*1e3,J=Date.now(),S=!1,a=(...e)=>{ee&&console.log("[BALANCE Pixel]",...e)};function Y(){try{return p==="local"?localStorage:sessionStorage}catch{return null}}function P(e){let t=Y();if(!t)return null;try{let n=O+e,o=t.getItem(n);if(!o&&p==="session")try{o=localStorage.getItem(n)}catch{}return o}catch{return null}}function I(e,t){let n=Y();if(n)try{n.setItem(O+e,t)}catch{}}function B(){if(p!=="local"){a("Upgrading storage tier: session -> local");try{let e=[];for(let t=0;t<sessionStorage.length;t++){let n=sessionStorage.key(t);n?.startsWith(O)&&e.push(n)}for(let t of e){let n=sessionStorage.getItem(t);n&&localStorage.setItem(t,n)}for(let t of e)sessionStorage.removeItem(t);p="local",a(`Storage tier upgraded, migrated ${e.length} items`)}catch(e){console.error("[BALANCE Pixel] Storage migration failed:",e)}}}function q(){return crypto&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,e=>{let t=Math.random()*16|0;return(e==="x"?t:t&3|8).toString(16)})}function re(){try{let e=P(z),t=P(D);if(e&&t&&Date.now()-parseInt(t,10)<te)return I(D,Date.now().toString()),e;let n=q();return I(z,n),I(D,Date.now().toString()),n}catch{return q()}}function ae(){let e=new URLSearchParams(window.location.search),t={};return["source","medium","campaign","content","term"].forEach(n=>{let o=e.get(`utm_${n}`);o&&(t[`utm_${n}`]=o)}),t}function oe(){try{let e=P(j);if(e){m=JSON.parse(e),a("Loaded attribution:",m);return}let t=ae();Object.keys(t).length>0&&(m=t,I(j,JSON.stringify(t)),a("Captured attribution:",m))}catch{}}function se(){try{f=P(M)}catch{}}function le(){try{let e=localStorage.getItem(K);e&&(l=JSON.parse(e).preferences||null,a("Loaded consent:",l))}catch{}}function ce(e){let t=l;l=e;try{let o={preferences:e,method:"explicit",version:1};localStorage.setItem(K,JSON.stringify(o)),a("Consent saved:",e)}catch(o){console.error("[BALANCE Pixel] Could not save consent:",o)}e.analytics===!0&&B();let n=h({event_name:"consent_updated",metadata:{consent_preferences:e,consent_method:"explicit",previous_consent:t||void 0}});y(n)}function de(){return l}function ue(e){return l?.[e]===!0}async function fe(e){let t=e.toLowerCase().trim(),o=new TextEncoder().encode(t),F=await crypto.subtle.digest("SHA-256",o);return Array.from(new Uint8Array(F)).map(xe=>xe.toString(16).padStart(2,"0")).join("")}function h(e){let t=s(),n={artist_id:d,fan_session_id:A,fan_id_hash:f||void 0,timestamp:new Date().toISOString(),source_url:window.location.href,referrer_url:document.referrer||void 0,user_agent:navigator.userAgent,device_type:t.device_type,browser:t.browser,os:t.os,...e,...m};return v&&!e.projectId&&(n.projectId=v),n}function y(e){g.push(e),a("Event queued:",e.event_name,"(queue:",g.length,")"),g.length>=10&&E()}async function E(){if(g.length===0)return;let e=[...g];g=[],a("Flushing",e.length,"events to",N);try{let t=await fetch(N,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({events:e}),keepalive:!0});if(!t.ok)throw new Error(`HTTP ${t.status}`);a("Events sent successfully")}catch(t){console.error("[BALANCE Pixel] Failed to send events:",t),g.length<50&&g.push(...e)}}function ge(){R&&clearInterval(R),R=window.setInterval(()=>{g.length>0&&E()},5e3)}function L(){b||(ne=Date.now()),b=Date.now(),x=!0,a("Active time tracking started/resumed")}function $(){b&&x&&(U+=Date.now()-b),x=!1,a("Active time tracking paused, accumulated:",U,"ms")}function pe(){let e=U;return x&&b&&(e+=Date.now()-b),e}function me(){J=Date.now(),S&&(S=!1,L(),a("User returned from idle"))}function G(){if(document.visibilityState==="hidden"){a("Skipping heartbeat - tab hidden");return}if(Date.now()-J>ie){S||(S=!0,$(),a("User idle - pausing heartbeat"));return}let e=pe(),t=Math.round(e/1e3),n=h({event_name:"engagement_heartbeat",metadata:{time_on_page_seconds:t,time_on_page_ms:e,heartbeat_interval_ms:T,is_active:x&&!S}});y(n),a("Heartbeat sent:",t,"seconds active")}function we(){if(!H){a('Heartbeat disabled via data-heartbeat="false"');return}w&&clearInterval(w),L(),w=window.setInterval(()=>{G()},T),a("Heartbeat started with interval:",T,"ms")}function be(){w&&(clearInterval(w),w=null),H&&(G(),a("Heartbeat stopped, final time sent"))}function W(e={}){let t=h({event_name:"pageview",page_title:e.title||document.title,source_url:e.url||window.location.href});y(t)}function he(e,t={}){let n=h({event_name:"custom",metadata:{event_type:e,...t}});y(n)}async function ye(e,t={}){try{if(l&&l.analytics===!1){a("Identify skipped - user declined analytics consent");return}f=await fe(e),l?.analytics===!0&&B(),I(M,f);let n=e.split("@"),o=n[0].charAt(0)+"***@"+(n[1]||"");a("Fan identified:",{name:t.name||"(no name)",email:o,hash:f.substring(0,16)+"...",traits:t,storageTier:p});let F=h({event_name:"identify",fan_id_hash:f,metadata:{email_sha256:f,traits:t,consent_preferences:l||void 0,storage_tier:p}});y(F)}catch(n){console.error("[BALANCE Pixel] Failed to identify:",n)}}function ve(e,t="USD",n={}){let o=h({event_name:"purchase",metadata:{revenue:e,currency:t,...n}});y(o)}function V(){le(),l?.analytics===!0?(p="local",a("Storage tier: local (analytics consent granted)")):(p="session",a("Storage tier: session (privacy by default)")),A=re(),se(),l||(l={analytics:!0,marketing:!0,personalization:!0,timestamp:new Date().toISOString()},a("Default consent enabled (all tracking):",l),B()),oe(),ge(),a("Initialized",{artistId:d,projectId:v||"(none - will track to all projects)",sessionId:A,fanIdHash:f,consent:l,storageTier:p,useEmulator:_,endpoint:N}),window._balanceInitialPageviewFired=!0,W(),we(),["mousemove","keydown","scroll","touchstart"].forEach(e=>{document.addEventListener(e,me,{passive:!0})}),window.addEventListener("beforeunload",()=>{be(),E()}),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"?($(),E()):L()})}window.balance={track:he,identify:ye,page:W,purchase:ve,getSessionId:()=>A,getFanIdHash:()=>f,getAttribution:()=>m,setConsent:ce,getConsent:de,hasConsent:ue},document.readyState==="loading"?document.addEventListener("DOMContentLoaded",V):V(),a("Pixel script loaded")})();var De=(i,r)=>{typeof window>"u"||(window.balance?window.balance.track(i,r):console.warn("[Balance Pixel] track() called before pixel initialized:",i))},Oe=(i,r)=>typeof window>"u"?Promise.resolve():window.balance?window.balance.identify(i,r):(console.warn("[Balance Pixel] identify() called before pixel initialized"),Promise.resolve()),Ne=i=>{typeof window>"u"||(window.balance?window.balance.page(i):console.warn("[Balance Pixel] page() called before pixel initialized"))},Re=(i,r,s)=>{typeof window>"u"||(window.balance?window.balance.purchase(i,r,s):console.warn("[Balance Pixel] purchase() called before pixel initialized"))},Ue=()=>typeof window>"u"?null:window.balance?.getSessionId()??null,Be=()=>typeof window>"u"?null:window.balance?.getFanIdHash()??null,Le=()=>typeof window>"u"?{}:window.balance?.getAttribution()??{},Fe=i=>{typeof window>"u"||(window.balance?window.balance.setConsent(i):console.warn("[Balance Pixel] setConsent() called before pixel initialized"))},He=()=>typeof window>"u"?null:window.balance?.getConsent()??null,ze=i=>typeof window>"u"?!1:window.balance?.hasConsent(i)??!1;return Ce(je);})();
1
+ var BalancePixel=(()=>{var Ae=Object.create;var T=Object.defineProperty;var ke=Object.getOwnPropertyDescriptor;var Pe=Object.getOwnPropertyNames;var Ee=Object.getPrototypeOf,Te=Object.prototype.hasOwnProperty;var Z=(i=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(i,{get:(r,s)=>(typeof require<"u"?require:r)[s]}):i)(function(i){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+i+'" is not supported')});var Ce=(i,r)=>{for(var s in r)T(i,s,{get:r[s],enumerable:!0})},ee=(i,r,s,l)=>{if(r&&typeof r=="object"||typeof r=="function")for(let d of Pe(r))!Te.call(i,d)&&d!==s&&T(i,d,{get:()=>r[d],enumerable:!(l=ke(r,d))||l.enumerable});return i};var De=(i,r,s)=>(s=i!=null?Ae(Ee(i)):{},ee(r||!i||!i.__esModule?T(s,"default",{value:i,enumerable:!0}):s,i)),Oe=i=>ee(T({},"__esModule",{value:!0}),i);var Je={};Ce(Je,{BalanceAnalytics:()=>te,getAttribution:()=>ze,getConsent:()=>Me,getFanIdHash:()=>He,getSessionId:()=>Fe,hasConsent:()=>Ke,identify:()=>Re,page:()=>Ue,purchase:()=>Be,setConsent:()=>je,track:()=>Ne});var u=De(Z("react")),C=Z("next/navigation");function Le(){let i=(0,C.usePathname)(),r=(0,C.useSearchParams)(),s=(0,u.useRef)(!0),l=(0,u.useRef)(null);return(0,u.useEffect)(()=>{if(typeof window>"u"||!window.balance)return;let d=i+(r?.toString()||"");if(l.current===d)return;if(s.current&&(s.current=!1,window._balanceInitialPageviewFired)){l.current=d,console.log("[BalanceAnalytics] Skipping initial pageview (script already fired)");return}let v=window.location.href,_=document.title;l.current=d,window.balance.page({url:v,title:_})},[i,r]),null}function te(){return u.default.createElement(u.Suspense,{fallback:null},u.default.createElement(Le,null))}(function(){function i(e){let t="desktop";/ipad|tablet|android(?!.*mobile)/i.test(e)?t="tablet":/mobile|iphone|android.*mobile|blackberry|iemobile/i.test(e)&&(t="mobile");let n="Unknown";/edg/i.test(e)?n="Edge":/opr|opera/i.test(e)?n="Opera":/firefox/i.test(e)?n="Firefox":/chrome/i.test(e)?n="Chrome":/safari/i.test(e)&&(n="Safari");let o="Unknown";return/iphone|ipad/i.test(e)?o="iOS":/android/i.test(e)?o="Android":/windows/i.test(e)?o="Windows":/mac os/i.test(e)?o="macOS":/linux/i.test(e)?o="Linux":/cros/i.test(e)&&(o="ChromeOS"),{device_type:t,browser:n,os:o}}let r=null;function s(){if(!r)try{r=i(navigator.userAgent)}catch{r={device_type:"desktop",browser:"Unknown",os:"Unknown"}}return r}let l=document.currentScript,d=l?.dataset.artistId,v=l?.dataset.projectId,_=l?.dataset.emulator==="true",ne=l?.dataset.debug==="true",D=parseInt(l?.dataset.heartbeatInterval||"30000",10),z=l?.dataset.heartbeat!=="false",j=l?.dataset.source;function ie(){if(j)return j;let e=typeof window.dataLayer<"u"&&Array.isArray(window.dataLayer),t=typeof window.gtag=="function";return e&&t?"gtm":"pixel"}let A="pixel";if(!d){console.error("[BALANCE Pixel] Error: data-artist-id attribute is required");return}let M="session_id",O="session_timestamp",K="attribution",J="fan_id_hash",Y="balance_consent",re=60*60*1e3,L="balance_",N=_?"http://localhost:5001/artist-os-distro/us-central1/pixelEndpoint":"https://us-central1-artist-os-distro.cloudfunctions.net/pixelEndpoint",k=null,f=null,c=null,m={},g=[],R=null,p="session",w=null,ae=0,U=0,y=0,x=!0,oe=2*60*1e3,q=Date.now(),S=!1,a=(...e)=>{ne&&console.log("[BALANCE Pixel]",...e)};function G(){try{return p==="local"?localStorage:sessionStorage}catch{return null}}function P(e){let t=G();if(!t)return null;try{let n=L+e,o=t.getItem(n);if(!o&&p==="session")try{o=localStorage.getItem(n)}catch{}return o}catch{return null}}function I(e,t){let n=G();if(n)try{n.setItem(L+e,t)}catch{}}function B(){if(p!=="local"){a("Upgrading storage tier: session -> local");try{let e=[];for(let t=0;t<sessionStorage.length;t++){let n=sessionStorage.key(t);n?.startsWith(L)&&e.push(n)}for(let t of e){let n=sessionStorage.getItem(t);n&&localStorage.setItem(t,n)}for(let t of e)sessionStorage.removeItem(t);p="local",a(`Storage tier upgraded, migrated ${e.length} items`)}catch(e){console.error("[BALANCE Pixel] Storage migration failed:",e)}}}function $(){return crypto&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,e=>{let t=Math.random()*16|0;return(e==="x"?t:t&3|8).toString(16)})}function se(){try{let e=P(M),t=P(O);if(e&&t&&Date.now()-parseInt(t,10)<re)return I(O,Date.now().toString()),e;let n=$();return I(M,n),I(O,Date.now().toString()),n}catch{return $()}}function ce(){let e=new URLSearchParams(window.location.search),t={};return["source","medium","campaign","content","term"].forEach(n=>{let o=e.get(`utm_${n}`);o&&(t[`utm_${n}`]=o)}),t}function le(){try{let e=P(K);if(e){m=JSON.parse(e),a("Loaded attribution:",m);return}let t=ce();Object.keys(t).length>0&&(m=t,I(K,JSON.stringify(t)),a("Captured attribution:",m))}catch{}}function de(){try{f=P(J)}catch{}}function ue(){try{let e=localStorage.getItem(Y);e&&(c=JSON.parse(e).preferences||null,a("Loaded consent:",c))}catch{}}function fe(e){let t=c;c=e;try{let o={preferences:e,method:"explicit",version:1};localStorage.setItem(Y,JSON.stringify(o)),a("Consent saved:",e)}catch(o){console.error("[BALANCE Pixel] Could not save consent:",o)}e.analytics===!0&&B();let n=b({event_name:"consent_updated",metadata:{consent_preferences:e,consent_method:"explicit",previous_consent:t||void 0}});h(n)}function ge(){return c}function pe(e){return c?.[e]===!0}async function me(e){let t=e.toLowerCase().trim(),o=new TextEncoder().encode(t),H=await crypto.subtle.digest("SHA-256",o);return Array.from(new Uint8Array(H)).map(_e=>_e.toString(16).padStart(2,"0")).join("")}function b(e){let t=s(),n={artist_id:d,fan_session_id:k,fan_id_hash:f||void 0,timestamp:new Date().toISOString(),source_url:window.location.href,referrer_url:document.referrer||void 0,user_agent:navigator.userAgent,device_type:t.device_type,browser:t.browser,os:t.os,tracking_source:A,...e,...m};return v&&!e.projectId&&(n.projectId=v),n}function h(e){g.push(e),a("Event queued:",e.event_name,"(queue:",g.length,")"),g.length>=10&&E()}async function E(){if(g.length===0)return;let e=[...g];g=[],a("Flushing",e.length,"events to",N);try{let t=await fetch(N,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({events:e}),keepalive:!0});if(!t.ok)throw new Error(`HTTP ${t.status}`);a("Events sent successfully")}catch(t){console.error("[BALANCE Pixel] Failed to send events:",t),g.length<50&&g.push(...e)}}function we(){R&&clearInterval(R),R=window.setInterval(()=>{g.length>0&&E()},5e3)}function F(){y||(ae=Date.now()),y=Date.now(),x=!0,a("Active time tracking started/resumed")}function W(){y&&x&&(U+=Date.now()-y),x=!1,a("Active time tracking paused, accumulated:",U,"ms")}function ye(){let e=U;return x&&y&&(e+=Date.now()-y),e}function be(){q=Date.now(),S&&(S=!1,F(),a("User returned from idle"))}function V(){if(document.visibilityState==="hidden"){a("Skipping heartbeat - tab hidden");return}if(Date.now()-q>oe){S||(S=!0,W(),a("User idle - pausing heartbeat"));return}let e=ye(),t=Math.round(e/1e3),n=b({event_name:"engagement_heartbeat",metadata:{time_on_page_seconds:t,time_on_page_ms:e,heartbeat_interval_ms:D,is_active:x&&!S}});h(n),a("Heartbeat sent:",t,"seconds active")}function he(){if(!z){a('Heartbeat disabled via data-heartbeat="false"');return}w&&clearInterval(w),F(),w=window.setInterval(()=>{V()},D),a("Heartbeat started with interval:",D,"ms")}function ve(){w&&(clearInterval(w),w=null),z&&(V(),a("Heartbeat stopped, final time sent"))}function Q(e={}){let t=b({event_name:"pageview",page_title:e.title||document.title,source_url:e.url||window.location.href});h(t)}function xe(e,t={}){let n=b({event_name:"custom",metadata:{event_type:e,...t}});h(n)}async function Se(e,t={}){try{if(c&&c.analytics===!1){a("Identify skipped - user declined analytics consent");return}f=await me(e),c?.analytics===!0&&B(),I(J,f);let n=e.split("@"),o=n[0].charAt(0)+"***@"+(n[1]||"");a("Fan identified:",{name:t.name||"(no name)",email:o,hash:f.substring(0,16)+"...",traits:t,storageTier:p});let H=b({event_name:"identify",fan_id_hash:f,metadata:{email_sha256:f,traits:t,consent_preferences:c||void 0,storage_tier:p}});h(H)}catch(n){console.error("[BALANCE Pixel] Failed to identify:",n)}}function Ie(e,t="USD",n={}){let o=b({event_name:"purchase",metadata:{revenue:e,currency:t,...n}});h(o)}function X(){A=ie(),a("Tracking source detected:",A),ue(),c?.analytics===!0?(p="local",a("Storage tier: local (analytics consent granted)")):(p="session",a("Storage tier: session (privacy by default)")),k=se(),de(),c||(c={analytics:!0,marketing:!0,personalization:!0,timestamp:new Date().toISOString()},a("Default consent enabled (all tracking):",c),B()),le(),we(),a("Initialized",{artistId:d,projectId:v||"(none - will track to all projects)",sessionId:k,fanIdHash:f,consent:c,storageTier:p,trackingSource:A,useEmulator:_,endpoint:N}),window._balanceInitialPageviewFired=!0,Q(),he(),["mousemove","keydown","scroll","touchstart"].forEach(e=>{document.addEventListener(e,be,{passive:!0})}),window.addEventListener("beforeunload",()=>{ve(),E()}),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"?(W(),E()):F()})}window.balance={track:xe,identify:Se,page:Q,purchase:Ie,getSessionId:()=>k,getFanIdHash:()=>f,getAttribution:()=>m,setConsent:fe,getConsent:ge,hasConsent:pe},document.readyState==="loading"?document.addEventListener("DOMContentLoaded",X):X(),a("Pixel script loaded")})();var Ne=(i,r)=>{typeof window>"u"||(window.balance?window.balance.track(i,r):console.warn("[Balance Pixel] track() called before pixel initialized:",i))},Re=(i,r)=>typeof window>"u"?Promise.resolve():window.balance?window.balance.identify(i,r):(console.warn("[Balance Pixel] identify() called before pixel initialized"),Promise.resolve()),Ue=i=>{typeof window>"u"||(window.balance?window.balance.page(i):console.warn("[Balance Pixel] page() called before pixel initialized"))},Be=(i,r,s)=>{typeof window>"u"||(window.balance?window.balance.purchase(i,r,s):console.warn("[Balance Pixel] purchase() called before pixel initialized"))},Fe=()=>typeof window>"u"?null:window.balance?.getSessionId()??null,He=()=>typeof window>"u"?null:window.balance?.getFanIdHash()??null,ze=()=>typeof window>"u"?{}:window.balance?.getAttribution()??{},je=i=>{typeof window>"u"||(window.balance?window.balance.setConsent(i):console.warn("[Balance Pixel] setConsent() called before pixel initialized"))},Me=()=>typeof window>"u"?null:window.balance?.getConsent()??null,Ke=i=>typeof window>"u"?!1:window.balance?.hasConsent(i)??!1;return Oe(Je);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hifilabs/pixel",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "BALANCE Pixel - Lightweight browser tracking script for artist fan analytics",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",