@wrelik/analytics 0.2.1 → 2.0.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/.turbo/turbo-build.log +20 -10
- package/CHANGELOG.md +17 -0
- package/dist/{chunk-ZASOR6XP.mjs → chunk-HZPXP2CE.mjs} +2 -2
- package/dist/client/index.d.mts +11 -0
- package/dist/client/index.d.ts +11 -0
- package/dist/client/index.js +51 -0
- package/dist/client/index.mjs +19 -0
- package/dist/server/index.d.mts +28 -0
- package/dist/server/index.d.ts +28 -0
- package/dist/server/index.js +95 -0
- package/dist/server/index.mjs +59 -0
- package/dist/shared/index.d.mts +3 -0
- package/dist/shared/index.d.ts +3 -0
- package/dist/shared/index.js +36 -0
- package/dist/shared/index.mjs +6 -0
- package/package.json +28 -19
- package/src/client/index.ts +25 -0
- package/src/server/index.test.ts +40 -0
- package/src/server/index.ts +83 -0
- package/src/shared/index.test.ts +12 -0
- package/src/{shared.ts → shared/index.ts} +2 -2
- package/tsconfig.json +3 -1
- package/dist/index.js +0 -78
- package/dist/index.mjs +0 -43
- package/dist/react-native.js +0 -73
- package/dist/react-native.mjs +0 -28
- package/src/index.ts +0 -51
- package/src/react-native.ts +0 -36
- package/src/shared.test.ts +0 -14
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
|
|
2
|
-
> @wrelik/analytics@0.
|
|
3
|
-
> tsup src/index.ts src/
|
|
2
|
+
> @wrelik/analytics@2.0.0 build /home/runner/work/wrelik-kit/wrelik-kit/packages/analytics
|
|
3
|
+
> tsup src/server/index.ts src/client/index.ts src/shared/index.ts --format cjs,esm --dts --clean
|
|
4
4
|
|
|
5
|
-
[34mCLI[39m Building entry: src/index.ts, src/
|
|
5
|
+
[34mCLI[39m Building entry: src/client/index.ts, src/server/index.ts, src/shared/index.ts
|
|
6
6
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
7
|
[34mCLI[39m tsup v8.5.1
|
|
8
8
|
[34mCLI[39m Target: es2022
|
|
9
9
|
[34mCLI[39m Cleaning output folder
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
11
|
[34mESM[39m Build start
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
[
|
|
16
|
-
[
|
|
17
|
-
[
|
|
18
|
-
[
|
|
12
|
+
[32mCJS[39m [1mdist/client/index.js [22m[32m1.63 KB[39m
|
|
13
|
+
[32mCJS[39m [1mdist/server/index.js [22m[32m2.85 KB[39m
|
|
14
|
+
[32mCJS[39m [1mdist/shared/index.js [22m[32m1.32 KB[39m
|
|
15
|
+
[32mCJS[39m ⚡️ Build success in 63ms
|
|
16
|
+
[32mESM[39m [1mdist/chunk-HZPXP2CE.mjs [22m[32m310.00 B[39m
|
|
17
|
+
[32mESM[39m [1mdist/server/index.mjs [22m[32m1.45 KB[39m
|
|
18
|
+
[32mESM[39m [1mdist/shared/index.mjs [22m[32m93.00 B[39m
|
|
19
|
+
[32mESM[39m [1mdist/client/index.mjs [22m[32m381.00 B[39m
|
|
20
|
+
[32mESM[39m ⚡️ Build success in 64ms
|
|
21
|
+
[34mDTS[39m Build start
|
|
22
|
+
[32mDTS[39m ⚡️ Build success in 6124ms
|
|
23
|
+
[32mDTS[39m [1mdist/client/index.d.ts [22m[32m545.00 B[39m
|
|
24
|
+
[32mDTS[39m [1mdist/server/index.d.ts [22m[32m1.20 KB[39m
|
|
25
|
+
[32mDTS[39m [1mdist/shared/index.d.ts [22m[32m88.00 B[39m
|
|
26
|
+
[32mDTS[39m [1mdist/client/index.d.mts [22m[32m545.00 B[39m
|
|
27
|
+
[32mDTS[39m [1mdist/server/index.d.mts [22m[32m1.20 KB[39m
|
|
28
|
+
[32mDTS[39m [1mdist/shared/index.d.mts [22m[32m88.00 B[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @wrelik/analytics
|
|
2
2
|
|
|
3
|
+
## 2.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 818ed58: Refactor runtime packages to strict subpath exports (`/server`, `/client`, `/shared`) with side-effect free entrypoints and hard CI/runtime boundary enforcement.
|
|
8
|
+
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- Add platform wrapper packages for Clerk (Next/Expo), Next Sentry client instrumentation, and Upstash server SDK access so apps can avoid direct vendor imports while staying inside the `@wrelik/*` boundary.
|
|
12
|
+
|
|
13
|
+
Add temporary deprecated server-side compatibility singleton exports to analytics, email, jobs, and storage to support DRX migration from root-import convenience APIs to runtime subpaths in one cutover.
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies [818ed58]
|
|
18
|
+
- @wrelik/errors@2.0.0
|
|
19
|
+
|
|
3
20
|
## 0.2.1
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface AnalyticsClient {
|
|
2
|
+
identify: (userId: string, traits?: Record<string, unknown>) => void;
|
|
3
|
+
capture: (event: string, properties?: Record<string, unknown>) => void;
|
|
4
|
+
}
|
|
5
|
+
interface AnalyticsClientAdapter {
|
|
6
|
+
identify?: (userId: string, traits?: Record<string, unknown>) => void;
|
|
7
|
+
capture?: (event: string, properties?: Record<string, unknown>) => void;
|
|
8
|
+
}
|
|
9
|
+
declare function createAnalyticsClient(adapter: AnalyticsClientAdapter): AnalyticsClient;
|
|
10
|
+
|
|
11
|
+
export { type AnalyticsClient, type AnalyticsClientAdapter, createAnalyticsClient };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface AnalyticsClient {
|
|
2
|
+
identify: (userId: string, traits?: Record<string, unknown>) => void;
|
|
3
|
+
capture: (event: string, properties?: Record<string, unknown>) => void;
|
|
4
|
+
}
|
|
5
|
+
interface AnalyticsClientAdapter {
|
|
6
|
+
identify?: (userId: string, traits?: Record<string, unknown>) => void;
|
|
7
|
+
capture?: (event: string, properties?: Record<string, unknown>) => void;
|
|
8
|
+
}
|
|
9
|
+
declare function createAnalyticsClient(adapter: AnalyticsClientAdapter): AnalyticsClient;
|
|
10
|
+
|
|
11
|
+
export { type AnalyticsClient, type AnalyticsClientAdapter, createAnalyticsClient };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/client/index.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
createAnalyticsClient: () => createAnalyticsClient
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(client_exports);
|
|
26
|
+
|
|
27
|
+
// src/shared/index.ts
|
|
28
|
+
var import_shared = require("@wrelik/errors/shared");
|
|
29
|
+
function validateEventName(event) {
|
|
30
|
+
const parts = event.split(".");
|
|
31
|
+
if (parts.length < 3) {
|
|
32
|
+
throw new import_shared.ValidationError(`Event name "${event}" must follow "app.action.object" format`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/client/index.ts
|
|
37
|
+
function createAnalyticsClient(adapter) {
|
|
38
|
+
return {
|
|
39
|
+
identify(userId, traits) {
|
|
40
|
+
adapter.identify?.(userId, traits);
|
|
41
|
+
},
|
|
42
|
+
capture(event, properties = {}) {
|
|
43
|
+
validateEventName(event);
|
|
44
|
+
adapter.capture?.(event, properties);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
49
|
+
0 && (module.exports = {
|
|
50
|
+
createAnalyticsClient
|
|
51
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
validateEventName
|
|
3
|
+
} from "../chunk-HZPXP2CE.mjs";
|
|
4
|
+
|
|
5
|
+
// src/client/index.ts
|
|
6
|
+
function createAnalyticsClient(adapter) {
|
|
7
|
+
return {
|
|
8
|
+
identify(userId, traits) {
|
|
9
|
+
adapter.identify?.(userId, traits);
|
|
10
|
+
},
|
|
11
|
+
capture(event, properties = {}) {
|
|
12
|
+
validateEventName(event);
|
|
13
|
+
adapter.capture?.(event, properties);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
createAnalyticsClient
|
|
19
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
interface AnalyticsServer {
|
|
2
|
+
identify: (userId: string, traits: Record<string, unknown>) => void;
|
|
3
|
+
capture: (event: string, properties?: Record<string, unknown>, options?: {
|
|
4
|
+
userId?: string;
|
|
5
|
+
}) => void;
|
|
6
|
+
shutdown: () => void | Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
declare function createAnalyticsServer(apiKey: string, host?: string): AnalyticsServer;
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...) and explicit dependency wiring.
|
|
11
|
+
*/
|
|
12
|
+
declare function initAnalytics(apiKey: string, host?: string): void;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...).identify(...)
|
|
15
|
+
*/
|
|
16
|
+
declare function identify(userId: string, traits: Record<string, unknown>): void;
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...).capture(...)
|
|
19
|
+
*/
|
|
20
|
+
declare function capture(event: string, properties?: Record<string, unknown>, options?: {
|
|
21
|
+
userId?: string;
|
|
22
|
+
}): void;
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...).shutdown()
|
|
25
|
+
*/
|
|
26
|
+
declare function shutdown(): Promise<void>;
|
|
27
|
+
|
|
28
|
+
export { type AnalyticsServer, capture, createAnalyticsServer, identify, initAnalytics, shutdown };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
interface AnalyticsServer {
|
|
2
|
+
identify: (userId: string, traits: Record<string, unknown>) => void;
|
|
3
|
+
capture: (event: string, properties?: Record<string, unknown>, options?: {
|
|
4
|
+
userId?: string;
|
|
5
|
+
}) => void;
|
|
6
|
+
shutdown: () => void | Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
declare function createAnalyticsServer(apiKey: string, host?: string): AnalyticsServer;
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...) and explicit dependency wiring.
|
|
11
|
+
*/
|
|
12
|
+
declare function initAnalytics(apiKey: string, host?: string): void;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...).identify(...)
|
|
15
|
+
*/
|
|
16
|
+
declare function identify(userId: string, traits: Record<string, unknown>): void;
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...).capture(...)
|
|
19
|
+
*/
|
|
20
|
+
declare function capture(event: string, properties?: Record<string, unknown>, options?: {
|
|
21
|
+
userId?: string;
|
|
22
|
+
}): void;
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...).shutdown()
|
|
25
|
+
*/
|
|
26
|
+
declare function shutdown(): Promise<void>;
|
|
27
|
+
|
|
28
|
+
export { type AnalyticsServer, capture, createAnalyticsServer, identify, initAnalytics, shutdown };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/server/index.ts
|
|
21
|
+
var server_exports = {};
|
|
22
|
+
__export(server_exports, {
|
|
23
|
+
capture: () => capture,
|
|
24
|
+
createAnalyticsServer: () => createAnalyticsServer,
|
|
25
|
+
identify: () => identify,
|
|
26
|
+
initAnalytics: () => initAnalytics,
|
|
27
|
+
shutdown: () => shutdown
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(server_exports);
|
|
30
|
+
var import_posthog_node = require("posthog-node");
|
|
31
|
+
|
|
32
|
+
// src/shared/index.ts
|
|
33
|
+
var import_shared = require("@wrelik/errors/shared");
|
|
34
|
+
function validateEventName(event) {
|
|
35
|
+
const parts = event.split(".");
|
|
36
|
+
if (parts.length < 3) {
|
|
37
|
+
throw new import_shared.ValidationError(`Event name "${event}" must follow "app.action.object" format`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/server/index.ts
|
|
42
|
+
function createAnalyticsServer(apiKey, host = "https://app.posthog.com") {
|
|
43
|
+
const client = new import_posthog_node.PostHog(apiKey, { host });
|
|
44
|
+
return {
|
|
45
|
+
identify(userId, traits) {
|
|
46
|
+
client.identify({
|
|
47
|
+
distinctId: userId,
|
|
48
|
+
properties: traits
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
capture(event, properties = {}, options) {
|
|
52
|
+
validateEventName(event);
|
|
53
|
+
if (!options?.userId) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
client.capture({
|
|
57
|
+
distinctId: options.userId,
|
|
58
|
+
event,
|
|
59
|
+
properties
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
shutdown() {
|
|
63
|
+
return client.shutdown();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
var analyticsSingleton = null;
|
|
68
|
+
function requireAnalyticsSingleton() {
|
|
69
|
+
if (!analyticsSingleton) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
"@wrelik/analytics/server compatibility API is not initialized. Call initAnalytics(apiKey, host) before identify/capture/shutdown."
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return analyticsSingleton;
|
|
75
|
+
}
|
|
76
|
+
function initAnalytics(apiKey, host) {
|
|
77
|
+
analyticsSingleton = createAnalyticsServer(apiKey, host);
|
|
78
|
+
}
|
|
79
|
+
function identify(userId, traits) {
|
|
80
|
+
requireAnalyticsSingleton().identify(userId, traits);
|
|
81
|
+
}
|
|
82
|
+
function capture(event, properties, options) {
|
|
83
|
+
requireAnalyticsSingleton().capture(event, properties, options);
|
|
84
|
+
}
|
|
85
|
+
async function shutdown() {
|
|
86
|
+
await Promise.resolve(requireAnalyticsSingleton().shutdown());
|
|
87
|
+
}
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
capture,
|
|
91
|
+
createAnalyticsServer,
|
|
92
|
+
identify,
|
|
93
|
+
initAnalytics,
|
|
94
|
+
shutdown
|
|
95
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
validateEventName
|
|
3
|
+
} from "../chunk-HZPXP2CE.mjs";
|
|
4
|
+
|
|
5
|
+
// src/server/index.ts
|
|
6
|
+
import { PostHog } from "posthog-node";
|
|
7
|
+
function createAnalyticsServer(apiKey, host = "https://app.posthog.com") {
|
|
8
|
+
const client = new PostHog(apiKey, { host });
|
|
9
|
+
return {
|
|
10
|
+
identify(userId, traits) {
|
|
11
|
+
client.identify({
|
|
12
|
+
distinctId: userId,
|
|
13
|
+
properties: traits
|
|
14
|
+
});
|
|
15
|
+
},
|
|
16
|
+
capture(event, properties = {}, options) {
|
|
17
|
+
validateEventName(event);
|
|
18
|
+
if (!options?.userId) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
client.capture({
|
|
22
|
+
distinctId: options.userId,
|
|
23
|
+
event,
|
|
24
|
+
properties
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
shutdown() {
|
|
28
|
+
return client.shutdown();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
var analyticsSingleton = null;
|
|
33
|
+
function requireAnalyticsSingleton() {
|
|
34
|
+
if (!analyticsSingleton) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
"@wrelik/analytics/server compatibility API is not initialized. Call initAnalytics(apiKey, host) before identify/capture/shutdown."
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return analyticsSingleton;
|
|
40
|
+
}
|
|
41
|
+
function initAnalytics(apiKey, host) {
|
|
42
|
+
analyticsSingleton = createAnalyticsServer(apiKey, host);
|
|
43
|
+
}
|
|
44
|
+
function identify(userId, traits) {
|
|
45
|
+
requireAnalyticsSingleton().identify(userId, traits);
|
|
46
|
+
}
|
|
47
|
+
function capture(event, properties, options) {
|
|
48
|
+
requireAnalyticsSingleton().capture(event, properties, options);
|
|
49
|
+
}
|
|
50
|
+
async function shutdown() {
|
|
51
|
+
await Promise.resolve(requireAnalyticsSingleton().shutdown());
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
capture,
|
|
55
|
+
createAnalyticsServer,
|
|
56
|
+
identify,
|
|
57
|
+
initAnalytics,
|
|
58
|
+
shutdown
|
|
59
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/shared/index.ts
|
|
21
|
+
var shared_exports = {};
|
|
22
|
+
__export(shared_exports, {
|
|
23
|
+
validateEventName: () => validateEventName
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(shared_exports);
|
|
26
|
+
var import_shared = require("@wrelik/errors/shared");
|
|
27
|
+
function validateEventName(event) {
|
|
28
|
+
const parts = event.split(".");
|
|
29
|
+
if (parts.length < 3) {
|
|
30
|
+
throw new import_shared.ValidationError(`Event name "${event}" must follow "app.action.object" format`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
+
0 && (module.exports = {
|
|
35
|
+
validateEventName
|
|
36
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wrelik/analytics",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -9,38 +9,47 @@
|
|
|
9
9
|
"url": "https://github.com/lwhite702/wrelik-kit.git",
|
|
10
10
|
"directory": "packages/analytics"
|
|
11
11
|
},
|
|
12
|
-
"
|
|
13
|
-
"types": "./dist/index.d.ts",
|
|
12
|
+
"sideEffects": false,
|
|
14
13
|
"exports": {
|
|
15
|
-
"
|
|
16
|
-
|
|
14
|
+
"./server": {
|
|
15
|
+
"types": "./dist/server/index.d.ts",
|
|
16
|
+
"import": "./dist/server/index.mjs",
|
|
17
|
+
"require": "./dist/server/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./client": {
|
|
20
|
+
"types": "./dist/client/index.d.ts",
|
|
21
|
+
"import": "./dist/client/index.mjs",
|
|
22
|
+
"require": "./dist/client/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./shared": {
|
|
25
|
+
"types": "./dist/shared/index.d.ts",
|
|
26
|
+
"import": "./dist/shared/index.mjs",
|
|
27
|
+
"require": "./dist/shared/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
17
30
|
},
|
|
18
31
|
"dependencies": {
|
|
19
32
|
"posthog-node": "^3.6.3",
|
|
20
|
-
"
|
|
21
|
-
"@wrelik/errors": "0.2.1"
|
|
33
|
+
"@wrelik/errors": "2.0.0"
|
|
22
34
|
},
|
|
23
35
|
"devDependencies": {
|
|
24
36
|
"@types/node": "^25.2.0",
|
|
25
|
-
"@types/react": "^19.2.10",
|
|
26
|
-
"@types/react-native": "^0.73.0",
|
|
27
|
-
"auth": "link:packages/auth",
|
|
28
|
-
"config": "link:packages/config",
|
|
29
|
-
"db": "link:packages/db",
|
|
30
|
-
"email": "link:packages/email",
|
|
31
|
-
"errors": "link:packages/errors",
|
|
32
|
-
"eslint-config": "link:packages/eslint-config",
|
|
33
|
-
"jobs": "link:packages/jobs",
|
|
34
|
-
"storage": "link:packages/storage",
|
|
35
|
-
"tsconfig": "link:packages/tsconfig",
|
|
36
37
|
"tsup": "^8.0.1",
|
|
37
38
|
"vitest": "^1.2.2",
|
|
38
39
|
"@wrelik/eslint-config": "0.1.1",
|
|
39
40
|
"@wrelik/tsconfig": "0.1.1"
|
|
40
41
|
},
|
|
42
|
+
"wrelik": {
|
|
43
|
+
"runtimes": [
|
|
44
|
+
"server",
|
|
45
|
+
"client",
|
|
46
|
+
"shared"
|
|
47
|
+
]
|
|
48
|
+
},
|
|
41
49
|
"scripts": {
|
|
42
|
-
"build": "tsup src/index.ts src/
|
|
50
|
+
"build": "tsup src/server/index.ts src/client/index.ts src/shared/index.ts --format cjs,esm --dts --clean",
|
|
43
51
|
"lint": "eslint src/",
|
|
52
|
+
"typecheck": "tsc --noEmit",
|
|
44
53
|
"test": "vitest run --passWithNoTests"
|
|
45
54
|
}
|
|
46
55
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { validateEventName } from '../shared';
|
|
2
|
+
|
|
3
|
+
export interface AnalyticsClient {
|
|
4
|
+
identify: (userId: string, traits?: Record<string, unknown>) => void;
|
|
5
|
+
capture: (event: string, properties?: Record<string, unknown>) => void;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface AnalyticsClientAdapter {
|
|
9
|
+
identify?: (userId: string, traits?: Record<string, unknown>) => void;
|
|
10
|
+
capture?: (event: string, properties?: Record<string, unknown>) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createAnalyticsClient(adapter: AnalyticsClientAdapter): AnalyticsClient {
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
identify(userId, traits) {
|
|
17
|
+
adapter.identify?.(userId, traits);
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
capture(event, properties = {}) {
|
|
21
|
+
validateEventName(event);
|
|
22
|
+
adapter.capture?.(event, properties);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
afterEach(() => {
|
|
4
|
+
vi.resetModules();
|
|
5
|
+
vi.clearAllMocks();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
describe('@wrelik/analytics/server', () => {
|
|
9
|
+
it('compatibility singleton throws before initAnalytics', async () => {
|
|
10
|
+
const mod = await import('./index.js');
|
|
11
|
+
expect(() => mod.capture('drx.user.created', { plan: 'pro' }, { userId: 'u1' })).toThrow(/initAnalytics/i);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('compatibility singleton delegates to the PostHog-backed server after initAnalytics', async () => {
|
|
15
|
+
const identify = vi.fn();
|
|
16
|
+
const capture = vi.fn();
|
|
17
|
+
const shutdown = vi.fn(async () => undefined);
|
|
18
|
+
const PostHog = vi.fn(() => ({ identify, capture, shutdown }));
|
|
19
|
+
|
|
20
|
+
vi.doMock('posthog-node', () => ({ PostHog }));
|
|
21
|
+
|
|
22
|
+
const mod = await import('./index.js');
|
|
23
|
+
mod.initAnalytics('ph_test', 'https://posthog.example');
|
|
24
|
+
mod.identify('u1', { role: 'admin' });
|
|
25
|
+
mod.capture('drx.user.created', { source: 'web' }, { userId: 'u1' });
|
|
26
|
+
await mod.shutdown();
|
|
27
|
+
|
|
28
|
+
expect(PostHog).toHaveBeenCalledWith('ph_test', { host: 'https://posthog.example' });
|
|
29
|
+
expect(identify).toHaveBeenCalledWith({
|
|
30
|
+
distinctId: 'u1',
|
|
31
|
+
properties: { role: 'admin' },
|
|
32
|
+
});
|
|
33
|
+
expect(capture).toHaveBeenCalledWith({
|
|
34
|
+
distinctId: 'u1',
|
|
35
|
+
event: 'drx.user.created',
|
|
36
|
+
properties: { source: 'web' },
|
|
37
|
+
});
|
|
38
|
+
expect(shutdown).toHaveBeenCalledTimes(1);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-imports */
|
|
2
|
+
import { PostHog } from 'posthog-node';
|
|
3
|
+
import { validateEventName } from '../shared';
|
|
4
|
+
|
|
5
|
+
export interface AnalyticsServer {
|
|
6
|
+
identify: (userId: string, traits: Record<string, unknown>) => void;
|
|
7
|
+
capture: (event: string, properties?: Record<string, unknown>, options?: { userId?: string }) => void;
|
|
8
|
+
shutdown: () => void | Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function createAnalyticsServer(apiKey: string, host = 'https://app.posthog.com'): AnalyticsServer {
|
|
12
|
+
const client = new PostHog(apiKey, { host });
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
identify(userId, traits) {
|
|
16
|
+
client.identify({
|
|
17
|
+
distinctId: userId,
|
|
18
|
+
properties: traits,
|
|
19
|
+
});
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
capture(event, properties = {}, options) {
|
|
23
|
+
validateEventName(event);
|
|
24
|
+
if (!options?.userId) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
client.capture({
|
|
29
|
+
distinctId: options.userId,
|
|
30
|
+
event,
|
|
31
|
+
properties,
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
shutdown() {
|
|
36
|
+
return client.shutdown();
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let analyticsSingleton: AnalyticsServer | null = null;
|
|
42
|
+
|
|
43
|
+
function requireAnalyticsSingleton(): AnalyticsServer {
|
|
44
|
+
if (!analyticsSingleton) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
'@wrelik/analytics/server compatibility API is not initialized. Call initAnalytics(apiKey, host) before identify/capture/shutdown.',
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return analyticsSingleton;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...) and explicit dependency wiring.
|
|
55
|
+
*/
|
|
56
|
+
export function initAnalytics(apiKey: string, host?: string): void {
|
|
57
|
+
analyticsSingleton = createAnalyticsServer(apiKey, host);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...).identify(...)
|
|
62
|
+
*/
|
|
63
|
+
export function identify(userId: string, traits: Record<string, unknown>): void {
|
|
64
|
+
requireAnalyticsSingleton().identify(userId, traits);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...).capture(...)
|
|
69
|
+
*/
|
|
70
|
+
export function capture(
|
|
71
|
+
event: string,
|
|
72
|
+
properties?: Record<string, unknown>,
|
|
73
|
+
options?: { userId?: string },
|
|
74
|
+
): void {
|
|
75
|
+
requireAnalyticsSingleton().capture(event, properties, options);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @deprecated Temporary DRX compatibility singleton. Prefer createAnalyticsServer(...).shutdown()
|
|
80
|
+
*/
|
|
81
|
+
export async function shutdown(): Promise<void> {
|
|
82
|
+
await Promise.resolve(requireAnalyticsSingleton().shutdown());
|
|
83
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { validateEventName } from './index';
|
|
3
|
+
|
|
4
|
+
describe('@wrelik/analytics/shared', () => {
|
|
5
|
+
it('accepts valid event names', () => {
|
|
6
|
+
expect(() => validateEventName('app.page.view')).not.toThrow();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('rejects invalid event names', () => {
|
|
10
|
+
expect(() => validateEventName('invalid')).toThrow(/must follow/);
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ValidationError } from '@wrelik/errors';
|
|
1
|
+
import { ValidationError } from '@wrelik/errors/shared';
|
|
2
2
|
|
|
3
|
-
export function validateEventName(event: string) {
|
|
3
|
+
export function validateEventName(event: string): void {
|
|
4
4
|
const parts = event.split('.');
|
|
5
5
|
if (parts.length < 3) {
|
|
6
6
|
throw new ValidationError(`Event name "${event}" must follow "app.action.object" format`);
|
package/tsconfig.json
CHANGED
package/dist/index.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
capture: () => capture,
|
|
24
|
-
identify: () => identify,
|
|
25
|
-
initAnalytics: () => initAnalytics,
|
|
26
|
-
shutdown: () => shutdown
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(index_exports);
|
|
29
|
-
var import_posthog_node = require("posthog-node");
|
|
30
|
-
|
|
31
|
-
// src/shared.ts
|
|
32
|
-
var import_errors = require("@wrelik/errors");
|
|
33
|
-
function validateEventName(event) {
|
|
34
|
-
const parts = event.split(".");
|
|
35
|
-
if (parts.length < 3) {
|
|
36
|
-
throw new import_errors.ValidationError(`Event name "${event}" must follow "app.action.object" format`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// src/index.ts
|
|
41
|
-
var client = null;
|
|
42
|
-
function initAnalytics(apiKey, host = "https://app.posthog.com") {
|
|
43
|
-
client = new import_posthog_node.PostHog(apiKey, { host });
|
|
44
|
-
}
|
|
45
|
-
function getClient(safe = false) {
|
|
46
|
-
if (!client && !safe) {
|
|
47
|
-
throw new Error("Analytics not initialized. Call initAnalytics first.");
|
|
48
|
-
}
|
|
49
|
-
return client;
|
|
50
|
-
}
|
|
51
|
-
function identify(userId, traits) {
|
|
52
|
-
getClient(true)?.identify({
|
|
53
|
-
distinctId: userId,
|
|
54
|
-
properties: traits
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
function capture(event, properties = {}, options) {
|
|
58
|
-
validateEventName(event);
|
|
59
|
-
if (!options?.userId) {
|
|
60
|
-
console.warn("Analytics capture called without userId");
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
getClient(true)?.capture({
|
|
64
|
-
distinctId: options.userId,
|
|
65
|
-
event,
|
|
66
|
-
properties
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
async function shutdown() {
|
|
70
|
-
await getClient(true)?.shutdown();
|
|
71
|
-
}
|
|
72
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
73
|
-
0 && (module.exports = {
|
|
74
|
-
capture,
|
|
75
|
-
identify,
|
|
76
|
-
initAnalytics,
|
|
77
|
-
shutdown
|
|
78
|
-
});
|
package/dist/index.mjs
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
validateEventName
|
|
3
|
-
} from "./chunk-ZASOR6XP.mjs";
|
|
4
|
-
|
|
5
|
-
// src/index.ts
|
|
6
|
-
import { PostHog } from "posthog-node";
|
|
7
|
-
var client = null;
|
|
8
|
-
function initAnalytics(apiKey, host = "https://app.posthog.com") {
|
|
9
|
-
client = new PostHog(apiKey, { host });
|
|
10
|
-
}
|
|
11
|
-
function getClient(safe = false) {
|
|
12
|
-
if (!client && !safe) {
|
|
13
|
-
throw new Error("Analytics not initialized. Call initAnalytics first.");
|
|
14
|
-
}
|
|
15
|
-
return client;
|
|
16
|
-
}
|
|
17
|
-
function identify(userId, traits) {
|
|
18
|
-
getClient(true)?.identify({
|
|
19
|
-
distinctId: userId,
|
|
20
|
-
properties: traits
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
function capture(event, properties = {}, options) {
|
|
24
|
-
validateEventName(event);
|
|
25
|
-
if (!options?.userId) {
|
|
26
|
-
console.warn("Analytics capture called without userId");
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
getClient(true)?.capture({
|
|
30
|
-
distinctId: options.userId,
|
|
31
|
-
event,
|
|
32
|
-
properties
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
async function shutdown() {
|
|
36
|
-
await getClient(true)?.shutdown();
|
|
37
|
-
}
|
|
38
|
-
export {
|
|
39
|
-
capture,
|
|
40
|
-
identify,
|
|
41
|
-
initAnalytics,
|
|
42
|
-
shutdown
|
|
43
|
-
};
|
package/dist/react-native.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/react-native.ts
|
|
31
|
-
var react_native_exports = {};
|
|
32
|
-
__export(react_native_exports, {
|
|
33
|
-
capture: () => capture,
|
|
34
|
-
identify: () => identify,
|
|
35
|
-
initAnalytics: () => initAnalytics,
|
|
36
|
-
shutdown: () => shutdown
|
|
37
|
-
});
|
|
38
|
-
module.exports = __toCommonJS(react_native_exports);
|
|
39
|
-
var import_posthog_react_native = __toESM(require("posthog-react-native"));
|
|
40
|
-
|
|
41
|
-
// src/shared.ts
|
|
42
|
-
var import_errors = require("@wrelik/errors");
|
|
43
|
-
function validateEventName(event) {
|
|
44
|
-
const parts = event.split(".");
|
|
45
|
-
if (parts.length < 3) {
|
|
46
|
-
throw new import_errors.ValidationError(`Event name "${event}" must follow "app.action.object" format`);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// src/react-native.ts
|
|
51
|
-
function initAnalytics(apiKey, host = "https://app.posthog.com") {
|
|
52
|
-
import_posthog_react_native.default.initAsync(apiKey, {
|
|
53
|
-
host
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
function identify(userId, traits) {
|
|
57
|
-
import_posthog_react_native.default.identify(userId, traits);
|
|
58
|
-
}
|
|
59
|
-
function capture(event, properties = {}, options) {
|
|
60
|
-
validateEventName(event);
|
|
61
|
-
if (options?.userId) {
|
|
62
|
-
}
|
|
63
|
-
import_posthog_react_native.default.capture(event, properties);
|
|
64
|
-
}
|
|
65
|
-
async function shutdown() {
|
|
66
|
-
}
|
|
67
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
68
|
-
0 && (module.exports = {
|
|
69
|
-
capture,
|
|
70
|
-
identify,
|
|
71
|
-
initAnalytics,
|
|
72
|
-
shutdown
|
|
73
|
-
});
|
package/dist/react-native.mjs
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
validateEventName
|
|
3
|
-
} from "./chunk-ZASOR6XP.mjs";
|
|
4
|
-
|
|
5
|
-
// src/react-native.ts
|
|
6
|
-
import PostHog from "posthog-react-native";
|
|
7
|
-
function initAnalytics(apiKey, host = "https://app.posthog.com") {
|
|
8
|
-
PostHog.initAsync(apiKey, {
|
|
9
|
-
host
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
function identify(userId, traits) {
|
|
13
|
-
PostHog.identify(userId, traits);
|
|
14
|
-
}
|
|
15
|
-
function capture(event, properties = {}, options) {
|
|
16
|
-
validateEventName(event);
|
|
17
|
-
if (options?.userId) {
|
|
18
|
-
}
|
|
19
|
-
PostHog.capture(event, properties);
|
|
20
|
-
}
|
|
21
|
-
async function shutdown() {
|
|
22
|
-
}
|
|
23
|
-
export {
|
|
24
|
-
capture,
|
|
25
|
-
identify,
|
|
26
|
-
initAnalytics,
|
|
27
|
-
shutdown
|
|
28
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-restricted-imports */
|
|
2
|
-
import { PostHog } from 'posthog-node';
|
|
3
|
-
import { validateEventName } from './shared';
|
|
4
|
-
|
|
5
|
-
let client: PostHog | null = null;
|
|
6
|
-
|
|
7
|
-
export function initAnalytics(apiKey: string, host = 'https://app.posthog.com') {
|
|
8
|
-
client = new PostHog(apiKey, { host });
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function getClient(safe = false) {
|
|
12
|
-
if (!client && !safe) {
|
|
13
|
-
throw new Error('Analytics not initialized. Call initAnalytics first.');
|
|
14
|
-
}
|
|
15
|
-
return client;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
-
export function identify(userId: string, traits: Record<string, any>) {
|
|
20
|
-
getClient(true)?.identify({
|
|
21
|
-
distinctId: userId,
|
|
22
|
-
properties: traits,
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function capture(
|
|
27
|
-
event: string,
|
|
28
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
-
properties: Record<string, any> = {},
|
|
30
|
-
options?: { userId?: string },
|
|
31
|
-
) {
|
|
32
|
-
// Validate naming convention: app.action.object
|
|
33
|
-
validateEventName(event);
|
|
34
|
-
|
|
35
|
-
// If no user is provided, we can't capture server-side easily without anonymous ID
|
|
36
|
-
// But usually servers have a userId context.
|
|
37
|
-
if (!options?.userId) {
|
|
38
|
-
console.warn('Analytics capture called without userId');
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
getClient(true)?.capture({
|
|
43
|
-
distinctId: options.userId,
|
|
44
|
-
event,
|
|
45
|
-
properties,
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export async function shutdown() {
|
|
50
|
-
await getClient(true)?.shutdown();
|
|
51
|
-
}
|
package/src/react-native.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import PostHog from 'posthog-react-native';
|
|
2
|
-
import { validateEventName } from './shared';
|
|
3
|
-
|
|
4
|
-
export function initAnalytics(apiKey: string, host = 'https://app.posthog.com') {
|
|
5
|
-
PostHog.initAsync(apiKey, {
|
|
6
|
-
host,
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
-
export function identify(userId: string, traits?: Record<string, any>) {
|
|
12
|
-
// @ts-expect-error - PostHog RN types might be missing or different version
|
|
13
|
-
PostHog.identify(userId, traits);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function capture(
|
|
17
|
-
event: string,
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
-
properties: Record<string, any> = {},
|
|
20
|
-
options?: { userId?: string; tenantId?: string },
|
|
21
|
-
) {
|
|
22
|
-
validateEventName(event);
|
|
23
|
-
|
|
24
|
-
// merge options into properties if needed, or handle separately if PostHog RN supports it
|
|
25
|
-
// Usually capturing with explicit distinctId requires identifying first
|
|
26
|
-
if (options?.userId) {
|
|
27
|
-
// Optional: re-identify? No, usually not done on every capture unless user switched
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// @ts-expect-error - PostHog RN types might be missing or different version
|
|
31
|
-
PostHog.capture(event, properties);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export async function shutdown() {
|
|
35
|
-
// PostHog RN doesn't need explicit shutdown usually
|
|
36
|
-
}
|
package/src/shared.test.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { validateEventName } from './shared';
|
|
3
|
-
|
|
4
|
-
describe('Analytics Shared', () => {
|
|
5
|
-
it('validates correct event names', () => {
|
|
6
|
-
expect(() => validateEventName('app.page.view')).not.toThrow();
|
|
7
|
-
expect(() => validateEventName('user.signup.success')).not.toThrow();
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it('throws on invalid event names', () => {
|
|
11
|
-
expect(() => validateEventName('invalid')).toThrow(/must follow/);
|
|
12
|
-
expect(() => validateEventName('almost.valid')).toThrow(/must follow/);
|
|
13
|
-
});
|
|
14
|
-
});
|