@stacksee/analytics 0.4.4 → 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.
Files changed (39) hide show
  1. package/dist/{src/adapters → adapters}/client/browser-analytics.d.ts +1 -1
  2. package/dist/{src/adapters → adapters}/server/server-analytics.d.ts +1 -1
  3. package/dist/client/index.d.ts +7 -0
  4. package/dist/client-RZPcOfAk.js +86 -0
  5. package/dist/client.d.ts +68 -1
  6. package/dist/client.js +26 -25
  7. package/dist/index.d.ts +2 -1
  8. package/dist/{src/providers → providers}/base.provider.d.ts +1 -1
  9. package/dist/providers/client.d.ts +3 -1
  10. package/dist/providers/client.js +4 -82
  11. package/dist/{src/providers → providers}/posthog/client.d.ts +2 -2
  12. package/dist/{src/providers → providers}/posthog/server.d.ts +2 -2
  13. package/dist/providers/server.d.ts +3 -1
  14. package/dist/providers/server.js +4 -83
  15. package/dist/server/index.d.ts +6 -0
  16. package/dist/server-CMRw9K0d.js +84 -0
  17. package/dist/server.d.ts +38 -1
  18. package/dist/server.js +18 -14
  19. package/package.json +1 -1
  20. package/dist/environment-Bnc8FqHv.js +0 -6
  21. package/dist/src/client/index.d.ts +0 -7
  22. package/dist/src/client.d.ts +0 -68
  23. package/dist/src/index.d.ts +0 -2
  24. package/dist/src/providers/client.d.ts +0 -3
  25. package/dist/src/providers/server.d.ts +0 -3
  26. package/dist/src/server/index.d.ts +0 -6
  27. package/dist/src/server.d.ts +0 -38
  28. package/dist/test/base-provider.test.d.ts +0 -1
  29. package/dist/test/client-analytics.test.d.ts +0 -1
  30. package/dist/test/client.test.d.ts +0 -1
  31. package/dist/test/events.test.d.ts +0 -1
  32. package/dist/test/mock-provider.d.ts +0 -34
  33. package/dist/test/providers.test.d.ts +0 -1
  34. package/dist/test/server-analytics.test.d.ts +0 -1
  35. package/dist/test/server.test.d.ts +0 -1
  36. package/dist/test/utils.test.d.ts +0 -1
  37. /package/dist/{src/core → core}/events/index.d.ts +0 -0
  38. /package/dist/{src/core → core}/events/types.d.ts +0 -0
  39. /package/dist/{src/utils → utils}/environment.d.ts +0 -0
@@ -1,4 +1,4 @@
1
- import { AnalyticsConfig, EventContext } from '../../../../src/core/events/types.js';
1
+ import { AnalyticsConfig, EventContext } from '../../core/events/types.js';
2
2
  type DefaultEventMap = Record<string, Record<string, unknown>>;
