@stacksee/analytics 0.4.2 → 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,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
- };