@kylelogue/eventfoundry-tracker 0.2.0 → 0.3.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/tracker.min.js +1 -22
- package/package.json +3 -2
package/dist/tracker.min.js
CHANGED
|
@@ -1,22 +1 @@
|
|
|
1
|
-
var EventFoundry=(()=>{var
|
|
2
|
-
.eventfoundry-highlight {
|
|
3
|
-
outline: 2px solid #3498db !important;
|
|
4
|
-
outline-offset: 2px;
|
|
5
|
-
cursor: pointer !important;
|
|
6
|
-
}
|
|
7
|
-
.eventfoundry-test-highlight {
|
|
8
|
-
outline: 3px solid #e74c3c !important;
|
|
9
|
-
outline-offset: 3px;
|
|
10
|
-
background-color: rgba(231, 76, 60, 0.1) !important;
|
|
11
|
-
}
|
|
12
|
-
.eventfoundry-overlay {
|
|
13
|
-
position: fixed;
|
|
14
|
-
top: 0;
|
|
15
|
-
left: 0;
|
|
16
|
-
right: 0;
|
|
17
|
-
bottom: 0;
|
|
18
|
-
z-index: 999998;
|
|
19
|
-
pointer-events: none;
|
|
20
|
-
cursor: crosshair;
|
|
21
|
-
}
|
|
22
|
-
`,document.head.appendChild(t);let n=document.createElement("div");n.className="eventfoundry-overlay",document.body.appendChild(n),document.body.addEventListener("mouseover",e=>{e.target.classList.contains("eventfoundry-overlay")||(f&&f.classList.remove("eventfoundry-highlight"),e.target.classList.add("eventfoundry-highlight"),f=e.target)},!0),document.body.addEventListener("mouseout",e=>{e.target.classList.contains("eventfoundry-overlay")||e.target.classList.remove("eventfoundry-highlight")},!0),document.body.addEventListener("click",e=>{if(e.target.classList.contains("eventfoundry-overlay"))return;e.preventDefault(),e.stopPropagation();let o=e.target,a="";o.className&&typeof o.className=="string"&&(a=o.className.trim().split(/\s+/).filter(r=>!r.startsWith("eventfoundry-")).join(" "));let i={tagName:o.tagName.toLowerCase(),text:o.textContent.trim().substring(0,50),id:o.id,className:a,selector:x(o)};window.parent.postMessage({type:"ELEMENT_SELECTED",element:i},"*"),console.log("\u2192 ELEMENT_SELECTED sent to parent")},!0),window.addEventListener("message",e=>{if(M(e.origin)&&(e.data.type==="HIGHLIGHT_SELECTOR"&&(console.log("\u2190 HIGHLIGHT_SELECTOR received:",e.data.selector),_(e.data.selector)),e.data.type==="COUNT_MATCHES")){let o=e.data.selector,a=0;try{a=document.querySelectorAll(o).length}catch(i){a=0}window.parent.postMessage({type:"MATCH_COUNT_RESULT",selector:o,count:a},"*")}})}function g(t){return t?t.replace(/([!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~])/g,"\\$1"):""}function F(t){return t?!!(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/i.test(t)||/\d{13}/.test(t)||/^[a-f0-9]{16,}$/i.test(t)||/-\d{4,}$/.test(t)):!1}function d(t,n){try{let e=document.querySelectorAll(n);return e.length===1&&e[0]===t}catch(e){return!1}}function x(t){let n=[];if(t.id&&!F(t.id)){let r=`#${g(t.id)}`;d(t,r)&&n.push({selector:r,priority:100})}let e=Array.from(t.attributes).filter(r=>r.name.startsWith("data-")&&r.value);for(let r of e){let s=`[${r.name}="${g(r.value)}"]`;if(d(t,s)){n.push({selector:s,priority:90});break}}if(t.className&&typeof t.className=="string"){let r=t.className.trim().split(/\s+/).filter(s=>!s.startsWith("eventfoundry-"));for(let s of r){let c=`.${g(s)}`;if(d(t,c)){n.push({selector:c,priority:80});break}}}let o=t.tagName.toLowerCase();t instanceof SVGElement&&(o=t.tagName.toLowerCase());let a=o,i=t.parentElement;if(i){let s=Array.from(i.children).indexOf(t)+1;a+=`:nth-child(${s})`}return d(t,a)&&n.push({selector:a,priority:50}),n.length>0?(n.sort((r,s)=>s.priority-r.priority),n[0].selector):o}function _(t){document.querySelectorAll(".eventfoundry-test-highlight").forEach(n=>{n.classList.remove("eventfoundry-test-highlight")});try{let n=document.querySelectorAll(t);n.forEach(e=>{e.classList.add("eventfoundry-test-highlight")}),setTimeout(()=>{n.forEach(e=>{e.classList.remove("eventfoundry-test-highlight")})},3e3)}catch(n){console.error("EventFoundry: Invalid selector",t,n)}}function A(t,n=!1){!t||t.length===0||document.addEventListener("click",e=>{if(w())return;let o=t.filter(r=>{try{return e.target.closest(r.selector)!==null}catch(s){return console.warn("EventFoundry: Invalid selector",r.selector,s.message),!1}});if(o.length===0)return;let a=o[0],i=E({event_category:"EventFoundry",event_label:a.name},a.gaEventParams);n?console.log("EventFoundry [DEV]: Tracked event (not sent to GA4)",a.gaEventName,i):(console.log("EventFoundry: Tracked event",a.gaEventName,i),window.gtag("event",a.gaEventName,i)),o.length>1&&console.warn("EventFoundry: Multiple events matched, only firing first:",o.map(r=>r.name))},!0)}function k(){return["http://localhost","https://eventfoundry.app"]}function O(t){return k().some(e=>e==="http://localhost"?t.startsWith("http://localhost"):t===e)}function h(){try{return window.self!==window.top}catch(t){return!0}}function C(){h()&&(window.addEventListener("message",t=>{if(!O(t.origin)){console.warn("EventFoundry: Rejected postMessage from untrusted origin:",t.origin);return}t.data.type==="ENTER_EDIT_MODE"&&(console.log("\u2190 ENTER_EDIT_MODE received"),G())}),setTimeout(()=>{window.parent&&(window.parent.postMessage({type:"IFRAME_READY",timestamp:Date.now()},"*"),console.log("\u2192 IFRAME_READY sent to parent"))},100))}function G(){L()}var S=window.location.hostname==="localhost"||window.location.hostname==="127.0.0.1",P=S?"http://localhost/api/tracking/events":"https://eventfoundry.app/api/tracking/events",p="eventfoundry_definitions",$=3600*1e3;function H(){let t=document.currentScript||document.querySelector("script[data-site-key]");if(!t)return console.error("EventFoundry: Could not find script tag"),null;let n=t.getAttribute("data-dev-mode"),e=n==="false"?!1:S||n==="true";return{siteKey:t.getAttribute("data-site-key"),measurementId:t.getAttribute("data-measurement-id"),devMode:e}}function j(t){return l(this,null,function*(){if(window.gtag&&typeof window.gtag=="function")return{hasGtag:!0,wasInjected:!1};if(!t)return console.warn("EventFoundry: No GA4 detected and no measurement ID provided. Events will not be tracked."),{hasGtag:!1,wasInjected:!1};let n=document.createElement("script");n.async=!0,n.src=`https://www.googletagmanager.com/gtag/js?id=${t}`,document.head.appendChild(n),window.dataLayer=window.dataLayer||[];function e(){dataLayer.push(arguments)}return window.gtag=e,e("js",new Date),e("config",t),yield new Promise(o=>{n.onload=o,setTimeout(o,5e3)}),{hasGtag:!0,wasInjected:!0}})}function N(){return l(this,null,function*(){let t=H();if(!t||!t.siteKey){console.error("EventFoundry: No site key provided");return}let{siteKey:n,measurementId:e,devMode:o}=t;console.log("\u2713 EventFoundry tracker loaded"),h()&&C();let{hasGtag:a,wasInjected:i}=yield j(e);if(!a){console.error("EventFoundry: Cannot track events without GA4");return}console.log(i?"\u2713 GA4 injected successfully":"\u2713 GA4 detected"),console.log(o?"\u26A0 Dev mode: Events disabled":"\u2713 Production mode: Events enabled");try{let r=yield R(n),s=r.events||r;A(s,o)}catch(r){console.error("EventFoundry: Failed to initialize:",r)}})}function R(t){return l(this,null,function*(){let n=q();if(n)return n;let e=yield fetch(`${P}?siteKey=${t}`);if(!e.ok)throw new Error(`API returned ${e.status}`);let o=yield e.json();return K(o),o})}function q(){try{let t=localStorage.getItem(p);if(!t)return null;let{data:n,timestamp:e}=JSON.parse(t);return Date.now()-e>$?(localStorage.removeItem(p),null):n}catch(t){return console.warn("EventFoundry: Cache read failed:",t),null}}function K(t){try{let n={data:t,timestamp:Date.now()};localStorage.setItem(p,JSON.stringify(n))}catch(n){console.warn("EventFoundry: Cache write failed:",n)}}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",N):N();})();
|
|
1
|
+
var EventFoundry=(()=>{var y=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames,b=Object.getOwnPropertySymbols;var A=Object.prototype.hasOwnProperty,$=Object.prototype.propertyIsEnumerable;var F=(e,n,t)=>n in e?y(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t,I=(e,n)=>{for(var t in n||(n={}))A.call(n,t)&&F(e,t,n[t]);if(b)for(var t of b(n))$.call(n,t)&&F(e,t,n[t]);return e};var j=(e,n)=>{for(var t in n)y(e,t,{get:n[t],enumerable:!0})},H=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of G(n))!A.call(e,r)&&r!==t&&y(e,r,{get:()=>n[r],enumerable:!(o=R(n,r))||o.enumerable});return e};var V=e=>H(y({},"__esModule",{value:!0}),e);var f=(e,n,t)=>new Promise((o,r)=>{var s=c=>{try{i(t.next(c))}catch(l){r(l)}},a=c=>{try{i(t.throw(c))}catch(l){r(l)}},i=c=>c.done?o(c.value):Promise.resolve(c.value).then(s,a);i((t=t.apply(e,n)).next())});var re={};j(re,{reloadEventDefinitions:()=>L});var d=null,g=!1,v=null,u=!1;function C(){return g}function h(){return u}function W(){return["http://localhost","https://eventfoundry.app"]}function K(e){return W().includes(e)}function M(){if(g)return;g=!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 n=document.createElement("div");n.className="eventfoundry-overlay",document.body.appendChild(n),v=n,document.body.classList.add("eventfoundry-edit-mode"),document.body.addEventListener("mouseover",t=>{u||t.target.classList.contains("eventfoundry-overlay")||(d&&d.classList.remove("eventfoundry-highlight"),t.target.classList.add("eventfoundry-highlight"),d=t.target)},!0),document.body.addEventListener("mouseout",t=>{u||t.target.classList.contains("eventfoundry-overlay")||t.target.classList.remove("eventfoundry-highlight")},!0),document.body.addEventListener("click",t=>{if(t.target.classList.contains("eventfoundry-overlay")||u)return;t.preventDefault(),t.stopPropagation();let o=t.target,r="";o.className&&typeof o.className=="string"&&(r=o.className.trim().split(/\s+/).filter(i=>!i.startsWith("eventfoundry-")).join(" "));let s=window.location.pathname+window.location.search+window.location.hash,a={tagName:o.tagName.toLowerCase(),text:o.textContent.trim().substring(0,50),id:o.id,className:r,selector:B(o),page_path:s};window.parent.postMessage({type:"ELEMENT_SELECTED",element:a},"*")},!0),window.addEventListener("message",t=>{if(K(t.origin)&&(t.data.type==="HIGHLIGHT_SELECTOR"&&U(t.data.selector),t.data.type==="COUNT_MATCHES")){let o=t.data.selector,r=0;try{r=document.querySelectorAll(o).length}catch(s){r=0}window.parent.postMessage({type:"MATCH_COUNT_RESULT",selector:o,count:r},"*")}})}function m(e){return e?e.replace(/([!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~])/g,"\\$1"):""}function q(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 p(e,n){try{let t=document.querySelectorAll(n);return t.length===1&&t[0]===e}catch(t){return!1}}function B(e){let n=[];if(e.id&&!q(e.id)){let a=`#${m(e.id)}`;p(e,a)&&n.push({selector:a,priority:100})}let t=Array.from(e.attributes).filter(a=>a.name.startsWith("data-")&&a.value);for(let a of t){let i=`[${a.name}="${m(a.value)}"]`;if(p(e,i)){n.push({selector:i,priority:90});break}}if(e.className&&typeof e.className=="string"){let a=e.className.trim().split(/\s+/).filter(i=>!i.startsWith("eventfoundry-"));for(let i of a){let c=`.${m(i)}`;if(p(e,c)){n.push({selector:c,priority:80});break}}}let o=e.tagName.toLowerCase();e instanceof SVGElement&&(o=e.tagName.toLowerCase());let r=o,s=e.parentElement;if(s){let a;if(s.className&&typeof s.className=="string"){let l=s.className.trim().split(/\s+/).filter(x=>!x.startsWith("eventfoundry-"));a=l.length>0?`.${m(l[0])}`:s.tagName.toLowerCase()}else a=s.tagName.toLowerCase();let c=Array.from(s.children).indexOf(e)+1;r=`${a} ${o}:nth-child(${c})`}return p(e,r)&&n.push({selector:r,priority:50}),n.length>0?(n.sort((a,i)=>i.priority-a.priority),n[0].selector):o}function U(e){document.querySelectorAll(".eventfoundry-test-highlight").forEach(n=>{n.classList.remove("eventfoundry-test-highlight")});try{let n=document.querySelectorAll(e);n.forEach(t=>{t.classList.add("eventfoundry-test-highlight")}),setTimeout(()=>{n.forEach(t=>{t.classList.remove("eventfoundry-test-highlight")})},3e3)}catch(n){}}function P(){g&&(u=!0,v&&(v.style.display="none"),document.body.classList.remove("eventfoundry-edit-mode"),d&&(d.classList.remove("eventfoundry-highlight"),d=null))}function D(){g&&(u=!1,v&&(v.style.display=""),document.body.classList.add("eventfoundry-edit-mode"))}function E(e,n=!1){!e||e.length===0||document.addEventListener("click",t=>{if(C()&&!h())return;let o=e.filter(a=>{try{return t.target.closest(a.selector)!==null}catch(i){return!1}});if(o.length===0)return;let r=o[0],s=I({event_category:"EventFoundry",event_label:r.name,transport_type:"beacon"},r.gaEventParams);if(n||window.gtag("event",r.gaEventName,s),h())try{window.parent.postMessage({type:"EVENT_FIRED",timestamp:Date.now(),eventName:r.name,gaEventName:r.gaEventName,selector:r.selector,params:s,pagePath:window.location.pathname},"*")}catch(a){}o.length>1},!0)}function z(){return["http://localhost","https://eventfoundry.app"]}function J(e){return z().some(t=>t==="http://localhost"?e.startsWith("http://localhost"):e===t)}function w(){try{return window.self!==window.top}catch(e){return!0}}function T(){w()&&(window.addEventListener("message",e=>{if(J(e.origin))switch(e.data.type){case"ENTER_EDIT_MODE":Y();break;case"ENABLE_PREVIEW":Q();break;case"DISABLE_PREVIEW":X();break;case"RELOAD_CONFIG":L();break}}),setTimeout(()=>{window.parent&&window.parent.postMessage({type:"IFRAME_READY"},"*")},100))}function Y(){M()}function Q(){P()}function X(){D()}var _=window.location.hostname==="localhost"||window.location.hostname==="127.0.0.1",Z=_?"http://localhost/api/tracking/events":"https://eventfoundry.app/api/tracking/events",ee=3600*1e3;function N(e){return`eventfoundry_definitions_${e}`}function k(){let e=document.currentScript||document.querySelector("script[data-site-key]");if(!e)return null;let n=e.getAttribute("data-dev-mode"),t=n==="false"?!1:_||n==="true";return{siteKey:e.getAttribute("data-site-key"),measurementId:e.getAttribute("data-measurement-id"),devMode:t}}function te(e){return f(this,null,function*(){if(window.gtag&&typeof window.gtag=="function")return{hasGtag:!0,wasInjected:!1};if(!e)return{hasGtag:!1,wasInjected:!1};let n=document.createElement("script");n.async=!0,n.src=`https://www.googletagmanager.com/gtag/js?id=${e}`,document.head.appendChild(n),window.dataLayer=window.dataLayer||[];function t(){dataLayer.push(arguments)}return window.gtag=t,t("js",new Date),t("config",e),yield new Promise(o=>{n.onload=o,setTimeout(o,5e3)}),{hasGtag:!0,wasInjected:!0}})}function S(){return f(this,null,function*(){let e=k();if(!e||!e.siteKey)return;let{siteKey:n,measurementId:t,devMode:o}=e;w()&&T();let{hasGtag:r,wasInjected:s}=yield te(t);if(r)try{let i=(yield O(n)).events;E(i,o)}catch(a){}})}function O(e){return f(this,null,function*(){let n=ne(e);if(n)return n;let t=yield fetch(`${Z}?siteKey=${e}`);if(!t.ok)throw new Error(`API returned ${t.status}`);let o=yield t.json();return oe(e,o),o})}function ne(e){try{let n=N(e),t=localStorage.getItem(n);if(!t)return null;let{data:o,timestamp:r}=JSON.parse(t);return Date.now()-r>ee?(localStorage.removeItem(n),null):o}catch(n){return null}}function oe(e,n){try{let t=N(e),o={data:n,timestamp:Date.now()};localStorage.setItem(t,JSON.stringify(o))}catch(t){}}function L(){return f(this,null,function*(){let e=k();if(!e||!e.siteKey)return;let{siteKey:n,devMode:t}=e;try{let o=N(n);localStorage.removeItem(o)}catch(o){}try{let r=(yield O(n)).events;E(r,t)}catch(o){}})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",S):S();return V(re);})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kylelogue/eventfoundry-tracker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "EventFoundry tracking script for custom GA4 event tracking",
|
|
5
5
|
"main": "dist/tracker.min.js",
|
|
6
6
|
"files": [
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"author": "Kyle Logue",
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"esbuild": "^0.27.0"
|
|
33
|
+
"esbuild": "^0.27.0",
|
|
34
|
+
"esbuild-minify-templates": "^0.13.1"
|
|
34
35
|
}
|
|
35
36
|
}
|