@allem-sdk/analytics 0.1.0
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/LICENSE +21 -0
- package/README.md +80 -0
- package/dist/index.d.mts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +89 -0
- package/dist/index.mjs +60 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ahmed Allem
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/kingofmit/allem-sdk/main/.github/AllemSDK.png" alt="Allem SDK" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://github.com/kingofmit/allem-sdk/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License" /></a>
|
|
7
|
+
<img src="https://img.shields.io/badge/react-18+-61dafb" alt="React 18+" />
|
|
8
|
+
<img src="https://img.shields.io/badge/typescript-strict-blue" alt="TypeScript" />
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
# @allem-sdk/analytics
|
|
12
|
+
|
|
13
|
+
Provider-agnostic analytics hooks for React. Bring your own analytics provider (Mixpanel, Segment, PostHog, etc.) via a simple adapter interface. Supports multiple adapters simultaneously.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @allem-sdk/analytics
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import { AnalyticsProvider, useTrack, usePageView, useIdentify } from "@allem-sdk/analytics";
|
|
25
|
+
|
|
26
|
+
// Define adapters for your providers — use one or many
|
|
27
|
+
const mixpanelAdapter = {
|
|
28
|
+
track: (event, properties) => mixpanel.track(event, properties),
|
|
29
|
+
page: (name, properties) => mixpanel.track_pageview({ page: name, ...properties }),
|
|
30
|
+
identify: (userId, traits) => mixpanel.identify(userId),
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const posthogAdapter = {
|
|
34
|
+
track: (event, properties) => posthog.capture(event, properties),
|
|
35
|
+
page: (name, properties) => posthog.capture("$pageview", { page: name, ...properties }),
|
|
36
|
+
identify: (userId, traits) => posthog.identify(userId, traits),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function App() {
|
|
40
|
+
return (
|
|
41
|
+
<AnalyticsProvider adapters={[mixpanelAdapter, posthogAdapter]}>
|
|
42
|
+
<MyApp />
|
|
43
|
+
</AnalyticsProvider>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function ProductPage({ product }) {
|
|
48
|
+
const track = useTrack();
|
|
49
|
+
usePageView("Product Page", { productId: product.id });
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<button onClick={() => track("Add to Cart", { productId: product.id })}>
|
|
53
|
+
Add to Cart
|
|
54
|
+
</button>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Exports
|
|
60
|
+
|
|
61
|
+
| Export | Type | Description |
|
|
62
|
+
|--------|------|-------------|
|
|
63
|
+
| `AnalyticsProvider` | Component | Context provider accepting one or multiple adapters |
|
|
64
|
+
| `useTrack` | Hook | Returns a `track(event, properties)` function |
|
|
65
|
+
| `usePageView` | Hook | Tracks a page view on mount |
|
|
66
|
+
| `useIdentify` | Hook | Returns an `identify(userId, traits)` function |
|
|
67
|
+
|
|
68
|
+
## Part of [Allem SDK](https://github.com/kingofmit/allem-sdk)
|
|
69
|
+
|
|
70
|
+
This package can be used standalone or as part of the full SDK. Install `allem-sdk` to get all packages in one install.
|
|
71
|
+
|
|
72
|
+
## Support
|
|
73
|
+
|
|
74
|
+
If you find Allem SDK useful, consider supporting its development:
|
|
75
|
+
|
|
76
|
+
<a href="https://buymeacoffee.com/kingofmit" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" height="50" /></a>
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
[MIT](https://github.com/kingofmit/allem-sdk/blob/main/LICENSE) - [Ahmed Allem](https://kingallem.com)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
interface AnalyticsAdapter {
|
|
5
|
+
track: (event: string, properties?: Record<string, unknown>) => void;
|
|
6
|
+
page: (name?: string, properties?: Record<string, unknown>) => void;
|
|
7
|
+
identify: (userId: string, traits?: Record<string, unknown>) => void;
|
|
8
|
+
}
|
|
9
|
+
interface AnalyticsProviderProps {
|
|
10
|
+
adapters: AnalyticsAdapter[];
|
|
11
|
+
children: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
declare function AnalyticsProvider({ adapters, children }: AnalyticsProviderProps): react_jsx_runtime.JSX.Element;
|
|
14
|
+
|
|
15
|
+
declare function useTrack(): (event: string, properties?: Record<string, unknown>) => void;
|
|
16
|
+
|
|
17
|
+
declare function usePageView(name?: string, properties?: Record<string, unknown>): void;
|
|
18
|
+
|
|
19
|
+
declare function useIdentify(): (userId: string, traits?: Record<string, unknown>) => void;
|
|
20
|
+
|
|
21
|
+
export { type AnalyticsAdapter, AnalyticsProvider, type AnalyticsProviderProps, useIdentify, usePageView, useTrack };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
interface AnalyticsAdapter {
|
|
5
|
+
track: (event: string, properties?: Record<string, unknown>) => void;
|
|
6
|
+
page: (name?: string, properties?: Record<string, unknown>) => void;
|
|
7
|
+
identify: (userId: string, traits?: Record<string, unknown>) => void;
|
|
8
|
+
}
|
|
9
|
+
interface AnalyticsProviderProps {
|
|
10
|
+
adapters: AnalyticsAdapter[];
|
|
11
|
+
children: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
declare function AnalyticsProvider({ adapters, children }: AnalyticsProviderProps): react_jsx_runtime.JSX.Element;
|
|
14
|
+
|
|
15
|
+
declare function useTrack(): (event: string, properties?: Record<string, unknown>) => void;
|
|
16
|
+
|
|
17
|
+
declare function usePageView(name?: string, properties?: Record<string, unknown>): void;
|
|
18
|
+
|
|
19
|
+
declare function useIdentify(): (userId: string, traits?: Record<string, unknown>) => void;
|
|
20
|
+
|
|
21
|
+
export { type AnalyticsAdapter, AnalyticsProvider, type AnalyticsProviderProps, useIdentify, usePageView, useTrack };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
AnalyticsProvider: () => AnalyticsProvider,
|
|
25
|
+
useIdentify: () => useIdentify,
|
|
26
|
+
usePageView: () => usePageView,
|
|
27
|
+
useTrack: () => useTrack
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
|
|
31
|
+
// src/AnalyticsProvider.tsx
|
|
32
|
+
var import_react = require("react");
|
|
33
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
34
|
+
var AnalyticsContext = (0, import_react.createContext)({ adapters: [] });
|
|
35
|
+
function AnalyticsProvider({ adapters, children }) {
|
|
36
|
+
const value = (0, import_react.useRef)({ adapters }).current;
|
|
37
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AnalyticsContext.Provider, { value, children });
|
|
38
|
+
}
|
|
39
|
+
function useAnalyticsAdapters() {
|
|
40
|
+
return (0, import_react.useContext)(AnalyticsContext).adapters;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/useTrack.ts
|
|
44
|
+
var import_react2 = require("react");
|
|
45
|
+
function useTrack() {
|
|
46
|
+
const adapters = useAnalyticsAdapters();
|
|
47
|
+
const track = (0, import_react2.useCallback)(
|
|
48
|
+
(event, properties) => {
|
|
49
|
+
for (const adapter of adapters) {
|
|
50
|
+
adapter.track(event, properties);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
[adapters]
|
|
54
|
+
);
|
|
55
|
+
return track;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/usePageView.ts
|
|
59
|
+
var import_react3 = require("react");
|
|
60
|
+
function usePageView(name, properties) {
|
|
61
|
+
const adapters = useAnalyticsAdapters();
|
|
62
|
+
(0, import_react3.useEffect)(() => {
|
|
63
|
+
for (const adapter of adapters) {
|
|
64
|
+
adapter.page(name, properties);
|
|
65
|
+
}
|
|
66
|
+
}, [adapters, name, properties]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/useIdentify.ts
|
|
70
|
+
var import_react4 = require("react");
|
|
71
|
+
function useIdentify() {
|
|
72
|
+
const adapters = useAnalyticsAdapters();
|
|
73
|
+
const identify = (0, import_react4.useCallback)(
|
|
74
|
+
(userId, traits) => {
|
|
75
|
+
for (const adapter of adapters) {
|
|
76
|
+
adapter.identify(userId, traits);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
[adapters]
|
|
80
|
+
);
|
|
81
|
+
return identify;
|
|
82
|
+
}
|
|
83
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
84
|
+
0 && (module.exports = {
|
|
85
|
+
AnalyticsProvider,
|
|
86
|
+
useIdentify,
|
|
87
|
+
usePageView,
|
|
88
|
+
useTrack
|
|
89
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/AnalyticsProvider.tsx
|
|
4
|
+
import { createContext, useContext, useRef } from "react";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
var AnalyticsContext = createContext({ adapters: [] });
|
|
7
|
+
function AnalyticsProvider({ adapters, children }) {
|
|
8
|
+
const value = useRef({ adapters }).current;
|
|
9
|
+
return /* @__PURE__ */ jsx(AnalyticsContext.Provider, { value, children });
|
|
10
|
+
}
|
|
11
|
+
function useAnalyticsAdapters() {
|
|
12
|
+
return useContext(AnalyticsContext).adapters;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/useTrack.ts
|
|
16
|
+
import { useCallback as useCallback2 } from "react";
|
|
17
|
+
function useTrack() {
|
|
18
|
+
const adapters = useAnalyticsAdapters();
|
|
19
|
+
const track = useCallback2(
|
|
20
|
+
(event, properties) => {
|
|
21
|
+
for (const adapter of adapters) {
|
|
22
|
+
adapter.track(event, properties);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
[adapters]
|
|
26
|
+
);
|
|
27
|
+
return track;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/usePageView.ts
|
|
31
|
+
import { useEffect } from "react";
|
|
32
|
+
function usePageView(name, properties) {
|
|
33
|
+
const adapters = useAnalyticsAdapters();
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
for (const adapter of adapters) {
|
|
36
|
+
adapter.page(name, properties);
|
|
37
|
+
}
|
|
38
|
+
}, [adapters, name, properties]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/useIdentify.ts
|
|
42
|
+
import { useCallback as useCallback3 } from "react";
|
|
43
|
+
function useIdentify() {
|
|
44
|
+
const adapters = useAnalyticsAdapters();
|
|
45
|
+
const identify = useCallback3(
|
|
46
|
+
(userId, traits) => {
|
|
47
|
+
for (const adapter of adapters) {
|
|
48
|
+
adapter.identify(userId, traits);
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
[adapters]
|
|
52
|
+
);
|
|
53
|
+
return identify;
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
AnalyticsProvider,
|
|
57
|
+
useIdentify,
|
|
58
|
+
usePageView,
|
|
59
|
+
useTrack
|
|
60
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@allem-sdk/analytics",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Provider-agnostic analytics hooks for React",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Ahmed Allem",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.mjs",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"require": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@testing-library/react": "^16.0.0",
|
|
23
|
+
"@types/react": "^19.0.0",
|
|
24
|
+
"jsdom": "^25.0.0",
|
|
25
|
+
"react": "^19.0.0",
|
|
26
|
+
"react-dom": "^19.0.0",
|
|
27
|
+
"tsup": "^8.4.0",
|
|
28
|
+
"typescript": "^5.8.0",
|
|
29
|
+
"vitest": "^3.0.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": ">=18.0.0"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/kingofmit/allem-sdk.git",
|
|
37
|
+
"directory": "packages/analytics"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"allem-sdk",
|
|
41
|
+
"react",
|
|
42
|
+
"hooks",
|
|
43
|
+
"analytics",
|
|
44
|
+
"tracking",
|
|
45
|
+
"mixpanel",
|
|
46
|
+
"segment"
|
|
47
|
+
],
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsup",
|
|
53
|
+
"dev": "tsup --watch",
|
|
54
|
+
"clean": "rm -rf dist",
|
|
55
|
+
"test": "vitest run"
|
|
56
|
+
}
|
|
57
|
+
}
|