@stacksee/analytics 0.4.2 → 0.4.4

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,184 +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 p {
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
- }, o = 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(o);
66
- }
67
- pageView(e) {
68
- this.ensureInitialized().catch((i) => {
69
- console.error("[Analytics] Failed to initialize during pageView:", 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.pageView(e, this.context);
79
- }
80
- pageLeave(e) {
81
- this.ensureInitialized().catch((i) => {
82
- console.error("[Analytics] Failed to initialize during pageLeave:", i);
83
- });
84
- for (const i of this.providers)
85
- i.pageLeave && i.pageLeave(e, this.context);
86
- }
87
- reset() {
88
- this.userId = void 0, this.sessionId = this.generateSessionId();
89
- for (const e of this.providers)
90
- e.reset();
91
- }
92
- updateContext(e) {
93
- var i, r, o;
94
- this.context = {
95
- ...this.context,
96
- ...e,
97
- page: e.page ? {
98
- path: e.page.path || ((i = this.context.page) == null ? void 0 : i.path) || window.location.pathname,
99
- title: e.page.title || ((r = this.context.page) == null ? void 0 : r.title),
100
- referrer: e.page.referrer || ((o = this.context.page) == null ? void 0 : o.referrer)
101
- } : this.context.page,
102
- device: {
103
- ...this.context.device,
104
- ...e.device
105
- },
106
- utm: {
107
- ...this.context.utm,
108
- ...e.utm
109
- }
110
- };
111
- }
112
- getCategoryFromEventName(e) {
113
- const i = e.split("_");
114
- return i.length > 1 && i[0] ? i[0] : "engagement";
115
- }
116
- generateSessionId() {
117
- return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
118
- }
119
- getDeviceType() {
120
- const e = navigator.userAgent;
121
- return /tablet|ipad|playbook|silk/i.test(e) ? "tablet" : /mobile|iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile/i.test(
122
- e
123
- ) ? "mobile" : "desktop";
124
- }
125
- getOS() {
126
- const e = navigator.userAgent;
127
- 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";
128
- }
129
- getBrowser() {
130
- const e = navigator.userAgent;
131
- return e.indexOf("Chrome") !== -1 ? "Chrome" : e.indexOf("Safari") !== -1 ? "Safari" : e.indexOf("Firefox") !== -1 ? "Firefox" : e.indexOf("Edge") !== -1 ? "Edge" : "Unknown";
132
- }
133
- }
134
- let n = null;
135
- function m(t) {
136
- if (n)
137
- return console.warn("[Analytics] Already initialized"), n;
138
- const e = {
139
- providers: t.providers || [],
140
- debug: t.debug,
141
- enabled: t.enabled
142
- };
143
- return n = new p(
144
- e
145
- ), n.initialize().catch((i) => {
146
- console.error("[Analytics] Failed to initialize:", i);
147
- }), n;
148
- }
149
- function a() {
150
- if (!n)
151
- throw new Error(
152
- "[Analytics] Not initialized. Call createAnalytics() first."
153
- );
154
- return n;
155
- }
156
- function v(t, e) {
157
- return a().track(t, e);
158
- }
159
- function y(t, e) {
160
- a().identify(t, e);
161
- }
162
- function w(t) {
163
- a().pageView(t);
164
- }
165
- function z(t) {
166
- a().pageLeave(t);
167
- }
168
- function x() {
169
- a().reset();
170
- }
171
- function A() {
172
- n = null;
173
- }
174
- export {
175
- p as B,
176
- z as a,
177
- A as b,
178
- m as c,
179
- a as g,
180
- y as i,
181
- w as p,
182
- x as r,
183
- v as t
184
- };
@@ -1,2 +0,0 @@
1
- export * from './src/providers/index'
2
- export {}
package/dist/providers.js DELETED
@@ -1,6 +0,0 @@
1
- import { B as e, P as s, a } from "./server-BKqARaUf.js";
2
- export {
3
- e as BaseAnalyticsProvider,
4
- s as PostHogClientProvider,
5
- a as PostHogServerProvider
6
- };
@@ -1,174 +0,0 @@
1
- var p = Object.defineProperty;
2
- var g = (a, s, e) => s in a ? p(a, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[s] = e;
3
- var t = (a, s, e) => g(a, typeof s != "symbol" ? s + "" : s, e);
4
- import { i as h } from "./environment-Bnc8FqHv.js";
5
- import { PostHog as o } from "posthog-node";
6
- class l {
7
- constructor(s) {
8
- t(this, "debug", !1);
9
- t(this, "enabled", !0);
10
- (s == null ? void 0 : s.debug) !== void 0 && (this.debug = s.debug), (s == null ? void 0 : s.enabled) !== void 0 && (this.enabled = s.enabled);
11
- }
12
- log(s, e) {
13
- this.debug && console.log(`[${this.name}] ${s}`, e);
14
- }
15
- isEnabled() {
16
- return this.enabled;
17
- }
18
- }
19
- class b extends l {
20
- constructor(e) {
21
- super({ debug: e.debug, enabled: e.enabled });
22
- t(this, "name", "PostHog-Client");
23
- t(this, "posthog");
24
- t(this, "initialized", !1);
25
- t(this, "config");
26
- this.config = e;
27
- }
28
- async initialize() {
29
- if (this.isEnabled() && !this.initialized) {
30
- if (!h()) {
31
- this.log("Skipping initialization - not in browser environment");
32
- return;
33
- }
34
- if (!this.config.token || typeof this.config.token != "string")
35
- throw new Error("PostHog requires a token");
36
- try {
37
- const { default: e } = await import("posthog-js"), { token: i, debug: r, ...d } = this.config;
38
- e.init(i, {
39
- ...d,
40
- debug: r ?? this.debug
41
- }), this.posthog = e, this.initialized = !0, this.log("Initialized successfully", this.config);
42
- } catch (e) {
43
- throw console.error("[PostHog-Client] Failed to initialize:", e), e;
44
- }
45
- }
46
- }
47
- identify(e, i) {
48
- !this.isEnabled() || !this.initialized || !this.posthog || (this.posthog.identify(e, i), this.log("Identified user", { userId: e, traits: i }));
49
- }
50
- track(e, i) {
51
- if (!this.isEnabled() || !this.initialized || !this.posthog) return;
52
- const r = {
53
- ...e.properties,
54
- category: e.category,
55
- timestamp: e.timestamp || Date.now(),
56
- ...e.userId && { userId: e.userId },
57
- ...e.sessionId && { sessionId: e.sessionId },
58
- ...(i == null ? void 0 : i.page) && { $current_url: i.page.path },
59
- ...(i == null ? void 0 : i.device) && { device: i.device },
60
- ...(i == null ? void 0 : i.utm) && { utm: i.utm }
61
- };
62
- this.posthog.capture(e.action, r), this.log("Tracked event", { event: e, context: i });
63
- }
64
- pageView(e, i) {
65
- if (!this.isEnabled() || !this.initialized || !this.posthog || !h())
66
- return;
67
- const r = {
68
- ...e,
69
- ...(i == null ? void 0 : i.page) && {
70
- path: i.page.path,
71
- title: i.page.title,
72
- referrer: i.page.referrer
73
- }
74
- };
75
- this.posthog.capture("$pageview", r), this.log("Tracked page view", { properties: e, context: i });
76
- }
77
- pageLeave(e, i) {
78
- if (!this.isEnabled() || !this.initialized || !this.posthog || !h())
79
- return;
80
- const r = {
81
- ...e,
82
- ...(i == null ? void 0 : i.page) && {
83
- path: i.page.path,
84
- title: i.page.title,
85
- referrer: i.page.referrer
86
- }
87
- };
88
- this.posthog.capture("$pageleave", r), this.log("Tracked page leave", { properties: e, context: i });
89
- }
90
- reset() {
91
- !this.isEnabled() || !this.initialized || !this.posthog || !h() || (this.posthog.reset(), this.log("Reset user session"));
92
- }
93
- }
94
- class m extends l {
95
- constructor(e) {
96
- super({ debug: e.debug, enabled: e.enabled });
97
- t(this, "name", "PostHog-Server");
98
- t(this, "client");
99
- t(this, "initialized", !1);
100
- t(this, "config");
101
- this.config = e;
102
- }
103
- initialize() {
104
- if (this.isEnabled() && !this.initialized) {
105
- if (!this.config.apiKey || typeof this.config.apiKey != "string")
106
- throw new Error("PostHog requires an apiKey");
107
- try {
108
- const { apiKey: e, ...i } = this.config;
109
- this.client = new o(e, {
110
- host: "https://app.posthog.com",
111
- flushAt: 20,
112
- flushInterval: 1e4,
113
- ...i
114
- }), this.initialized = !0, this.log("Initialized successfully", this.config);
115
- } catch (e) {
116
- throw console.error("[PostHog-Server] Failed to initialize:", e), e;
117
- }
118
- }
119
- }
120
- identify(e, i) {
121
- !this.isEnabled() || !this.initialized || !this.client || (this.client.identify({
122
- distinctId: e,
123
- properties: i
124
- }), this.log("Identified user", { userId: e, traits: i }));
125
- }
126
- track(e, i) {
127
- if (!this.isEnabled() || !this.initialized || !this.client) return;
128
- const r = {
129
- ...e.properties,
130
- category: e.category,
131
- timestamp: e.timestamp ? new Date(e.timestamp) : /* @__PURE__ */ new Date(),
132
- ...e.sessionId && { sessionId: e.sessionId },
133
- ...(i == null ? void 0 : i.page) && {
134
- $current_url: i.page.path,
135
- $page_title: i.page.title,
136
- $referrer: i.page.referrer
137
- },
138
- ...(i == null ? void 0 : i.device) && { device: i.device },
139
- ...(i == null ? void 0 : i.utm) && { utm: i.utm }
140
- };
141
- this.client.capture({
142
- distinctId: e.userId || "anonymous",
143
- event: e.action,
144
- properties: r
145
- }), this.log("Tracked event", { event: e, context: i });
146
- }
147
- pageView(e, i) {
148
- if (!this.isEnabled() || !this.initialized || !this.client) return;
149
- const r = {
150
- ...e,
151
- ...(i == null ? void 0 : i.page) && {
152
- path: i.page.path,
153
- title: i.page.title,
154
- referrer: i.page.referrer
155
- }
156
- };
157
- this.client.capture({
158
- distinctId: "anonymous",
159
- event: "$pageview",
160
- properties: r
161
- }), this.log("Tracked page view", { properties: e, context: i });
162
- }
163
- async reset() {
164
- !this.isEnabled() || !this.initialized || !this.client || (await this.client.flush(), this.log("Flushed pending events"));
165
- }
166
- async shutdown() {
167
- this.client && (await this.client.shutdown(), this.log("Shutdown complete"));
168
- }
169
- }
170
- export {
171
- l as B,
172
- b as P,
173
- m as a
174
- };
File without changes