@hifilabs/pixel 0.16.2 → 0.16.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/browser.js CHANGED
@@ -8,18 +8,7 @@ var ArtistPixel = (() => {
8
8
 
9
9
  // src/browser.ts
10
10
  (function() {
11
- const PIXEL_VERSION = "0.16.2";
12
- const currentScript = document.currentScript;
13
- const debug = currentScript?.dataset.debug === "true";
14
- const SILENT_MODE = !debug;
15
- const log = (...args) => {
16
- if (debug)
17
- console.log("[artistPixel]", ...args);
18
- };
19
- const logError = (...args) => {
20
- if (!SILENT_MODE)
21
- console.error("[artistPixel]", ...args);
22
- };
11
+ const PIXEL_VERSION = "0.16.3";
23
12
  function parseUserAgent(ua) {
24
13
  let device_type = "desktop";
25
14
  if (/ipad|tablet|android(?!.*mobile)/i.test(ua))
@@ -63,6 +52,7 @@ var ArtistPixel = (() => {
63
52
  }
64
53
  return cachedDeviceInfo;
65
54
  }
55
+ const currentScript = document.currentScript;
66
56
  const artistId = currentScript?.dataset.artistId;
67
57
  const rawProjectId = currentScript?.dataset.projectId;
68
58
  const projectId = rawProjectId ? normalizeProjectId(rawProjectId) : void 0;
@@ -75,6 +65,7 @@ var ArtistPixel = (() => {
75
65
  return `custom_${id}`;
76
66
  }
77
67
  const useEmulator = currentScript?.dataset.emulator === "true";
68
+ const debug = currentScript?.dataset.debug === "true";
78
69
  const heartbeatInterval = parseInt(currentScript?.dataset.heartbeatInterval || "120000", 10);
79
70
  const heartbeatEnabled = currentScript?.dataset.heartbeat !== "false";
80
71
  const explicitSource = currentScript?.dataset.source;
@@ -236,6 +227,15 @@ var ArtistPixel = (() => {
236
227
  const IDLE_TIMEOUT = 2 * 60 * 1e3;
237
228
  let lastActivityTime = Date.now();
238
229
  let isIdle = false;
230
+ const SILENT_MODE = !debug;
231
+ const log = (...args) => {
232
+ if (debug)
233
+ console.log("[artistPixel]", ...args);
234
+ };
235
+ const logError = (...args) => {
236
+ if (!SILENT_MODE)
237
+ console.error("[artistPixel]", ...args);
238
+ };
239
239
  const CONSENT_STYLES = {
240
240
  base: `
241
241
  :host {
@@ -993,6 +993,14 @@ var ArtistPixel = (() => {
993
993
  if (!visitorId) {
994
994
  visitorId = getOrCreateVisitorId();
995
995
  }
996
+ if (fanIdHash) {
997
+ try {
998
+ localStorage.setItem(STORAGE_PREFIX + FAN_ID_KEY, fanIdHash);
999
+ log("Fan ID hash persisted after consent granted");
1000
+ } catch {
1001
+ storageSet(FAN_ID_KEY, fanIdHash);
1002
+ }
1003
+ }
996
1004
  }
997
1005
  const event = buildEvent({
998
1006
  event_name: "consent_updated",
@@ -1270,10 +1278,12 @@ var ArtistPixel = (() => {
1270
1278
  if (consent?.analytics === true) {
1271
1279
  upgradeStorageTier();
1272
1280
  }
1273
- try {
1274
- localStorage.setItem(STORAGE_PREFIX + FAN_ID_KEY, fanIdHash);
1275
- } catch {
1276
- storageSet(FAN_ID_KEY, fanIdHash);
1281
+ if (consent?.analytics === true) {
1282
+ try {
1283
+ localStorage.setItem(STORAGE_PREFIX + FAN_ID_KEY, fanIdHash);
1284
+ } catch {
1285
+ storageSet(FAN_ID_KEY, fanIdHash);
1286
+ }
1277
1287
  }
1278
1288
  const emailParts = email.split("@");
1279
1289
  const maskedEmail = emailParts[0].charAt(0) + "***@" + (emailParts[1] || "");
@@ -1308,11 +1318,15 @@ var ArtistPixel = (() => {
1308
1318
  return;
1309
1319
  }
1310
1320
  fanIdHash = hash;
1311
- try {
1312
- localStorage.setItem(STORAGE_PREFIX + FAN_ID_KEY, hash);
1313
- log("Fan ID hash set from server:", hash.substring(0, 16) + "...");
1314
- } catch {
1315
- storageSet(FAN_ID_KEY, hash);
1321
+ if (consent?.analytics === true) {
1322
+ try {
1323
+ localStorage.setItem(STORAGE_PREFIX + FAN_ID_KEY, hash);
1324
+ log("Fan ID hash persisted:", hash.substring(0, 16) + "...");
1325
+ } catch {
1326
+ storageSet(FAN_ID_KEY, hash);
1327
+ }
1328
+ } else {
1329
+ log("Fan ID hash set (memory only - no consent):", hash.substring(0, 16) + "...");
1316
1330
  }
1317
1331
  }
1318
1332
  function purchase(revenue, currency = "USD", properties = {}) {
@@ -1,4 +1,4 @@
1
- var ArtistPixel=(()=>{var Ue=Object.defineProperty;var qe=(y,i,x)=>i in y?Ue(y,i,{enumerable:!0,configurable:!0,writable:!0,value:x}):y[i]=x;var K=(y,i,x)=>(qe(y,typeof i!="symbol"?i+"":i,x),x);(function(){let y="0.16.2",i=document.currentScript,x=i?.dataset.debug==="true",Wt=!x,a=(...t)=>{x&&console.log("[artistPixel]",...t)},I=(...t)=>{Wt||console.error("[artistPixel]",...t)};function Kt(t){let e="desktop";/ipad|tablet|android(?!.*mobile)/i.test(t)?e="tablet":/mobile|iphone|android.*mobile|blackberry|iemobile/i.test(t)&&(e="mobile");let n="Unknown";/edg/i.test(t)?n="Edge":/opr|opera/i.test(t)?n="Opera":/firefox/i.test(t)?n="Firefox":/chrome/i.test(t)?n="Chrome":/safari/i.test(t)&&(n="Safari");let o="Unknown";return/iphone|ipad/i.test(t)?o="iOS":/android/i.test(t)?o="Android":/windows/i.test(t)?o="Windows":/mac os/i.test(t)?o="macOS":/linux/i.test(t)?o="Linux":/cros/i.test(t)&&(o="ChromeOS"),{device_type:e,browser:n,os:o}}let $=null;function Yt(){if(!$)try{$=Kt(navigator.userAgent)}catch{$={device_type:"desktop",browser:"Unknown",os:"Unknown"}}return $}let Y=i?.dataset.artistId,J=i?.dataset.projectId,X=J?Jt(J):void 0;function Jt(t){return!t||t.startsWith("release_")||t.startsWith("merch_")||t.startsWith("link_")||t.startsWith("custom_")?t:`custom_${t}`}let Q=i?.dataset.emulator==="true",Z=parseInt(i?.dataset.heartbeatInterval||"120000",10),yt=i?.dataset.heartbeat!=="false",bt=i?.dataset.source,vt=i?.dataset.endpoint,g=window.__artistPixelConfig||{},M=i?.dataset.metaPixelId||g.metaPixelId,P=i?.dataset.tiktokPixelId||g.tiktokPixelId,w=i?.dataset.googleAdsId||g.googleAdsId,_t=i?.dataset.googleAdsConversionLabel||g.googleAdsConversionLabel,O=i?.dataset.googleTagManagerId||g.googleTagManagerId,p=i?.dataset.googleAnalyticsId||g.googleAnalyticsId,f={pageview:(i?.dataset.forwardPageview??g.forwardPageview)!=="false",purchase:(i?.dataset.forwardPurchase??g.forwardPurchase)!=="false",addToCart:(i?.dataset.forwardAddToCart??g.forwardAddToCart)!=="false",lead:(i?.dataset.forwardLead??g.forwardLead)!=="false",viewContent:(i?.dataset.forwardViewContent??g.forwardViewContent)!=="false",initiateCheckout:(i?.dataset.forwardInitiateCheckout??g.forwardInitiateCheckout)!=="false",completeRegistration:(i?.dataset.forwardCompleteRegistration??g.forwardCompleteRegistration)!=="false",subscribe:(i?.dataset.forwardSubscribe??g.forwardSubscribe)!=="false",search:(i?.dataset.forwardSearch??g.forwardSearch)!=="false",contact:(i?.dataset.forwardContact??g.forwardContact)!=="false",custom:(i?.dataset.forwardCustom||g.forwardCustom||"").split(",").filter(Boolean)},j=!!(M||P||w||O||p),tt=i?.dataset.consentUi==="true",Xt=i?.dataset.consentStyle||"brutalist",Qt=i?.dataset.primaryColor,Zt=i?.dataset.bannerPosition||"bottom",kt=i?.dataset.excludePages?.split(",").filter(Boolean)||[],xt=i?.dataset.trackFileDownloads==="true",te=i?.dataset.hashRouting==="true";function ee(){if(bt)return bt;let t=typeof window.dataLayer<"u"&&Array.isArray(window.dataLayer),e=typeof window.gtag=="function";return t&&e?"gtm":"pixel"}let V="pixel";if(!Y){I(" Error: data-artist-id attribute is required");return}let It="session_id",et="session_timestamp",Ct="attribution",E="fan_id_hash",nt="visitor_id",G="artistPixel_consent",ne=365*24*60*60*1e3,ae=30*24*60*60*1e3,oe=60*60*1e3,b="balance_";function ie(t){let e=t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*");return new RegExp("^"+e+"$")}function re(t){if(kt.length===0)return!1;try{let e=new URL(t,window.location.origin).pathname;return kt.some(n=>ie(n).test(e))}catch{return!1}}let se=[".pdf",".zip",".tar",".gz",".rar",".7z",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".mp3",".wav",".flac",".aac",".ogg",".mp4",".mov",".avi",".mkv",".webm",".exe",".dmg",".pkg",".deb",".rpm",".csv",".json",".xml",".txt"];function St(t){try{let e=new URL(t,window.location.origin).pathname.toLowerCase();return se.some(n=>e.endsWith(n))}catch{return!1}}function ce(t,e){try{let o=new URL(t,window.location.origin).pathname.split("/").pop()||"unknown",c=o.includes(".")?o.split(".").pop():"unknown";a("File download tracked:",{url:t,fileName:o,fileExtension:c});let d=C({event_name:"custom",metadata:{event_type:"file_download",file_url:t,file_name:o,file_extension:c,link_text:e?.textContent?.trim().substring(0,100)||""}});S(d)}catch(n){I("Failed to track file download:",n)}}let de="https://e.os.xyz",Pt="https://us-central1-artist-os-distro.cloudfunctions.net/ingestEvents",T=vt||(Q?"http://localhost:5001/artist-os-distro/us-central1/ingestEvents":de),U=null,h=null,m=null,u=null,L={},at={},v=[],ot=null,_="session",A=null,it=0,rt=0,R=0,q=!0,st=0,ct=!1,le=2*60*1e3,Et=Date.now(),F=!1,dt={base:`
1
+ var ArtistPixel=(()=>{var Ue=Object.defineProperty;var qe=(v,_,h)=>_ in v?Ue(v,_,{enumerable:!0,configurable:!0,writable:!0,value:h}):v[_]=h;var K=(v,_,h)=>(qe(v,typeof _!="symbol"?_+"":_,h),h);(function(){let v="0.16.3";function _(t){let e="desktop";/ipad|tablet|android(?!.*mobile)/i.test(t)?e="tablet":/mobile|iphone|android.*mobile|blackberry|iemobile/i.test(t)&&(e="mobile");let n="Unknown";/edg/i.test(t)?n="Edge":/opr|opera/i.test(t)?n="Opera":/firefox/i.test(t)?n="Firefox":/chrome/i.test(t)?n="Chrome":/safari/i.test(t)&&(n="Safari");let i="Unknown";return/iphone|ipad/i.test(t)?i="iOS":/android/i.test(t)?i="Android":/windows/i.test(t)?i="Windows":/mac os/i.test(t)?i="macOS":/linux/i.test(t)?i="Linux":/cros/i.test(t)&&(i="ChromeOS"),{device_type:e,browser:n,os:i}}let h=null;function Kt(){if(!h)try{h=_(navigator.userAgent)}catch{h={device_type:"desktop",browser:"Unknown",os:"Unknown"}}return h}let r=document.currentScript,Y=r?.dataset.artistId,J=r?.dataset.projectId,X=J?Yt(J):void 0;function Yt(t){return!t||t.startsWith("release_")||t.startsWith("merch_")||t.startsWith("link_")||t.startsWith("custom_")?t:`custom_${t}`}let Q=r?.dataset.emulator==="true",ht=r?.dataset.debug==="true",Z=parseInt(r?.dataset.heartbeatInterval||"120000",10),bt=r?.dataset.heartbeat!=="false",vt=r?.dataset.source,_t=r?.dataset.endpoint,u=window.__artistPixelConfig||{},O=r?.dataset.metaPixelId||u.metaPixelId,L=r?.dataset.tiktokPixelId||u.tiktokPixelId,w=r?.dataset.googleAdsId||u.googleAdsId,kt=r?.dataset.googleAdsConversionLabel||u.googleAdsConversionLabel,j=r?.dataset.googleTagManagerId||u.googleTagManagerId,p=r?.dataset.googleAnalyticsId||u.googleAnalyticsId,m={pageview:(r?.dataset.forwardPageview??u.forwardPageview)!=="false",purchase:(r?.dataset.forwardPurchase??u.forwardPurchase)!=="false",addToCart:(r?.dataset.forwardAddToCart??u.forwardAddToCart)!=="false",lead:(r?.dataset.forwardLead??u.forwardLead)!=="false",viewContent:(r?.dataset.forwardViewContent??u.forwardViewContent)!=="false",initiateCheckout:(r?.dataset.forwardInitiateCheckout??u.forwardInitiateCheckout)!=="false",completeRegistration:(r?.dataset.forwardCompleteRegistration??u.forwardCompleteRegistration)!=="false",subscribe:(r?.dataset.forwardSubscribe??u.forwardSubscribe)!=="false",search:(r?.dataset.forwardSearch??u.forwardSearch)!=="false",contact:(r?.dataset.forwardContact??u.forwardContact)!=="false",custom:(r?.dataset.forwardCustom||u.forwardCustom||"").split(",").filter(Boolean)},U=!!(O||L||w||j||p),tt=r?.dataset.consentUi==="true",Jt=r?.dataset.consentStyle||"brutalist",Xt=r?.dataset.primaryColor,Qt=r?.dataset.bannerPosition||"bottom",xt=r?.dataset.excludePages?.split(",").filter(Boolean)||[],It=r?.dataset.trackFileDownloads==="true",Zt=r?.dataset.hashRouting==="true";function te(){if(vt)return vt;let t=typeof window.dataLayer<"u"&&Array.isArray(window.dataLayer),e=typeof window.gtag=="function";return t&&e?"gtm":"pixel"}let V="pixel";if(!Y){S(" Error: data-artist-id attribute is required");return}let Ct="session_id",et="session_timestamp",St="attribution",C="fan_id_hash",nt="visitor_id",G="artistPixel_consent",ee=365*24*60*60*1e3,ne=30*24*60*60*1e3,ae=60*60*1e3,b="balance_";function ie(t){let e=t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*");return new RegExp("^"+e+"$")}function oe(t){if(xt.length===0)return!1;try{let e=new URL(t,window.location.origin).pathname;return xt.some(n=>ie(n).test(e))}catch{return!1}}let re=[".pdf",".zip",".tar",".gz",".rar",".7z",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".mp3",".wav",".flac",".aac",".ogg",".mp4",".mov",".avi",".mkv",".webm",".exe",".dmg",".pkg",".deb",".rpm",".csv",".json",".xml",".txt"];function Pt(t){try{let e=new URL(t,window.location.origin).pathname.toLowerCase();return re.some(n=>e.endsWith(n))}catch{return!1}}function se(t,e){try{let i=new URL(t,window.location.origin).pathname.split("/").pop()||"unknown",c=i.includes(".")?i.split(".").pop():"unknown";a("File download tracked:",{url:t,fileName:i,fileExtension:c});let l=P({event_name:"custom",metadata:{event_type:"file_download",file_url:t,file_name:i,file_extension:c,link_text:e?.textContent?.trim().substring(0,100)||""}});E(l)}catch(n){S("Failed to track file download:",n)}}let ce="https://e.os.xyz",Et="https://us-central1-artist-os-distro.cloudfunctions.net/ingestEvents",A=_t||(Q?"http://localhost:5001/artist-os-distro/us-central1/ingestEvents":ce),q=null,y=null,f=null,d=null,R={},at={},k=[],it=null,x="session",D=null,ot=0,rt=0,M=0,F=!0,st=0,ct=!1,de=2*60*1e3,Tt=Date.now(),z=!1,le=!ht,a=(...t)=>{ht&&console.log("[artistPixel]",...t)},S=(...t)=>{le||console.error("[artistPixel]",...t)},dt={base:`
2
2
  :host {
3
3
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
4
4
  position: fixed;
@@ -83,12 +83,12 @@ var ArtistPixel=(()=>{var Ue=Object.defineProperty;var qe=(y,i,x)=>i in y?Ue(y,i
83
83
  }
84
84
  .btn.accept { background: #fff; color: #000; border-color: #fff; }
85
85
  .btn:hover { opacity: 0.8; }
86
- `};class ge{constructor(e){K(this,"container",null);K(this,"shadow",null);K(this,"config");if(this.config=e,this.hasStoredConsent()){a("ConsentManager: Consent already exists, not showing banner");return}this.container=document.createElement("div"),this.container.id="artistPixel-consent-manager",this.shadow=this.container.attachShadow({mode:"closed"}),this.render(),document.body.appendChild(this.container),a("ConsentManager: Banner rendered")}hasStoredConsent(){try{return window._balanceConsentNeedsRefresh?!1:localStorage.getItem(G)!==null}catch{return!1}}render(){if(!this.shadow)return;let e=this.config.style||"brutalist",n=dt.base,o=dt[e]||dt.brutalist,c=this.config.position==="top"?"top: 0;":"bottom: 0;",d=this.config.primaryColor?`.btn.accept { background: ${this.config.primaryColor} !important; border-color: ${this.config.primaryColor} !important; color: #fff !important; }`:"";this.shadow.innerHTML=`
86
+ `};class ge{constructor(e){K(this,"container",null);K(this,"shadow",null);K(this,"config");if(this.config=e,this.hasStoredConsent()){a("ConsentManager: Consent already exists, not showing banner");return}this.container=document.createElement("div"),this.container.id="artistPixel-consent-manager",this.shadow=this.container.attachShadow({mode:"closed"}),this.render(),document.body.appendChild(this.container),a("ConsentManager: Banner rendered")}hasStoredConsent(){try{return window._balanceConsentNeedsRefresh?!1:localStorage.getItem(G)!==null}catch{return!1}}render(){if(!this.shadow)return;let e=this.config.style||"brutalist",n=dt.base,i=dt[e]||dt.brutalist,c=this.config.position==="top"?"top: 0;":"bottom: 0;",l=this.config.primaryColor?`.btn.accept { background: ${this.config.primaryColor} !important; border-color: ${this.config.primaryColor} !important; color: #fff !important; }`:"";this.shadow.innerHTML=`
87
87
  <style>
88
88
  ${n}
89
89
  :host { ${c} }
90
- ${o}
91
- ${d}
90
+ ${i}
91
+ ${l}
92
92
  </style>
93
93
  <div class="banner" role="dialog" aria-label="Cookie consent" aria-modal="false">
94
94
  <p class="text">
@@ -99,4 +99,4 @@ var ArtistPixel=(()=>{var Ue=Object.defineProperty;var qe=(y,i,x)=>i in y?Ue(y,i
99
99
  <button id="accept" class="btn accept">Accept</button>
100
100
  </div>
101
101
  </div>
102
- `,this.shadow.getElementById("accept")?.addEventListener("click",()=>this.handleConsent(!0)),this.shadow.getElementById("decline")?.addEventListener("click",()=>this.handleConsent(!1))}handleConsent(e){window.balance?.setConsent&&window.balance.setConsent({analytics:e,marketing:e,personalization:e,timestamp:new Date().toISOString()}),e&&!window._balanceInitialPageviewFired&&(window._balanceInitialPageviewFired=!0,window.balance?.page&&window.balance.page(),ft(),a("Initial pageview fired after consent granted")),this.remove()}remove(){this.container&&(this.container.remove(),this.container=null,this.shadow=null,a("ConsentManager: Banner removed"))}}let Tt=!1,z=!1;function ue(){if(!M)return;if(window.fbq){a("[Platform:Meta] Already loaded, skipping");return}a("[Platform:Meta] Injecting pixel:",M);let t=function(...n){t.callMethod?t.callMethod.apply(t,n):t.queue.push(n)};window._fbq||(window._fbq=t),t.push=t,t.loaded=!0,t.version="2.0",t.queue=[],window.fbq=t;let e=document.createElement("script");e.async=!0,e.src="https://connect.facebook.net/en_US/fbevents.js",e.onload=()=>{window.fbq("init",M),f.pageview&&window.fbq("track","PageView"),a("[Platform:Meta] Initialized and PageView tracked")},document.head.appendChild(e)}function fe(){if(!P)return;if(window.ttq?._i?.[P]){a("[Platform:TikTok] Already loaded, skipping");return}a("[Platform:TikTok] Injecting pixel:",P);let t=window,e="ttq";t.TiktokAnalyticsObject=e;let n=t[e]=t[e]||[];n.methods=["page","track","identify","instances","debug","on","off","once","ready","alias","group","enableCookie","disableCookie","holdConsent","revokeConsent","grantConsent"],n.setAndDefer=function(o,c){o[c]=function(...d){o.push([c].concat(d))}};for(let o=0;o<n.methods.length;o++)n.setAndDefer(n,n.methods[o]);n.instance=function(o){let c=n._i[o]||[];for(let d=0;d<n.methods.length;d++)n.setAndDefer(c,n.methods[d]);return c},n.load=function(o){let c="https://analytics.tiktok.com/i18n/pixel/events.js";n._i=n._i||{},n._i[o]=[],n._i[o]._u=c,n._t=n._t||{},n._t[o]=+new Date,n._o=n._o||{};let d=document.createElement("script");d.type="text/javascript",d.async=!0,d.src=c+"?sdkid="+o+"&lib="+e,document.head.appendChild(d)},n.load(P),f.pageview&&n.page(),a("[Platform:TikTok] Initialized and page() tracked")}function me(){if(!w)return;if(window.dataLayer=window.dataLayer||[],typeof window.gtag!="function"&&(window.gtag=function(...e){window.dataLayer.push(e)}),document.querySelector('script[src*="googletagmanager.com/gtag/js"]')){a("[Platform:GoogleAds] gtag.js already loaded, configuring"),window.gtag("config",w);return}a("[Platform:GoogleAds] Injecting gtag:",w),window.gtag("consent","default",{ad_storage:"granted",ad_user_data:"granted",ad_personalization:"granted",analytics_storage:"granted"});let t=document.createElement("script");t.async=!0,t.src=`https://www.googletagmanager.com/gtag/js?id=${w}`,t.onload=()=>{window.gtag("js",new Date),window.gtag("config",w),a("[Platform:GoogleAds] Initialized")},document.head.appendChild(t)}function we(){if(!O)return;if(document.querySelector(`script[src*="googletagmanager.com/gtm.js?id=${O}"]`)){a("[Platform:GTM] Already loaded, skipping");return}a("[Platform:GTM] Injecting GTM:",O),window.dataLayer=window.dataLayer||[],typeof window.gtag!="function"&&(window.gtag=function(...n){window.dataLayer.push(n)});let t=z?"granted":"denied";window.gtag("consent","default",{ad_storage:t,ad_user_data:t,ad_personalization:t,analytics_storage:t}),window.gtag("set","wait_for_update",500),window.dataLayer.push({"gtm.start":new Date().getTime(),event:"gtm.js"});let e=document.createElement("script");e.async=!0,e.src=`https://www.googletagmanager.com/gtm.js?id=${O}`,e.onload=()=>{a("[Platform:GTM] Initialized")},document.head.appendChild(e)}function pe(){if(!p)return;let t=document.querySelector('script[src*="googletagmanager.com/gtag/js"]');window.dataLayer=window.dataLayer||[],typeof window.gtag!="function"&&(window.gtag=function(...o){window.dataLayer.push(o)});let e=z?"granted":"denied";if(window.gtag("consent","default",{ad_storage:e,ad_user_data:e,ad_personalization:e,analytics_storage:e}),t){a("[Platform:GA4] gtag.js already loaded, configuring"),window.gtag("config",p,{send_page_view:!1});return}a("[Platform:GA4] Injecting gtag:",p);let n=document.createElement("script");n.async=!0,n.src=`https://www.googletagmanager.com/gtag/js?id=${p}`,n.onload=()=>{window.gtag("js",new Date),window.gtag("config",p,{send_page_view:!1}),a("[Platform:GA4] Initialized")},document.head.appendChild(n)}function he(){if(j){if(Tt){a("[Platform] Already loaded, skipping");return}if(!z){a("[Platform] Marketing consent not granted, deferring");return}a("[Platform] Injecting platform pixels (consent granted)"),Tt=!0,ue(),fe(),me(),we(),pe()}}function Lt(t){if(z=t,a("[Platform] Marketing consent:",t?"granted":"denied"),t&&he(),window.ttq&&(t?window.ttq.grantConsent?.():window.ttq.revokeConsent?.()),typeof window.gtag=="function"){let e=t?"granted":"denied";window.gtag("consent","update",{ad_storage:e,ad_user_data:e,ad_personalization:e,analytics_storage:e})}}function lt(t){if(!j||!z)return!1;let e=t.toLowerCase();return e==="pageview"||e==="page_view"?f.pageview:e==="purchase"?f.purchase:e==="addtocart"||e==="add_to_cart"?f.addToCart:e==="lead"||e==="form_submit"?f.lead:e==="viewcontent"||e==="view_content"?f.viewContent:e==="initiatecheckout"||e==="initiate_checkout"?f.initiateCheckout:e==="completeregistration"||e==="complete_registration"?f.completeRegistration:e==="subscribe"?f.subscribe:e==="search"?f.search:e==="contact"?f.contact:f.custom.length>0?f.custom.some(n=>n.toLowerCase()===e):!1}function ye(){lt("pageview")&&(a("[Platform] Forwarding PageView"),window.fbq&&window.fbq("track","PageView"),window.ttq&&window.ttq.page(),typeof window.gtag=="function"&&p&&window.gtag("event","page_view",{send_to:p}))}function be(t,e,n){if(lt("purchase")){if(a("[Platform] Forwarding Purchase:",t,e),window.fbq&&window.fbq("track","Purchase",{value:t,currency:e,content_ids:n?.contentIds,content_type:n?.contentType||"product",order_id:n?.transactionId}),window.ttq&&window.ttq.track("CompletePayment",{value:t,currency:e,order_id:n?.transactionId}),typeof window.gtag=="function"&&w){let o=_t?`${w}/${_t}`:w;window.gtag("event","conversion",{send_to:o,value:t,currency:e,transaction_id:n?.transactionId}),window.gtag("event","purchase",{send_to:w,value:t,currency:e,transaction_id:n?.transactionId})}typeof window.gtag=="function"&&p&&window.gtag("event","purchase",{send_to:p,value:t,currency:e,transaction_id:n?.transactionId,items:n?.items})}}function ve(t,e){if(!lt(t))return;let n=t.toLowerCase();a("[Platform] Forwarding custom event:",t);let o={addtocart:"AddToCart",add_to_cart:"AddToCart",lead:"Lead",form_submit:"Lead",viewcontent:"ViewContent",view_content:"ViewContent",search:"Search",subscribe:"Subscribe",contact:"Contact",completeregistration:"CompleteRegistration",complete_registration:"CompleteRegistration",initiatecheckout:"InitiateCheckout",initiate_checkout:"InitiateCheckout"},c={addtocart:"AddToCart",add_to_cart:"AddToCart",lead:"SubmitForm",form_submit:"SubmitForm",viewcontent:"ViewContent",view_content:"ViewContent",search:"Search",subscribe:"Subscribe",contact:"Contact",completeregistration:"CompleteRegistration",complete_registration:"CompleteRegistration",initiatecheckout:"InitiateCheckout",initiate_checkout:"InitiateCheckout"},d={addtocart:"add_to_cart",add_to_cart:"add_to_cart",lead:"generate_lead",form_submit:"generate_lead",viewcontent:"view_item",view_content:"view_item",search:"search",subscribe:"subscribe",contact:"contact",completeregistration:"sign_up",complete_registration:"sign_up",initiatecheckout:"begin_checkout",initiate_checkout:"begin_checkout"};if(window.fbq){let s=o[n];s?window.fbq("track",s,e):window.fbq("trackCustom",t,e)}if(window.ttq){let s=c[n]||t;window.ttq.track(s,e)}if(typeof window.gtag=="function"&&w){let s=d[n]||t;window.gtag("event",s,{send_to:w,...e})}if(typeof window.gtag=="function"&&p){let s=d[n]||t;window.gtag("event",s,{send_to:p,...e})}}function At(){try{return _==="local"?localStorage:sessionStorage}catch{return null}}function B(t){let e=At();if(!e)return null;try{let n=b+t,o=e.getItem(n);if(!o&&_==="session")try{o=localStorage.getItem(n)}catch{}return o}catch{return null}}function D(t,e){let n=At();if(n)try{n.setItem(b+t,e)}catch{}}function Rt(){if(_!=="local"){a("Upgrading storage tier: session -> local");try{let t=[];for(let e=0;e<sessionStorage.length;e++){let n=sessionStorage.key(e);n?.startsWith(b)&&t.push(n)}for(let e of t){let n=sessionStorage.getItem(e);n&&localStorage.setItem(e,n)}for(let e of t)sessionStorage.removeItem(e);_="local",a(`Storage tier upgraded, migrated ${t.length} items`)}catch(t){I(" Storage migration failed:",t)}}}function W(){return crypto&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,t=>{let e=Math.random()*16|0;return(t==="x"?e:e&3|8).toString(16)})}function _e(){try{let t=B(It),e=B(et);if(t&&e&&Date.now()-parseInt(e,10)<oe)return D(et,Date.now().toString()),t;let n=W();return D(It,n),D(et,Date.now().toString()),n}catch{return W()}}function ke(){let t=new URLSearchParams(window.location.search),e={};return["source","medium","campaign","content","term"].forEach(n=>{let o=t.get(`utm_${n}`);o&&(e[`utm_${n}`]=o)}),e}function xe(){try{let t=B(Ct);if(t){L=JSON.parse(t),a("Loaded attribution:",L);return}let e=ke();Object.keys(e).length>0&&(L=e,D(Ct,JSON.stringify(e)),a("Captured attribution:",L))}catch{}}function Ie(){try{m=localStorage.getItem(b+E),m&&a("Loaded fan ID hash:",m.substring(0,16)+"...")}catch{try{m=B(E)}catch{}}}function gt(){try{let t=localStorage.getItem(b+nt);if(t)return t;let e=W();return localStorage.setItem(b+nt,e),a("Created persistent visitor ID:",e.substring(0,8)+"..."),e}catch{return a("localStorage unavailable, using session ID as fallback"),U||W()}}function Ce(){try{h=localStorage.getItem(b+nt),h&&a("Loaded visitor ID:",h.substring(0,8)+"...")}catch{}}function Se(){try{let t=localStorage.getItem(G);if(t){let e=JSON.parse(t);if(e.expiresAt&&Date.now()>e.expiresAt){a("Consent expired - clearing stored consent"),localStorage.removeItem(G),u=null;return}if(e.expiresAt){let n=e.expiresAt-Date.now();n>0&&n<ae&&(a("Consent nearing expiration - will prompt for refresh"),window._balanceConsentNeedsRefresh=!0)}u=e.preferences||null,a("Loaded consent:",u)}}catch{}}function Dt(t){let e=u;u=t;try{let o={preferences:t,method:"explicit",version:1,expiresAt:Date.now()+ne};localStorage.setItem(G,JSON.stringify(o)),a("Consent saved with TTL:",t),window._balanceConsentNeedsRefresh=!1}catch(o){I(" Could not save consent:",o)}t.analytics===!0&&(Rt(),h||(h=gt()));let n=C({event_name:"consent_updated",metadata:{consent_preferences:t,consent_method:"explicit",previous_consent:e||void 0}});S(n);try{window.dispatchEvent(new CustomEvent("artistPixel:consent:updated",{detail:t})),a("DOM event artistPixel:consent:updated dispatched")}catch{}j&&Lt(t.marketing===!0)}function Pe(){return u}function Ee(t){return u?.[t]===!0}function Te(t){for(let[e,n]of Object.entries(t))try{let o=Mt(e);at[o]=n}catch{at[e]=n}a("Registered",Object.keys(t).length,"links for tracking enrichment")}function Mt(t){try{let e=new URL(t);return`${e.protocol}//${e.host}${e.pathname}${e.search}`}catch{return t}}function Le(t){let e=Mt(t);return at[e]||null}async function Ae(t){let e=t.toLowerCase().trim(),o=new TextEncoder().encode(e),c=await crypto.subtle.digest("SHA-256",o);return Array.from(new Uint8Array(c)).map(s=>s.toString(16).padStart(2,"0")).join("")}function C(t){let e=Yt(),n={artist_id:Y,fan_session_id:U,visitor_id:h||gt(),fan_id_hash:m||void 0,timestamp:new Date().toISOString(),source_url:window.location.href,referrer_url:document.referrer||void 0,user_agent:navigator.userAgent,device_type:e.device_type,browser:e.browser,os:e.os,tracking_source:V,...t,...L};return X&&!t.projectId&&(n.projectId=X),n}function S(t){v.push(t),a("Event queued:",t.event_name,"(queue:",v.length,")"),v.length>=10&&N()}async function N(){if(v.length===0)return;let t=[...v];v=[],a("Flushing",t.length,"events to",T);try{let e=await fetch(T,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({events:t}),keepalive:!0});if(!e.ok)throw new Error(`HTTP ${e.status}`);a("Events sent successfully")}catch(e){if(I(" Failed to send events, trying fallback:",e),T!==Pt&&!Q)try{if((await fetch(Pt,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({events:t}),keepalive:!0})).ok){a("Events sent via fallback (no geo)");return}}catch(n){I(" Fallback also failed:",n)}v.length<50&&v.push(...t)}}function Re(){ot&&clearInterval(ot),ot=window.setInterval(()=>{v.length>0&&N()},5e3)}function ut(){R||(it=Date.now()),R=Date.now(),q=!0,a("Active time tracking started/resumed")}function Ot(){R&&q&&(rt+=Date.now()-R),q=!1,a("Active time tracking paused, accumulated:",rt,"ms")}function jt(){let t=rt;return q&&R&&(t+=Date.now()-R),t}function De(){Et=Date.now(),F&&(F=!1,ut(),a("User returned from idle"))}function Me(){if(document.visibilityState==="hidden"){a("Skipping heartbeat - tab hidden");return}if(Date.now()-Et>le){F||(F=!0,Ot(),a("User idle - pausing heartbeat"));return}let t=jt(),e=Math.round(t/1e3),n=C({event_name:"engagement_heartbeat",metadata:{time_on_page_seconds:e,time_on_page_ms:t,heartbeat_interval_ms:Z,is_active:q&&!F}});S(n),st++,a("Heartbeat sent:",e,"seconds active")}function ft(){if(!yt){a('Heartbeat disabled via data-heartbeat="false"');return}A&&clearInterval(A),ut(),A=window.setInterval(()=>{Me()},Z),a("Heartbeat started with interval:",Z,"ms")}function mt(){if(!ct){if(ct=!0,A&&(clearInterval(A),A=null),yt){let t=it?Date.now()-it:0,e=jt(),n=C({event_name:"engagement_summary",metadata:{total_time_ms:t,active_time_ms:e,heartbeat_samples:st,page_path:window.location.pathname}});if(navigator.sendBeacon&&T){let o=JSON.stringify({events:[n]});navigator.sendBeacon(T,o),a("Engagement summary sent via sendBeacon:",Math.round(e/1e3),"seconds active")}else S(n),a("Engagement summary enqueued:",Math.round(e/1e3),"seconds active")}st=0}}function H(t={}){let e=t.url||window.location.href;if(re(e)){a("Page excluded from tracking:",e);return}let n=C({event_name:"pageview",page_title:t.title||document.title,source_url:e});S(n),ye()}function wt(t,e={}){let n=C({event_name:"custom",metadata:{event_type:t,...e}});S(n),ve(t,e)}async function Ut(t,e={}){try{if(u&&u.analytics===!1){a("Identify skipped - user declined analytics consent");return}m=await Ae(t),u?.analytics===!0&&Rt();try{localStorage.setItem(b+E,m)}catch{D(E,m)}let n=t.split("@"),o=n[0].charAt(0)+"***@"+(n[1]||"");a("Fan identified:",{name:e.name||"(no name)",email:o,hash:m.substring(0,16)+"...",traits:e,storageTier:_});let c=C({event_name:"identify",fan_id_hash:m,metadata:{email_sha256:m,email_display:o,traits:e,consent_preferences:u||void 0,storage_tier:_}});S(c)}catch(n){I(" Failed to identify:",n)}}function Oe(t){if(!t||typeof t!="string"){I("setFanIdHash: invalid hash provided");return}m=t;try{localStorage.setItem(b+E,t),a("Fan ID hash set from server:",t.substring(0,16)+"...")}catch{D(E,t)}}function qt(t,e="USD",n={}){let o=C({event_name:"purchase",metadata:{revenue:t,currency:e,...n}});S(o),be(t,e,n)}function Ft(){V=ee(),a("Tracking source detected:",V),Se(),u?.analytics===!0?(_="local",a("Storage tier: local (analytics consent granted)")):(_="session",a("Storage tier: session (privacy by default)")),U=_e(),Ie(),Ce(),h||(h=gt()),u||a(tt?"Consent UI enabled, waiting for explicit user consent (Tier 0 mode)":"No consent - operating in privacy-first mode (session storage only, limited tracking)"),j&&u?.marketing===!0?(a("[Platform] Marketing consent already granted, initializing platform pixels"),Lt(!0)):j&&a("[Platform] Platform pixels configured:",{meta:M||"(none)",tiktok:P||"(none)",googleAds:w||"(none)"}),xe(),Re(),a("Initialized",{artistId:Y,projectId:X||"(none - will track to all projects)",rawProjectId:J||"(none)",sessionId:U,visitorId:h?h.substring(0,8)+"...":null,fanIdHash:m,consent:u,storageTier:_,trackingSource:V,useEmulator:Q,endpoint:T}),u?.analytics===!0?(window._balanceInitialPageviewFired=!0,H(),ft(),a("Full tracking enabled (user consented)")):tt?a("Tracking dormant - waiting for explicit consent via ConsentManager"):(window._balanceInitialPageviewFired=!0,H(),ft(),a("Privacy-first tracking enabled (session-scoped, visitor_id for deduplication)")),["mousemove","keydown","scroll","touchstart"].forEach(s=>{document.addEventListener(s,De,{passive:!0})});let t=window.location.hostname;function e(s){try{let r=new URL(s).hostname.toLowerCase();return r.includes("spotify")?"spotify":r.includes("apple")||r.includes("music.apple")?"apple_music":r.includes("youtube")||r.includes("youtu.be")?"youtube":r.includes("soundcloud")?"soundcloud":r.includes("tidal")?"tidal":r.includes("deezer")?"deezer":r.includes("amazon")||r.includes("music.amazon")?"amazon_music":r.includes("bandcamp")?"bandcamp":r.includes("lnk.to")||r.includes("linkfire")?"linkfire":r.includes("linktr.ee")?"linktree":r.includes("shop")||r.includes("store")||r.includes("merch")?"shop":r.includes("ticketmaster")||r.includes("eventbrite")||r.includes("dice.fm")?"tickets":r.includes("instagram")?"instagram":r.includes("twitter")||r.includes("x.com")?"twitter":r.includes("tiktok")?"tiktok":r.includes("facebook")?"facebook":"external"}catch{return"external"}}function n(s){try{return new URL(s,window.location.origin).hostname!==t}catch{return!1}}function o(s,r){let l=e(s),k=Le(s),Vt=k?.title||r?.textContent?.trim().substring(0,100)||"",Gt=k?.id||r?.id||r?.getAttribute("data-link-id")||void 0,Bt=k?.category;a("External link clicked:",{url:s,platform:l,linkText:Vt,linkId:Gt,fromRegistry:!!k}),wt("page_link_click",{link_url:s,link_text:Vt,link_id:Gt,platform:l,link_type:"external",...Bt&&{link_category:Bt},...k&&Object.fromEntries(Object.entries(k).filter(([ht])=>!["title","id","category"].includes(ht)).map(([ht,je])=>[`link_${ht}`,je]))})}let c=window.open;if(window.open=function(s,r,l){if(s){let k=s.toString();n(k)&&o(k)}return c.call(window,s,r,l)},a("window.open interception enabled (always on)"),i?.dataset.autoTrackLinks==="true"&&(document.addEventListener("click",s=>{let l=s.target.closest("a");if(l&&l.href&&n(l.href)){if(xt&&(St(l.href)||l.hasAttribute("download")))return;o(l.href,l)}},{capture:!0,passive:!0}),a("External link click listener enabled (opt-in)")),xt&&(document.addEventListener("click",s=>{let l=s.target.closest("a");l?.href&&(St(l.href)||l.hasAttribute("download"))&&ce(l.href,l)},{capture:!0,passive:!0}),a("File download tracking enabled")),te){let s=window.location.href;window.addEventListener("hashchange",()=>{let r=window.location.href;r!==s&&(s=r,setTimeout(()=>{H({title:document.title,url:r})},0))}),a("Hash routing tracking enabled")}document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"?(Ot(),mt(),N()):(ut(),ct=!1)}),window.addEventListener("pagehide",()=>{mt(),N()}),window.addEventListener("beforeunload",()=>{mt(),N()})}if(window.balance?._version&&window.balance._version!==y){console.warn(`[artistPixel] Version conflict: ${window.balance._version} already loaded, skipping ${y}`);return}let pt=window.balance?.q||[];function zt(){if(pt.length>0){a("Processing",pt.length,"queued commands");for(let t of pt){let[e,...n]=t;switch(e){case"track":wt(n[0],n[1]);break;case"identify":Ut(n[0],n[1]);break;case"page":H(n[0]);break;case"purchase":qt(n[0],n[1],n[2]);break;case"setConsent":Dt(n[0]);break;default:a("Unknown queued command:",e)}}}}let Nt={track:wt,identify:Ut,page:H,purchase:qt,getSessionId:()=>U,getVisitorId:()=>h,getFanIdHash:()=>m,setFanIdHash:Oe,getAttribution:()=>L,setConsent:Dt,getConsent:Pe,hasConsent:Ee,registerLinks:Te,_version:y};window.artistPixel=Nt,window.balance=Nt;function Ht(){tt&&!u&&new ge({style:Xt,primaryColor:Qt,position:Zt})}let $t=window.requestIdleCallback||(t=>setTimeout(t,1));document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{Ft(),zt(),$t(()=>Ht())}):(Ft(),zt(),$t(()=>Ht())),a("Pixel script loaded")})();})();
102
+ `,this.shadow.getElementById("accept")?.addEventListener("click",()=>this.handleConsent(!0)),this.shadow.getElementById("decline")?.addEventListener("click",()=>this.handleConsent(!1))}handleConsent(e){window.balance?.setConsent&&window.balance.setConsent({analytics:e,marketing:e,personalization:e,timestamp:new Date().toISOString()}),e&&!window._balanceInitialPageviewFired&&(window._balanceInitialPageviewFired=!0,window.balance?.page&&window.balance.page(),ft(),a("Initial pageview fired after consent granted")),this.remove()}remove(){this.container&&(this.container.remove(),this.container=null,this.shadow=null,a("ConsentManager: Banner removed"))}}let Lt=!1,N=!1;function ue(){if(!O)return;if(window.fbq){a("[Platform:Meta] Already loaded, skipping");return}a("[Platform:Meta] Injecting pixel:",O);let t=function(...n){t.callMethod?t.callMethod.apply(t,n):t.queue.push(n)};window._fbq||(window._fbq=t),t.push=t,t.loaded=!0,t.version="2.0",t.queue=[],window.fbq=t;let e=document.createElement("script");e.async=!0,e.src="https://connect.facebook.net/en_US/fbevents.js",e.onload=()=>{window.fbq("init",O),m.pageview&&window.fbq("track","PageView"),a("[Platform:Meta] Initialized and PageView tracked")},document.head.appendChild(e)}function fe(){if(!L)return;if(window.ttq?._i?.[L]){a("[Platform:TikTok] Already loaded, skipping");return}a("[Platform:TikTok] Injecting pixel:",L);let t=window,e="ttq";t.TiktokAnalyticsObject=e;let n=t[e]=t[e]||[];n.methods=["page","track","identify","instances","debug","on","off","once","ready","alias","group","enableCookie","disableCookie","holdConsent","revokeConsent","grantConsent"],n.setAndDefer=function(i,c){i[c]=function(...l){i.push([c].concat(l))}};for(let i=0;i<n.methods.length;i++)n.setAndDefer(n,n.methods[i]);n.instance=function(i){let c=n._i[i]||[];for(let l=0;l<n.methods.length;l++)n.setAndDefer(c,n.methods[l]);return c},n.load=function(i){let c="https://analytics.tiktok.com/i18n/pixel/events.js";n._i=n._i||{},n._i[i]=[],n._i[i]._u=c,n._t=n._t||{},n._t[i]=+new Date,n._o=n._o||{};let l=document.createElement("script");l.type="text/javascript",l.async=!0,l.src=c+"?sdkid="+i+"&lib="+e,document.head.appendChild(l)},n.load(L),m.pageview&&n.page(),a("[Platform:TikTok] Initialized and page() tracked")}function me(){if(!w)return;if(window.dataLayer=window.dataLayer||[],typeof window.gtag!="function"&&(window.gtag=function(...e){window.dataLayer.push(e)}),document.querySelector('script[src*="googletagmanager.com/gtag/js"]')){a("[Platform:GoogleAds] gtag.js already loaded, configuring"),window.gtag("config",w);return}a("[Platform:GoogleAds] Injecting gtag:",w),window.gtag("consent","default",{ad_storage:"granted",ad_user_data:"granted",ad_personalization:"granted",analytics_storage:"granted"});let t=document.createElement("script");t.async=!0,t.src=`https://www.googletagmanager.com/gtag/js?id=${w}`,t.onload=()=>{window.gtag("js",new Date),window.gtag("config",w),a("[Platform:GoogleAds] Initialized")},document.head.appendChild(t)}function we(){if(!j)return;if(document.querySelector(`script[src*="googletagmanager.com/gtm.js?id=${j}"]`)){a("[Platform:GTM] Already loaded, skipping");return}a("[Platform:GTM] Injecting GTM:",j),window.dataLayer=window.dataLayer||[],typeof window.gtag!="function"&&(window.gtag=function(...n){window.dataLayer.push(n)});let t=N?"granted":"denied";window.gtag("consent","default",{ad_storage:t,ad_user_data:t,ad_personalization:t,analytics_storage:t}),window.gtag("set","wait_for_update",500),window.dataLayer.push({"gtm.start":new Date().getTime(),event:"gtm.js"});let e=document.createElement("script");e.async=!0,e.src=`https://www.googletagmanager.com/gtm.js?id=${j}`,e.onload=()=>{a("[Platform:GTM] Initialized")},document.head.appendChild(e)}function pe(){if(!p)return;let t=document.querySelector('script[src*="googletagmanager.com/gtag/js"]');window.dataLayer=window.dataLayer||[],typeof window.gtag!="function"&&(window.gtag=function(...i){window.dataLayer.push(i)});let e=N?"granted":"denied";if(window.gtag("consent","default",{ad_storage:e,ad_user_data:e,ad_personalization:e,analytics_storage:e}),t){a("[Platform:GA4] gtag.js already loaded, configuring"),window.gtag("config",p,{send_page_view:!1});return}a("[Platform:GA4] Injecting gtag:",p);let n=document.createElement("script");n.async=!0,n.src=`https://www.googletagmanager.com/gtag/js?id=${p}`,n.onload=()=>{window.gtag("js",new Date),window.gtag("config",p,{send_page_view:!1}),a("[Platform:GA4] Initialized")},document.head.appendChild(n)}function ye(){if(U){if(Lt){a("[Platform] Already loaded, skipping");return}if(!N){a("[Platform] Marketing consent not granted, deferring");return}a("[Platform] Injecting platform pixels (consent granted)"),Lt=!0,ue(),fe(),me(),we(),pe()}}function At(t){if(N=t,a("[Platform] Marketing consent:",t?"granted":"denied"),t&&ye(),window.ttq&&(t?window.ttq.grantConsent?.():window.ttq.revokeConsent?.()),typeof window.gtag=="function"){let e=t?"granted":"denied";window.gtag("consent","update",{ad_storage:e,ad_user_data:e,ad_personalization:e,analytics_storage:e})}}function lt(t){if(!U||!N)return!1;let e=t.toLowerCase();return e==="pageview"||e==="page_view"?m.pageview:e==="purchase"?m.purchase:e==="addtocart"||e==="add_to_cart"?m.addToCart:e==="lead"||e==="form_submit"?m.lead:e==="viewcontent"||e==="view_content"?m.viewContent:e==="initiatecheckout"||e==="initiate_checkout"?m.initiateCheckout:e==="completeregistration"||e==="complete_registration"?m.completeRegistration:e==="subscribe"?m.subscribe:e==="search"?m.search:e==="contact"?m.contact:m.custom.length>0?m.custom.some(n=>n.toLowerCase()===e):!1}function he(){lt("pageview")&&(a("[Platform] Forwarding PageView"),window.fbq&&window.fbq("track","PageView"),window.ttq&&window.ttq.page(),typeof window.gtag=="function"&&p&&window.gtag("event","page_view",{send_to:p}))}function be(t,e,n){if(lt("purchase")){if(a("[Platform] Forwarding Purchase:",t,e),window.fbq&&window.fbq("track","Purchase",{value:t,currency:e,content_ids:n?.contentIds,content_type:n?.contentType||"product",order_id:n?.transactionId}),window.ttq&&window.ttq.track("CompletePayment",{value:t,currency:e,order_id:n?.transactionId}),typeof window.gtag=="function"&&w){let i=kt?`${w}/${kt}`:w;window.gtag("event","conversion",{send_to:i,value:t,currency:e,transaction_id:n?.transactionId}),window.gtag("event","purchase",{send_to:w,value:t,currency:e,transaction_id:n?.transactionId})}typeof window.gtag=="function"&&p&&window.gtag("event","purchase",{send_to:p,value:t,currency:e,transaction_id:n?.transactionId,items:n?.items})}}function ve(t,e){if(!lt(t))return;let n=t.toLowerCase();a("[Platform] Forwarding custom event:",t);let i={addtocart:"AddToCart",add_to_cart:"AddToCart",lead:"Lead",form_submit:"Lead",viewcontent:"ViewContent",view_content:"ViewContent",search:"Search",subscribe:"Subscribe",contact:"Contact",completeregistration:"CompleteRegistration",complete_registration:"CompleteRegistration",initiatecheckout:"InitiateCheckout",initiate_checkout:"InitiateCheckout"},c={addtocart:"AddToCart",add_to_cart:"AddToCart",lead:"SubmitForm",form_submit:"SubmitForm",viewcontent:"ViewContent",view_content:"ViewContent",search:"Search",subscribe:"Subscribe",contact:"Contact",completeregistration:"CompleteRegistration",complete_registration:"CompleteRegistration",initiatecheckout:"InitiateCheckout",initiate_checkout:"InitiateCheckout"},l={addtocart:"add_to_cart",add_to_cart:"add_to_cart",lead:"generate_lead",form_submit:"generate_lead",viewcontent:"view_item",view_content:"view_item",search:"search",subscribe:"subscribe",contact:"contact",completeregistration:"sign_up",complete_registration:"sign_up",initiatecheckout:"begin_checkout",initiate_checkout:"begin_checkout"};if(window.fbq){let s=i[n];s?window.fbq("track",s,e):window.fbq("trackCustom",t,e)}if(window.ttq){let s=c[n]||t;window.ttq.track(s,e)}if(typeof window.gtag=="function"&&w){let s=l[n]||t;window.gtag("event",s,{send_to:w,...e})}if(typeof window.gtag=="function"&&p){let s=l[n]||t;window.gtag("event",s,{send_to:p,...e})}}function Rt(){try{return x==="local"?localStorage:sessionStorage}catch{return null}}function B(t){let e=Rt();if(!e)return null;try{let n=b+t,i=e.getItem(n);if(!i&&x==="session")try{i=localStorage.getItem(n)}catch{}return i}catch{return null}}function T(t,e){let n=Rt();if(n)try{n.setItem(b+t,e)}catch{}}function Dt(){if(x!=="local"){a("Upgrading storage tier: session -> local");try{let t=[];for(let e=0;e<sessionStorage.length;e++){let n=sessionStorage.key(e);n?.startsWith(b)&&t.push(n)}for(let e of t){let n=sessionStorage.getItem(e);n&&localStorage.setItem(e,n)}for(let e of t)sessionStorage.removeItem(e);x="local",a(`Storage tier upgraded, migrated ${t.length} items`)}catch(t){S(" Storage migration failed:",t)}}}function W(){return crypto&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,t=>{let e=Math.random()*16|0;return(t==="x"?e:e&3|8).toString(16)})}function _e(){try{let t=B(Ct),e=B(et);if(t&&e&&Date.now()-parseInt(e,10)<ae)return T(et,Date.now().toString()),t;let n=W();return T(Ct,n),T(et,Date.now().toString()),n}catch{return W()}}function ke(){let t=new URLSearchParams(window.location.search),e={};return["source","medium","campaign","content","term"].forEach(n=>{let i=t.get(`utm_${n}`);i&&(e[`utm_${n}`]=i)}),e}function xe(){try{let t=B(St);if(t){R=JSON.parse(t),a("Loaded attribution:",R);return}let e=ke();Object.keys(e).length>0&&(R=e,T(St,JSON.stringify(e)),a("Captured attribution:",R))}catch{}}function Ie(){try{f=localStorage.getItem(b+C),f&&a("Loaded fan ID hash:",f.substring(0,16)+"...")}catch{try{f=B(C)}catch{}}}function gt(){try{let t=localStorage.getItem(b+nt);if(t)return t;let e=W();return localStorage.setItem(b+nt,e),a("Created persistent visitor ID:",e.substring(0,8)+"..."),e}catch{return a("localStorage unavailable, using session ID as fallback"),q||W()}}function Ce(){try{y=localStorage.getItem(b+nt),y&&a("Loaded visitor ID:",y.substring(0,8)+"...")}catch{}}function Se(){try{let t=localStorage.getItem(G);if(t){let e=JSON.parse(t);if(e.expiresAt&&Date.now()>e.expiresAt){a("Consent expired - clearing stored consent"),localStorage.removeItem(G),d=null;return}if(e.expiresAt){let n=e.expiresAt-Date.now();n>0&&n<ne&&(a("Consent nearing expiration - will prompt for refresh"),window._balanceConsentNeedsRefresh=!0)}d=e.preferences||null,a("Loaded consent:",d)}}catch{}}function Mt(t){let e=d;d=t;try{let i={preferences:t,method:"explicit",version:1,expiresAt:Date.now()+ee};localStorage.setItem(G,JSON.stringify(i)),a("Consent saved with TTL:",t),window._balanceConsentNeedsRefresh=!1}catch(i){S(" Could not save consent:",i)}if(t.analytics===!0&&(Dt(),y||(y=gt()),f))try{localStorage.setItem(b+C,f),a("Fan ID hash persisted after consent granted")}catch{T(C,f)}let n=P({event_name:"consent_updated",metadata:{consent_preferences:t,consent_method:"explicit",previous_consent:e||void 0}});E(n);try{window.dispatchEvent(new CustomEvent("artistPixel:consent:updated",{detail:t})),a("DOM event artistPixel:consent:updated dispatched")}catch{}U&&At(t.marketing===!0)}function Pe(){return d}function Ee(t){return d?.[t]===!0}function Te(t){for(let[e,n]of Object.entries(t))try{let i=Ot(e);at[i]=n}catch{at[e]=n}a("Registered",Object.keys(t).length,"links for tracking enrichment")}function Ot(t){try{let e=new URL(t);return`${e.protocol}//${e.host}${e.pathname}${e.search}`}catch{return t}}function Le(t){let e=Ot(t);return at[e]||null}async function Ae(t){let e=t.toLowerCase().trim(),i=new TextEncoder().encode(e),c=await crypto.subtle.digest("SHA-256",i);return Array.from(new Uint8Array(c)).map(s=>s.toString(16).padStart(2,"0")).join("")}function P(t){let e=Kt(),n={artist_id:Y,fan_session_id:q,visitor_id:y||gt(),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:e.device_type,browser:e.browser,os:e.os,tracking_source:V,...t,...R};return X&&!t.projectId&&(n.projectId=X),n}function E(t){k.push(t),a("Event queued:",t.event_name,"(queue:",k.length,")"),k.length>=10&&H()}async function H(){if(k.length===0)return;let t=[...k];k=[],a("Flushing",t.length,"events to",A);try{let e=await fetch(A,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({events:t}),keepalive:!0});if(!e.ok)throw new Error(`HTTP ${e.status}`);a("Events sent successfully")}catch(e){if(S(" Failed to send events, trying fallback:",e),A!==Et&&!Q)try{if((await fetch(Et,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({events:t}),keepalive:!0})).ok){a("Events sent via fallback (no geo)");return}}catch(n){S(" Fallback also failed:",n)}k.length<50&&k.push(...t)}}function Re(){it&&clearInterval(it),it=window.setInterval(()=>{k.length>0&&H()},5e3)}function ut(){M||(ot=Date.now()),M=Date.now(),F=!0,a("Active time tracking started/resumed")}function jt(){M&&F&&(rt+=Date.now()-M),F=!1,a("Active time tracking paused, accumulated:",rt,"ms")}function Ut(){let t=rt;return F&&M&&(t+=Date.now()-M),t}function De(){Tt=Date.now(),z&&(z=!1,ut(),a("User returned from idle"))}function Me(){if(document.visibilityState==="hidden"){a("Skipping heartbeat - tab hidden");return}if(Date.now()-Tt>de){z||(z=!0,jt(),a("User idle - pausing heartbeat"));return}let t=Ut(),e=Math.round(t/1e3),n=P({event_name:"engagement_heartbeat",metadata:{time_on_page_seconds:e,time_on_page_ms:t,heartbeat_interval_ms:Z,is_active:F&&!z}});E(n),st++,a("Heartbeat sent:",e,"seconds active")}function ft(){if(!bt){a('Heartbeat disabled via data-heartbeat="false"');return}D&&clearInterval(D),ut(),D=window.setInterval(()=>{Me()},Z),a("Heartbeat started with interval:",Z,"ms")}function mt(){if(!ct){if(ct=!0,D&&(clearInterval(D),D=null),bt){let t=ot?Date.now()-ot:0,e=Ut(),n=P({event_name:"engagement_summary",metadata:{total_time_ms:t,active_time_ms:e,heartbeat_samples:st,page_path:window.location.pathname}});if(navigator.sendBeacon&&A){let i=JSON.stringify({events:[n]});navigator.sendBeacon(A,i),a("Engagement summary sent via sendBeacon:",Math.round(e/1e3),"seconds active")}else E(n),a("Engagement summary enqueued:",Math.round(e/1e3),"seconds active")}st=0}}function $(t={}){let e=t.url||window.location.href;if(oe(e)){a("Page excluded from tracking:",e);return}let n=P({event_name:"pageview",page_title:t.title||document.title,source_url:e});E(n),he()}function wt(t,e={}){let n=P({event_name:"custom",metadata:{event_type:t,...e}});E(n),ve(t,e)}async function qt(t,e={}){try{if(d&&d.analytics===!1){a("Identify skipped - user declined analytics consent");return}if(f=await Ae(t),d?.analytics===!0&&Dt(),d?.analytics===!0)try{localStorage.setItem(b+C,f)}catch{T(C,f)}let n=t.split("@"),i=n[0].charAt(0)+"***@"+(n[1]||"");a("Fan identified:",{name:e.name||"(no name)",email:i,hash:f.substring(0,16)+"...",traits:e,storageTier:x});let c=P({event_name:"identify",fan_id_hash:f,metadata:{email_sha256:f,email_display:i,traits:e,consent_preferences:d||void 0,storage_tier:x}});E(c)}catch(n){S(" Failed to identify:",n)}}function Oe(t){if(!t||typeof t!="string"){S("setFanIdHash: invalid hash provided");return}if(f=t,d?.analytics===!0)try{localStorage.setItem(b+C,t),a("Fan ID hash persisted:",t.substring(0,16)+"...")}catch{T(C,t)}else a("Fan ID hash set (memory only - no consent):",t.substring(0,16)+"...")}function Ft(t,e="USD",n={}){let i=P({event_name:"purchase",metadata:{revenue:t,currency:e,...n}});E(i),be(t,e,n)}function zt(){V=te(),a("Tracking source detected:",V),Se(),d?.analytics===!0?(x="local",a("Storage tier: local (analytics consent granted)")):(x="session",a("Storage tier: session (privacy by default)")),q=_e(),Ie(),Ce(),y||(y=gt()),d||a(tt?"Consent UI enabled, waiting for explicit user consent (Tier 0 mode)":"No consent - operating in privacy-first mode (session storage only, limited tracking)"),U&&d?.marketing===!0?(a("[Platform] Marketing consent already granted, initializing platform pixels"),At(!0)):U&&a("[Platform] Platform pixels configured:",{meta:O||"(none)",tiktok:L||"(none)",googleAds:w||"(none)"}),xe(),Re(),a("Initialized",{artistId:Y,projectId:X||"(none - will track to all projects)",rawProjectId:J||"(none)",sessionId:q,visitorId:y?y.substring(0,8)+"...":null,fanIdHash:f,consent:d,storageTier:x,trackingSource:V,useEmulator:Q,endpoint:A}),d?.analytics===!0?(window._balanceInitialPageviewFired=!0,$(),ft(),a("Full tracking enabled (user consented)")):tt?a("Tracking dormant - waiting for explicit consent via ConsentManager"):(window._balanceInitialPageviewFired=!0,$(),ft(),a("Privacy-first tracking enabled (session-scoped, visitor_id for deduplication)")),["mousemove","keydown","scroll","touchstart"].forEach(s=>{document.addEventListener(s,De,{passive:!0})});let t=window.location.hostname;function e(s){try{let o=new URL(s).hostname.toLowerCase();return o.includes("spotify")?"spotify":o.includes("apple")||o.includes("music.apple")?"apple_music":o.includes("youtube")||o.includes("youtu.be")?"youtube":o.includes("soundcloud")?"soundcloud":o.includes("tidal")?"tidal":o.includes("deezer")?"deezer":o.includes("amazon")||o.includes("music.amazon")?"amazon_music":o.includes("bandcamp")?"bandcamp":o.includes("lnk.to")||o.includes("linkfire")?"linkfire":o.includes("linktr.ee")?"linktree":o.includes("shop")||o.includes("store")||o.includes("merch")?"shop":o.includes("ticketmaster")||o.includes("eventbrite")||o.includes("dice.fm")?"tickets":o.includes("instagram")?"instagram":o.includes("twitter")||o.includes("x.com")?"twitter":o.includes("tiktok")?"tiktok":o.includes("facebook")?"facebook":"external"}catch{return"external"}}function n(s){try{return new URL(s,window.location.origin).hostname!==t}catch{return!1}}function i(s,o){let g=e(s),I=Le(s),Gt=I?.title||o?.textContent?.trim().substring(0,100)||"",Bt=I?.id||o?.id||o?.getAttribute("data-link-id")||void 0,Wt=I?.category;a("External link clicked:",{url:s,platform:g,linkText:Gt,linkId:Bt,fromRegistry:!!I}),wt("page_link_click",{link_url:s,link_text:Gt,link_id:Bt,platform:g,link_type:"external",...Wt&&{link_category:Wt},...I&&Object.fromEntries(Object.entries(I).filter(([yt])=>!["title","id","category"].includes(yt)).map(([yt,je])=>[`link_${yt}`,je]))})}let c=window.open;if(window.open=function(s,o,g){if(s){let I=s.toString();n(I)&&i(I)}return c.call(window,s,o,g)},a("window.open interception enabled (always on)"),r?.dataset.autoTrackLinks==="true"&&(document.addEventListener("click",s=>{let g=s.target.closest("a");if(g&&g.href&&n(g.href)){if(It&&(Pt(g.href)||g.hasAttribute("download")))return;i(g.href,g)}},{capture:!0,passive:!0}),a("External link click listener enabled (opt-in)")),It&&(document.addEventListener("click",s=>{let g=s.target.closest("a");g?.href&&(Pt(g.href)||g.hasAttribute("download"))&&se(g.href,g)},{capture:!0,passive:!0}),a("File download tracking enabled")),Zt){let s=window.location.href;window.addEventListener("hashchange",()=>{let o=window.location.href;o!==s&&(s=o,setTimeout(()=>{$({title:document.title,url:o})},0))}),a("Hash routing tracking enabled")}document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"?(jt(),mt(),H()):(ut(),ct=!1)}),window.addEventListener("pagehide",()=>{mt(),H()}),window.addEventListener("beforeunload",()=>{mt(),H()})}if(window.balance?._version&&window.balance._version!==v){console.warn(`[artistPixel] Version conflict: ${window.balance._version} already loaded, skipping ${v}`);return}let pt=window.balance?.q||[];function Nt(){if(pt.length>0){a("Processing",pt.length,"queued commands");for(let t of pt){let[e,...n]=t;switch(e){case"track":wt(n[0],n[1]);break;case"identify":qt(n[0],n[1]);break;case"page":$(n[0]);break;case"purchase":Ft(n[0],n[1],n[2]);break;case"setConsent":Mt(n[0]);break;default:a("Unknown queued command:",e)}}}}let Ht={track:wt,identify:qt,page:$,purchase:Ft,getSessionId:()=>q,getVisitorId:()=>y,getFanIdHash:()=>f,setFanIdHash:Oe,getAttribution:()=>R,setConsent:Mt,getConsent:Pe,hasConsent:Ee,registerLinks:Te,_version:v};window.artistPixel=Ht,window.balance=Ht;function $t(){tt&&!d&&new ge({style:Jt,primaryColor:Xt,position:Qt})}let Vt=window.requestIdleCallback||(t=>setTimeout(t,1));document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{zt(),Nt(),Vt(()=>$t())}):(zt(),Nt(),Vt(()=>$t())),a("Pixel script loaded")})();})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hifilabs/pixel",
3
- "version": "0.16.2",
3
+ "version": "0.16.3",
4
4
  "description": "artistPixel - Lightweight browser tracking script for artist fan analytics",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",