3
3
  export declare class BrowserAnalytics<TEventMap extends DefaultEventMap = DefaultEventMap> {
4
4
  private providers;
@@ -1,4 +1,4 @@
1
- import { AnalyticsConfig, EventContext } from '../../../../src/core/events/types.js';
1
+ import { AnalyticsConfig, EventContext } from '../../core/events/types.js';
2
2
  type DefaultEventMap = Record<string, Record<string, unknown>>;
3
3
  export declare class ServerAnalytics<TEventMap extends DefaultEventMap = DefaultEventMap> {
4
4
  private providers;
@@ -0,0 +1,7 @@
1
+ export { createClientAnalytics, createAnalytics, getAnalytics, track, identify, pageView, pageLeave, reset, type ClientAnalyticsConfig, } from '../client.js';
2
+ export { BrowserAnalytics } from '../adapters/client/browser-analytics.js';
3
+ export { PostHogClientProvider } from '../providers/posthog/client.js';
4
+ export type { PostHogConfig } from 'posthog-js';
5
+ export { BaseAnalyticsProvider } from '../providers/base.provider.js';
6
+ export type { EventCategory, BaseEvent, EventContext, AnalyticsProvider, AnalyticsConfig, } from '../core/events/types.js';
7
+ export type { CreateEventDefinition, ExtractEventNames, ExtractEventPropertiesFromCollection, EventCollection, AnyEventName, AnyEventProperties, } from '../core/events/index.js';
@@ -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 +1,68 @@
1
- export {}
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;
package/dist/client.js CHANGED
@@ -1,8 +1,10 @@
1
1
  var c = Object.defineProperty;
2
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 {
3
+ var n = (t, e, i) => u(t, typeof e != "symbol" ? e + "" : e, i);
4
+ import { i as h } from "./client-RZPcOfAk.js";
5
+ import { P } from "./client-RZPcOfAk.js";
6
+ import { B as O } from "./base.provider-AfFL5W_P.js";
7
+ class p {
6
8
  /**
7
9
  * Creates a new BrowserAnalytics instance for client-side event tracking.
8
10
  *
@@ -34,12 +36,12 @@ class f {
34
36
  * ```
35
37
  */
36
38
  constructor(e) {
37
- s(this, "providers", []);
38
- s(this, "context", {});
39
- s(this, "userId");
40
- s(this, "sessionId");
41
- s(this, "initialized", !1);
42
- s(this, "initializePromise");
39
+ n(this, "providers", []);
40
+ n(this, "context", {});
41
+ n(this, "userId");
42
+ n(this, "sessionId");
43
+ n(this, "initialized", !1);
44
+ n(this, "initializePromise");
43
45
  this.providers = e.providers, e.defaultContext && (this.context = { ...e.defaultContext }), this.sessionId = this.generateSessionId();
44
46
  }
45
47
  /**
@@ -524,32 +526,32 @@ class f {
524
526
  return e.indexOf("Chrome") !== -1 ? "Chrome" : e.indexOf("Safari") !== -1 ? "Safari" : e.indexOf("Firefox") !== -1 ? "Firefox" : e.indexOf("Edge") !== -1 ? "Edge" : "Unknown";
525
527
  }
526
528
  }
527
- let n = null;
529
+ let s = null;
528
530
  function m(t) {
529
- if (n)
530
- return console.warn("[Analytics] Already initialized"), n;
531
+ if (s)
532
+ return console.warn("[Analytics] Already initialized"), s;
531
533
  const e = {
532
534
  providers: t.providers || [],
533
535
  debug: t.debug,
534
536
  enabled: t.enabled
535
537
  };
536
- return n = new f(
538
+ return s = new p(
537
539
  e
538
- ), n.initialize().catch((i) => {
540
+ ), s.initialize().catch((i) => {
539
541
  console.error("[Analytics] Failed to initialize:", i);
540
- }), n;
542
+ }), s;
541
543
  }
542
544
  function a() {
543
- if (!n)
545
+ if (!s)
544
546
  throw new Error(
545
547
  "[Analytics] Not initialized. Call createAnalytics() first."
546
548
  );
547
- return n;
549
+ return s;
548
550
  }
549
- function y(t, e) {
551
+ function v(t, e) {
550
552
  return a().track(t, e);
551
553
  }
552
- function v(t, e) {
554
+ function y(t, e) {
553
555
  a().identify(t, e);
554
556
  }
555
557
  function w(t) {
@@ -561,17 +563,16 @@ function z(t) {
561
563
  function x() {
562
564
  a().reset();
563
565
  }
564
- function A() {
565
- n = null;
566
- }
567
566
  export {
567
+ O as BaseAnalyticsProvider,
568
+ p as BrowserAnalytics,
569
+ P as PostHogClientProvider,
568
570
  m as createAnalytics,
569
571
  m as createClientAnalytics,
570
572
  a as getAnalytics,
571
- v as identify,
573
+ y as identify,
572
574
  z as pageLeave,
573
575
  w as pageView,
574
576
  x as reset,
575
- A as resetAnalyticsInstance,
576
- y as track
577
+ v as track
577
578
  };
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
- export {}
1
+ export type { EventCategory, BaseEvent, EventContext, AnalyticsProvider, AnalyticsConfig, } from './core/events/types.js';
2
+ export type { CreateEventDefinition, ExtractEventNames, ExtractEventPropertiesFromCollection, EventCollection, AnyEventName, AnyEventProperties, EventMapFromCollection, } from './core/events/index.js';
@@ -1,4 +1,4 @@
1
- import { AnalyticsProvider, BaseEvent, EventContext } from '../../../src/core/events/types.js';
1
+ import { AnalyticsProvider, BaseEvent, EventContext } from '../core/events/types.js';
2
2
  export declare abstract class BaseAnalyticsProvider implements AnalyticsProvider {
3
3
  abstract name: string;
4
4
  protected debug: boolean;
@@ -1 +1,3 @@
1
- export {}
1
+ export { BaseAnalyticsProvider } from './base.provider.js';
2
+ export { PostHogClientProvider } from './posthog/client.js';
3
+ export type { PostHogConfig } from 'posthog-js';
@@ -1,84 +1,6 @@
1
- var o = Object.defineProperty;
2
- var p = (a, e, s) => e in a ? o(a, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : a[e] = s;
3
- var t = (a, e, s) => p(a, typeof e != "symbol" ? e + "" : e, s);
4
- import { B as l } from "../base.provider-AfFL5W_P.js";
5
- import { i as g } from "../environment-Bnc8FqHv.js";
6
- class f extends l {
7
- constructor(s) {
8
- super({ debug: s.debug, enabled: s.enabled });
9
- t(this, "name", "PostHog-Client");
10
- t(this, "posthog");
11
- t(this, "initialized", !1);
12
- t(this, "config");
13
- this.config = s;
14
- }
15
- async initialize() {
16
- if (this.isEnabled() && !this.initialized) {
17
- if (!g()) {
18
- this.log("Skipping initialization - not in browser environment");
19
- return;
20
- }
21
- if (!this.config.token || typeof this.config.token != "string")
22
- throw new Error("PostHog requires a token");
23
- try {
24
- const { default: s } = await import("posthog-js"), { token: i, debug: r, ...h } = this.config;
25
- s.init(i, {
26
- ...h,
27
- debug: r ?? this.debug
28
- }), this.posthog = s, this.initialized = !0, this.log("Initialized successfully", this.config);
29
- } catch (s) {
30
- throw console.error("[PostHog-Client] Failed to initialize:", s), s;
31
- }
32
- }
33
- }
34
- identify(s, i) {
35
- !this.isEnabled() || !this.initialized || !this.posthog || (this.posthog.identify(s, i), this.log("Identified user", { userId: s, traits: i }));
36
- }
37
- track(s, i) {
38
- if (!this.isEnabled() || !this.initialized || !this.posthog) return;
39
- const r = {
40
- ...s.properties,
41
- category: s.category,
42
- timestamp: s.timestamp || Date.now(),
43
- ...s.userId && { userId: s.userId },
44
- ...s.sessionId && { sessionId: s.sessionId },
45
- ...(i == null ? void 0 : i.page) && { $current_url: i.page.path },
46
- ...(i == null ? void 0 : i.device) && { device: i.device },
47
- ...(i == null ? void 0 : i.utm) && { utm: i.utm }
48
- };
49
- this.posthog.capture(s.action, r), this.log("Tracked event", { event: s, context: i });
50
- }
51
- pageView(s, i) {
52
- if (!this.isEnabled() || !this.initialized || !this.posthog || !g())
53
- return;
54
- const r = {
55
- ...s,
56
- ...(i == null ? void 0 : i.page) && {
57
- path: i.page.path,
58
- title: i.page.title,
59
- referrer: i.page.referrer
60
- }
61
- };
62
- this.posthog.capture("$pageview", r), this.log("Tracked page view", { properties: s, context: i });
63
- }
64
- pageLeave(s, i) {
65
- if (!this.isEnabled() || !this.initialized || !this.posthog || !g())
66
- return;
67
- const r = {
68
- ...s,
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("$pageleave", r), this.log("Tracked page leave", { properties: s, context: i });
76
- }
77
- reset() {
78
- !this.isEnabled() || !this.initialized || !this.posthog || !g() || (this.posthog.reset(), this.log("Reset user session"));
79
- }
80
- }
1
+ import { B as e } from "../base.provider-AfFL5W_P.js";
2
+ import { P as t } from "../client-RZPcOfAk.js";
81
3
  export {
82
- l as BaseAnalyticsProvider,
83
- f as PostHogClientProvider
4
+ e as BaseAnalyticsProvider,
5
+ t as PostHogClientProvider
84
6
  };
@@ -1,5 +1,5 @@
1
- import { BaseEvent, EventContext } from '../../../../src/core/events/types.js';
2
- import { BaseAnalyticsProvider } from '../../../../src/providers/base.provider.js';
1
+ import { BaseEvent, EventContext } from '../../core/events/types.js';
2
+ import { BaseAnalyticsProvider } from '../base.provider.js';
3
3
  import { PostHogConfig } from 'posthog-js';
4
4
  export declare class PostHogClientProvider extends BaseAnalyticsProvider {
5
5
  name: string;
@@ -1,5 +1,5 @@
1
- import { BaseEvent, EventContext } from '../../../../src/core/events/types.js';
2
- import { BaseAnalyticsProvider } from '../../../../src/providers/base.provider.js';
1
+ import { BaseEvent, EventContext } from '../../core/events/types.js';
2
+ import { BaseAnalyticsProvider } from '../base.provider.js';
3
3
  import { PostHogOptions } from 'posthog-node';
4
4
  export declare class PostHogServerProvider extends BaseAnalyticsProvider {
5
5
  name: string;
@@ -1 +1,3 @@
1
- export {}
1
+ export { BaseAnalyticsProvider } from './base.provider.js';
2
+ export { PostHogServerProvider } from './posthog/server.js';
3
+ export type { PostHogOptions } from 'posthog-node';
@@ -1,85 +1,6 @@
1
- var l = Object.defineProperty;
2
- var h = (r, s, i) => s in r ? l(r, s, { enumerable: !0, configurable: !0, writable: !0, value: i }) : r[s] = i;
3
- var t = (r, s, i) => h(r, typeof s != "symbol" ? s + "" : s, i);
4
- import { B as n } from "../base.provider-AfFL5W_P.js";
5
- import { PostHog as p } from "posthog-node";
6
- class u extends n {
7
- constructor(i) {
8
- super({ debug: i.debug, enabled: i.enabled });
9
- t(this, "name", "PostHog-Server");
10
- t(this, "client");
11
- t(this, "initialized", !1);
12
- t(this, "config");
13
- this.config = i;
14
- }
15
- initialize() {
16
- if (this.isEnabled() && !this.initialized) {
17
- if (!this.config.apiKey || typeof this.config.apiKey != "string")
18
- throw new Error("PostHog requires an apiKey");
19
- try {
20
- const { apiKey: i, ...e } = this.config;
21
- this.client = new p(i, {
22
- host: "https://app.posthog.com",
23
- flushAt: 20,
24
- flushInterval: 1e4,
25
- ...e
26
- }), this.initialized = !0, this.log("Initialized successfully", this.config);
27
- } catch (i) {
28
- throw console.error("[PostHog-Server] Failed to initialize:", i), i;
29
- }
30
- }
31
- }
32
- identify(i, e) {
33
- !this.isEnabled() || !this.initialized || !this.client || (this.client.identify({
34
- distinctId: i,
35
- properties: e
36
- }), this.log("Identified user", { userId: i, traits: e }));
37
- }
38
- track(i, e) {
39
- if (!this.isEnabled() || !this.initialized || !this.client) return;
40
- const a = {
41
- ...i.properties,
42
- category: i.category,
43
- timestamp: i.timestamp ? new Date(i.timestamp) : /* @__PURE__ */ new Date(),
44
- ...i.sessionId && { sessionId: i.sessionId },
45
- ...(e == null ? void 0 : e.page) && {
46
- $current_url: e.page.path,
47
- $page_title: e.page.title,
48
- $referrer: e.page.referrer
49
- },
50
- ...(e == null ? void 0 : e.device) && { device: e.device },
51
- ...(e == null ? void 0 : e.utm) && { utm: e.utm }
52
- };
53
- this.client.capture({
54
- distinctId: i.userId || "anonymous",
55
- event: i.action,
56
- properties: a
57
- }), this.log("Tracked event", { event: i, context: e });
58
- }
59
- pageView(i, e) {
60
- if (!this.isEnabled() || !this.initialized || !this.client) return;
61
- const a = {
62
- ...i,
63
- ...(e == null ? void 0 : e.page) && {
64
- path: e.page.path,
65
- title: e.page.title,
66
- referrer: e.page.referrer
67
- }
68
- };
69
- this.client.capture({
70
- distinctId: "anonymous",
71
- event: "$pageview",
72
- properties: a
73
- }), this.log("Tracked page view", { properties: i, context: e });
74
- }
75
- async reset() {
76
- !this.isEnabled() || !this.initialized || !this.client || (await this.client.flush(), this.log("Flushed pending events"));
77
- }
78
- async shutdown() {
79
- this.client && (await this.client.shutdown(), this.log("Shutdown complete"));
80
- }
81
- }
1
+ import { B as e } from "../base.provider-AfFL5W_P.js";
2
+ import { P as a } from "../server-CMRw9K0d.js";
82
3
  export {
83
- n as BaseAnalyticsProvider,
84
- u as PostHogServerProvider
4
+ e as BaseAnalyticsProvider,
5
+ a as PostHogServerProvider
85
6
  };
@@ -0,0 +1,6 @@
1
+ export { createServerAnalytics, ServerAnalytics, type ServerAnalyticsConfig, } from '../server.js';
2
+ export { PostHogServerProvider } from '../providers/posthog/server.js';
3
+ export type { PostHogOptions } from 'posthog-node';
4
+ export { BaseAnalyticsProvider } from '../providers/base.provider.js';
5
+ export type { EventCategory, BaseEvent, EventContext, AnalyticsProvider, AnalyticsConfig, } from '../core/events/types.js';
6
+ export type { CreateEventDefinition, ExtractEventNames, ExtractEventPropertiesFromCollection, EventCollection, AnyEventName, AnyEventProperties, } from '../core/events/index.js';
@@ -0,0 +1,84 @@
1
+ var l = Object.defineProperty;
2
+ var h = (r, s, i) => s in r ? l(r, s, { enumerable: !0, configurable: !0, writable: !0, value: i }) : r[s] = i;
3
+ var t = (r, s, i) => h(r, typeof s != "symbol" ? s + "" : s, i);
4
+ import { B as n } from "./base.provider-AfFL5W_P.js";
5
+ import { PostHog as p } from "posthog-node";
6
+ class u extends n {
7
+ constructor(i) {
8
+ super({ debug: i.debug, enabled: i.enabled });
9
+ t(this, "name", "PostHog-Server");
10
+ t(this, "client");
11
+ t(this, "initialized", !1);
12
+ t(this, "config");
13
+ this.config = i;
14
+ }
15
+ initialize() {
16
+ if (this.isEnabled() && !this.initialized) {
17
+ if (!this.config.apiKey || typeof this.config.apiKey != "string")
18
+ throw new Error("PostHog requires an apiKey");
19
+ try {
20
+ const { apiKey: i, ...e } = this.config;
21
+ this.client = new p(i, {
22
+ host: "https://app.posthog.com",
23
+ flushAt: 20,
24
+ flushInterval: 1e4,
25
+ ...e
26
+ }), this.initialized = !0, this.log("Initialized successfully", this.config);
27
+ } catch (i) {
28
+ throw console.error("[PostHog-Server] Failed to initialize:", i), i;
29
+ }
30
+ }
31
+ }
32
+ identify(i, e) {
33
+ !this.isEnabled() || !this.initialized || !this.client || (this.client.identify({
34
+ distinctId: i,
35
+ properties: e
36
+ }), this.log("Identified user", { userId: i, traits: e }));
37
+ }
38
+ track(i, e) {
39
+ if (!this.isEnabled() || !this.initialized || !this.client) return;
40
+ const a = {
41
+ ...i.properties,
42
+ category: i.category,
43
+ timestamp: i.timestamp ? new Date(i.timestamp) : /* @__PURE__ */ new Date(),
44
+ ...i.sessionId && { sessionId: i.sessionId },
45
+ ...(e == null ? void 0 : e.page) && {
46
+ $current_url: e.page.path,
47
+ $page_title: e.page.title,
48
+ $referrer: e.page.referrer
49
+ },
50
+ ...(e == null ? void 0 : e.device) && { device: e.device },
51
+ ...(e == null ? void 0 : e.utm) && { utm: e.utm }
52
+ };
53
+ this.client.capture({
54
+ distinctId: i.userId || "anonymous",
55
+ event: i.action,
56
+ properties: a
57
+ }), this.log("Tracked event", { event: i, context: e });
58
+ }
59
+ pageView(i, e) {
60
+ if (!this.isEnabled() || !this.initialized || !this.client) return;
61
+ const a = {
62
+ ...i,
63
+ ...(e == null ? void 0 : e.page) && {
64
+ path: e.page.path,
65
+ title: e.page.title,
66
+ referrer: e.page.referrer
67
+ }
68
+ };
69
+ this.client.capture({
70
+ distinctId: "anonymous",
71
+ event: "$pageview",
72
+ properties: a
73
+ }), this.log("Tracked page view", { properties: i, context: e });
74
+ }
75
+ async reset() {
76
+ !this.isEnabled() || !this.initialized || !this.client || (await this.client.flush(), this.log("Flushed pending events"));
77
+ }
78
+ async shutdown() {
79
+ this.client && (await this.client.shutdown(), this.log("Shutdown complete"));
80
+ }
81
+ }
82
+ export {
83
+ u as P
84
+ };
package/dist/server.d.ts CHANGED
@@ -1 +1,38 @@
1
- export {}
1
+ import { ServerAnalytics } from './adapters/server/server-analytics.js';
2
+ import { AnalyticsProvider } from './core/events/types.js';
3
+ import { EventMapFromCollection } from './core/events/index.js';
4
+ export interface ServerAnalyticsConfig {
5
+ providers?: AnalyticsProvider[];
6
+ debug?: boolean;
7
+ enabled?: boolean;
8
+ }
9
+ /**
10
+ * Create a server analytics instance
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { createServerAnalytics } from '@stacksee/analytics/server';
15
+ * import { PostHogServerProvider } from '@stacksee/analytics/providers/posthog';
16
+ * import { AppEvents } from './events';
17
+ *
18
+ * const analytics = createServerAnalytics<typeof AppEvents>({
19
+ * providers: [
20
+ * new PostHogServerProvider({
21
+ * apiKey: process.env.POSTHOG_API_KEY,
22
+ * host: process.env.POSTHOG_HOST
23
+ * })
24
+ * ],
25
+ * debug: true,
26
+ * enabled: true
27
+ * });
28
+ *
29
+ * // Now event names and properties are fully typed!
30
+ * await analytics.track('user_signed_up', {
31
+ * userId: 'user-123',
32
+ * email: 'user@example.com',
33
+ * plan: 'pro'
34
+ * }, { userId: 'user-123' });
35
+ * ```
36
+ */
37
+ export declare function createServerAnalytics<TEvents = never>(config: ServerAnalyticsConfig): ServerAnalytics<EventMapFromCollection<TEvents>>;
38
+ export { ServerAnalytics };
package/dist/server.js CHANGED
@@ -1,6 +1,8 @@
1
- var o = Object.defineProperty;
2
- var f = (r, e, t) => e in r ? o(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
- var n = (r, e, t) => f(r, typeof e != "symbol" ? e + "" : e, t);
1
+ var l = Object.defineProperty;
2
+ var f = (r, e, t) => e in r ? l(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
+ var s = (r, e, t) => f(r, typeof e != "symbol" ? e + "" : e, t);
4
+ import { P as m } from "./server-CMRw9K0d.js";
5
+ import { B as x } from "./base.provider-AfFL5W_P.js";
4
6
  class u {
5
7
  /**
6
8
  * Creates a new ServerAnalytics instance for server-side event tracking.
@@ -33,9 +35,9 @@ class u {
33
35
  * ```
34
36
  */
35
37
  constructor(e) {
36
- n(this, "providers", []);
37
- n(this, "config");
38
- n(this, "initialized", !1);
38
+ s(this, "providers", []);
39
+ s(this, "config");
40
+ s(this, "initialized", !1);
39
41
  this.config = e, this.providers = e.providers;
40
42
  }
41
43
  /**
@@ -234,17 +236,17 @@ class u {
234
236
  }, c = {
235
237
  ...this.config.defaultContext,
236
238
  ...i == null ? void 0 : i.context
237
- }, d = this.providers.map(async (s) => {
239
+ }, o = this.providers.map(async (n) => {
238
240
  try {
239
- await s.track(a, c);
240
- } catch (l) {
241
+ await n.track(a, c);
242
+ } catch (d) {
241
243
  console.error(
242
- `[Analytics] Provider ${s.name} failed to track event:`,
243
- l
244
+ `[Analytics] Provider ${n.name} failed to track event:`,
245
+ d
244
246
  );
245
247
  }
246
248
  });
247
- await Promise.all(d);
249
+ await Promise.all(o);
248
250
  }
249
251
  /**
250
252
  * Tracks a page view event from the server side.
@@ -476,7 +478,7 @@ class u {
476
478
  return t.length > 1 && t[0] ? t[0] : "engagement";
477
479
  }
478
480
  }
479
- function v(r) {
481
+ function h(r) {
480
482
  const e = {
481
483
  providers: r.providers || [],
482
484
  debug: r.debug,
@@ -487,6 +489,8 @@ function v(r) {
487
489
  return t.initialize(), t;
488
490
  }
489
491
  export {
492
+ x as BaseAnalyticsProvider,
493
+ m as PostHogServerProvider,
490
494
  u as ServerAnalytics,
491
- v as createServerAnalytics
495
+ h as createServerAnalytics
492
496
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stacksee/analytics",
3
- "version": "0.4.4",
3
+ "version": "0.4.5",
4
4
  "description": "A highly typed, provider-agnostic analytics library for TypeScript applications",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,6 +0,0 @@
1
- function e() {
2
- return typeof window < "u" && typeof window.document < "u";
3
- }
4
- export {
5
- e as i
6
- };
@@ -1,7 +0,0 @@
1
- export { createClientAnalytics, createAnalytics, getAnalytics, track, identify, pageView, pageLeave, reset, type ClientAnalyticsConfig, } from '../../../src/client.js';
2
- export { BrowserAnalytics } from '../../../src/adapters/client/browser-analytics.js';
3
- export { PostHogClientProvider } from '../../../src/providers/posthog/client.js';
4
- export type { PostHogConfig } from 'posthog-js';
5
- export { BaseAnalyticsProvider } from '../../../src/providers/base.provider.js';
6
- export type { EventCategory, BaseEvent, EventContext, AnalyticsProvider, AnalyticsConfig, } from '../../../src/core/events/types.js';
7
- export type { CreateEventDefinition, ExtractEventNames, ExtractEventPropertiesFromCollection, EventCollection, AnyEventName, AnyEventProperties, } from '../../../src/core/events/index.js';
@@ -1,68 +0,0 @@
1
- import { BrowserAnalytics } from '../../src/adapters/client/browser-analytics.js';
2
- import { AnalyticsProvider } from '../../src/core/events/types.js';
3
- import { EventMapFromCollection } from '../../src/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;
@@ -1,2 +0,0 @@
1
- export type { EventCategory, BaseEvent, EventContext, AnalyticsProvider, AnalyticsConfig, } from '../../src/core/events/types.js';
2
- export type { CreateEventDefinition, ExtractEventNames, ExtractEventPropertiesFromCollection, EventCollection, AnyEventName, AnyEventProperties, EventMapFromCollection, } from '../../src/core/events/index.js';
@@ -1,3 +0,0 @@
1
- export { BaseAnalyticsProvider } from './base.provider.js';
2
- export { PostHogClientProvider } from './posthog/client.js';
3
- export type { PostHogConfig } from 'posthog-js';
@@ -1,3 +0,0 @@
1
- export { BaseAnalyticsProvider } from './base.provider.js';
2
- export { PostHogServerProvider } from './posthog/server.js';
3
- export type { PostHogOptions } from 'posthog-node';
@@ -1,6 +0,0 @@
1
- export { createServerAnalytics, ServerAnalytics, type ServerAnalyticsConfig, } from '../../../src/server.js';
2
- export { PostHogServerProvider } from '../../../src/providers/posthog/server.js';
3
- export type { PostHogOptions } from 'posthog-node';
4
- export { BaseAnalyticsProvider } from '../../../src/providers/base.provider.js';
5
- export type { EventCategory, BaseEvent, EventContext, AnalyticsProvider, AnalyticsConfig, } from '../../../src/core/events/types.js';
6
- export type { CreateEventDefinition, ExtractEventNames, ExtractEventPropertiesFromCollection, EventCollection, AnyEventName, AnyEventProperties, } from '../../../src/core/events/index.js';
@@ -1,38 +0,0 @@
1
- import { ServerAnalytics } from '../../src/adapters/server/server-analytics.js';
2
- import { AnalyticsProvider } from '../../src/core/events/types.js';
3
- import { EventMapFromCollection } from '../../src/core/events/index.js';
4
- export interface ServerAnalyticsConfig {
5
- providers?: AnalyticsProvider[];
6
- debug?: boolean;
7
- enabled?: boolean;
8
- }
9
- /**
10
- * Create a server analytics instance
11
- *
12
- * @example
13
- * ```typescript
14
- * import { createServerAnalytics } from '@stacksee/analytics/server';
15
- * import { PostHogServerProvider } from '@stacksee/analytics/providers/posthog';
16
- * import { AppEvents } from './events';
17
- *
18
- * const analytics = createServerAnalytics<typeof AppEvents>({
19
- * providers: [
20
- * new PostHogServerProvider({
21
- * apiKey: process.env.POSTHOG_API_KEY,
22
- * host: process.env.POSTHOG_HOST
23
- * })
24
- * ],
25
- * debug: true,
26
- * enabled: true
27
- * });
28
- *
29
- * // Now event names and properties are fully typed!
30
- * await analytics.track('user_signed_up', {
31
- * userId: 'user-123',
32
- * email: 'user@example.com',
33
- * plan: 'pro'
34
- * }, { userId: 'user-123' });
35
- * ```
36
- */
37
- export declare function createServerAnalytics<TEvents = never>(config: ServerAnalyticsConfig): ServerAnalytics<EventMapFromCollection<TEvents>>;
38
- export { ServerAnalytics };
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,34 +0,0 @@
1
- import { BaseAnalyticsProvider } from '../../src/providers/base.provider';
2
- import { BaseEvent, EventContext } from '../../src/core/events/types';
3
- export declare class MockAnalyticsProvider extends BaseAnalyticsProvider {
4
- name: string;
5
- private initialized;
6
- calls: {
7
- initialize: number;
8
- identify: Array<{
9
- userId: string;
10
- traits?: Record<string, unknown>;
11
- }>;
12
- track: Array<{
13
- event: BaseEvent;
14
- context?: EventContext;
15
- }>;
16
- pageView: Array<{
17
- properties?: Record<string, unknown>;
18
- context?: EventContext;
19
- }>;
20
- pageLeave: Array<{
21
- properties?: Record<string, unknown>;
22
- context?: EventContext;
23
- }>;
24
- reset: number;
25
- };
26
- initialize(): void;
27
- identify(userId: string, traits?: Record<string, unknown>): void;
28
- track(event: BaseEvent, context?: EventContext): void;
29
- pageView(properties?: Record<string, unknown>, context?: EventContext): void;
30
- pageLeave(properties?: Record<string, unknown>, context?: EventContext): void;
31
- reset(): void;
32
- clearCalls(): void;
33
- isInitialized(): boolean;
34
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
File without changes
File without changes
File without changes