@wrelik/analytics 0.1.0 → 0.2.1

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.
@@ -0,0 +1,18 @@
1
+
2
+ > @wrelik/analytics@0.2.1 build /home/runner/work/wrelik-kit/wrelik-kit/packages/analytics
3
+ > tsup src/index.ts src/react-native.ts --format cjs,esm --clean
4
+
5
+ CLI Building entry: src/index.ts, src/react-native.ts
6
+ CLI Using tsconfig: tsconfig.json
7
+ CLI tsup v8.5.1
8
+ CLI Target: es2022
9
+ CLI Cleaning output folder
10
+ CJS Build start
11
+ ESM Build start
12
+ ESM dist/index.mjs 937.00 B
13
+ ESM dist/react-native.mjs 558.00 B
14
+ ESM dist/chunk-ZASOR6XP.mjs 297.00 B
15
+ ESM ⚡️ Build success in 55ms
16
+ CJS dist/index.js 2.24 KB
17
+ CJS dist/react-native.js 2.57 KB
18
+ CJS ⚡️ Build success in 55ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @wrelik/analytics
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [2b9cbf7]
8
+ - @wrelik/errors@0.2.1
9
+
10
+ ## 0.2.0
11
+
12
+ ### Minor Changes
13
+
14
+ - a38ebcb: Add React Native / Expo support and enforce server-only boundaries.
15
+
16
+ ### Patch Changes
17
+
18
+ - Updated dependencies [a38ebcb]
19
+ - @wrelik/errors@0.2.0
20
+
21
+ ## 0.1.1
22
+
23
+ ### Patch Changes
24
+
25
+ - Update publishConfig to access:public for all packages.
26
+ - Updated dependencies
27
+ - @wrelik/errors@0.1.1
28
+
3
29
  ## 0.1.0
4
30
 
5
31
  ### Minor Changes
@@ -0,0 +1,12 @@
1
+ // src/shared.ts
2
+ import { ValidationError } from "@wrelik/errors";
3
+ function validateEventName(event) {
4
+ const parts = event.split(".");
5
+ if (parts.length < 3) {
6
+ throw new ValidationError(`Event name "${event}" must follow "app.action.object" format`);
7
+ }
8
+ }
9
+
10
+ export {
11
+ validateEventName
12
+ };
package/dist/index.js ADDED
@@ -0,0 +1,78 @@
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 ADDED
@@ -0,0 +1,43 @@
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
+ };
@@ -0,0 +1,73 @@
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
+ });
@@ -0,0 +1,28 @@
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/package.json CHANGED
@@ -1,20 +1,45 @@
1
1
  {
2
2
  "name": "@wrelik/analytics",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/lwhite702/wrelik-kit.git",
10
+ "directory": "packages/analytics"
11
+ },
4
12
  "main": "./dist/index.js",
5
13
  "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": "./dist/index.js",
16
+ "./react-native": "./dist/react-native.js"
17
+ },
6
18
  "dependencies": {
7
19
  "posthog-node": "^3.6.3",
8
- "@wrelik/errors": "0.1.0"
20
+ "posthog-react-native": "^2.10.1",
21
+ "@wrelik/errors": "0.2.1"
9
22
  },
10
23
  "devDependencies": {
24
+ "@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",
11
36
  "tsup": "^8.0.1",
12
37
  "vitest": "^1.2.2",
13
- "@wrelik/eslint-config": "0.1.0",
14
- "@wrelik/tsconfig": "0.1.0"
38
+ "@wrelik/eslint-config": "0.1.1",
39
+ "@wrelik/tsconfig": "0.1.1"
15
40
  },
16
41
  "scripts": {
17
- "build": "tsup src/index.ts --format cjs,esm --dts --clean",
42
+ "build": "tsup src/index.ts src/react-native.ts --format cjs,esm --clean",
18
43
  "lint": "eslint src/",
19
44
  "test": "vitest run --passWithNoTests"
20
45
  }
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
+ /* eslint-disable no-restricted-imports */
1
2
  import { PostHog } from 'posthog-node';
2
- import { ValidationError } from '@wrelik/errors';
3
+ import { validateEventName } from './shared';
3
4
 
4
5
  let client: PostHog | null = null;
5
6
 
@@ -14,6 +15,7 @@ function getClient(safe = false) {
14
15
  return client;
15
16
  }
16
17
 
18
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
19
  export function identify(userId: string, traits: Record<string, any>) {
18
20
  getClient(true)?.identify({
19
21
  distinctId: userId,
@@ -23,14 +25,12 @@ export function identify(userId: string, traits: Record<string, any>) {
23
25
 
24
26
  export function capture(
25
27
  event: string,
28
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
29
  properties: Record<string, any> = {},
27
30
  options?: { userId?: string },
28
31
  ) {
29
32
  // Validate naming convention: app.action.object
30
- const parts = event.split('.');
31
- if (parts.length < 3) {
32
- throw new ValidationError(`Event name "${event}" must follow "app.action.object" format`);
33
- }
33
+ validateEventName(event);
34
34
 
35
35
  // If no user is provided, we can't capture server-side easily without anonymous ID
36
36
  // But usually servers have a userId context.
@@ -0,0 +1,36 @@
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
+ }
@@ -0,0 +1,14 @@
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
+ });
package/src/shared.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { ValidationError } from '@wrelik/errors';
2
+
3
+ export function validateEventName(event: string) {
4
+ const parts = event.split('.');
5
+ if (parts.length < 3) {
6
+ throw new ValidationError(`Event name "${event}" must follow "app.action.object" format`);
7
+ }
8
+ }
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "extends": "@wrelik/tsconfig/node.json",
3
3
  "compilerOptions": {
4
- "outDir": "dist"
4
+ "outDir": "dist", "skipLibCheck": true
5
5
  },
6
6
  "include": ["src"]
7
7
  }