@stacksee/analytics 0.3.4 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,173 +0,0 @@
1
- var c = Object.defineProperty;
2
- var u = (t, e, i) => e in t ? c(t, e, { enumerable: !0, configurable: !0, writable: !0, value: i }) : t[e] = i;
3
- var s = (t, e, i) => u(t, typeof e != "symbol" ? e + "" : e, i);
4
- import { i as h } from "./environment-Bnc8FqHv.js";
5
- class f {
6
- constructor(e) {
7
- s(this, "providers", []);
8
- s(this, "context", {});
9
- s(this, "userId");
10
- s(this, "sessionId");
11
- s(this, "initialized", !1);
12
- s(this, "initializePromise");
13
- this.providers = e.providers, e.defaultContext && (this.context = { ...e.defaultContext }), this.sessionId = this.generateSessionId();
14
- }
15
- async initialize() {
16
- if (h() && !this.initialized)
17
- return this.initializePromise ? this.initializePromise : (this.initializePromise = this._doInitialize(), this.initializePromise);
18
- }
19
- async _doInitialize() {
20
- const e = this.providers.map(
21
- (i) => i.initialize()
22
- );
23
- await Promise.all(e), this.initialized = !0, this.updateContext({
24
- page: {
25
- path: window.location.pathname,
26
- title: document.title,
27
- referrer: document.referrer
28
- },
29
- device: {
30
- type: this.getDeviceType(),
31
- os: this.getOS(),
32
- browser: this.getBrowser()
33
- }
34
- });
35
- }
36
- async ensureInitialized() {
37
- !this.initialized && !this.initializePromise ? await this.initialize() : this.initializePromise && await this.initializePromise;
38
- }
39
- identify(e, i) {
40
- this.userId = e, this.ensureInitialized().catch((r) => {
41
- console.error("[Analytics] Failed to initialize during identify:", r);
42
- });
43
- for (const r of this.providers)
44
- r.identify(e, i);
45
- }
46
- async track(e, i) {
47
- await this.ensureInitialized();
48
- const r = {
49
- action: e,
50
- category: this.getCategoryFromEventName(e),
51
- properties: i,
52
- timestamp: Date.now(),
53
- userId: this.userId,
54
- sessionId: this.sessionId
55
- }, a = this.providers.map(async (d) => {
56
- try {
57
- await d.track(r, this.context);
58
- } catch (l) {
59
- console.error(
60
- `[Analytics] Provider ${d.name} failed to track event:`,
61
- l
62
- );
63
- }
64
- });
65
- await Promise.all(a);
66
- }
67
- page(e) {
68
- this.ensureInitialized().catch((i) => {
69
- console.error("[Analytics] Failed to initialize during page:", i);
70
- }), this.updateContext({
71
- page: {
72
- path: window.location.pathname,
73
- title: document.title,
74
- referrer: document.referrer
75
- }
76
- });
77
- for (const i of this.providers)
78
- i.page(e, this.context);
79
- }
80
- reset() {
81
- this.userId = void 0, this.sessionId = this.generateSessionId();
82
- for (const e of this.providers)
83
- e.reset();
84
- }
85
- updateContext(e) {
86
- var i, r, a;
87
- this.context = {
88
- ...this.context,
89
- ...e,
90
- page: e.page ? {
91
- path: e.page.path || ((i = this.context.page) == null ? void 0 : i.path) || window.location.pathname,
92
- title: e.page.title || ((r = this.context.page) == null ? void 0 : r.title),
93
- referrer: e.page.referrer || ((a = this.context.page) == null ? void 0 : a.referrer)
94
- } : this.context.page,
95
- device: {
96
- ...this.context.device,
97
- ...e.device
98
- },
99
- utm: {
100
- ...this.context.utm,
101
- ...e.utm
102
- }
103
- };
104
- }
105
- getCategoryFromEventName(e) {
106
- const i = e.split("_");
107
- return i.length > 1 && i[0] ? i[0] : "engagement";
108
- }
109
- generateSessionId() {
110
- return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
111
- }
112
- getDeviceType() {
113
- const e = navigator.userAgent;
114
- return /tablet|ipad|playbook|silk/i.test(e) ? "tablet" : /mobile|iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile/i.test(
115
- e
116
- ) ? "mobile" : "desktop";
117
- }
118
- getOS() {
119
- const e = navigator.userAgent;
120
- return e.indexOf("Win") !== -1 ? "Windows" : e.indexOf("Mac") !== -1 ? "macOS" : e.indexOf("Linux") !== -1 ? "Linux" : e.indexOf("Android") !== -1 ? "Android" : e.indexOf("iOS") !== -1 ? "iOS" : "Unknown";
121
- }
122
- getBrowser() {
123
- const e = navigator.userAgent;
124
- return e.indexOf("Chrome") !== -1 ? "Chrome" : e.indexOf("Safari") !== -1 ? "Safari" : e.indexOf("Firefox") !== -1 ? "Firefox" : e.indexOf("Edge") !== -1 ? "Edge" : "Unknown";
125
- }
126
- }
127
- let n = null;
128
- function m(t) {
129
- if (n)
130
- return console.warn("[Analytics] Already initialized"), n;
131
- const e = {
132
- providers: t.providers || [],
133
- debug: t.debug,
134
- enabled: t.enabled
135
- };
136
- return n = new f(
137
- e
138
- ), n.initialize().catch((i) => {
139
- console.error("[Analytics] Failed to initialize:", i);
140
- }), n;
141
- }
142
- function o() {
143
- if (!n)
144
- throw new Error(
145
- "[Analytics] Not initialized. Call createAnalytics() first."
146
- );
147
- return n;
148
- }
149
- function y(t, e) {
150
- return o().track(t, e);
151
- }
152
- function w(t, e) {
153
- o().identify(t, e);
154
- }
155
- function z(t) {
156
- o().page(t);
157
- }
158
- function v() {
159
- o().reset();
160
- }
161
- function x() {
162
- n = null;
163
- }
164
- export {
165
- f as B,
166
- x as a,
167
- m as c,
168
- o as g,
169
- w as i,
170
- z as p,
171
- v as r,
172
- y as t
173
- };