@keystonepzjr/web-product-analytics 0.0.0-stage → 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/package.json +34 -4
- package/src/analytics.ts +114 -0
- package/src/events.ts +29 -0
- package/src/index.ts +4 -0
- package/src/regions.ts +35 -0
- package/src/visitor.ts +49 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keystonepzjr/web-product-analytics",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Keystone's product analytics for its web apps (the marketing site and the console): one PostHog setup, one typed event list, one person from the first visit on.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Keystone-PZJR/keystone-web-product-analytics.git"
|
|
10
|
+
},
|
|
11
|
+
"packageManager": "pnpm@10.33.4",
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"!src/**/*.test.ts"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./src/index.ts",
|
|
22
|
+
"./regions": "./src/regions.ts"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"test": "vitest run"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"posthog-js": "^1.454.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"posthog-js": "^1.454.2",
|
|
33
|
+
"typescript": "^5.9.0",
|
|
34
|
+
"vitest": "^4.0.0"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/analytics.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import posthog, { type PostHog, type PostHogConfig } from "posthog-js";
|
|
2
|
+
import type { App, EventName, Events } from "./events.ts";
|
|
3
|
+
import { INGEST_PATH, type Region } from "./regions.ts";
|
|
4
|
+
import { takeVisitor, withVisitor } from "./visitor.ts";
|
|
5
|
+
|
|
6
|
+
export interface AnalyticsOptions {
|
|
7
|
+
app: App;
|
|
8
|
+
/** The project's key. Without one analytics is off and every call does nothing. */
|
|
9
|
+
key: string | undefined;
|
|
10
|
+
region: Region;
|
|
11
|
+
/** Events go through this origin's `INGEST_PATH`, which the app forwards to the region. */
|
|
12
|
+
proxied: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* `asked`: the visitor chooses, on the marketing site's consent bar. Until they agree, and after
|
|
15
|
+
* they decline, PostHog counts them without cookies or storage and records no replay.
|
|
16
|
+
* `given`: the app's terms cover it (the console), so it captures from the start.
|
|
17
|
+
*/
|
|
18
|
+
consent: "asked" | "given";
|
|
19
|
+
/**
|
|
20
|
+
* What replays hide. `inputs`: what people type. `text`: every text on the page as well, for an
|
|
21
|
+
* app that shows other people's details (the console shows a business's customers).
|
|
22
|
+
*/
|
|
23
|
+
mask: "inputs" | "text";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface Analytics {
|
|
27
|
+
/** Starts PostHog in the browser, picking up a visitor carried in the address. Once only. */
|
|
28
|
+
start(): void;
|
|
29
|
+
/** `now` sends at once, for an event the page is about to leave behind. */
|
|
30
|
+
track<E extends EventName>(event: E, properties: Events[E], options?: { now?: boolean }): void;
|
|
31
|
+
/** The person behind the visitor, by Heimdal's user id: their visits before and after join up. */
|
|
32
|
+
identify(person: { id: string; email: string | null }): void;
|
|
33
|
+
/** The business the person is working in, by sor's id: what they do counts for it too. */
|
|
34
|
+
business(id: string): void;
|
|
35
|
+
/** Signed out: what happens next is a new visitor's. */
|
|
36
|
+
reset(): void;
|
|
37
|
+
/** The visitor's choice on the consent bar. */
|
|
38
|
+
consent(granted: boolean): void;
|
|
39
|
+
/** `url` carrying this visitor and session, so the app it opens carries on as the same person. */
|
|
40
|
+
carry(url: string): string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The PostHog instance's name: its own, apart from any other PostHog on the page. */
|
|
44
|
+
const INSTANCE = "keystone";
|
|
45
|
+
|
|
46
|
+
/** The SDK's defaults as of this date; a later date is a deliberate upgrade. */
|
|
47
|
+
const DEFAULTS = "2026-08-30";
|
|
48
|
+
|
|
49
|
+
export function createAnalytics(options: AnalyticsOptions): Analytics {
|
|
50
|
+
let client: PostHog | null = null;
|
|
51
|
+
|
|
52
|
+
const carrying = () =>
|
|
53
|
+
client !== null &&
|
|
54
|
+
(options.consent === "given" || client.get_explicit_consent_status() === "granted");
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
start() {
|
|
58
|
+
if (client !== null || options.key === undefined || typeof window === "undefined") return;
|
|
59
|
+
const visitor = takeVisitor(window.location, window.history);
|
|
60
|
+
const bootstrap = visitor && { distinctID: visitor.distinctId, sessionID: visitor.sessionId };
|
|
61
|
+
client = posthog.init(options.key, configFor(options, bootstrap), INSTANCE) ?? null;
|
|
62
|
+
},
|
|
63
|
+
track(event, properties, { now = false } = {}) {
|
|
64
|
+
client?.capture(event, properties, now ? { send_instantly: true } : undefined);
|
|
65
|
+
},
|
|
66
|
+
identify({ id, email }) {
|
|
67
|
+
client?.identify(id, email === null ? undefined : { email });
|
|
68
|
+
},
|
|
69
|
+
business(id) {
|
|
70
|
+
client?.group("business", id);
|
|
71
|
+
},
|
|
72
|
+
reset() {
|
|
73
|
+
client?.reset();
|
|
74
|
+
},
|
|
75
|
+
consent(granted) {
|
|
76
|
+
if (granted) client?.opt_in_capturing();
|
|
77
|
+
else client?.opt_out_capturing();
|
|
78
|
+
},
|
|
79
|
+
carry(url) {
|
|
80
|
+
if (client === null || !carrying()) return url;
|
|
81
|
+
const sessionId = client.get_session_id();
|
|
82
|
+
return sessionId ? withVisitor(url, { distinctId: client.get_distinct_id(), sessionId }) : url;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function configFor(
|
|
88
|
+
options: AnalyticsOptions,
|
|
89
|
+
bootstrap: PostHogConfig["bootstrap"] | null,
|
|
90
|
+
): Partial<PostHogConfig> {
|
|
91
|
+
return {
|
|
92
|
+
api_host: options.proxied ? INGEST_PATH : options.region.ingest,
|
|
93
|
+
ui_host: options.region.app,
|
|
94
|
+
defaults: DEFAULTS,
|
|
95
|
+
person_profiles: "identified_only",
|
|
96
|
+
capture_exceptions: true,
|
|
97
|
+
capture_heatmaps: true,
|
|
98
|
+
capture_dead_clicks: true,
|
|
99
|
+
capture_performance: { web_vitals: true, network_timing: true },
|
|
100
|
+
mask_personal_data_properties: true,
|
|
101
|
+
session_recording: {
|
|
102
|
+
maskAllInputs: true,
|
|
103
|
+
maskTextSelector: options.mask === "text" ? "*" : null,
|
|
104
|
+
},
|
|
105
|
+
...(options.consent === "asked"
|
|
106
|
+
? { cookieless_mode: "on_reject", opt_out_capturing_by_default: true }
|
|
107
|
+
: {}),
|
|
108
|
+
...(bootstrap ? { bootstrap } : {}),
|
|
109
|
+
before_send: (event) => {
|
|
110
|
+
if (event) event.properties = { ...event.properties, app: options.app };
|
|
111
|
+
return event;
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
package/src/events.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every event Keystone's web apps send, by name, with its properties.
|
|
3
|
+
*
|
|
4
|
+
* A name is an object and a past-tense verb in snake_case (`message_sent`). One event covers every
|
|
5
|
+
* case of what happened, and a property tells the cases apart, so a new question is a filter or a
|
|
6
|
+
* breakdown in PostHog (the first reply is the first `message_sent` from `keystone`), never a new
|
|
7
|
+
* event. Properties are snake_case too. PostHog's own events (`$pageview`, `$autocapture`,
|
|
8
|
+
* `$exception`, `$web_vitals`) are the SDK's and are not listed.
|
|
9
|
+
*/
|
|
10
|
+
export interface Events {
|
|
11
|
+
/** A message in a chat with Keystone, from either side. */
|
|
12
|
+
message_sent: {
|
|
13
|
+
sender: "person" | "keystone";
|
|
14
|
+
/** The composer it was written in. */
|
|
15
|
+
surface: Surface;
|
|
16
|
+
/** The thread it joined; none for a first message, which opens one. */
|
|
17
|
+
thread_id?: string;
|
|
18
|
+
characters: number;
|
|
19
|
+
attachments?: number;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type EventName = keyof Events;
|
|
24
|
+
|
|
25
|
+
/** The app an event came from, on every event as the `app` property. */
|
|
26
|
+
export type App = "marketing_site" | "console";
|
|
27
|
+
|
|
28
|
+
/** Where a composer is: the marketing site's fold and foot, and the console's chat. */
|
|
29
|
+
export type Surface = "site_hero" | "site_foot" | "console_chat";
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { createAnalytics, type Analytics, type AnalyticsOptions } from "./analytics.ts";
|
|
2
|
+
export type { App, EventName, Events, Surface } from "./events.ts";
|
|
3
|
+
export { INGEST_PATH, REGIONS, regionNamed, type Region, type RegionName } from "./regions.ts";
|
|
4
|
+
export { takeVisitor, withVisitor, type Visitor } from "./visitor.ts";
|
package/src/regions.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** PostHog Cloud's hosts for a project's region. */
|
|
2
|
+
export interface Region {
|
|
3
|
+
/** Where events go. */
|
|
4
|
+
readonly ingest: string;
|
|
5
|
+
/** Where the SDK's lazy parts (the recorder, surveys, web vitals) load from. */
|
|
6
|
+
readonly assets: string;
|
|
7
|
+
/** The app, which links in the SDK's tools (the toolbar) point at. */
|
|
8
|
+
readonly app: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const REGIONS = {
|
|
12
|
+
us: {
|
|
13
|
+
ingest: "https://us.i.posthog.com",
|
|
14
|
+
assets: "https://us-assets.i.posthog.com",
|
|
15
|
+
app: "https://us.posthog.com",
|
|
16
|
+
},
|
|
17
|
+
eu: {
|
|
18
|
+
ingest: "https://eu.i.posthog.com",
|
|
19
|
+
assets: "https://eu-assets.i.posthog.com",
|
|
20
|
+
app: "https://eu.posthog.com",
|
|
21
|
+
},
|
|
22
|
+
} as const satisfies Record<string, Region>;
|
|
23
|
+
|
|
24
|
+
export type RegionName = keyof typeof REGIONS;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The path each app forwards to PostHog from its own origin, so a blocker that drops requests to
|
|
28
|
+
* PostHog's hosts doesn't drop the events. `${INGEST_PATH}/static/*` goes to `assets`, the rest to
|
|
29
|
+
* `ingest`.
|
|
30
|
+
*/
|
|
31
|
+
export const INGEST_PATH = "/ingest";
|
|
32
|
+
|
|
33
|
+
export function regionNamed(name: string | undefined): Region {
|
|
34
|
+
return name === "eu" ? REGIONS.eu : REGIONS.us;
|
|
35
|
+
}
|
package/src/visitor.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Who a person is to PostHog, and the session they're in, carried from one app's origin to another's. */
|
|
2
|
+
export interface Visitor {
|
|
3
|
+
readonly distinctId: string;
|
|
4
|
+
readonly sessionId: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const VISITOR = "ks-visitor";
|
|
8
|
+
const SESSION = "ks-session";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `url` with the visitor in its fragment. A fragment never reaches a server, and the address's
|
|
12
|
+
* other fragment parts (the handoff's note, an anchor) stay as they are.
|
|
13
|
+
*/
|
|
14
|
+
export function withVisitor(url: string, visitor: Visitor): string {
|
|
15
|
+
const at = url.indexOf("#");
|
|
16
|
+
const parts = at < 0 ? [] : url.slice(at + 1).split("&");
|
|
17
|
+
const kept = parts.filter((part) => part !== "" && !isOurs(part));
|
|
18
|
+
const carried = [
|
|
19
|
+
`${VISITOR}=${encodeURIComponent(visitor.distinctId)}`,
|
|
20
|
+
`${SESSION}=${encodeURIComponent(visitor.sessionId)}`,
|
|
21
|
+
];
|
|
22
|
+
return `${at < 0 ? url : url.slice(0, at)}#${[...kept, ...carried].join("&")}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The visitor this page's address carries, taken off it so a reload, Back or a copied link carries
|
|
27
|
+
* no one. The rest of the fragment is left for whoever else reads it.
|
|
28
|
+
*/
|
|
29
|
+
export function takeVisitor(location: Location, history: History): Visitor | null {
|
|
30
|
+
const parts = location.hash.slice(1).split("&");
|
|
31
|
+
const value = (name: string) => {
|
|
32
|
+
const part = parts.find((candidate) => candidate.startsWith(`${name}=`));
|
|
33
|
+
return part === undefined ? null : decodeURIComponent(part.slice(name.length + 1));
|
|
34
|
+
};
|
|
35
|
+
const distinctId = value(VISITOR);
|
|
36
|
+
const sessionId = value(SESSION);
|
|
37
|
+
if (distinctId === null && sessionId === null) return null;
|
|
38
|
+
const rest = parts.filter((part) => part !== "" && !isOurs(part)).join("&");
|
|
39
|
+
history.replaceState(
|
|
40
|
+
history.state,
|
|
41
|
+
"",
|
|
42
|
+
location.pathname + location.search + (rest === "" ? "" : `#${rest}`),
|
|
43
|
+
);
|
|
44
|
+
return distinctId && sessionId ? { distinctId, sessionId } : null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isOurs(part: string): boolean {
|
|
48
|
+
return part.startsWith(`${VISITOR}=`) || part.startsWith(`${SESSION}=`);
|
|
49
|
+
}
|
package/README.md
DELETED