@nodewrite-labs/analytics-sdk 0.1.0 → 0.2.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 CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 nodewrite-hq
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.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 nodewrite-hq
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 CHANGED
@@ -75,6 +75,27 @@ const result = await recordAnalyticsEvent(
75
75
 
76
76
  `enabled: false` writes nothing and returns status 204. A stored view or click returns status 200 and `{ id }`. A recorded duration returns 204. An event that cannot be stored returns 400.
77
77
 
78
+ ## Payload
79
+
80
+ `analyticsPlugin` adds the hidden `analytics-events` collection, the ingest endpoint at `/analytics/events` under the Payload API route (`/api/analytics/events` by default), and an admin screen at `/analytics` under the admin route. The nav link follows `routes.admin`.
81
+
82
+ The package names no host's geo headers. Pass `geo` to read country and city from the request headers your host sets. Without `geo`, the country and city modules store null.
83
+
84
+ ```ts
85
+ import { analyticsPlugin } from "@nodewrite-labs/analytics-sdk/payload";
86
+
87
+ analyticsPlugin({
88
+ enabled: true,
89
+ modules: { country: true, city: false, sessions: false, clicks: true },
90
+ // Vercel, for example. Cloudflare sends `cf-ipcountry`.
91
+ geo: (headers) => ({ country: headers.get("x-vercel-ip-country"), city: null }),
92
+ });
93
+ ```
94
+
95
+ The browser then posts to the Payload endpoint: `startAnalytics({ endpoint: "/api/analytics/events", … })`.
96
+
97
+ The plugin adds a collection, so a Payload project with migrations needs a new one (`payload migrate:create`).
98
+
78
99
  ## Store
79
100
 
80
101
  `AnalyticsEventStore` is three methods: `insertAnalyticsEvent`, `recordAnalyticsDuration`, and `summarizeAnalytics`.
@@ -1,4 +1,4 @@
1
- import { A as AnalyticsClientConfig } from '../options-D3s0dcJ-.js';
1
+ import { A as AnalyticsClientConfig } from '../options-Dpx2vs4R.js';
2
2
  export { A as AnalyticsEventBody } from '../event-body-fpV2mYpY.js';
3
3
 
4
4
  declare function recordNamedClick(name: string): void;
@@ -1,4 +1,4 @@
1
- import { a as AnalyticsModules } from './options-D3s0dcJ-.js';
1
+ import { a as AnalyticsModules } from './options-Dpx2vs4R.js';
2
2
 
3
3
  declare const AnalyticsEventKind: {
4
4
  readonly VIEW: "view";
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { S as StoredAnalyticsEvent, A as AnalyticsSummaryQuery, a as AnalyticsSummary, b as AnalyticsEventStore, N as NewAnalyticsEvent } from './event-store-BhisLdw_.js';
2
- export { c as AnalyticsEventKind } from './event-store-BhisLdw_.js';
3
- import { a as AnalyticsModules } from './options-D3s0dcJ-.js';
4
- export { A as AnalyticsClientConfig, b as AnalyticsServerOptions } from './options-D3s0dcJ-.js';
1
+ import { S as StoredAnalyticsEvent, A as AnalyticsSummaryQuery, a as AnalyticsSummary, b as AnalyticsEventStore, N as NewAnalyticsEvent } from './event-store-BH-ZR3yD.js';
2
+ export { c as AnalyticsEventKind } from './event-store-BH-ZR3yD.js';
3
+ import { a as AnalyticsModules } from './options-Dpx2vs4R.js';
4
+ export { A as AnalyticsClientConfig, b as AnalyticsServerOptions, R as ReadGeoFromHeaders, c as RequestGeo } from './options-Dpx2vs4R.js';
5
5
  export { A as AnalyticsEventBody } from './event-body-fpV2mYpY.js';
6
6
 
7
7
  /** Keeps a duration inside 0..1800. NaN and infinities are rejected. */
@@ -0,0 +1,29 @@
1
+ interface AnalyticsModules {
2
+ country: boolean;
3
+ city: boolean;
4
+ sessions: boolean;
5
+ clicks: boolean;
6
+ }
7
+ interface AnalyticsServerOptions {
8
+ enabled: boolean;
9
+ modules: AnalyticsModules;
10
+ }
11
+ /** Country and city for one request, as the host reports them. */
12
+ interface RequestGeo {
13
+ country: string | null;
14
+ city: string | null;
15
+ }
16
+ /**
17
+ * Reads the visitor's country and city from the request headers. The app supplies it, because
18
+ * each host names its geo headers differently.
19
+ */
20
+ type ReadGeoFromHeaders = (headers: Headers) => RequestGeo;
21
+ interface AnalyticsClientConfig {
22
+ endpoint: string;
23
+ enabled: boolean;
24
+ locale?: string;
25
+ sessions: boolean;
26
+ clicks: boolean;
27
+ }
28
+
29
+ export type { AnalyticsClientConfig as A, ReadGeoFromHeaders as R, AnalyticsModules as a, AnalyticsServerOptions as b, RequestGeo as c };
@@ -1,5 +1,5 @@
1
1
  import { Access, Config, Payload } from 'payload';
2
- import { AnalyticsModules, AnalyticsServerOptions } from '../index.js';
2
+ import { AnalyticsModules, AnalyticsServerOptions, ReadGeoFromHeaders } from '../index.js';
3
3
  import { AnalyticsSummary, AnalyticsEventStore, NewAnalyticsEvent, StoredAnalyticsEvent, AnalyticsSummaryQuery } from '../index.js';
4
4
 
5
5
  /**
@@ -46,13 +46,19 @@ type AdminSection = {
46
46
  */
47
47
  declare function adminSections(summary: AnalyticsSummary, modules: AnalyticsModules, query: AdminSectionsQuery): AdminSection[];
48
48
 
49
+ interface AnalyticsEndpointOptions extends AnalyticsServerOptions {
50
+ /** Without it, the country and city modules store null. */
51
+ geo?: ReadGeoFromHeaders;
52
+ }
53
+
54
+ interface AnalyticsPluginOptions extends AnalyticsEndpointOptions {
55
+ read?: Access;
56
+ }
49
57
  /**
50
58
  * Adds the analytics collection, the ingest endpoint, and the admin screen.
51
59
  * Existing collections, endpoints, and views stay in place.
52
60
  */
53
- declare function analyticsPlugin(options: AnalyticsServerOptions & {
54
- read?: Access;
55
- }): (config: Config) => Config;
61
+ declare function analyticsPlugin(options: AnalyticsPluginOptions): (config: Config) => Config;
56
62
 
57
63
  /**
58
64
  * AnalyticsEventStore on Payload's Local API.
@@ -68,3 +74,4 @@ declare class PayloadAnalyticsEventStore implements AnalyticsEventStore {
68
74
  }
69
75
 
70
76
  export { PayloadAnalyticsEventStore, adminSections, analyticsEventsAccess, analyticsPlugin };
77
+ export type { AnalyticsEndpointOptions, AnalyticsPluginOptions };
@@ -12,9 +12,15 @@ import "../chunk-Q4JRXZ6M.js";
12
12
  import "../chunk-F7BY2JQ3.js";
13
13
 
14
14
  // src/payload/endpoint.ts
15
+ var NO_GEO = { country: null, city: null };
15
16
  async function handleAnalyticsEvent(request, deps) {
16
17
  const result = await recordAnalyticsEvent(
17
- { body: request.body, headers: headerRecord(request.headers) },
18
+ {
19
+ body: request.body,
20
+ headers: headerRecord(request.headers),
21
+ country: request.country ?? null,
22
+ city: request.city ?? null
23
+ },
18
24
  deps
19
25
  );
20
26
  if (result.status === 204 || result.body === null) {
@@ -37,7 +43,11 @@ async function handlePayloadAnalyticsEvent(req, options) {
37
43
  } catch {
38
44
  body = void 0;
39
45
  }
40
- return handleAnalyticsEvent({ body, headers: req.headers }, { store, options });
46
+ const geo = options.geo ? options.geo(req.headers) : NO_GEO;
47
+ return handleAnalyticsEvent(
48
+ { body, headers: req.headers, country: geo.country, city: geo.city },
49
+ { store, options }
50
+ );
41
51
  }
42
52
  function headerRecord(headers) {
43
53
  if (headers instanceof Headers) {
@@ -51,8 +61,11 @@ function headerRecord(headers) {
51
61
  }
52
62
 
53
63
  // src/payload/plugin.ts
64
+ var DEFAULT_ADMIN_ROUTE = "/admin";
65
+ var ANALYTICS_VIEW_PATH = "/analytics";
54
66
  function analyticsPlugin(options) {
55
67
  return (config) => {
68
+ const adminRoute = config.routes?.admin ?? DEFAULT_ADMIN_ROUTE;
56
69
  const admin = config.admin ?? {};
57
70
  const components = admin.components ?? {};
58
71
  return {
@@ -70,14 +83,15 @@ function analyticsPlugin(options) {
70
83
  path: "@nodewrite-labs/analytics-sdk/payload/admin-view",
71
84
  exportName: "AnalyticsAdminView"
72
85
  },
73
- path: "/analytics"
86
+ path: ANALYTICS_VIEW_PATH
74
87
  }
75
88
  },
76
89
  afterNavLinks: [
77
90
  ...components.afterNavLinks ?? [],
78
91
  {
79
92
  path: "@nodewrite-labs/analytics-sdk/payload/nav-link",
80
- exportName: "AnalyticsNavLink"
93
+ exportName: "AnalyticsNavLink",
94
+ clientProps: { href: `${adminRoute}${ANALYTICS_VIEW_PATH}` }
81
95
  }
82
96
  ]
83
97
  }
@@ -1,5 +1,10 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
 
3
- declare function AnalyticsNavLink(): react_jsx_runtime.JSX.Element;
3
+ interface AnalyticsNavLinkProps {
4
+ /** The analytics view under the app's admin route; the plugin sets it. */
5
+ href: string;
6
+ }
7
+ declare function AnalyticsNavLink({ href }: AnalyticsNavLinkProps): react_jsx_runtime.JSX.Element;
4
8
 
5
9
  export { AnalyticsNavLink };
10
+ export type { AnalyticsNavLinkProps };
@@ -1,7 +1,7 @@
1
1
  // src/payload/admin/nav-link.tsx
2
2
  import { jsx } from "react/jsx-runtime";
3
- function AnalyticsNavLink() {
4
- return /* @__PURE__ */ jsx("a", { href: "/admin/analytics", children: "Analytics" });
3
+ function AnalyticsNavLink({ href }) {
4
+ return /* @__PURE__ */ jsx("a", { href, children: "Analytics" });
5
5
  }
6
6
  export {
7
7
  AnalyticsNavLink
@@ -1,10 +1,7 @@
1
- import { AnalyticsEventStore } from '../index.js';
1
+ import { c as RequestGeo } from '../options-Dpx2vs4R.js';
2
2
  import { AnalyticsServerOptions } from '../index.js';
3
+ import { AnalyticsEventStore } from '../index.js';
3
4
 
4
- interface RequestGeo {
5
- country: string | null;
6
- city: string | null;
7
- }
8
5
  /** Reads `country` and `city` from the request object the app passed in. */
9
6
  declare function readRequestGeo(request: object): RequestGeo;
10
7
 
@@ -40,5 +37,5 @@ interface RecordAnalyticsEventResult {
40
37
  */
41
38
  declare function recordAnalyticsEvent(request: AnalyticsEventRequest | Request, deps: RecordAnalyticsEventDeps): Promise<RecordAnalyticsEventResult>;
42
39
 
43
- export { readRequestGeo, recordAnalyticsEvent };
44
- export type { AnalyticsEventRequest, AnalyticsHeaderBag, RecordAnalyticsEventDeps, RecordAnalyticsEventResult, RequestGeo };
40
+ export { RequestGeo, readRequestGeo, recordAnalyticsEvent };
41
+ export type { AnalyticsEventRequest, AnalyticsHeaderBag, RecordAnalyticsEventDeps, RecordAnalyticsEventResult };
package/package.json CHANGED
@@ -1,90 +1,90 @@
1
- {
2
- "name": "@nodewrite-labs/analytics-sdk",
3
- "version": "0.1.0",
4
- "description": "Analytics SDK for page views and named clicks, with Postgres storage and a Payload admin screen.",
5
- "publishConfig": {
6
- "access": "public"
7
- },
8
- "packageManager": "pnpm@12.11.2",
9
- "engines": {
10
- "node": ">=22"
11
- },
12
- "workspaces": [
13
- "playground"
14
- ],
15
- "type": "module",
16
- "main": "./dist/index.js",
17
- "types": "./dist/index.d.ts",
18
- "exports": {
19
- ".": {
20
- "types": "./dist/index.d.ts",
21
- "import": "./dist/index.js"
22
- },
23
- "./client": {
24
- "types": "./dist/client/index.d.ts",
25
- "import": "./dist/client/index.js"
26
- },
27
- "./server": {
28
- "types": "./dist/server/index.d.ts",
29
- "import": "./dist/server/index.js"
30
- },
31
- "./postgres": {
32
- "types": "./dist/postgres/index.d.ts",
33
- "import": "./dist/postgres/index.js"
34
- },
35
- "./payload": {
36
- "types": "./dist/payload/index.d.ts",
37
- "import": "./dist/payload/index.js"
38
- },
39
- "./payload/admin-view": {
40
- "types": "./dist/payload/admin-view.d.ts",
41
- "import": "./dist/payload/admin-view.js"
42
- },
43
- "./payload/nav-link": {
44
- "types": "./dist/payload/nav-link.d.ts",
45
- "import": "./dist/payload/nav-link.js"
46
- }
47
- },
48
- "files": [
49
- "dist",
50
- "src/postgres/schema.sql",
51
- "README.md"
52
- ],
53
- "scripts": {
54
- "typecheck": "tsc --noEmit",
55
- "test": "vitest run",
56
- "build": "tsup"
57
- },
58
- "peerDependencies": {
59
- "@payloadcms/next": "*",
60
- "payload": "*",
61
- "pg": "*"
62
- },
63
- "peerDependenciesMeta": {
64
- "@payloadcms/next": {
65
- "optional": true
66
- },
67
- "payload": {
68
- "optional": true
69
- },
70
- "pg": {
71
- "optional": true
72
- }
73
- },
74
- "devDependencies": {
75
- "@payloadcms/next": "3.92.0",
76
- "@types/node": "^22.20.5",
77
- "@types/pg": "^8.23.1",
78
- "@types/react": "19.2.14",
79
- "esbuild": "^0.28.2",
80
- "graphql": "^16.14.2",
81
- "jsdom": "^30.1.3",
82
- "payload": "3.92.0",
83
- "pg": "^8.23.1",
84
- "react": "^19.3.1",
85
- "tsup": "^9.0.0",
86
- "typescript": "6.0.3",
87
- "vite": "^8.3.5",
88
- "vitest": "^5.0.5"
89
- }
90
- }
1
+ {
2
+ "name": "@nodewrite-labs/analytics-sdk",
3
+ "version": "0.2.0",
4
+ "description": "Analytics SDK for page views and named clicks, with Postgres storage and a Payload admin screen.",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "packageManager": "pnpm@12.11.2",
9
+ "engines": {
10
+ "node": ">=22"
11
+ },
12
+ "workspaces": [
13
+ "playground"
14
+ ],
15
+ "type": "module",
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ },
23
+ "./client": {
24
+ "types": "./dist/client/index.d.ts",
25
+ "import": "./dist/client/index.js"
26
+ },
27
+ "./server": {
28
+ "types": "./dist/server/index.d.ts",
29
+ "import": "./dist/server/index.js"
30
+ },
31
+ "./postgres": {
32
+ "types": "./dist/postgres/index.d.ts",
33
+ "import": "./dist/postgres/index.js"
34
+ },
35
+ "./payload": {
36
+ "types": "./dist/payload/index.d.ts",
37
+ "import": "./dist/payload/index.js"
38
+ },
39
+ "./payload/admin-view": {
40
+ "types": "./dist/payload/admin-view.d.ts",
41
+ "import": "./dist/payload/admin-view.js"
42
+ },
43
+ "./payload/nav-link": {
44
+ "types": "./dist/payload/nav-link.d.ts",
45
+ "import": "./dist/payload/nav-link.js"
46
+ }
47
+ },
48
+ "files": [
49
+ "dist",
50
+ "src/postgres/schema.sql",
51
+ "README.md"
52
+ ],
53
+ "scripts": {
54
+ "typecheck": "tsc --noEmit",
55
+ "test": "vitest run",
56
+ "build": "tsup"
57
+ },
58
+ "peerDependencies": {
59
+ "@payloadcms/next": "*",
60
+ "payload": "*",
61
+ "pg": "*"
62
+ },
63
+ "peerDependenciesMeta": {
64
+ "@payloadcms/next": {
65
+ "optional": true
66
+ },
67
+ "payload": {
68
+ "optional": true
69
+ },
70
+ "pg": {
71
+ "optional": true
72
+ }
73
+ },
74
+ "devDependencies": {
75
+ "@payloadcms/next": "3.92.0",
76
+ "@types/node": "^22.20.5",
77
+ "@types/pg": "^8.23.1",
78
+ "@types/react": "19.2.14",
79
+ "esbuild": "^0.28.2",
80
+ "graphql": "^16.14.2",
81
+ "jsdom": "^30.1.3",
82
+ "payload": "3.92.0",
83
+ "pg": "^8.23.1",
84
+ "react": "^19.3.1",
85
+ "tsup": "^9.0.0",
86
+ "typescript": "6.0.3",
87
+ "vite": "^8.3.5",
88
+ "vitest": "^5.0.5"
89
+ }
90
+ }
@@ -1,12 +1,12 @@
1
- CREATE TABLE IF NOT EXISTS analytics_event (
2
- id uuid primary key,
3
- kind text not null,
4
- path text not null,
5
- locale text,
6
- occurred_at timestamptz not null,
7
- duration_seconds integer,
8
- country text,
9
- city text,
10
- session_id text,
11
- event_name text
12
- );
1
+ CREATE TABLE IF NOT EXISTS analytics_event (
2
+ id uuid primary key,
3
+ kind text not null,
4
+ path text not null,
5
+ locale text,
6
+ occurred_at timestamptz not null,
7
+ duration_seconds integer,
8
+ country text,
9
+ city text,
10
+ session_id text,
11
+ event_name text
12
+ );
@@ -1,19 +0,0 @@
1
- interface AnalyticsModules {
2
- country: boolean;
3
- city: boolean;
4
- sessions: boolean;
5
- clicks: boolean;
6
- }
7
- interface AnalyticsServerOptions {
8
- enabled: boolean;
9
- modules: AnalyticsModules;
10
- }
11
- interface AnalyticsClientConfig {
12
- endpoint: string;
13
- enabled: boolean;
14
- locale?: string;
15
- sessions: boolean;
16
- clicks: boolean;
17
- }
18
-
19
- export type { AnalyticsClientConfig as A, AnalyticsModules as a, AnalyticsServerOptions as b };