@stacksee/analytics 0.4.3 → 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.
- package/dist/base.provider-AfFL5W_P.js +19 -0
- package/dist/client.d.ts +0 -1
- package/dist/client.js +575 -10
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -14
- package/dist/providers/client.d.ts +1 -0
- package/dist/providers/client.js +84 -0
- package/dist/providers/server.d.ts +1 -0
- package/dist/{providers.js → providers/server.js} +1 -3
- package/dist/server.d.ts +0 -1
- package/dist/src/adapters/client/browser-analytics.d.ts +1 -1
- package/dist/src/adapters/server/server-analytics.d.ts +1 -1
- package/dist/src/client/index.d.ts +6 -6
- package/dist/src/client.d.ts +3 -3
- package/dist/src/index.d.ts +2 -5
- package/dist/src/providers/base.provider.d.ts +1 -1
- package/dist/src/providers/posthog/client.d.ts +2 -2
- package/dist/src/providers/posthog/server.d.ts +2 -2
- package/dist/src/providers/{index.d.ts → server.d.ts} +0 -2
- package/dist/src/server/index.d.ts +5 -5
- package/dist/src/server.d.ts +3 -3
- package/dist/test/mock-provider.d.ts +2 -2
- 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/providers.d.ts +0 -2
- /package/dist/test/{index.test.d.ts → client.test.d.ts} +0 -0
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
}
|
|
81
|
+
export {
|
|
82
|
+
l as BaseAnalyticsProvider,
|
|
83
|
+
f as PostHogClientProvider
|
|
84
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
var l = Object.defineProperty;
|
|
2
2
|
var h = (r, s, i) => s in r ? l(r, s, { enumerable: !0, configurable: !0, writable: !0, value: i }) : r[s] = i;
|
|
3
3
|
var t = (r, s, i) => h(r, typeof s != "symbol" ? s + "" : s, i);
|
|
4
|
-
import { B as n } from "
|
|
5
|
-
import { P as m } from "./client-MwIAm9fk.js";
|
|
4
|
+
import { B as n } from "../base.provider-AfFL5W_P.js";
|
|
6
5
|
import { PostHog as p } from "posthog-node";
|
|
7
6
|
class u extends n {
|
|
8
7
|
constructor(i) {
|
|
@@ -82,6 +81,5 @@ class u extends n {
|
|
|
82
81
|
}
|
|
83
82
|
export {
|
|
84
83
|
n as BaseAnalyticsProvider,
|
|
85
|
-
m as PostHogClientProvider,
|
|
86
84
|
u as PostHogServerProvider
|
|
87
85
|
};
|
package/dist/server.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnalyticsConfig, EventContext } from '
|
|
1
|
+
import { AnalyticsConfig, EventContext } from '../../../../src/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 '
|
|
1
|
+
import { AnalyticsConfig, EventContext } from '../../../../src/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;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { createClientAnalytics, createAnalytics, getAnalytics, track, identify, pageView, pageLeave, reset, type ClientAnalyticsConfig, } from '
|
|
2
|
-
export { BrowserAnalytics } from '
|
|
3
|
-
export { PostHogClientProvider } from '
|
|
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
4
|
export type { PostHogConfig } from 'posthog-js';
|
|
5
|
-
export { BaseAnalyticsProvider } from '
|
|
6
|
-
export type { EventCategory, BaseEvent, EventContext, AnalyticsProvider, AnalyticsConfig, } from '
|
|
7
|
-
export type { CreateEventDefinition, ExtractEventNames, ExtractEventPropertiesFromCollection, EventCollection, AnyEventName, AnyEventProperties, } from '
|
|
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';
|
package/dist/src/client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BrowserAnalytics } from '
|
|
2
|
-
import { AnalyticsProvider } from '
|
|
3
|
-
import { EventMapFromCollection } from '
|
|
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
4
|
type DefaultEventMap = Record<string, Record<string, unknown>>;
|
|
5
5
|
export interface ClientAnalyticsConfig {
|
|
6
6
|
providers?: AnalyticsProvider[];
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
export type { EventCategory, BaseEvent, EventContext, AnalyticsProvider, AnalyticsConfig, } from '
|
|
2
|
-
export type { CreateEventDefinition, ExtractEventNames, ExtractEventPropertiesFromCollection, EventCollection, AnyEventName, AnyEventProperties, EventMapFromCollection, } from '
|
|
3
|
-
export { createAnalytics as createClientAnalytics, getAnalytics, track as trackClient, identify as identifyClient, pageView as pageViewClient, pageLeave as pageLeaveClient, reset as resetClient, type ClientAnalyticsConfig, } from './client.js';
|
|
4
|
-
export { BaseAnalyticsProvider, PostHogClientProvider, type PostHogConfig, } from './providers/client.js';
|
|
5
|
-
export { BrowserAnalytics } from './adapters/client/browser-analytics.js';
|
|
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,4 +1,4 @@
|
|
|
1
|
-
import { AnalyticsProvider, BaseEvent, EventContext } from '
|
|
1
|
+
import { AnalyticsProvider, BaseEvent, EventContext } from '../../../src/core/events/types.js';
|
|
2
2
|
export declare abstract class BaseAnalyticsProvider implements AnalyticsProvider {
|
|
3
3
|
abstract name: string;
|
|
4
4
|
protected debug: boolean;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseEvent, EventContext } from '
|
|
2
|
-
import { BaseAnalyticsProvider } from '
|
|
1
|
+
import { BaseEvent, EventContext } from '../../../../src/core/events/types.js';
|
|
2
|
+
import { BaseAnalyticsProvider } from '../../../../src/providers/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 '
|
|
2
|
-
import { BaseAnalyticsProvider } from '
|
|
1
|
+
import { BaseEvent, EventContext } from '../../../../src/core/events/types.js';
|
|
2
|
+
import { BaseAnalyticsProvider } from '../../../../src/providers/base.provider.js';
|
|
3
3
|
import { PostHogOptions } from 'posthog-node';
|
|
4
4
|
export declare class PostHogServerProvider extends BaseAnalyticsProvider {
|
|
5
5
|
name: string;
|
|
@@ -1,5 +1,3 @@
|
|
|
1
1
|
export { BaseAnalyticsProvider } from './base.provider.js';
|
|
2
|
-
export { PostHogClientProvider } from './posthog/client.js';
|
|
3
2
|
export { PostHogServerProvider } from './posthog/server.js';
|
|
4
|
-
export type { PostHogConfig } from 'posthog-js';
|
|
5
3
|
export type { PostHogOptions } from 'posthog-node';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { createServerAnalytics, ServerAnalytics, type ServerAnalyticsConfig, } from '
|
|
2
|
-
export { PostHogServerProvider } from '
|
|
1
|
+
export { createServerAnalytics, ServerAnalytics, type ServerAnalyticsConfig, } from '../../../src/server.js';
|
|
2
|
+
export { PostHogServerProvider } from '../../../src/providers/posthog/server.js';
|
|
3
3
|
export type { PostHogOptions } from 'posthog-node';
|
|
4
|
-
export { BaseAnalyticsProvider } from '
|
|
5
|
-
export type { EventCategory, BaseEvent, EventContext, AnalyticsProvider, AnalyticsConfig, } from '
|
|
6
|
-
export type { CreateEventDefinition, ExtractEventNames, ExtractEventPropertiesFromCollection, EventCollection, AnyEventName, AnyEventProperties, } from '
|
|
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';
|
package/dist/src/server.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ServerAnalytics } from '
|
|
2
|
-
import { AnalyticsProvider } from '
|
|
3
|
-
import { EventMapFromCollection } from '
|
|
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
4
|
export interface ServerAnalyticsConfig {
|
|
5
5
|
providers?: AnalyticsProvider[];
|
|
6
6
|
debug?: boolean;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseAnalyticsProvider } from '
|
|
2
|
-
import { BaseEvent, EventContext } from '
|
|
1
|
+
import { BaseAnalyticsProvider } from '../../src/providers/base.provider';
|
|
2
|
+
import { BaseEvent, EventContext } from '../../src/core/events/types';
|
|
3
3
|
export declare class MockAnalyticsProvider extends BaseAnalyticsProvider {
|
|
4
4
|
name: string;
|
|
5
5
|
private initialized;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksee/analytics",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "A highly typed, provider-agnostic analytics library for TypeScript applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -16,9 +16,13 @@
|
|
|
16
16
|
"types": "./dist/server.d.ts",
|
|
17
17
|
"import": "./dist/server.js"
|
|
18
18
|
},
|
|
19
|
-
"./providers": {
|
|
20
|
-
"types": "./dist/providers.d.ts",
|
|
21
|
-
"import": "./dist/providers.js"
|
|
19
|
+
"./providers/client": {
|
|
20
|
+
"types": "./dist/providers/client.d.ts",
|
|
21
|
+
"import": "./dist/providers/client.js"
|
|
22
|
+
},
|
|
23
|
+
"./providers/server": {
|
|
24
|
+
"types": "./dist/providers/server.d.ts",
|
|
25
|
+
"import": "./dist/providers/server.js"
|
|
22
26
|
}
|
|
23
27
|
},
|
|
24
28
|
"main": "./dist/index.js",
|
package/readme.md
CHANGED
|
@@ -97,7 +97,7 @@ Tip: If you have a lot of events, you can also divide your events into multiple
|
|
|
97
97
|
|
|
98
98
|
```typescript
|
|
99
99
|
import { createClientAnalytics } from '@stacksee/analytics/client';
|
|
100
|
-
import { PostHogClientProvider } from '@stacksee/analytics/providers/
|
|
100
|
+
import { PostHogClientProvider } from '@stacksee/analytics/providers/client';
|
|
101
101
|
import type { AppEvents } from './events';
|
|
102
102
|
|
|
103
103
|
// Initialize analytics with providers as plugins
|
|
@@ -138,7 +138,7 @@ analytics.identify('user-123', {
|
|
|
138
138
|
|
|
139
139
|
```typescript
|
|
140
140
|
import { createServerAnalytics } from '@stacksee/analytics/server';
|
|
141
|
-
import { PostHogServerProvider } from '@stacksee/analytics/providers/
|
|
141
|
+
import { PostHogServerProvider } from '@stacksee/analytics/providers/server';
|
|
142
142
|
import type { AppEvents } from './events';
|
|
143
143
|
|
|
144
144
|
// Create analytics instance with providers as plugins
|
|
@@ -268,7 +268,7 @@ Here's a complete example using Svelte 5 that demonstrates both client and serve
|
|
|
268
268
|
```typescript
|
|
269
269
|
// src/lib/config/analytics.ts
|
|
270
270
|
import { createClientAnalytics } from '@stacksee/analytics/client';
|
|
271
|
-
import { PostHogClientProvider } from '@stacksee/analytics/providers/
|
|
271
|
+
import { PostHogClientProvider } from '@stacksee/analytics/providers/client';
|
|
272
272
|
import { PUBLIC_POSTHOG_API_KEY, PUBLIC_POSTHOG_HOST } from '$env/static/public';
|
|
273
273
|
|
|
274
274
|
// Define your events for the waitlist
|
|
@@ -306,7 +306,7 @@ export const clientAnalytics = createClientAnalytics<AppEvents>({
|
|
|
306
306
|
```typescript
|
|
307
307
|
// src/lib/server/analytics.ts
|
|
308
308
|
import { createServerAnalytics } from '@stacksee/analytics/server';
|
|
309
|
-
import { PostHogServerProvider } from '@stacksee/analytics/providers/
|
|
309
|
+
import { PostHogServerProvider } from '@stacksee/analytics/providers/server';
|
|
310
310
|
import { AppEvents } from '$lib/config/analytics'; // Import AppEvents
|
|
311
311
|
import { PUBLIC_POSTHOG_API_KEY, PUBLIC_POSTHOG_HOST } from '$env/static/public';
|
|
312
312
|
|
|
@@ -564,6 +564,12 @@ const analytics = await createClientAnalytics<typeof AppEvents>({
|
|
|
564
564
|
|
|
565
565
|
### Client-Only and Server-Only Providers
|
|
566
566
|
|
|
567
|
+
**Important**: To avoid bundling Node.js dependencies in your client code, always use the environment-specific provider imports:
|
|
568
|
+
|
|
569
|
+
- **Client-side**: `@stacksee/analytics/providers/client` - Only includes browser-compatible providers
|
|
570
|
+
- **Server-side**: `@stacksee/analytics/providers/server` - Only includes Node.js providers
|
|
571
|
+
- **Both**: `@stacksee/analytics/providers` - Includes all providers (may cause bundling issues in browsers)
|
|
572
|
+
|
|
567
573
|
Some analytics libraries are designed to work only in specific environments. For example:
|
|
568
574
|
- **Client-only**: Google Analytics (gtag.js), Hotjar, FullStory
|
|
569
575
|
- **Server-only**: Some enterprise analytics APIs that require secret keys
|
|
@@ -638,7 +644,7 @@ The plugin architecture makes it easy to send events to multiple analytics servi
|
|
|
638
644
|
|
|
639
645
|
```typescript
|
|
640
646
|
import { createClientAnalytics } from '@stacksee/analytics/client';
|
|
641
|
-
import { PostHogClientProvider } from '@stacksee/analytics/providers/
|
|
647
|
+
import { PostHogClientProvider } from '@stacksee/analytics/providers/client';
|
|
642
648
|
// Import your custom providers
|
|
643
649
|
import { GoogleAnalyticsProvider } from './providers/google-analytics';
|
|
644
650
|
import { MixpanelProvider } from './providers/mixpanel';
|