@stacksee/analytics 0.4.3 → 0.4.5
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/base.provider-AfFL5W_P.js +19 -0
- package/dist/client-RZPcOfAk.js +86 -0
- package/dist/client.d.ts +68 -2
- package/dist/client.js +576 -10
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -14
- package/dist/providers/client.js +6 -0
- package/dist/{src/providers/index.d.ts → providers/server.d.ts} +0 -2
- package/dist/providers/server.js +6 -0
- package/dist/{providers.js → server-CMRw9K0d.js} +2 -5
- package/dist/server.d.ts +38 -2
- package/dist/server.js +18 -14
- package/package.json +8 -4
- package/readme.md +11 -5
- package/dist/client-CyvENC-f.js +0 -577
- package/dist/client-MwIAm9fk.js +0 -96
- package/dist/environment-Bnc8FqHv.js +0 -6
- package/dist/providers.d.ts +0 -2
- package/dist/src/client.d.ts +0 -68
- package/dist/src/index.d.ts +0 -5
- package/dist/src/server.d.ts +0 -38
- package/dist/test/base-provider.test.d.ts +0 -1
- package/dist/test/client-analytics.test.d.ts +0 -1
- package/dist/test/events.test.d.ts +0 -1
- package/dist/test/index.test.d.ts +0 -1
- package/dist/test/mock-provider.d.ts +0 -34
- package/dist/test/providers.test.d.ts +0 -1
- package/dist/test/server-analytics.test.d.ts +0 -1
- package/dist/test/server.test.d.ts +0 -1
- package/dist/test/utils.test.d.ts +0 -1
- /package/dist/{src/adapters → adapters}/client/browser-analytics.d.ts +0 -0
- /package/dist/{src/adapters → adapters}/server/server-analytics.d.ts +0 -0
- /package/dist/{src/client → client}/index.d.ts +0 -0
- /package/dist/{src/core → core}/events/index.d.ts +0 -0
- /package/dist/{src/core → core}/events/types.d.ts +0 -0
- /package/dist/{src/providers → providers}/base.provider.d.ts +0 -0
- /package/dist/{src/providers → providers}/client.d.ts +0 -0
- /package/dist/{src/providers → providers}/posthog/client.d.ts +0 -0
- /package/dist/{src/providers → providers}/posthog/server.d.ts +0 -0
- /package/dist/{src/server → server}/index.d.ts +0 -0
- /package/dist/{src/utils → utils}/environment.d.ts +0 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
var l = Object.defineProperty;
|
|
2
|
+
var b = (d, e, s) => e in d ? l(d, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : d[e] = s;
|
|
3
|
+
var a = (d, e, s) => b(d, typeof e != "symbol" ? e + "" : e, s);
|
|
4
|
+
class r {
|
|
5
|
+
constructor(e) {
|
|
6
|
+
a(this, "debug", !1);
|
|
7
|
+
a(this, "enabled", !0);
|
|
8
|
+
(e == null ? void 0 : e.debug) !== void 0 && (this.debug = e.debug), (e == null ? void 0 : e.enabled) !== void 0 && (this.enabled = e.enabled);
|
|
9
|
+
}
|
|
10
|
+
log(e, s) {
|
|
11
|
+
this.debug && console.log(`[${this.name}] ${e}`, s);
|
|
12
|
+
}
|
|
13
|
+
isEnabled() {
|
|
14
|
+
return this.enabled;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
r as B
|
|
19
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
var h = Object.defineProperty;
|
|
2
|
+
var p = (a, s, e) => s in a ? h(a, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[s] = e;
|
|
3
|
+
var t = (a, s, e) => p(a, typeof s != "symbol" ? s + "" : s, e);
|
|
4
|
+
import { B as d } from "./base.provider-AfFL5W_P.js";
|
|
5
|
+
function o() {
|
|
6
|
+
return typeof window < "u" && typeof window.document < "u";
|
|
7
|
+
}
|
|
8
|
+
class u extends d {
|
|
9
|
+
constructor(e) {
|
|
10
|
+
super({ debug: e.debug, enabled: e.enabled });
|
|
11
|
+
t(this, "name", "PostHog-Client");
|
|
12
|
+
t(this, "posthog");
|
|
13
|
+
t(this, "initialized", !1);
|
|
14
|
+
t(this, "config");
|
|
15
|
+
this.config = e;
|
|
16
|
+
}
|
|
17
|
+
async initialize() {
|
|
18
|
+
if (this.isEnabled() && !this.initialized) {
|
|
19
|
+
if (!o()) {
|
|
20
|
+
this.log("Skipping initialization - not in browser environment");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (!this.config.token || typeof this.config.token != "string")
|
|
24
|
+
throw new Error("PostHog requires a token");
|
|
25
|
+
try {
|
|
26
|
+
const { default: e } = await import("posthog-js"), { token: i, debug: r, ...g } = this.config;
|
|
27
|
+
e.init(i, {
|
|
28
|
+
...g,
|
|
29
|
+
debug: r ?? this.debug
|
|
30
|
+
}), this.posthog = e, this.initialized = !0, this.log("Initialized successfully", this.config);
|
|
31
|
+
} catch (e) {
|
|
32
|
+
throw console.error("[PostHog-Client] Failed to initialize:", e), e;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
identify(e, i) {
|
|
37
|
+
!this.isEnabled() || !this.initialized || !this.posthog || (this.posthog.identify(e, i), this.log("Identified user", { userId: e, traits: i }));
|
|
38
|
+
}
|
|
39
|
+
track(e, i) {
|
|
40
|
+
if (!this.isEnabled() || !this.initialized || !this.posthog) return;
|
|
41
|
+
const r = {
|
|
42
|
+
...e.properties,
|
|
43
|
+
category: e.category,
|
|
44
|
+
timestamp: e.timestamp || Date.now(),
|
|
45
|
+
...e.userId && { userId: e.userId },
|
|
46
|
+
...e.sessionId && { sessionId: e.sessionId },
|
|
47
|
+
...(i == null ? void 0 : i.page) && { $current_url: i.page.path },
|
|
48
|
+
...(i == null ? void 0 : i.device) && { device: i.device },
|
|
49
|
+
...(i == null ? void 0 : i.utm) && { utm: i.utm }
|
|
50
|
+
};
|
|
51
|
+
this.posthog.capture(e.action, r), this.log("Tracked event", { event: e, context: i });
|
|
52
|
+
}
|
|
53
|
+
pageView(e, i) {
|
|
54
|
+
if (!this.isEnabled() || !this.initialized || !this.posthog || !o())
|
|
55
|
+
return;
|
|
56
|
+
const r = {
|
|
57
|
+
...e,
|
|
58
|
+
...(i == null ? void 0 : i.page) && {
|
|
59
|
+
path: i.page.path,
|
|
60
|
+
title: i.page.title,
|
|
61
|
+
referrer: i.page.referrer
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
this.posthog.capture("$pageview", r), this.log("Tracked page view", { properties: e, context: i });
|
|
65
|
+
}
|
|
66
|
+
pageLeave(e, i) {
|
|
67
|
+
if (!this.isEnabled() || !this.initialized || !this.posthog || !o())
|
|
68
|
+
return;
|
|
69
|
+
const r = {
|
|
70
|
+
...e,
|
|
71
|
+
...(i == null ? void 0 : i.page) && {
|
|
72
|
+
path: i.page.path,
|
|
73
|
+
title: i.page.title,
|
|
74
|
+
referrer: i.page.referrer
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
this.posthog.capture("$pageleave", r), this.log("Tracked page leave", { properties: e, context: i });
|
|
78
|
+
}
|
|
79
|
+
reset() {
|
|
80
|
+
!this.isEnabled() || !this.initialized || !this.posthog || !o() || (this.posthog.reset(), this.log("Reset user session"));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
u as P,
|
|
85
|
+
o as i
|
|
86
|
+
};
|
package/dist/client.d.ts
CHANGED
|
@@ -1,2 +1,68 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { BrowserAnalytics } from './adapters/client/browser-analytics.js';
|
|
2
|
+
import { AnalyticsProvider } from './core/events/types.js';
|
|
3
|
+
import { EventMapFromCollection } from './core/events/index.js';
|
|
4
|
+
type DefaultEventMap = Record<string, Record<string, unknown>>;
|
|
5
|
+
export interface ClientAnalyticsConfig {
|
|
6
|
+
providers?: AnalyticsProvider[];
|
|
7
|
+
debug?: boolean;
|
|
8
|
+
enabled?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Initialize analytics for the browser
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { createClientAnalytics } from '@stacksee/analytics/client';
|
|
16
|
+
* import { PostHogClientProvider } from '@stacksee/analytics/providers/posthog';
|
|
17
|
+
* import { AppEvents } from './events';
|
|
18
|
+
*
|
|
19
|
+
* const analytics = createClientAnalytics<typeof AppEvents>({
|
|
20
|
+
* providers: [
|
|
21
|
+
* new PostHogClientProvider({
|
|
22
|
+
* apiKey: 'your-api-key',
|
|
23
|
+
* host: 'https://app.posthog.com'
|
|
24
|
+
* })
|
|
25
|
+
* ],
|
|
26
|
+
* debug: true,
|
|
27
|
+
* enabled: true
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* // Now event names and properties are fully typed!
|
|
31
|
+
* analytics.track('user_signed_up', {
|
|
32
|
+
* userId: 'user-123',
|
|
33
|
+
* email: 'user@example.com',
|
|
34
|
+
* plan: 'pro'
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function createClientAnalytics<TEvents = never>(config: ClientAnalyticsConfig): BrowserAnalytics<EventMapFromCollection<TEvents>>;
|
|
39
|
+
export { createClientAnalytics as createAnalytics };
|
|
40
|
+
/**
|
|
41
|
+
* Get the current analytics instance
|
|
42
|
+
*/
|
|
43
|
+
export declare function getAnalytics(): BrowserAnalytics<DefaultEventMap>;
|
|
44
|
+
/**
|
|
45
|
+
* Convenience function to track events
|
|
46
|
+
*/
|
|
47
|
+
export declare function track(eventName: string, properties: Record<string, unknown>): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Convenience function to identify users
|
|
50
|
+
*/
|
|
51
|
+
export declare function identify(userId: string, traits?: Record<string, unknown>): void;
|
|
52
|
+
/**
|
|
53
|
+
* Convenience function to track page views
|
|
54
|
+
*/
|
|
55
|
+
export declare function pageView(properties?: Record<string, unknown>): void;
|
|
56
|
+
/**
|
|
57
|
+
* Convenience function to track page leave events
|
|
58
|
+
*/
|
|
59
|
+
export declare function pageLeave(properties?: Record<string, unknown>): void;
|
|
60
|
+
/**
|
|
61
|
+
* Convenience function to reset user session
|
|
62
|
+
*/
|
|
63
|
+
export declare function reset(): void;
|
|
64
|
+
/**
|
|
65
|
+
* Reset the analytics instance (for testing purposes)
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
export declare function resetAnalyticsInstance(): void;
|