UNPKG

@caesar1030/react-analytics

Version:
148 lines (147 loc) 4.05 kB
var c = Object.defineProperty; var d = Object.getOwnPropertySymbols; var l = Object.prototype.hasOwnProperty, h = Object.prototype.propertyIsEnumerable; var o = (s, t, e) => t in s ? c(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e, n = (s, t) => { for (var e in t || (t = {})) l.call(t, e) && o(s, e, t[e]); if (d) for (var e of d(t)) h.call(t, e) && o(s, e, t[e]); return s; }; var a = (s, t, e) => o(s, typeof t != "symbol" ? t + "" : t, e); import g from "react-dom"; class u { constructor(t) { a(this, "isScriptLoaded", !1); a(this, "analyticsConfig", null); a(this, "unHandledCommandList", []); a(this, "loadHandlers", []); this.measurementId = t; } loadGoogleAnalytics(t) { window.dataLayer = window.dataLayer || []; const e = function() { window.dataLayer.push(arguments); }; window.gtag = e; const i = document.createElement("script"); i.src = `https://www.googletagmanager.com/gtag/js?id=${this.measurementId}`, i.async = !0, g.preload(i.src, { as: "script" }), i.onload = () => { this.printDebug("GA script loaded successfully"), window.gtag("js", /* @__PURE__ */ new Date()), window.gtag( "config", this.measurementId, t != null ? t : { send_page_view: !1 } ), i.remove(), this.isScriptLoaded = !0, this.handleQueuedCommands(), this.loadHandlers.forEach((r) => r()); }, i.onerror = (r) => { this.printError("Failed to load GA script:", r); }, document.body.appendChild(i), this.printDebug("GA script injected"); } initialize(t) { if (this.isScriptLoaded) { this.printDebug("Already initialized"); return; } this.analyticsConfig = n({}, t), this.loadGoogleAnalytics(t); } event(t, e) { if (!this.isScriptLoaded) { this.queueCommand({ command: "event", eventName: t, params: e }); return; } try { const i = n(n({}, this.analyticsConfig), e); window.gtag("event", t, i); } catch (i) { this.printError("Failed to send GA event:", i); } } config(t) { if (!this.isScriptLoaded) { this.queueCommand({ command: "config", params: t }); return; } try { window.gtag("config", this.measurementId, t), this.analyticsConfig = n(n({}, this.analyticsConfig), t); } catch (e) { this.printError("Failed to update GA config:", e); } } consent(t, e) { if (!this.isScriptLoaded) { this.queueCommand({ command: "consent", consentArgs: t, params: e }); return; } try { window.gtag("consent", t, e); } catch (i) { this.printError("Failed to update GA consent:", i); } } set(t, e) { if (!this.isScriptLoaded) { this.queueCommand({ command: "set", setArgs: t, params: e }); return; } try { t ? window.gtag("set", t, e) : window.gtag("set", e); } catch (i) { this.printError("Failed to update GA set:", i); } } onLoad(t) { this.isScriptLoaded ? t() : this.loadHandlers.push(t); } get configInfo() { return structuredClone(this.analyticsConfig); } queueCommand(t) { this.unHandledCommandList.push(t); } handleQueuedCommands() { this.unHandledCommandList.forEach((t) => { switch (t.command) { case "event": this.event(t.eventName, t.params); break; case "set": this.set(t.setArgs, t.params); break; case "config": this.config(t.params); break; case "consent": this.consent(t.consentArgs, t.params); break; } }), this.unHandledCommandList = []; } printDebug(t, ...e) { var i; (i = this.analyticsConfig) != null && i.debug_mode && console.log(`[Analytics] ${t}`, ...e); } printError(t, ...e) { console.error(`[Analytics] ${t}`, ...e); } } export { u as GA4 };