@hifilabs/pixel 0.11.1 → 0.13.0
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 +88 -2
- package/dist/browser.min.js +5 -5
- package/package.json +1 -1
package/dist/browser.js
CHANGED
|
@@ -75,6 +75,8 @@ var ArtistPixel = (() => {
|
|
|
75
75
|
const tiktokPixelId = currentScript?.dataset.tiktokPixelId || gtmConfig.tiktokPixelId;
|
|
76
76
|
const googleAdsId = currentScript?.dataset.googleAdsId || gtmConfig.googleAdsId;
|
|
77
77
|
const googleAdsConversionLabel = currentScript?.dataset.googleAdsConversionLabel || gtmConfig.googleAdsConversionLabel;
|
|
78
|
+
const googleTagManagerId = currentScript?.dataset.googleTagManagerId || gtmConfig.googleTagManagerId;
|
|
79
|
+
const googleAnalyticsId = currentScript?.dataset.googleAnalyticsId || gtmConfig.googleAnalyticsId;
|
|
78
80
|
const forwardingConfig = {
|
|
79
81
|
pageview: (currentScript?.dataset.forwardPageview ?? gtmConfig.forwardPageview) !== "false",
|
|
80
82
|
purchase: (currentScript?.dataset.forwardPurchase ?? gtmConfig.forwardPurchase) !== "false",
|
|
@@ -82,7 +84,7 @@ var ArtistPixel = (() => {
|
|
|
82
84
|
lead: (currentScript?.dataset.forwardLead ?? gtmConfig.forwardLead) !== "false",
|
|
83
85
|
custom: (currentScript?.dataset.forwardCustom || gtmConfig.forwardCustom || "").split(",").filter(Boolean)
|
|
84
86
|
};
|
|
85
|
-
const hasPlatformPixels = !!(metaPixelId || tiktokPixelId || googleAdsId);
|
|
87
|
+
const hasPlatformPixels = !!(metaPixelId || tiktokPixelId || googleAdsId || googleTagManagerId || googleAnalyticsId);
|
|
86
88
|
const consentUIEnabled = currentScript?.dataset.consentUi === "true";
|
|
87
89
|
const consentStyle = currentScript?.dataset.consentStyle || "brutalist";
|
|
88
90
|
const consentPrimaryColor = currentScript?.dataset.primaryColor;
|
|
@@ -512,6 +514,73 @@ var ArtistPixel = (() => {
|
|
|
512
514
|
};
|
|
513
515
|
document.head.appendChild(script);
|
|
514
516
|
}
|
|
517
|
+
function injectGoogleTagManager() {
|
|
518
|
+
if (!googleTagManagerId)
|
|
519
|
+
return;
|
|
520
|
+
if (document.querySelector(`script[src*="googletagmanager.com/gtm.js?id=${googleTagManagerId}"]`)) {
|
|
521
|
+
log("[Platform:GTM] Already loaded, skipping");
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
log("[Platform:GTM] Injecting GTM:", googleTagManagerId);
|
|
525
|
+
window.dataLayer = window.dataLayer || [];
|
|
526
|
+
if (typeof window.gtag !== "function") {
|
|
527
|
+
window.gtag = function(...args) {
|
|
528
|
+
window.dataLayer.push(args);
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
const consentDefaults = marketingConsentGranted ? "granted" : "denied";
|
|
532
|
+
window.gtag("consent", "default", {
|
|
533
|
+
ad_storage: consentDefaults,
|
|
534
|
+
ad_user_data: consentDefaults,
|
|
535
|
+
ad_personalization: consentDefaults,
|
|
536
|
+
analytics_storage: consentDefaults
|
|
537
|
+
});
|
|
538
|
+
window.gtag("set", "wait_for_update", 500);
|
|
539
|
+
window.dataLayer.push({
|
|
540
|
+
"gtm.start": (/* @__PURE__ */ new Date()).getTime(),
|
|
541
|
+
event: "gtm.js"
|
|
542
|
+
});
|
|
543
|
+
const script = document.createElement("script");
|
|
544
|
+
script.async = true;
|
|
545
|
+
script.src = `https://www.googletagmanager.com/gtm.js?id=${googleTagManagerId}`;
|
|
546
|
+
script.onload = () => {
|
|
547
|
+
log("[Platform:GTM] Initialized");
|
|
548
|
+
};
|
|
549
|
+
document.head.appendChild(script);
|
|
550
|
+
}
|
|
551
|
+
function injectGoogleAnalytics() {
|
|
552
|
+
if (!googleAnalyticsId)
|
|
553
|
+
return;
|
|
554
|
+
const existingGtagScript = document.querySelector('script[src*="googletagmanager.com/gtag/js"]');
|
|
555
|
+
window.dataLayer = window.dataLayer || [];
|
|
556
|
+
if (typeof window.gtag !== "function") {
|
|
557
|
+
window.gtag = function(...args) {
|
|
558
|
+
window.dataLayer.push(args);
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
const consentDefaults = marketingConsentGranted ? "granted" : "denied";
|
|
562
|
+
window.gtag("consent", "default", {
|
|
563
|
+
ad_storage: consentDefaults,
|
|
564
|
+
ad_user_data: consentDefaults,
|
|
565
|
+
ad_personalization: consentDefaults,
|
|
566
|
+
analytics_storage: consentDefaults
|
|
567
|
+
});
|
|
568
|
+
if (existingGtagScript) {
|
|
569
|
+
log("[Platform:GA4] gtag.js already loaded, configuring");
|
|
570
|
+
window.gtag("config", googleAnalyticsId, { send_page_view: false });
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
log("[Platform:GA4] Injecting gtag:", googleAnalyticsId);
|
|
574
|
+
const script = document.createElement("script");
|
|
575
|
+
script.async = true;
|
|
576
|
+
script.src = `https://www.googletagmanager.com/gtag/js?id=${googleAnalyticsId}`;
|
|
577
|
+
script.onload = () => {
|
|
578
|
+
window.gtag("js", /* @__PURE__ */ new Date());
|
|
579
|
+
window.gtag("config", googleAnalyticsId, { send_page_view: false });
|
|
580
|
+
log("[Platform:GA4] Initialized");
|
|
581
|
+
};
|
|
582
|
+
document.head.appendChild(script);
|
|
583
|
+
}
|
|
515
584
|
function injectPlatformPixels() {
|
|
516
585
|
if (!hasPlatformPixels)
|
|
517
586
|
return;
|
|
@@ -528,6 +597,8 @@ var ArtistPixel = (() => {
|
|
|
528
597
|
injectMetaPixel();
|
|
529
598
|
injectTikTokPixel();
|
|
530
599
|
injectGoogleAds();
|
|
600
|
+
injectGoogleTagManager();
|
|
601
|
+
injectGoogleAnalytics();
|
|
531
602
|
}
|
|
532
603
|
function setPlatformMarketingConsent(granted) {
|
|
533
604
|
marketingConsentGranted = granted;
|
|
@@ -547,7 +618,8 @@ var ArtistPixel = (() => {
|
|
|
547
618
|
window.gtag("consent", "update", {
|
|
548
619
|
ad_storage: consentValue,
|
|
549
620
|
ad_user_data: consentValue,
|
|
550
|
-
ad_personalization: consentValue
|
|
621
|
+
ad_personalization: consentValue,
|
|
622
|
+
analytics_storage: consentValue
|
|
551
623
|
});
|
|
552
624
|
}
|
|
553
625
|
}
|
|
@@ -578,6 +650,11 @@ var ArtistPixel = (() => {
|
|
|
578
650
|
if (window.ttq) {
|
|
579
651
|
window.ttq.page();
|
|
580
652
|
}
|
|
653
|
+
if (typeof window.gtag === "function" && googleAnalyticsId) {
|
|
654
|
+
window.gtag("event", "page_view", {
|
|
655
|
+
send_to: googleAnalyticsId
|
|
656
|
+
});
|
|
657
|
+
}
|
|
581
658
|
}
|
|
582
659
|
function forwardPurchase(value, currency, properties) {
|
|
583
660
|
if (!shouldForwardEvent("purchase"))
|
|
@@ -614,6 +691,15 @@ var ArtistPixel = (() => {
|
|
|
614
691
|
transaction_id: properties?.transactionId
|
|
615
692
|
});
|
|
616
693
|
}
|
|
694
|
+
if (typeof window.gtag === "function" && googleAnalyticsId) {
|
|
695
|
+
window.gtag("event", "purchase", {
|
|
696
|
+
send_to: googleAnalyticsId,
|
|
697
|
+
value,
|
|
698
|
+
currency,
|
|
699
|
+
transaction_id: properties?.transactionId,
|
|
700
|
+
items: properties?.items
|
|
701
|
+
});
|
|
702
|
+
}
|
|
617
703
|
}
|
|
618
704
|
function forwardCustomEvent(eventName, properties) {
|
|
619
705
|
if (!shouldForwardEvent(eventName))
|
package/dist/browser.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var ArtistPixel=(()=>{var
|
|
1
|
+
var ArtistPixel=(()=>{var je=Object.defineProperty;var Ue=(y,h,w)=>h in y?je(y,h,{enumerable:!0,configurable:!0,writable:!0,value:w}):y[h]=w;var W=(y,h,w)=>(Ue(y,typeof h!="symbol"?h+"":h,w),w);(function(){let y="0.11.0";function h(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 w=null;function Kt(){if(!w)try{w=h(navigator.userAgent)}catch{w={device_type:"desktop",browser:"Unknown",os:"Unknown"}}return w}let s=document.currentScript,K=s?.dataset.artistId,Y=s?.dataset.projectId,J=Y?Yt(Y):void 0;function Yt(t){return!t||t.startsWith("release_")||t.startsWith("merch_")||t.startsWith("link_")||t.startsWith("custom_")?t:`custom_${t}`}let X=s?.dataset.emulator==="true",yt=s?.dataset.debug==="true",Q=parseInt(s?.dataset.heartbeatInterval||"120000",10),ht=s?.dataset.heartbeat!=="false",bt=s?.dataset.source,vt=s?.dataset.endpoint,f=window.__artistPixelConfig||{},D=s?.dataset.metaPixelId||f.metaPixelId,P=s?.dataset.tiktokPixelId||f.tiktokPixelId,u=s?.dataset.googleAdsId||f.googleAdsId,_t=s?.dataset.googleAdsConversionLabel||f.googleAdsConversionLabel,M=s?.dataset.googleTagManagerId||f.googleTagManagerId,p=s?.dataset.googleAnalyticsId||f.googleAnalyticsId,x={pageview:(s?.dataset.forwardPageview??f.forwardPageview)!=="false",purchase:(s?.dataset.forwardPurchase??f.forwardPurchase)!=="false",addToCart:(s?.dataset.forwardAddToCart??f.forwardAddToCart)!=="false",lead:(s?.dataset.forwardLead??f.forwardLead)!=="false",custom:(s?.dataset.forwardCustom||f.forwardCustom||"").split(",").filter(Boolean)},O=!!(D||P||u||M||p),Z=s?.dataset.consentUi==="true",Jt=s?.dataset.consentStyle||"brutalist",Xt=s?.dataset.primaryColor,Qt=s?.dataset.bannerPosition||"bottom",kt=s?.dataset.excludePages?.split(",").filter(Boolean)||[],xt=s?.dataset.trackFileDownloads==="true",Zt=s?.dataset.hashRouting==="true";function te(){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 H="pixel";if(!K){S(" Error: data-artist-id attribute is required");return}let It="session_id",tt="session_timestamp",Ct="attribution",St="fan_id_hash",et="visitor_id",G="artistPixel_consent",ee=365*24*60*60*1e3,ne=30*24*60*60*1e3,ae=60*60*1e3,E="balance_";function oe(t){let e=t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*");return new RegExp("^"+e+"$")}function ie(t){if(kt.length===0)return!1;try{let e=new URL(t,window.location.origin).pathname;return kt.some(n=>oe(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 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=I({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)||""}});C(d)}catch(n){S("Failed to track file download:",n)}}let ce="https://balance-pixel-proxy.hifilabs.workers.dev",Et="https://us-central1-artist-os-distro.cloudfunctions.net/ingestEvents",T=vt||(X?"http://localhost:5001/artist-os-distro/us-central1/ingestEvents":ce),j=null,m=null,b=null,g=null,L={},nt={},v=[],at=null,_="session",A=null,ot=0,it=0,R=0,U=!0,rt=0,st=!1,de=2*60*1e3,Tt=Date.now(),q=!1,le=!yt,a=(...t)=>{yt&&console.log("[artistPixel]",...t)},S=(...t)=>{le||console.error("[artistPixel]",...t)},ct={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 Re=Object.defineProperty;var De=(p,w,m)=>w in p?Re(p,w
|
|
|
83
83
|
}
|
|
84
84
|
.btn.accept { background: #fff; color: #000; border-color: #fff; }
|
|
85
85
|
.btn:hover { opacity: 0.8; }
|
|
86
|
-
`};class
|
|
86
|
+
`};class ge{constructor(e){W(this,"container",null);W(this,"shadow",null);W(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=ct.base,o=ct[e]||ct.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=`
|
|
87
87
|
<style>
|
|
88
88
|
${n}
|
|
89
89
|
:host { ${c} }
|
|
90
|
-
${
|
|
91
|
-
${
|
|
90
|
+
${o}
|
|
91
|
+
${d}
|
|
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 Re=Object.defineProperty;var De=(p,w,m)=>w in p?Re(p,w
|
|
|
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(),dt(),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 Et=!1,rt=!1;function de(){if(!R)return;if(window.fbq){a("[Platform:Meta] Already loaded, skipping");return}a("[Platform:Meta] Injecting pixel:",R);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",R),_.pageview&&window.fbq("track","PageView"),a("[Platform:Meta] Initialized and PageView tracked")},document.head.appendChild(e)}function ue(){if(!S)return;if(window.ttq?._i?.[S]){a("[Platform:TikTok] Already loaded, skipping");return}a("[Platform:TikTok] Injecting pixel:",S);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(S),_.pageview&&n.page(),a("[Platform:TikTok] Initialized and page() tracked")}function fe(){if(!f)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",f);return}a("[Platform:GoogleAds] Injecting gtag:",f),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=${f}`,t.onload=()=>{window.gtag("js",new Date),window.gtag("config",f),a("[Platform:GoogleAds] Initialized")},document.head.appendChild(t)}function ge(){if(D){if(Et){a("[Platform] Already loaded, skipping");return}if(!rt){a("[Platform] Marketing consent not granted, deferring");return}a("[Platform] Injecting platform pixels (consent granted)"),Et=!0,de(),ue(),fe()}}function Tt(t){if(rt=t,a("[Platform] Marketing consent:",t?"granted":"denied"),t&&ge(),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})}}function st(t){if(!D||!rt)return!1;let e=t.toLowerCase();return e==="pageview"||e==="page_view"?_.pageview:e==="purchase"?_.purchase:e==="addtocart"||e==="add_to_cart"?_.addToCart:e==="lead"||e==="form_submit"?_.lead:_.custom.length>0?_.custom.some(n=>n.toLowerCase()===e):!1}function me(){st("pageview")&&(a("[Platform] Forwarding PageView"),window.fbq&&window.fbq("track","PageView"),window.ttq&&window.ttq.page())}function pe(t,e,n){if(st("purchase")&&(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"&&f)){let i=yt?`${f}/${yt}`:f;window.gtag("event","conversion",{send_to:i,value:t,currency:e,transaction_id:n?.transactionId}),window.gtag("event","purchase",{send_to:f,value:t,currency:e,transaction_id:n?.transactionId})}}function we(t,e){if(!st(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 r=i[n];r?window.fbq("track",r,e):window.fbq("trackCustom",t,e)}if(window.ttq){let r=c[n]||t;window.ttq.track(r,e)}if(typeof window.gtag=="function"&&f){let r=l[n]||t;window.gtag("event",r,{send_to:f,...e})}}function Lt(){try{return v==="local"?localStorage:sessionStorage}catch{return null}}function H(t){let e=Lt();if(!e)return null;try{let n=P+t,i=e.getItem(n);if(!i&&v==="session")try{i=localStorage.getItem(n)}catch{}return i}catch{return null}}function q(t,e){let n=Lt();if(n)try{n.setItem(P+t,e)}catch{}}function At(){if(v!=="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(P)&&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);v="local",a(`Storage tier upgraded, migrated ${t.length} items`)}catch(t){C(" Storage migration failed:",t)}}}function $(){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 he(){try{let t=H(_t),e=H(X);if(t&&e&&Date.now()-parseInt(e,10)<ee)return q(X,Date.now().toString()),t;let n=$();return q(_t,n),q(X,Date.now().toString()),n}catch{return $()}}function be(){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 ye(){try{let t=H(xt);if(t){T=JSON.parse(t),a("Loaded attribution:",T);return}let e=be();Object.keys(e).length>0&&(T=e,q(xt,JSON.stringify(e)),a("Captured attribution:",T))}catch{}}function ve(){try{b=H(It)}catch{}}function ct(){try{let t=localStorage.getItem(P+Q);if(t)return t;let e=$();return localStorage.setItem(P+Q,e),a("Created persistent visitor ID:",e.substring(0,8)+"..."),e}catch{return a("localStorage unavailable, using session ID as fallback"),O||$()}}function ke(){try{g=localStorage.getItem(P+Q),g&&a("Loaded visitor ID:",g.substring(0,8)+"...")}catch{}}function _e(){try{let t=localStorage.getItem(z);if(t){let e=JSON.parse(t);if(e.expiresAt&&Date.now()>e.expiresAt){a("Consent expired - clearing stored consent"),localStorage.removeItem(z),u=null;return}if(e.expiresAt){let n=e.expiresAt-Date.now();n>0&&n<te&&(a("Consent nearing expiration - will prompt for refresh"),window._balanceConsentNeedsRefresh=!0)}u=e.preferences||null,a("Loaded consent:",u)}}catch{}}function Rt(t){let e=u;u=t;try{let i={preferences:t,method:"explicit",version:1,expiresAt:Date.now()+Zt};localStorage.setItem(z,JSON.stringify(i)),a("Consent saved with TTL:",t),window._balanceConsentNeedsRefresh=!1}catch(i){C(" Could not save consent:",i)}t.analytics===!0&&(At(),g||(g=ct()));let n=x({event_name:"consent_updated",metadata:{consent_preferences:t,consent_method:"explicit",previous_consent:e||void 0}});I(n);try{window.dispatchEvent(new CustomEvent("artistPixel:consent:updated",{detail:t})),a("DOM event artistPixel:consent:updated dispatched")}catch{}D&&Tt(t.marketing===!0)}function xe(){return u}function Ie(t){return u?.[t]===!0}function Ce(t){for(let[e,n]of Object.entries(t))try{let i=Dt(e);Z[i]=n}catch{Z[e]=n}a("Registered",Object.keys(t).length,"links for tracking enrichment")}function Dt(t){try{let e=new URL(t);return`${e.protocol}//${e.host}${e.pathname}${e.search}`}catch{return t}}function Se(t){let e=Dt(t);return Z[e]||null}async function Pe(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(r=>r.toString(16).padStart(2,"0")).join("")}function x(t){let e=Wt(),n={artist_id:B,fan_session_id:O,visitor_id:g||ct(),fan_id_hash:b||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:N,...t,...T};return G&&!t.projectId&&(n.projectId=G),n}function I(t){y.push(t),a("Event queued:",t.event_name,"(queue:",y.length,")"),y.length>=10&&j()}async function j(){if(y.length===0)return;let t=[...y];y=[],a("Flushing",t.length,"events to",E);try{let e=await fetch(E,{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(C(" Failed to send events, trying fallback:",e),E!==St&&!K)try{if((await fetch(St,{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){C(" Fallback also failed:",n)}y.length<50&&y.push(...t)}}function Ee(){tt&&clearInterval(tt),tt=window.setInterval(()=>{y.length>0&&j()},5e3)}function lt(){A||(et=Date.now()),A=Date.now(),M=!0,a("Active time tracking started/resumed")}function Ot(){A&&M&&(nt+=Date.now()-A),M=!1,a("Active time tracking paused, accumulated:",nt,"ms")}function Mt(){let t=nt;return M&&A&&(t+=Date.now()-A),t}function Te(){Pt=Date.now(),U&&(U=!1,lt(),a("User returned from idle"))}function Le(){if(document.visibilityState==="hidden"){a("Skipping heartbeat - tab hidden");return}if(Date.now()-Pt>se){U||(U=!0,Ot(),a("User idle - pausing heartbeat"));return}let t=Mt(),e=Math.round(t/1e3),n=x({event_name:"engagement_heartbeat",metadata:{time_on_page_seconds:e,time_on_page_ms:t,heartbeat_interval_ms:Y,is_active:M&&!U}});I(n),at++,a("Heartbeat sent:",e,"seconds active")}function dt(){if(!wt){a('Heartbeat disabled via data-heartbeat="false"');return}L&&clearInterval(L),lt(),L=window.setInterval(()=>{Le()},Y),a("Heartbeat started with interval:",Y,"ms")}function ut(){if(!it){if(it=!0,L&&(clearInterval(L),L=null),wt){let t=et?Date.now()-et:0,e=Mt(),n=x({event_name:"engagement_summary",metadata:{total_time_ms:t,active_time_ms:e,heartbeat_samples:at,page_path:window.location.pathname}});if(navigator.sendBeacon&&E){let i=JSON.stringify({events:[n]});navigator.sendBeacon(E,i),a("Engagement summary sent via sendBeacon:",Math.round(e/1e3),"seconds active")}else I(n),a("Engagement summary enqueued:",Math.round(e/1e3),"seconds active")}at=0}}function F(t={}){let e=t.url||window.location.href;if(ae(e)){a("Page excluded from tracking:",e);return}let n=x({event_name:"pageview",page_title:t.title||document.title,source_url:e});I(n),me()}function ft(t,e={}){let n=x({event_name:"custom",metadata:{event_type:t,...e}});I(n),we(t,e)}async function Ut(t,e={}){try{if(u&&u.analytics===!1){a("Identify skipped - user declined analytics consent");return}b=await Pe(t),u?.analytics===!0&&At(),q(It,b);let n=t.split("@"),i=n[0].charAt(0)+"***@"+(n[1]||"");a("Fan identified:",{name:e.name||"(no name)",email:i,hash:b.substring(0,16)+"...",traits:e,storageTier:v});let c=x({event_name:"identify",fan_id_hash:b,metadata:{email_sha256:b,email_display:i,traits:e,consent_preferences:u||void 0,storage_tier:v}});I(c)}catch(n){C(" Failed to identify:",n)}}function qt(t,e="USD",n={}){let i=x({event_name:"purchase",metadata:{revenue:t,currency:e,...n}});I(i),pe(t,e,n)}function jt(){N=Qt(),a("Tracking source detected:",N),_e(),u?.analytics===!0?(v="local",a("Storage tier: local (analytics consent granted)")):(v="session",a("Storage tier: session (privacy by default)")),O=he(),ve(),ke(),g||(g=ct()),u||a(J?"Consent UI enabled, waiting for explicit user consent (Tier 0 mode)":"No consent - operating in privacy-first mode (session storage only, limited tracking)"),D&&u?.marketing===!0?(a("[Platform] Marketing consent already granted, initializing platform pixels"),Tt(!0)):D&&a("[Platform] Platform pixels configured:",{meta:R||"(none)",tiktok:S||"(none)",googleAds:f||"(none)"}),ye(),Ee(),a("Initialized",{artistId:B,projectId:G||"(none - will track to all projects)",rawProjectId:W||"(none)",sessionId:O,visitorId:g?g.substring(0,8)+"...":null,fanIdHash:b,consent:u,storageTier:v,trackingSource:N,useEmulator:K,endpoint:E}),u?.analytics===!0?(window._balanceInitialPageviewFired=!0,F(),dt(),a("Full tracking enabled (user consented)")):J?a("Tracking dormant - waiting for explicit consent via ConsentManager"):(window._balanceInitialPageviewFired=!0,F(),dt(),a("Privacy-first tracking enabled (session-scoped, visitor_id for deduplication)")),["mousemove","keydown","scroll","touchstart"].forEach(r=>{document.addEventListener(r,Te,{passive:!0})});let t=window.location.hostname;function e(r){try{let o=new URL(r).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(r){try{return new URL(r,window.location.origin).hostname!==t}catch{return!1}}function i(r,o){let d=e(r),k=Se(r),$t=k?.title||o?.textContent?.trim().substring(0,100)||"",Vt=k?.id||o?.id||o?.getAttribute("data-link-id")||void 0,Bt=k?.category;a("External link clicked:",{url:r,platform:d,linkText:$t,linkId:Vt,fromRegistry:!!k}),ft("page_link_click",{link_url:r,link_text:$t,link_id:Vt,platform:d,link_type:"external",...Bt&&{link_category:Bt},...k&&Object.fromEntries(Object.entries(k).filter(([mt])=>!["title","id","category"].includes(mt)).map(([mt,Ae])=>[`link_${mt}`,Ae]))})}let c=window.open;if(window.open=function(r,o,d){if(r){let k=r.toString();n(k)&&i(k)}return c.call(window,r,o,d)},a("window.open interception enabled (always on)"),s?.dataset.autoTrackLinks==="true"&&(document.addEventListener("click",r=>{let d=r.target.closest("a");if(d&&d.href&&n(d.href)){if(kt&&(Ct(d.href)||d.hasAttribute("download")))return;i(d.href,d)}},{capture:!0,passive:!0}),a("External link click listener enabled (opt-in)")),kt&&(document.addEventListener("click",r=>{let d=r.target.closest("a");d?.href&&(Ct(d.href)||d.hasAttribute("download"))&&oe(d.href,d)},{capture:!0,passive:!0}),a("File download tracking enabled")),Xt){let r=window.location.href;window.addEventListener("hashchange",()=>{let o=window.location.href;o!==r&&(r=o,setTimeout(()=>{F({title:document.title,url:o})},0))}),a("Hash routing tracking enabled")}document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"?(Ot(),ut(),j()):(lt(),it=!1)}),window.addEventListener("pagehide",()=>{ut(),j()}),window.addEventListener("beforeunload",()=>{ut(),j()})}if(window.balance?._version&&window.balance._version!==p){console.warn(`[artistPixel] Version conflict: ${window.balance._version} already loaded, skipping ${p}`);return}let gt=window.balance?.q||[];function Ft(){if(gt.length>0){a("Processing",gt.length,"queued commands");for(let t of gt){let[e,...n]=t;switch(e){case"track":ft(n[0],n[1]);break;case"identify":Ut(n[0],n[1]);break;case"page":F(n[0]);break;case"purchase":qt(n[0],n[1],n[2]);break;case"setConsent":Rt(n[0]);break;default:a("Unknown queued command:",e)}}}}let Nt={track:ft,identify:Ut,page:F,purchase:qt,getSessionId:()=>O,getVisitorId:()=>g,getFanIdHash:()=>b,getAttribution:()=>T,setConsent:Rt,getConsent:xe,hasConsent:Ie,registerLinks:Ce,_version:p};window.artistPixel=Nt,window.balance=Nt;function zt(){J&&!u&&new le({style:Kt,primaryColor:Yt,position:Jt})}let Ht=window.requestIdleCallback||(t=>setTimeout(t,1));document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{jt(),Ft(),Ht(()=>zt())}):(jt(),Ft(),Ht(()=>zt())),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(),ut(),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,F=!1;function ue(){if(!D)return;if(window.fbq){a("[Platform:Meta] Already loaded, skipping");return}a("[Platform:Meta] Injecting pixel:",D);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",D),x.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),x.pageview&&n.page(),a("[Platform:TikTok] Initialized and page() tracked")}function me(){if(!u)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",u);return}a("[Platform:GoogleAds] Injecting gtag:",u),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=${u}`,t.onload=()=>{window.gtag("js",new Date),window.gtag("config",u),a("[Platform:GoogleAds] Initialized")},document.head.appendChild(t)}function we(){if(!M)return;if(document.querySelector(`script[src*="googletagmanager.com/gtm.js?id=${M}"]`)){a("[Platform:GTM] Already loaded, skipping");return}a("[Platform:GTM] Injecting GTM:",M),window.dataLayer=window.dataLayer||[],typeof window.gtag!="function"&&(window.gtag=function(...n){window.dataLayer.push(n)});let t=F?"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=${M}`,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=F?"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(O){if(Lt){a("[Platform] Already loaded, skipping");return}if(!F){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(F=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 dt(t){if(!O||!F)return!1;let e=t.toLowerCase();return e==="pageview"||e==="page_view"?x.pageview:e==="purchase"?x.purchase:e==="addtocart"||e==="add_to_cart"?x.addToCart:e==="lead"||e==="form_submit"?x.lead:x.custom.length>0?x.custom.some(n=>n.toLowerCase()===e):!1}function he(){dt("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(dt("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"&&u){let o=_t?`${u}/${_t}`:u;window.gtag("event","conversion",{send_to:o,value:t,currency:e,transaction_id:n?.transactionId}),window.gtag("event","purchase",{send_to:u,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(!dt(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 r=o[n];r?window.fbq("track",r,e):window.fbq("trackCustom",t,e)}if(window.ttq){let r=c[n]||t;window.ttq.track(r,e)}if(typeof window.gtag=="function"&&u){let r=d[n]||t;window.gtag("event",r,{send_to:u,...e})}}function Rt(){try{return _==="local"?localStorage:sessionStorage}catch{return null}}function V(t){let e=Rt();if(!e)return null;try{let n=E+t,o=e.getItem(n);if(!o&&_==="session")try{o=localStorage.getItem(n)}catch{}return o}catch{return null}}function z(t,e){let n=Rt();if(n)try{n.setItem(E+t,e)}catch{}}function Dt(){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(E)&&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){S(" Storage migration failed:",t)}}}function B(){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=V(It),e=V(tt);if(t&&e&&Date.now()-parseInt(e,10)<ae)return z(tt,Date.now().toString()),t;let n=B();return z(It,n),z(tt,Date.now().toString()),n}catch{return B()}}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=V(Ct);if(t){L=JSON.parse(t),a("Loaded attribution:",L);return}let e=ke();Object.keys(e).length>0&&(L=e,z(Ct,JSON.stringify(e)),a("Captured attribution:",L))}catch{}}function Ie(){try{b=V(St)}catch{}}function lt(){try{let t=localStorage.getItem(E+et);if(t)return t;let e=B();return localStorage.setItem(E+et,e),a("Created persistent visitor ID:",e.substring(0,8)+"..."),e}catch{return a("localStorage unavailable, using session ID as fallback"),j||B()}}function Ce(){try{m=localStorage.getItem(E+et),m&&a("Loaded visitor ID:",m.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),g=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)}g=e.preferences||null,a("Loaded consent:",g)}}catch{}}function Mt(t){let e=g;g=t;try{let o={preferences:t,method:"explicit",version:1,expiresAt:Date.now()+ee};localStorage.setItem(G,JSON.stringify(o)),a("Consent saved with TTL:",t),window._balanceConsentNeedsRefresh=!1}catch(o){S(" Could not save consent:",o)}t.analytics===!0&&(Dt(),m||(m=lt()));let n=I({event_name:"consent_updated",metadata:{consent_preferences:t,consent_method:"explicit",previous_consent:e||void 0}});C(n);try{window.dispatchEvent(new CustomEvent("artistPixel:consent:updated",{detail:t})),a("DOM event artistPixel:consent:updated dispatched")}catch{}O&&At(t.marketing===!0)}function Pe(){return g}function Ee(t){return g?.[t]===!0}function Te(t){for(let[e,n]of Object.entries(t))try{let o=Ot(e);nt[o]=n}catch{nt[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 nt[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(r=>r.toString(16).padStart(2,"0")).join("")}function I(t){let e=Kt(),n={artist_id:K,fan_session_id:j,visitor_id:m||lt(),fan_id_hash:b||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:H,...t,...L};return J&&!t.projectId&&(n.projectId=J),n}function C(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(S(" Failed to send events, trying fallback:",e),T!==Et&&!X)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)}v.length<50&&v.push(...t)}}function Re(){at&&clearInterval(at),at=window.setInterval(()=>{v.length>0&&N()},5e3)}function gt(){R||(ot=Date.now()),R=Date.now(),U=!0,a("Active time tracking started/resumed")}function jt(){R&&U&&(it+=Date.now()-R),U=!1,a("Active time tracking paused, accumulated:",it,"ms")}function Ut(){let t=it;return U&&R&&(t+=Date.now()-R),t}function De(){Tt=Date.now(),q&&(q=!1,gt(),a("User returned from idle"))}function Me(){if(document.visibilityState==="hidden"){a("Skipping heartbeat - tab hidden");return}if(Date.now()-Tt>de){q||(q=!0,jt(),a("User idle - pausing heartbeat"));return}let t=Ut(),e=Math.round(t/1e3),n=I({event_name:"engagement_heartbeat",metadata:{time_on_page_seconds:e,time_on_page_ms:t,heartbeat_interval_ms:Q,is_active:U&&!q}});C(n),rt++,a("Heartbeat sent:",e,"seconds active")}function ut(){if(!ht){a('Heartbeat disabled via data-heartbeat="false"');return}A&&clearInterval(A),gt(),A=window.setInterval(()=>{Me()},Q),a("Heartbeat started with interval:",Q,"ms")}function ft(){if(!st){if(st=!0,A&&(clearInterval(A),A=null),ht){let t=ot?Date.now()-ot:0,e=Ut(),n=I({event_name:"engagement_summary",metadata:{total_time_ms:t,active_time_ms:e,heartbeat_samples:rt,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 C(n),a("Engagement summary enqueued:",Math.round(e/1e3),"seconds active")}rt=0}}function $(t={}){let e=t.url||window.location.href;if(ie(e)){a("Page excluded from tracking:",e);return}let n=I({event_name:"pageview",page_title:t.title||document.title,source_url:e});C(n),he()}function mt(t,e={}){let n=I({event_name:"custom",metadata:{event_type:t,...e}});C(n),ve(t,e)}async function qt(t,e={}){try{if(g&&g.analytics===!1){a("Identify skipped - user declined analytics consent");return}b=await Ae(t),g?.analytics===!0&&Dt(),z(St,b);let n=t.split("@"),o=n[0].charAt(0)+"***@"+(n[1]||"");a("Fan identified:",{name:e.name||"(no name)",email:o,hash:b.substring(0,16)+"...",traits:e,storageTier:_});let c=I({event_name:"identify",fan_id_hash:b,metadata:{email_sha256:b,email_display:o,traits:e,consent_preferences:g||void 0,storage_tier:_}});C(c)}catch(n){S(" Failed to identify:",n)}}function Ft(t,e="USD",n={}){let o=I({event_name:"purchase",metadata:{revenue:t,currency:e,...n}});C(o),be(t,e,n)}function zt(){H=te(),a("Tracking source detected:",H),Se(),g?.analytics===!0?(_="local",a("Storage tier: local (analytics consent granted)")):(_="session",a("Storage tier: session (privacy by default)")),j=_e(),Ie(),Ce(),m||(m=lt()),g||a(Z?"Consent UI enabled, waiting for explicit user consent (Tier 0 mode)":"No consent - operating in privacy-first mode (session storage only, limited tracking)"),O&&g?.marketing===!0?(a("[Platform] Marketing consent already granted, initializing platform pixels"),At(!0)):O&&a("[Platform] Platform pixels configured:",{meta:D||"(none)",tiktok:P||"(none)",googleAds:u||"(none)"}),xe(),Re(),a("Initialized",{artistId:K,projectId:J||"(none - will track to all projects)",rawProjectId:Y||"(none)",sessionId:j,visitorId:m?m.substring(0,8)+"...":null,fanIdHash:b,consent:g,storageTier:_,trackingSource:H,useEmulator:X,endpoint:T}),g?.analytics===!0?(window._balanceInitialPageviewFired=!0,$(),ut(),a("Full tracking enabled (user consented)")):Z?a("Tracking dormant - waiting for explicit consent via ConsentManager"):(window._balanceInitialPageviewFired=!0,$(),ut(),a("Privacy-first tracking enabled (session-scoped, visitor_id for deduplication)")),["mousemove","keydown","scroll","touchstart"].forEach(r=>{document.addEventListener(r,De,{passive:!0})});let t=window.location.hostname;function e(r){try{let i=new URL(r).hostname.toLowerCase();return i.includes("spotify")?"spotify":i.includes("apple")||i.includes("music.apple")?"apple_music":i.includes("youtube")||i.includes("youtu.be")?"youtube":i.includes("soundcloud")?"soundcloud":i.includes("tidal")?"tidal":i.includes("deezer")?"deezer":i.includes("amazon")||i.includes("music.amazon")?"amazon_music":i.includes("bandcamp")?"bandcamp":i.includes("lnk.to")||i.includes("linkfire")?"linkfire":i.includes("linktr.ee")?"linktree":i.includes("shop")||i.includes("store")||i.includes("merch")?"shop":i.includes("ticketmaster")||i.includes("eventbrite")||i.includes("dice.fm")?"tickets":i.includes("instagram")?"instagram":i.includes("twitter")||i.includes("x.com")?"twitter":i.includes("tiktok")?"tiktok":i.includes("facebook")?"facebook":"external"}catch{return"external"}}function n(r){try{return new URL(r,window.location.origin).hostname!==t}catch{return!1}}function o(r,i){let l=e(r),k=Le(r),Vt=k?.title||i?.textContent?.trim().substring(0,100)||"",Bt=k?.id||i?.id||i?.getAttribute("data-link-id")||void 0,Wt=k?.category;a("External link clicked:",{url:r,platform:l,linkText:Vt,linkId:Bt,fromRegistry:!!k}),mt("page_link_click",{link_url:r,link_text:Vt,link_id:Bt,platform:l,link_type:"external",...Wt&&{link_category:Wt},...k&&Object.fromEntries(Object.entries(k).filter(([pt])=>!["title","id","category"].includes(pt)).map(([pt,Oe])=>[`link_${pt}`,Oe]))})}let c=window.open;if(window.open=function(r,i,l){if(r){let k=r.toString();n(k)&&o(k)}return c.call(window,r,i,l)},a("window.open interception enabled (always on)"),s?.dataset.autoTrackLinks==="true"&&(document.addEventListener("click",r=>{let l=r.target.closest("a");if(l&&l.href&&n(l.href)){if(xt&&(Pt(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",r=>{let l=r.target.closest("a");l?.href&&(Pt(l.href)||l.hasAttribute("download"))&&se(l.href,l)},{capture:!0,passive:!0}),a("File download tracking enabled")),Zt){let r=window.location.href;window.addEventListener("hashchange",()=>{let i=window.location.href;i!==r&&(r=i,setTimeout(()=>{$({title:document.title,url:i})},0))}),a("Hash routing tracking enabled")}document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"?(jt(),ft(),N()):(gt(),st=!1)}),window.addEventListener("pagehide",()=>{ft(),N()}),window.addEventListener("beforeunload",()=>{ft(),N()})}if(window.balance?._version&&window.balance._version!==y){console.warn(`[artistPixel] Version conflict: ${window.balance._version} already loaded, skipping ${y}`);return}let wt=window.balance?.q||[];function Nt(){if(wt.length>0){a("Processing",wt.length,"queued commands");for(let t of wt){let[e,...n]=t;switch(e){case"track":mt(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 $t={track:mt,identify:qt,page:$,purchase:Ft,getSessionId:()=>j,getVisitorId:()=>m,getFanIdHash:()=>b,getAttribution:()=>L,setConsent:Mt,getConsent:Pe,hasConsent:Ee,registerLinks:Te,_version:y};window.artistPixel=$t,window.balance=$t;function Ht(){Z&&!g&&new ge({style:Jt,primaryColor:Xt,position:Qt})}let Gt=window.requestIdleCallback||(t=>setTimeout(t,1));document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>{zt(),Nt(),Gt(()=>Ht())}):(zt(),Nt(),Gt(()=>Ht())),a("Pixel script loaded")})();})();
|