@kylelogue/eventfoundry-tracker 0.3.5 → 0.3.7

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.
@@ -1 +1 @@
1
- var EventFoundry=(()=>{var h=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames,A=Object.getOwnPropertySymbols;var _=Object.prototype.hasOwnProperty,B=Object.prototype.propertyIsEnumerable;var I=(e,t,n)=>t in e?h(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,T=(e,t)=>{for(var n in t||(t={}))_.call(t,n)&&I(e,n,t[n]);if(A)for(var n of A(t))B.call(t,n)&&I(e,n,t[n]);return e};var K=(e,t)=>{for(var n in t)h(e,n,{get:t[n],enumerable:!0})},U=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of j(t))!_.call(e,r)&&r!==n&&h(e,r,{get:()=>t[r],enumerable:!(o=W(t,r))||o.enumerable});return e};var q=e=>U(h({},"__esModule",{value:!0}),e);var f=(e,t,n)=>new Promise((o,r)=>{var a=d=>{try{c(n.next(d))}catch(w){r(w)}},s=d=>{try{c(n.throw(d))}catch(w){r(w)}},c=d=>d.done?o(d.value):Promise.resolve(d.value).then(a,s);c((n=n.apply(e,t)).next())});var ye={};K(ye,{reloadEventDefinitions:()=>L});var m=null;function p(){return m}function z(){return["http://localhost","https://eventfoundry.app"]}function J(e){return z().some(n=>n==="http://localhost"?e.startsWith("http://localhost"):e===n)}function N(){try{return window.self!==window.top}catch(e){return!0}}function D(){N()&&(console.log("EventFoundry: Running in iframe, waiting for editor activation"),window.addEventListener("message",e=>{var o,r;console.log("[DEBUG] Received postMessage:",{type:(o=e.data)==null?void 0:o.type,origin:e.origin,data:e.data});let t=(r=e.data)==null?void 0:r.type;if(!t||!["ENTER_EDIT_MODE","ENABLE_PREVIEW","DISABLE_PREVIEW","RELOAD_CONFIG"].includes(t)){console.log("[DEBUG] Ignoring non-EventFoundry message:",t);return}if(!J(e.origin))if(e.origin===null)console.warn("EventFoundry: Received message with null origin (possibly intercepted by browser extension or site script):",t),console.warn("EventFoundry: Allowing message due to null origin interception issue. Message type:",t);else{console.warn("EventFoundry: Rejected postMessage from untrusted origin:",e.origin);return}switch(!m&&e.origin!==null&&(m=e.origin,console.log("EventFoundry: Parent origin established:",m)),e.data.type){case"ENTER_EDIT_MODE":console.log("\u2190 ENTER_EDIT_MODE received"),Y();break;case"ENABLE_PREVIEW":console.log("\u2190 ENABLE_PREVIEW received"),Q();break;case"DISABLE_PREVIEW":console.log("\u2190 DISABLE_PREVIEW received"),X();break;case"RELOAD_CONFIG":console.log("\u2190 RELOAD_CONFIG received"),L();break}}),setTimeout(()=>{window.parent&&(window.parent.postMessage({type:"IFRAME_READY"},"*"),console.log("\u2192 IFRAME_READY sent to parent"))},100))}function Y(){P()}function Q(){console.log("EventFoundry: Enabling Preview Mode"),M()}function X(){console.log("EventFoundry: Disabling Preview Mode (Edit Mode active)"),O()}var u=null,E=!1,y=null,g=!1;function $(){return E}function S(){return g}function Z(){return["http://localhost","https://eventfoundry.app"]}function ee(e){return Z().some(n=>n==="http://localhost"?e.startsWith("http://localhost"):e===n)}function P(){if(E)return;E=!0;let e=document.createElement("style");e.textContent=` .eventfoundry-highlight { outline: 2px solid #3498db !important; outline-offset: 2px; cursor: pointer !important; } .eventfoundry-test-highlight { outline: 3px solid #e74c3c !important; outline-offset: 3px; background-color: rgba(231, 76, 60, 0.1) !important; } .eventfoundry-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 999998; pointer-events: none; cursor: crosshair; } body.eventfoundry-edit-mode, body.eventfoundry-edit-mode * { cursor: crosshair !important; } `,document.head.appendChild(e);let t=document.createElement("div");t.className="eventfoundry-overlay",document.body.appendChild(t),y=t,document.body.classList.add("eventfoundry-edit-mode"),document.body.addEventListener("mouseover",n=>{g||n.target.classList.contains("eventfoundry-overlay")||(u&&u.classList.remove("eventfoundry-highlight"),n.target.classList.add("eventfoundry-highlight"),u=n.target)},!0),document.body.addEventListener("mouseout",n=>{g||n.target.classList.contains("eventfoundry-overlay")||n.target.classList.remove("eventfoundry-highlight")},!0),document.body.addEventListener("click",n=>{if(n.target.classList.contains("eventfoundry-overlay"))return;if(g){console.log("EventFoundry: Preview Mode - allowing click to pass through");return}n.preventDefault(),n.stopPropagation();let o=n.target,r="";o.className&&typeof o.className=="string"&&(r=o.className.trim().split(/\s+/).filter(d=>!d.startsWith("eventfoundry-")).join(" "));let a=window.location.pathname+window.location.search+window.location.hash,s={tagName:o.tagName.toLowerCase(),text:o.textContent.trim().substring(0,50),id:o.id,className:r,selector:le(o),page_path:a},c=p();c?(window.parent.postMessage({type:"ELEMENT_SELECTED",element:s},c),console.log("\u2192 ELEMENT_SELECTED sent to parent")):(console.warn("EventFoundry: Parent origin not established, using wildcard target"),window.parent.postMessage({type:"ELEMENT_SELECTED",element:s},"*"),console.log("\u2192 ELEMENT_SELECTED sent to parent (wildcard)"))},!0),window.addEventListener("message",n=>{if(ee(n.origin)&&(n.data.type==="HIGHLIGHT_SELECTOR"&&(console.log("\u2190 HIGHLIGHT_SELECTOR received:",n.data.selector),de(n.data.selector)),n.data.type==="COUNT_MATCHES")){let o=n.data.selector,r=0;try{r=document.querySelectorAll(o).length}catch(s){r=0}let a=p();a?window.parent.postMessage({type:"MATCH_COUNT_RESULT",selector:o,count:r},a):window.parent.postMessage({type:"MATCH_COUNT_RESULT",selector:o,count:r},"*")}})}function i(e){return e?e.replace(/([!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~])/g,"\\$1"):""}function te(e){return e?!!(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/i.test(e)||/\d{13}/.test(e)||/^[a-f0-9]{16,}$/i.test(e)||/-\d{4,}$/.test(e)):!1}function ne(e,t){try{let n=document.querySelectorAll(t);return n.length===1&&n[0]===e}catch(n){return!1}}function b(e){return!e.className||typeof e.className!="string"?[]:e.className.trim().split(/\s+/).filter(t=>t&&!t.startsWith("eventfoundry-"))}function R(e,t=!0){let n=e.parentElement;if(!n)return null;let o=b(n);return t&&o.length>0?`.${i(o[0])}`:n.tagName.toLowerCase()}function l(e,t,n,o){return ne(o,t)?(e.push({selector:t,priority:n}),!0):!1}function oe(e){let t=[];if(e.id&&!te(e.id)){let n=`#${i(e.id)}`;l(t,n,100,e)}return t}function re(e){let t=[],n=Array.from(e.attributes).filter(o=>o.name.startsWith("data-")&&o.value);for(let o of n){let r=`[${o.name}="${i(o.value)}"]`;if(l(t,r,90,e))break}return t}function ae(e){let t=[],n=b(e);if(n.length===0)return t;let o=e.tagName.toLowerCase();for(let r of n){let a=`${o}.${i(r)}`;if(l(t,a,85,e))break}if(n.length>=2){let r=`.${i(n[0])}.${i(n[1])}`;if(l(t,r,82,e),n.length>=3){let a=`.${i(n[0])}.${i(n[1])}.${i(n[2])}`;l(t,a,82,e)}}for(let r of n){let a=`.${i(r)}`;if(l(t,a,80,e))break}return t}function se(e){let t=[],n=e.tagName.toLowerCase(),o=["type","name","href","role","aria-label"];for(let r of o){let a=e.getAttribute(r);if(a){let s=`${n}[${r}="${i(a)}"]`;if(l(t,s,70,e))break}}return t}function ie(e){let t=[],n=R(e,!0);if(!n)return t;let o=e.tagName.toLowerCase(),r=b(e);if(r.length>0){let s=`${n} .${i(r[0])}`;l(t,s,75,e)}if(r.length>0){let s=`${n} ${o}.${i(r[0])}`;l(t,s,72,e)}let a=`${n} ${o}`;return l(t,a,65,e),t}function ce(e){let t=[],n=e.tagName.toLowerCase(),o=e.parentElement;if(!o)return[{selector:n,priority:10}];let r=R(e,!0),s=Array.from(o.children).indexOf(e)+1,c=`${r} ${n}:nth-child(${s})`;return l(t,c,50,e),t}function le(e){let t=[];return t.push(...oe(e)),t.push(...re(e)),t.push(...ae(e)),t.push(...se(e)),t.push(...ie(e)),t.push(...ce(e)),t.length>0?(t.sort((n,o)=>o.priority-n.priority),t[0].selector):e.tagName.toLowerCase()}function de(e){document.querySelectorAll(".eventfoundry-test-highlight").forEach(t=>{t.classList.remove("eventfoundry-test-highlight")});try{let t=document.querySelectorAll(e);t.forEach(n=>{n.classList.add("eventfoundry-test-highlight")}),setTimeout(()=>{t.forEach(n=>{n.classList.remove("eventfoundry-test-highlight")})},3e3)}catch(t){console.error("EventFoundry: Invalid selector",e,t)}}function M(){if(!E){console.warn("EventFoundry: Cannot enable preview - overlay not active");return}g=!0,console.log("EventFoundry: Preview Mode enabled - all interactions allowed"),y&&(y.style.display="none"),document.body.classList.remove("eventfoundry-edit-mode"),u&&(u.classList.remove("eventfoundry-highlight"),u=null)}function O(){if(!E){console.warn("EventFoundry: Cannot disable preview - overlay not active");return}g=!1,console.log("EventFoundry: Preview Mode disabled - Edit Mode active"),y&&(y.style.display=""),document.body.classList.add("eventfoundry-edit-mode")}var x=console.log;function v(e,...t){e&&x&&x(...t)}function C(e,t=!1){!e||e.length===0||document.addEventListener("click",n=>{if($()&&!S())return;let o=e.filter(s=>{try{return n.target.closest(s.selector)!==null}catch(c){return console.warn("EventFoundry: Invalid selector",s.selector,c.message),!1}});if(o.length===0)return;let r=o[0],a=T({event_category:"EventFoundry",event_label:r.name,transport_type:"beacon"},r.gaEventParams);if(v(t,"EventFoundry [DEV]: Tracked event (not sent to GA4)",r.gaEventName,a),t||window.gtag("event",r.gaEventName,a),S())try{let s=p();s?(window.parent.postMessage({type:"EVENT_FIRED",timestamp:Date.now(),eventName:r.name,gaEventName:r.gaEventName,selector:r.selector,params:a,pagePath:window.location.pathname},s),console.log("\u2192 EVENT_FIRED sent to parent")):(window.parent.postMessage({type:"EVENT_FIRED",timestamp:Date.now(),eventName:r.name,gaEventName:r.gaEventName,selector:r.selector,params:a,pagePath:window.location.pathname},"*"),console.log("\u2192 EVENT_FIRED sent to parent (wildcard)"))}catch(s){console.warn("EventFoundry: Failed to send EVENT_FIRED postMessage",s)}o.length>1&&console.warn("EventFoundry: Multiple events matched, only firing first:",o.map(s=>s.name))},!0)}var G=window.location.hostname==="localhost"||window.location.hostname==="127.0.0.1",ue=G?"http://localhost/api/tracking/events":"https://eventfoundry.app/api/tracking/events",ge=3600*1e3;function F(e){return`eventfoundry_definitions_${e}`}function V(){let e=document.currentScript||document.querySelector("script[data-site-key]");if(!e)return console.error("EventFoundry: Could not find script tag"),null;let t=e.getAttribute("data-dev-mode"),n=t==="false"?!1:G||t==="true";return{siteKey:e.getAttribute("data-site-key"),measurementId:e.getAttribute("data-measurement-id"),devMode:n}}function fe(e){return f(this,null,function*(){if(window.gtag&&typeof window.gtag=="function")return{hasGtag:!0,wasInjected:!1};if(!e)return console.warn("EventFoundry: No GA4 detected and no measurement ID provided. Events will not be tracked."),{hasGtag:!1,wasInjected:!1};let t=document.createElement("script");t.async=!0,t.src=`https://www.googletagmanager.com/gtag/js?id=${e}`,document.head.appendChild(t),window.dataLayer=window.dataLayer||[];function n(){dataLayer.push(arguments)}return window.gtag=n,n("js",new Date),n("config",e),yield new Promise(o=>{t.onload=o,setTimeout(o,5e3)}),{hasGtag:!0,wasInjected:!0}})}function k(){return f(this,null,function*(){let e=V();if(!e||!e.siteKey){console.error("EventFoundry: No site key provided");return}let{siteKey:t,measurementId:n,devMode:o}=e;console.log("\u2713 EventFoundry tracker loaded"),N()&&D();let{hasGtag:r,wasInjected:a}=yield fe(n);if(!r){console.error("EventFoundry: Cannot track events without GA4");return}console.log(a?"\u2713 GA4 injected successfully":"\u2713 GA4 detected"),v(o,"\u26A0 Dev mode: Events disabled (v0.3.5)"),v(!o,"\u2713 Production mode: Events enabled (v0.3.5)");try{let c=(yield H(t)).events;C(c,o)}catch(s){console.error("EventFoundry: Failed to initialize:",s)}})}function H(e){return f(this,null,function*(){let t=pe(e);if(t)return t;let n=yield fetch(`${ue}?siteKey=${e}`);if(!n.ok)throw new Error(`API returned ${n.status}`);let o=yield n.json();return Ee(e,o),o})}function pe(e){try{let t=F(e),n=localStorage.getItem(t);if(!n)return null;let{data:o,timestamp:r}=JSON.parse(n);return Date.now()-r>ge?(localStorage.removeItem(t),null):o}catch(t){return console.warn("EventFoundry: Cache read failed:",t),null}}function Ee(e,t){try{let n=F(e),o={data:t,timestamp:Date.now()};localStorage.setItem(n,JSON.stringify(o))}catch(n){console.warn("EventFoundry: Cache write failed:",n)}}function L(){return f(this,null,function*(){let e=V();if(!e||!e.siteKey){console.error("EventFoundry: No site key available for reload");return}let{siteKey:t,devMode:n}=e;try{let o=F(t);localStorage.removeItem(o),console.log("EventFoundry: Cache cleared, reloading definitions")}catch(o){console.warn("EventFoundry: Failed to clear cache",o)}try{let r=(yield H(t)).events;C(r,n),console.log("EventFoundry: Definitions reloaded successfully")}catch(o){console.error("EventFoundry: Failed to reload definitions",o)}})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",k):k();return q(ye);})();
1
+ var EventFoundry=(()=>{var h=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames,F=Object.getOwnPropertySymbols;var _=Object.prototype.hasOwnProperty,B=Object.prototype.propertyIsEnumerable;var I=(e,t,n)=>t in e?h(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,P=(e,t)=>{for(var n in t||(t={}))_.call(t,n)&&I(e,n,t[n]);if(F)for(var n of F(t))B.call(t,n)&&I(e,n,t[n]);return e};var q=(e,t)=>{for(var n in t)h(e,n,{get:t[n],enumerable:!0})},U=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of K(t))!_.call(e,r)&&r!==n&&h(e,r,{get:()=>t[r],enumerable:!(o=H(t,r))||o.enumerable});return e};var z=e=>U(h({},"__esModule",{value:!0}),e);var g=(e,t,n)=>new Promise((o,r)=>{var a=d=>{try{c(n.next(d))}catch(w){r(w)}},s=d=>{try{c(n.throw(d))}catch(w){r(w)}},c=d=>d.done?o(d.value):Promise.resolve(d.value).then(a,s);c((n=n.apply(e,t)).next())});var Ee={};q(Ee,{reloadEventDefinitions:()=>N});var m=null;function v(){return m}function J(){return["http://localhost","https://eventfoundry.app"]}function T(e){return J().some(n=>n==="http://localhost"?e.startsWith("http://localhost"):e===n)}function L(){try{return window.self!==window.top}catch(e){return!0}}function D(){L()&&(console.log("EventFoundry: Running in iframe, waiting for editor activation"),window.addEventListener("message",e=>{let t=e.data;Array.isArray(t)&&t.length>0&&(t=t[0]);let n=t==null?void 0:t.type;if(!(!n||!["ENTER_EDIT_MODE","ENABLE_PREVIEW","DISABLE_PREVIEW","RELOAD_CONFIG"].includes(n))){if(console.log("[DEBUG] EventFoundry message received:",{type:n,origin:e.origin,isAllowed:T(e.origin)}),!T(e.origin)){console.warn("EventFoundry: Rejected postMessage from untrusted origin:",e.origin);return}switch(m||(m=e.origin,console.log("EventFoundry: Parent origin established:",m)),t.type){case"ENTER_EDIT_MODE":console.log("\u2190 ENTER_EDIT_MODE received"),Y();break;case"ENABLE_PREVIEW":console.log("\u2190 ENABLE_PREVIEW received"),Q();break;case"DISABLE_PREVIEW":console.log("\u2190 DISABLE_PREVIEW received"),X();break;case"RELOAD_CONFIG":console.log("\u2190 RELOAD_CONFIG received"),N();break}}}),setTimeout(()=>{window.parent&&(window.parent.postMessage({type:"IFRAME_READY"},"*"),console.log("\u2192 IFRAME_READY sent to parent"))},100))}function Y(){$()}function Q(){console.log("EventFoundry: Enabling Preview Mode"),M()}function X(){console.log("EventFoundry: Disabling Preview Mode (Edit Mode active)"),O()}var u=null,y=!1,E=null,f=!1;function R(){return y}function S(){return f}function Z(){return["http://localhost","https://eventfoundry.app"]}function ee(e){return Z().some(n=>n==="http://localhost"?e.startsWith("http://localhost"):e===n)}function $(){if(y)return;y=!0;let e=document.createElement("style");e.textContent=` .eventfoundry-highlight { outline: 2px solid #3498db !important; outline-offset: 2px; cursor: pointer !important; } .eventfoundry-test-highlight { outline: 3px solid #e74c3c !important; outline-offset: 3px; background-color: rgba(231, 76, 60, 0.1) !important; } .eventfoundry-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 999998; pointer-events: none; cursor: crosshair; } body.eventfoundry-edit-mode, body.eventfoundry-edit-mode * { cursor: crosshair !important; } `,document.head.appendChild(e);let t=document.createElement("div");t.className="eventfoundry-overlay",document.body.appendChild(t),E=t,document.body.classList.add("eventfoundry-edit-mode"),document.body.addEventListener("mouseover",n=>{f||n.target.classList.contains("eventfoundry-overlay")||(u&&u.classList.remove("eventfoundry-highlight"),n.target.classList.add("eventfoundry-highlight"),u=n.target)},!0),document.body.addEventListener("mouseout",n=>{f||n.target.classList.contains("eventfoundry-overlay")||n.target.classList.remove("eventfoundry-highlight")},!0),document.body.addEventListener("click",n=>{if(n.target.classList.contains("eventfoundry-overlay"))return;if(f){console.log("EventFoundry: Preview Mode - allowing click to pass through");return}n.preventDefault(),n.stopPropagation();let o=n.target,r="";o.className&&typeof o.className=="string"&&(r=o.className.trim().split(/\s+/).filter(d=>!d.startsWith("eventfoundry-")).join(" "));let a=window.location.pathname+window.location.search+window.location.hash,s={tagName:o.tagName.toLowerCase(),text:o.textContent.trim().substring(0,50),id:o.id,className:r,selector:le(o),page_path:a},c=v();c?(window.parent.postMessage({type:"ELEMENT_SELECTED",element:s},c),console.log("\u2192 ELEMENT_SELECTED sent to parent")):console.warn("EventFoundry: Cannot send ELEMENT_SELECTED - parent origin not established")},!0),window.addEventListener("message",n=>{if(ee(n.origin)&&(n.data.type==="HIGHLIGHT_SELECTOR"&&(console.log("\u2190 HIGHLIGHT_SELECTOR received:",n.data.selector),de(n.data.selector)),n.data.type==="COUNT_MATCHES")){let o=n.data.selector,r=0;try{r=document.querySelectorAll(o).length}catch(s){r=0}let a=v();a&&window.parent.postMessage({type:"MATCH_COUNT_RESULT",selector:o,count:r},a)}})}function i(e){return e?e.replace(/([!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~])/g,"\\$1"):""}function te(e){return e?!!(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/i.test(e)||/\d{13}/.test(e)||/^[a-f0-9]{16,}$/i.test(e)||/-\d{4,}$/.test(e)):!1}function ne(e,t){try{let n=document.querySelectorAll(t);return n.length===1&&n[0]===e}catch(n){return!1}}function C(e){return!e.className||typeof e.className!="string"?[]:e.className.trim().split(/\s+/).filter(t=>t&&!t.startsWith("eventfoundry-"))}function x(e,t=!0){let n=e.parentElement;if(!n)return null;let o=C(n);return t&&o.length>0?`.${i(o[0])}`:n.tagName.toLowerCase()}function l(e,t,n,o){return ne(o,t)?(e.push({selector:t,priority:n}),!0):!1}function oe(e){let t=[];if(e.id&&!te(e.id)){let n=`#${i(e.id)}`;l(t,n,100,e)}return t}function re(e){let t=[],n=Array.from(e.attributes).filter(o=>o.name.startsWith("data-")&&o.value);for(let o of n){let r=`[${o.name}="${i(o.value)}"]`;if(l(t,r,90,e))break}return t}function ae(e){let t=[],n=C(e);if(n.length===0)return t;let o=e.tagName.toLowerCase();for(let r of n){let a=`${o}.${i(r)}`;if(l(t,a,85,e))break}if(n.length>=2){let r=`.${i(n[0])}.${i(n[1])}`;if(l(t,r,82,e),n.length>=3){let a=`.${i(n[0])}.${i(n[1])}.${i(n[2])}`;l(t,a,82,e)}}for(let r of n){let a=`.${i(r)}`;if(l(t,a,80,e))break}return t}function se(e){let t=[],n=e.tagName.toLowerCase(),o=["type","name","href","role","aria-label"];for(let r of o){let a=e.getAttribute(r);if(a){let s=`${n}[${r}="${i(a)}"]`;if(l(t,s,70,e))break}}return t}function ie(e){let t=[],n=x(e,!0);if(!n)return t;let o=e.tagName.toLowerCase(),r=C(e);if(r.length>0){let s=`${n} .${i(r[0])}`;l(t,s,75,e)}if(r.length>0){let s=`${n} ${o}.${i(r[0])}`;l(t,s,72,e)}let a=`${n} ${o}`;return l(t,a,65,e),t}function ce(e){let t=[],n=e.tagName.toLowerCase(),o=e.parentElement;if(!o)return[{selector:n,priority:10}];let r=x(e,!0),s=Array.from(o.children).indexOf(e)+1,c=`${r} ${n}:nth-child(${s})`;return l(t,c,50,e),t}function le(e){let t=[];return t.push(...oe(e)),t.push(...re(e)),t.push(...ae(e)),t.push(...se(e)),t.push(...ie(e)),t.push(...ce(e)),t.length>0?(t.sort((n,o)=>o.priority-n.priority),t[0].selector):e.tagName.toLowerCase()}function de(e){document.querySelectorAll(".eventfoundry-test-highlight").forEach(t=>{t.classList.remove("eventfoundry-test-highlight")});try{let t=document.querySelectorAll(e);t.forEach(n=>{n.classList.add("eventfoundry-test-highlight")}),setTimeout(()=>{t.forEach(n=>{n.classList.remove("eventfoundry-test-highlight")})},3e3)}catch(t){console.error("EventFoundry: Invalid selector",e,t)}}function M(){if(!y){console.warn("EventFoundry: Cannot enable preview - overlay not active");return}f=!0,console.log("EventFoundry: Preview Mode enabled - all interactions allowed"),E&&(E.style.display="none"),document.body.classList.remove("eventfoundry-edit-mode"),u&&(u.classList.remove("eventfoundry-highlight"),u=null)}function O(){if(!y){console.warn("EventFoundry: Cannot disable preview - overlay not active");return}f=!1,console.log("EventFoundry: Preview Mode disabled - Edit Mode active"),E&&(E.style.display=""),document.body.classList.add("eventfoundry-edit-mode")}var k=console.log;function p(e,...t){e&&k&&k(...t)}function b(e,t=!1){!e||e.length===0||document.addEventListener("click",n=>{if(R()&&!S())return;let o=e.filter(s=>{try{return n.target.closest(s.selector)!==null}catch(c){return console.warn("EventFoundry: Invalid selector",s.selector,c.message),!1}});if(o.length===0)return;let r=o[0],a=P({event_category:"EventFoundry",event_label:r.name,transport_type:"beacon"},r.gaEventParams);if(p(t,"EventFoundry [DEV]: Tracked event (not sent to GA4)",r.gaEventName,a),t||window.gtag("event",r.gaEventName,a),S())try{let s=v();s?(window.parent.postMessage({type:"EVENT_FIRED",timestamp:Date.now(),eventName:r.name,gaEventName:r.gaEventName,selector:r.selector,params:a,pagePath:window.location.pathname},s),console.log("\u2192 EVENT_FIRED sent to parent")):console.warn("EventFoundry: Cannot send EVENT_FIRED - parent origin not established")}catch(s){console.warn("EventFoundry: Failed to send EVENT_FIRED postMessage",s)}o.length>1&&console.warn("EventFoundry: Multiple events matched, only firing first:",o.map(s=>s.name))},!0)}var V=window.location.hostname==="localhost"||window.location.hostname==="127.0.0.1",ue=V?"http://localhost/api/tracking/events":"https://eventfoundry.app/api/tracking/events",fe=3600*1e3;function A(e){return`eventfoundry_definitions_${e}`}function W(){let e=document.currentScript||document.querySelector("script[data-site-key]");if(!e)return console.error("EventFoundry: Could not find script tag"),null;let t=e.getAttribute("data-dev-mode"),n=t==="false"?!1:V||t==="true";return{siteKey:e.getAttribute("data-site-key"),measurementId:e.getAttribute("data-measurement-id"),devMode:n}}function ge(e){return g(this,null,function*(){if(window.gtag&&typeof window.gtag=="function")return{hasGtag:!0,wasInjected:!1};if(!e)return console.warn("EventFoundry: No GA4 detected and no measurement ID provided. Events will not be tracked."),{hasGtag:!1,wasInjected:!1};let t=document.createElement("script");t.async=!0,t.src=`https://www.googletagmanager.com/gtag/js?id=${e}`,document.head.appendChild(t),window.dataLayer=window.dataLayer||[];function n(){dataLayer.push(arguments)}return window.gtag=n,n("js",new Date),n("config",e),yield new Promise(o=>{t.onload=o,setTimeout(o,5e3)}),{hasGtag:!0,wasInjected:!0}})}function G(){return g(this,null,function*(){let e=W();if(!e||!e.siteKey){console.error("EventFoundry: No site key provided");return}let{siteKey:t,measurementId:n,devMode:o}=e;console.log("\u2713 EventFoundry tracker loaded"),L()&&D();let{hasGtag:r,wasInjected:a}=yield ge(n);if(!r){console.error("EventFoundry: Cannot track events without GA4");return}console.log(a?"\u2713 GA4 injected successfully":"\u2713 GA4 detected"),p(o,"\u26A0 Dev mode: Events disabled (v0.3.7)"),p(!o,"\u2713 Production mode: Events enabled (v0.3.7)");try{let c=(yield j(t)).events;b(c,o)}catch(s){console.error("EventFoundry: Failed to initialize:",s)}})}function j(e){return g(this,null,function*(){let t=ve(e);if(t)return t;let n=yield fetch(`${ue}?siteKey=${e}`);if(!n.ok)throw new Error(`API returned ${n.status}`);let o=yield n.json();return ye(e,o),o})}function ve(e){try{let t=A(e),n=localStorage.getItem(t);if(!n)return null;let{data:o,timestamp:r}=JSON.parse(n);return Date.now()-r>fe?(localStorage.removeItem(t),null):o}catch(t){return console.warn("EventFoundry: Cache read failed:",t),null}}function ye(e,t){try{let n=A(e),o={data:t,timestamp:Date.now()};localStorage.setItem(n,JSON.stringify(o))}catch(n){console.warn("EventFoundry: Cache write failed:",n)}}function N(){return g(this,null,function*(){let e=W();if(!e||!e.siteKey){console.error("EventFoundry: No site key available for reload");return}let{siteKey:t,devMode:n}=e;try{let o=A(t);localStorage.removeItem(o),console.log("EventFoundry: Cache cleared, reloading definitions")}catch(o){console.warn("EventFoundry: Failed to clear cache",o)}try{let r=(yield j(t)).events;b(r,n),console.log("EventFoundry: Definitions reloaded successfully")}catch(o){console.error("EventFoundry: Failed to reload definitions",o)}})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",G):G();return z(Ee);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kylelogue/eventfoundry-tracker",
3
- "version": "0.3.5",
3
+ "version": "0.3.7",
4
4
  "description": "EventFoundry tracking script for custom GA4 event tracking",
5
5
  "main": "dist/tracker.min.js",
6
6
  "files": [