@allem-sdk/analytics 0.1.2 → 0.1.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.
- package/README.md +32 -0
- package/dist/index.d.mts +57 -1
- package/dist/index.d.ts +57 -1
- package/dist/index.js +41 -0
- package/dist/index.mjs +37 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -57,6 +57,34 @@ function ProductPage({ product }) {
|
|
|
57
57
|
}
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
+
## Built-in Adapters
|
|
61
|
+
|
|
62
|
+
Pre-built adapters for popular analytics providers:
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
import { mixpanelAdapter, posthogAdapter, segmentAdapter, consoleAdapter } from "@allem-sdk/analytics";
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
| Adapter | Usage | Description |
|
|
69
|
+
|---------|-------|-------------|
|
|
70
|
+
| `mixpanelAdapter(mixpanel)` | Pass your initialized Mixpanel instance | Tracks events, page views, and identifies users with `people.set` |
|
|
71
|
+
| `posthogAdapter(posthog)` | Pass your initialized PostHog instance | Uses `capture` for events and `$pageview` for pages |
|
|
72
|
+
| `segmentAdapter(analytics)` | Pass `window.analytics` or Analytics.js instance | Direct mapping to Segment's `track`, `page`, `identify` |
|
|
73
|
+
| `consoleAdapter(prefix?)` | No dependencies | Logs all events to console. Great for development. |
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
import mixpanel from "mixpanel-browser";
|
|
77
|
+
import posthog from "posthog-js";
|
|
78
|
+
|
|
79
|
+
mixpanel.init("YOUR_TOKEN");
|
|
80
|
+
posthog.init("YOUR_KEY", { api_host: "https://app.posthog.com" });
|
|
81
|
+
|
|
82
|
+
// Use one or many adapters simultaneously
|
|
83
|
+
<AnalyticsProvider adapters={[mixpanelAdapter(mixpanel), posthogAdapter(posthog), consoleAdapter()]}>
|
|
84
|
+
<App />
|
|
85
|
+
</AnalyticsProvider>
|
|
86
|
+
```
|
|
87
|
+
|
|
60
88
|
## Exports
|
|
61
89
|
|
|
62
90
|
| Export | Type | Description |
|
|
@@ -65,6 +93,10 @@ function ProductPage({ product }) {
|
|
|
65
93
|
| `useTrack` | Hook | Returns a `track(event, properties)` function |
|
|
66
94
|
| `usePageView` | Hook | Tracks a page view on mount |
|
|
67
95
|
| `useIdentify` | Hook | Returns an `identify(userId, traits)` function |
|
|
96
|
+
| `mixpanelAdapter` | Factory | Adapter for Mixpanel |
|
|
97
|
+
| `posthogAdapter` | Factory | Adapter for PostHog |
|
|
98
|
+
| `segmentAdapter` | Factory | Adapter for Segment / Analytics.js |
|
|
99
|
+
| `consoleAdapter` | Factory | Console-logging adapter for development |
|
|
68
100
|
|
|
69
101
|
## Part of [Allem SDK](https://github.com/kingofmit/allem-sdk)
|
|
70
102
|
|
package/dist/index.d.mts
CHANGED
|
@@ -18,4 +18,60 @@ declare function usePageView(name?: string, properties?: Record<string, unknown>
|
|
|
18
18
|
|
|
19
19
|
declare function useIdentify(): (userId: string, traits?: Record<string, unknown>) => void;
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Mixpanel adapter.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* import mixpanel from "mixpanel-browser";
|
|
27
|
+
* mixpanel.init("YOUR_TOKEN");
|
|
28
|
+
*
|
|
29
|
+
* <AnalyticsProvider adapters={[mixpanelAdapter(mixpanel)]}>
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
declare function mixpanelAdapter(mixpanel: {
|
|
33
|
+
track: (event: string, properties?: Record<string, unknown>) => void;
|
|
34
|
+
identify: (id: string) => void;
|
|
35
|
+
people: {
|
|
36
|
+
set: (props: Record<string, unknown>) => void;
|
|
37
|
+
};
|
|
38
|
+
}): AnalyticsAdapter;
|
|
39
|
+
/**
|
|
40
|
+
* PostHog adapter.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* import posthog from "posthog-js";
|
|
45
|
+
* posthog.init("YOUR_KEY", { api_host: "https://app.posthog.com" });
|
|
46
|
+
*
|
|
47
|
+
* <AnalyticsProvider adapters={[posthogAdapter(posthog)]}>
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function posthogAdapter(posthog: {
|
|
51
|
+
capture: (event: string, properties?: Record<string, unknown>) => void;
|
|
52
|
+
identify: (id: string, properties?: Record<string, unknown>) => void;
|
|
53
|
+
}): AnalyticsAdapter;
|
|
54
|
+
/**
|
|
55
|
+
* Segment (Analytics.js) adapter.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```tsx
|
|
59
|
+
* <AnalyticsProvider adapters={[segmentAdapter(window.analytics)]}>
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
declare function segmentAdapter(analytics: {
|
|
63
|
+
track: (event: string, properties?: Record<string, unknown>) => void;
|
|
64
|
+
page: (name?: string, properties?: Record<string, unknown>) => void;
|
|
65
|
+
identify: (userId: string, traits?: Record<string, unknown>) => void;
|
|
66
|
+
}): AnalyticsAdapter;
|
|
67
|
+
/**
|
|
68
|
+
* Console adapter for development/debugging. Logs all events to console.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```tsx
|
|
72
|
+
* <AnalyticsProvider adapters={[consoleAdapter()]}>
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
declare function consoleAdapter(prefix?: string): AnalyticsAdapter;
|
|
76
|
+
|
|
77
|
+
export { type AnalyticsAdapter, AnalyticsProvider, type AnalyticsProviderProps, consoleAdapter, mixpanelAdapter, posthogAdapter, segmentAdapter, useIdentify, usePageView, useTrack };
|
package/dist/index.d.ts
CHANGED
|
@@ -18,4 +18,60 @@ declare function usePageView(name?: string, properties?: Record<string, unknown>
|
|
|
18
18
|
|
|
19
19
|
declare function useIdentify(): (userId: string, traits?: Record<string, unknown>) => void;
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Mixpanel adapter.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* import mixpanel from "mixpanel-browser";
|
|
27
|
+
* mixpanel.init("YOUR_TOKEN");
|
|
28
|
+
*
|
|
29
|
+
* <AnalyticsProvider adapters={[mixpanelAdapter(mixpanel)]}>
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
declare function mixpanelAdapter(mixpanel: {
|
|
33
|
+
track: (event: string, properties?: Record<string, unknown>) => void;
|
|
34
|
+
identify: (id: string) => void;
|
|
35
|
+
people: {
|
|
36
|
+
set: (props: Record<string, unknown>) => void;
|
|
37
|
+
};
|
|
38
|
+
}): AnalyticsAdapter;
|
|
39
|
+
/**
|
|
40
|
+
* PostHog adapter.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* import posthog from "posthog-js";
|
|
45
|
+
* posthog.init("YOUR_KEY", { api_host: "https://app.posthog.com" });
|
|
46
|
+
*
|
|
47
|
+
* <AnalyticsProvider adapters={[posthogAdapter(posthog)]}>
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function posthogAdapter(posthog: {
|
|
51
|
+
capture: (event: string, properties?: Record<string, unknown>) => void;
|
|
52
|
+
identify: (id: string, properties?: Record<string, unknown>) => void;
|
|
53
|
+
}): AnalyticsAdapter;
|
|
54
|
+
/**
|
|
55
|
+
* Segment (Analytics.js) adapter.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```tsx
|
|
59
|
+
* <AnalyticsProvider adapters={[segmentAdapter(window.analytics)]}>
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
declare function segmentAdapter(analytics: {
|
|
63
|
+
track: (event: string, properties?: Record<string, unknown>) => void;
|
|
64
|
+
page: (name?: string, properties?: Record<string, unknown>) => void;
|
|
65
|
+
identify: (userId: string, traits?: Record<string, unknown>) => void;
|
|
66
|
+
}): AnalyticsAdapter;
|
|
67
|
+
/**
|
|
68
|
+
* Console adapter for development/debugging. Logs all events to console.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```tsx
|
|
72
|
+
* <AnalyticsProvider adapters={[consoleAdapter()]}>
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
declare function consoleAdapter(prefix?: string): AnalyticsAdapter;
|
|
76
|
+
|
|
77
|
+
export { type AnalyticsAdapter, AnalyticsProvider, type AnalyticsProviderProps, consoleAdapter, mixpanelAdapter, posthogAdapter, segmentAdapter, useIdentify, usePageView, useTrack };
|
package/dist/index.js
CHANGED
|
@@ -22,6 +22,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
22
22
|
var index_exports = {};
|
|
23
23
|
__export(index_exports, {
|
|
24
24
|
AnalyticsProvider: () => AnalyticsProvider,
|
|
25
|
+
consoleAdapter: () => consoleAdapter,
|
|
26
|
+
mixpanelAdapter: () => mixpanelAdapter,
|
|
27
|
+
posthogAdapter: () => posthogAdapter,
|
|
28
|
+
segmentAdapter: () => segmentAdapter,
|
|
25
29
|
useIdentify: () => useIdentify,
|
|
26
30
|
usePageView: () => usePageView,
|
|
27
31
|
useTrack: () => useTrack
|
|
@@ -80,9 +84,46 @@ function useIdentify() {
|
|
|
80
84
|
);
|
|
81
85
|
return identify;
|
|
82
86
|
}
|
|
87
|
+
|
|
88
|
+
// src/adapters.ts
|
|
89
|
+
function mixpanelAdapter(mixpanel) {
|
|
90
|
+
return {
|
|
91
|
+
track: (event, properties) => mixpanel.track(event, properties),
|
|
92
|
+
page: (name, properties) => mixpanel.track("Page View", { page: name, ...properties }),
|
|
93
|
+
identify: (userId, traits) => {
|
|
94
|
+
mixpanel.identify(userId);
|
|
95
|
+
if (traits) mixpanel.people.set(traits);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function posthogAdapter(posthog) {
|
|
100
|
+
return {
|
|
101
|
+
track: (event, properties) => posthog.capture(event, properties),
|
|
102
|
+
page: (name, properties) => posthog.capture("$pageview", { page: name, ...properties }),
|
|
103
|
+
identify: (userId, traits) => posthog.identify(userId, traits)
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function segmentAdapter(analytics) {
|
|
107
|
+
return {
|
|
108
|
+
track: (event, properties) => analytics.track(event, properties),
|
|
109
|
+
page: (name, properties) => analytics.page(name, properties),
|
|
110
|
+
identify: (userId, traits) => analytics.identify(userId, traits)
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function consoleAdapter(prefix = "[analytics]") {
|
|
114
|
+
return {
|
|
115
|
+
track: (event, properties) => console.log(`${prefix} track:`, event, properties),
|
|
116
|
+
page: (name, properties) => console.log(`${prefix} page:`, name, properties),
|
|
117
|
+
identify: (userId, traits) => console.log(`${prefix} identify:`, userId, traits)
|
|
118
|
+
};
|
|
119
|
+
}
|
|
83
120
|
// Annotate the CommonJS export names for ESM import in node:
|
|
84
121
|
0 && (module.exports = {
|
|
85
122
|
AnalyticsProvider,
|
|
123
|
+
consoleAdapter,
|
|
124
|
+
mixpanelAdapter,
|
|
125
|
+
posthogAdapter,
|
|
126
|
+
segmentAdapter,
|
|
86
127
|
useIdentify,
|
|
87
128
|
usePageView,
|
|
88
129
|
useTrack
|
package/dist/index.mjs
CHANGED
|
@@ -52,8 +52,45 @@ function useIdentify() {
|
|
|
52
52
|
);
|
|
53
53
|
return identify;
|
|
54
54
|
}
|
|
55
|
+
|
|
56
|
+
// src/adapters.ts
|
|
57
|
+
function mixpanelAdapter(mixpanel) {
|
|
58
|
+
return {
|
|
59
|
+
track: (event, properties) => mixpanel.track(event, properties),
|
|
60
|
+
page: (name, properties) => mixpanel.track("Page View", { page: name, ...properties }),
|
|
61
|
+
identify: (userId, traits) => {
|
|
62
|
+
mixpanel.identify(userId);
|
|
63
|
+
if (traits) mixpanel.people.set(traits);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function posthogAdapter(posthog) {
|
|
68
|
+
return {
|
|
69
|
+
track: (event, properties) => posthog.capture(event, properties),
|
|
70
|
+
page: (name, properties) => posthog.capture("$pageview", { page: name, ...properties }),
|
|
71
|
+
identify: (userId, traits) => posthog.identify(userId, traits)
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function segmentAdapter(analytics) {
|
|
75
|
+
return {
|
|
76
|
+
track: (event, properties) => analytics.track(event, properties),
|
|
77
|
+
page: (name, properties) => analytics.page(name, properties),
|
|
78
|
+
identify: (userId, traits) => analytics.identify(userId, traits)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function consoleAdapter(prefix = "[analytics]") {
|
|
82
|
+
return {
|
|
83
|
+
track: (event, properties) => console.log(`${prefix} track:`, event, properties),
|
|
84
|
+
page: (name, properties) => console.log(`${prefix} page:`, name, properties),
|
|
85
|
+
identify: (userId, traits) => console.log(`${prefix} identify:`, userId, traits)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
55
88
|
export {
|
|
56
89
|
AnalyticsProvider,
|
|
90
|
+
consoleAdapter,
|
|
91
|
+
mixpanelAdapter,
|
|
92
|
+
posthogAdapter,
|
|
93
|
+
segmentAdapter,
|
|
57
94
|
useIdentify,
|
|
58
95
|
usePageView,
|
|
59
96
|
useTrack
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allem-sdk/analytics",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Provider-agnostic analytics hooks for React. Works with Mixpanel, Segment, PostHog, Amplitude, and any custom adapter. useTrack, usePageView, useIdentify.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Ahmed Allem",
|