@posthog/next 0.4.47 → 0.4.48

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.
@@ -1,5 +1,3 @@
1
- export declare const COOKIE_PREFIX = "ph_";
2
- export declare const COOKIE_SUFFIX = "_posthog";
3
1
  export declare const DEFAULT_API_HOST = "https://us.i.posthog.com";
4
2
  export declare const COOKIE_MAX_AGE_SECONDS: number;
5
3
  export declare const DEFAULT_INGEST_PATH = "/ingest";
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/shared/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,QAAQ,CAAA;AAClC,eAAO,MAAM,aAAa,aAAa,CAAA;AACvC,eAAO,MAAM,gBAAgB,6BAA6B,CAAA;AAC1D,eAAO,MAAM,sBAAsB,QAAqB,CAAA;AACxD,eAAO,MAAM,mBAAmB,YAAY,CAAA"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/shared/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,6BAA6B,CAAA;AAC1D,eAAO,MAAM,sBAAsB,QAAqB,CAAA;AACxD,eAAO,MAAM,mBAAmB,YAAY,CAAA"}
@@ -1,5 +1,3 @@
1
- export const COOKIE_PREFIX = 'ph_';
2
- export const COOKIE_SUFFIX = '_posthog';
3
1
  export const DEFAULT_API_HOST = 'https://us.i.posthog.com';
4
2
  export const COOKIE_MAX_AGE_SECONDS = 365 * 24 * 60 * 60;
5
3
  export const DEFAULT_INGEST_PATH = '/ingest';
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/shared/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,CAAA;AAClC,MAAM,CAAC,MAAM,aAAa,GAAG,UAAU,CAAA;AACvC,MAAM,CAAC,MAAM,gBAAgB,GAAG,0BAA0B,CAAA;AAC1D,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAA;AACxD,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,CAAA"}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/shared/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,gBAAgB,GAAG,0BAA0B,CAAA;AAC1D,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAA;AACxD,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,CAAA"}
@@ -1,69 +1,3 @@
1
- /**
2
- * Minimal cookie-reading interface compatible with Next.js `cookies()`,
3
- * `request.cookies`, and plain objects.
4
- */
5
- export interface CookieStore {
6
- get(name: string): {
7
- value: string;
8
- } | undefined;
9
- }
10
- /**
11
- * Adapts a raw `Cookie` header string into a {@link CookieStore}.
12
- */
13
- export declare function cookieStoreFromHeader(cookieHeader: string): CookieStore;
14
- export interface PostHogCookieState {
15
- distinctId: string;
16
- isIdentified: boolean;
17
- sessionId?: string;
18
- deviceId?: string;
19
- }
20
- /**
21
- * Returns the PostHog cookie name for the given API key.
22
- *
23
- * PostHog-js stores state in a cookie named `ph_<sanitized_token>_posthog`.
24
- * The token is sanitized by replacing `+` with `PL`, `/` with `SL`, `=` with `EQ`.
25
- *
26
- * @param apiKey - The PostHog project API key
27
- * @returns The cookie name string
28
- */
29
- export declare function getPostHogCookieName(apiKey: string): string;
30
- /**
31
- * Serializes an anonymous ID into the JSON format posthog-js expects.
32
- *
33
- * When `distinct_id === $device_id`, posthog-js treats the user as anonymous.
34
- *
35
- * @param anonymousId - The anonymous distinct ID to serialize
36
- * @returns JSON string suitable for the PostHog cookie value
37
- */
38
- export declare function serializePostHogCookie(anonymousId: string): string;
39
- /**
40
- * Reads and parses the PostHog cookie from a cookie store.
41
- *
42
- * Compatible with Next.js `cookies()`, `request.cookies`, and any object
43
- * with a `get(name)` method that returns `{ value: string } | undefined`.
44
- */
45
- export declare function readPostHogCookie(cookies: CookieStore, apiKey: string): PostHogCookieState | null;
46
- /**
47
- * Converts cookie state into PostHog properties (e.g. `$session_id`, `$device_id`).
48
- */
49
- export declare function cookieStateToProperties(state: PostHogCookieState | null): Record<string, string> | undefined;
50
- /**
51
- * Parses a PostHog cookie value and extracts identity information.
52
- *
53
- * The cookie value is a JSON object containing `distinct_id` and `$user_state`.
54
- * A user is considered identified if `$user_state` is `'identified'`.
55
- *
56
- * @param cookieValue - The raw cookie string value
57
- * @returns Parsed identity state, or null if the cookie is missing/invalid
58
- */
59
- export declare function parsePostHogCookie(cookieValue: string): PostHogCookieState | null;
60
- export interface ConsentCookieConfig {
61
- consent_persistence_name?: string | null;
62
- opt_out_capturing_cookie_prefix?: string | null;
63
- }
64
- export declare function getConsentCookieName(apiKey: string, config?: ConsentCookieConfig): string;
65
- export interface ConsentConfig extends ConsentCookieConfig {
66
- opt_out_capturing_by_default?: boolean;
67
- }
68
- export declare function isOptedOut(cookies: CookieStore, apiKey: string, config?: ConsentConfig): boolean;
1
+ export { cookieStateToProperties, cookieStoreFromHeader, getConsentCookieName, getPostHogCookieName, isOptedOut, parsePostHogCookie, readPostHogCookie, serializePostHogCookie, } from '@posthog/core';
2
+ export type { ConsentConfig, ConsentCookieConfig, CookieStore, PostHogCookieState } from '@posthog/core';
69
3
  //# sourceMappingURL=cookie.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../src/shared/cookie.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,WAAW,WAAW;IACxB,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAA;CACnD;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,YAAY,EAAE,MAAM,GAAG,WAAW,CAWvE;AAED,MAAM,WAAW,kBAAkB;IAC/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pB,UAAU,EACV,kBAAkB,EAClB,iBAAiB,EACjB,sBAAsB,GACzB,MAAM,eAAe,CAAA;AACtB,YAAY,EAAE,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA"}
@@ -1,126 +1,6 @@
1
- import { uuidv7, isNoLike, isArray } from '@posthog/core';
2
- import { COOKIE_PREFIX, COOKIE_SUFFIX } from './constants.js';
3
- /**
4
- * Adapts a raw `Cookie` header string into a {@link CookieStore}.
5
- */
6
- export function cookieStoreFromHeader(cookieHeader) {
7
- const cookies = {};
8
- if (cookieHeader) {
9
- for (const pair of cookieHeader.split(';')) {
10
- const [key, ...valueParts] = pair.trim().split('=');
11
- if (key) {
12
- cookies[key.trim()] = decodeURIComponent(valueParts.join('=').trim());
13
- }
14
- }
15
- }
16
- return { get: (name) => (name in cookies ? { value: cookies[name] } : undefined) };
17
- }
18
- /**
19
- * Returns the PostHog cookie name for the given API key.
20
- *
21
- * PostHog-js stores state in a cookie named `ph_<sanitized_token>_posthog`.
22
- * The token is sanitized by replacing `+` with `PL`, `/` with `SL`, `=` with `EQ`.
23
- *
24
- * @param apiKey - The PostHog project API key
25
- * @returns The cookie name string
26
- */
27
- export function getPostHogCookieName(apiKey) {
28
- const sanitized = apiKey.replace(/\+/g, 'PL').replace(/\//g, 'SL').replace(/=/g, 'EQ');
29
- return `${COOKIE_PREFIX}${sanitized}${COOKIE_SUFFIX}`;
30
- }
31
- /**
32
- * Serializes an anonymous ID into the JSON format posthog-js expects.
33
- *
34
- * When `distinct_id === $device_id`, posthog-js treats the user as anonymous.
35
- *
36
- * @param anonymousId - The anonymous distinct ID to serialize
37
- * @returns JSON string suitable for the PostHog cookie value
38
- */
39
- export function serializePostHogCookie(anonymousId) {
40
- const now = Date.now();
41
- const sessionId = uuidv7();
42
- return JSON.stringify({
43
- distinct_id: anonymousId,
44
- $device_id: anonymousId,
45
- $user_state: 'anonymous',
46
- $sesid: [now, sessionId, now],
47
- });
48
- }
49
- /**
50
- * Reads and parses the PostHog cookie from a cookie store.
51
- *
52
- * Compatible with Next.js `cookies()`, `request.cookies`, and any object
53
- * with a `get(name)` method that returns `{ value: string } | undefined`.
54
- */
55
- export function readPostHogCookie(cookies, apiKey) {
56
- const cookieName = getPostHogCookieName(apiKey);
57
- const cookie = cookies.get(cookieName);
58
- return cookie ? parsePostHogCookie(cookie.value) : null;
59
- }
60
- /**
61
- * Converts cookie state into PostHog properties (e.g. `$session_id`, `$device_id`).
62
- */
63
- export function cookieStateToProperties(state) {
64
- if (!state) {
65
- return undefined;
66
- }
67
- const props = {};
68
- if (state.sessionId) {
69
- props.$session_id = state.sessionId;
70
- }
71
- if (state.deviceId) {
72
- props.$device_id = state.deviceId;
73
- }
74
- return Object.keys(props).length > 0 ? props : undefined;
75
- }
76
- /**
77
- * Parses a PostHog cookie value and extracts identity information.
78
- *
79
- * The cookie value is a JSON object containing `distinct_id` and `$user_state`.
80
- * A user is considered identified if `$user_state` is `'identified'`.
81
- *
82
- * @param cookieValue - The raw cookie string value
83
- * @returns Parsed identity state, or null if the cookie is missing/invalid
84
- */
85
- export function parsePostHogCookie(cookieValue) {
86
- if (!cookieValue) {
87
- return null;
88
- }
89
- try {
90
- const parsed = JSON.parse(cookieValue);
91
- if (!parsed || typeof parsed !== 'object' || !parsed.distinct_id) {
92
- return null;
93
- }
94
- // $sesid is stored as [lastActivityTimestamp, sessionId, sessionStartTimestamp]
95
- const sesid = isArray(parsed.$sesid) ? parsed.$sesid[1] : undefined;
96
- return {
97
- distinctId: String(parsed.distinct_id),
98
- isIdentified: parsed.$user_state === 'identified',
99
- sessionId: typeof sesid === 'string' ? sesid : undefined,
100
- deviceId: typeof parsed.$device_id === 'string' ? parsed.$device_id : undefined,
101
- };
102
- }
103
- catch {
104
- return null;
105
- }
106
- }
107
- const CONSENT_PREFIX = '__ph_opt_in_out_';
108
- export function getConsentCookieName(apiKey, config) {
109
- if (config?.consent_persistence_name) {
110
- return config.consent_persistence_name;
111
- }
112
- if (config?.opt_out_capturing_cookie_prefix) {
113
- return config.opt_out_capturing_cookie_prefix + apiKey;
114
- }
115
- return CONSENT_PREFIX + apiKey;
116
- }
117
- export function isOptedOut(cookies, apiKey, config) {
118
- const cookieName = getConsentCookieName(apiKey, config);
119
- const cookie = cookies.get(cookieName);
120
- if (cookie) {
121
- return isNoLike(cookie.value);
122
- }
123
- // No consent cookie means pending — defer to config
124
- return config?.opt_out_capturing_by_default ?? false;
125
- }
1
+ // Cookie helpers were lifted to `@posthog/core` so all SDKs (posthog-node,
2
+ // posthog-js-lite, etc.) can share them. This module re-exports the public
3
+ // surface for any internal `@posthog/next` imports — keep using these from
4
+ // `./shared/cookie` within next/, or import directly from `@posthog/core`.
5
+ export { cookieStateToProperties, cookieStoreFromHeader, getConsentCookieName, getPostHogCookieName, isOptedOut, parsePostHogCookie, readPostHogCookie, serializePostHogCookie, } from '@posthog/core';
126
6
  //# sourceMappingURL=cookie.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../src/shared/cookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AACzD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAU7D;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,YAAoB,EAAe;IACrE,MAAM,OAAO,GAA2B,EAAE,CAAA;IAC1C,IAAI,YAAY,EAAE,CAAC;QACf,KAAK,MAAM,IAAI,IAAI,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;YACzC,MAAM,CAAC,GAAG,EAAE,GAAG,UAAU,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACnD,IAAI,GAAG,EAAE,CAAC;gBACN,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,GAAG,kBAAkB,CAA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BAAsB,GACzB,MAAM,eAAe,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/next",
3
- "version": "0.4.47",
3
+ "version": "0.4.48",
4
4
  "description": "PostHog integration for Next.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -50,10 +50,10 @@
50
50
  ],
51
51
  "dependencies": {
52
52
  "server-only": "^0.0.1",
53
- "@posthog/core": "1.28.7",
53
+ "@posthog/core": "1.29.0",
54
54
  "@posthog/react": "1.9.0",
55
- "posthog-node": "5.33.7",
56
- "posthog-js": "1.373.2"
55
+ "posthog-js": "1.373.3",
56
+ "posthog-node": "5.34.0"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@testing-library/jest-dom": "^6.6.3",
@@ -1,5 +1,3 @@
1
- export const COOKIE_PREFIX = 'ph_'
2
- export const COOKIE_SUFFIX = '_posthog'
3
1
  export const DEFAULT_API_HOST = 'https://us.i.posthog.com'
4
2
  export const COOKIE_MAX_AGE_SECONDS = 365 * 24 * 60 * 60
5
3
  export const DEFAULT_INGEST_PATH = '/ingest'
@@ -1,162 +1,15 @@
1
- import { uuidv7, isNoLike, isArray } from '@posthog/core'
2
- import { COOKIE_PREFIX, COOKIE_SUFFIX } from './constants.js'
3
-
4
- /**
5
- * Minimal cookie-reading interface compatible with Next.js `cookies()`,
6
- * `request.cookies`, and plain objects.
7
- */
8
- export interface CookieStore {
9
- get(name: string): { value: string } | undefined
10
- }
11
-
12
- /**
13
- * Adapts a raw `Cookie` header string into a {@link CookieStore}.
14
- */
15
- export function cookieStoreFromHeader(cookieHeader: string): CookieStore {
16
- const cookies: Record<string, string> = {}
17
- if (cookieHeader) {
18
- for (const pair of cookieHeader.split(';')) {
19
- const [key, ...valueParts] = pair.trim().split('=')
20
- if (key) {
21
- cookies[key.trim()] = decodeURIComponent(valueParts.join('=').trim())
22
- }
23
- }
24
- }
25
- return { get: (name: string) => (name in cookies ? { value: cookies[name] } : undefined) }
26
- }
27
-
28
- export interface PostHogCookieState {
29
- distinctId: string
30
- isIdentified: boolean
31
- sessionId?: string
32
- deviceId?: string
33
- }
34
-
35
- /**
36
- * Returns the PostHog cookie name for the given API key.
37
- *
38
- * PostHog-js stores state in a cookie named `ph_<sanitized_token>_posthog`.
39
- * The token is sanitized by replacing `+` with `PL`, `/` with `SL`, `=` with `EQ`.
40
- *
41
- * @param apiKey - The PostHog project API key
42
- * @returns The cookie name string
43
- */
44
- export function getPostHogCookieName(apiKey: string): string {
45
- const sanitized = apiKey.replace(/\+/g, 'PL').replace(/\//g, 'SL').replace(/=/g, 'EQ')
46
- return `${COOKIE_PREFIX}${sanitized}${COOKIE_SUFFIX}`
47
- }
48
-
49
- /**
50
- * Serializes an anonymous ID into the JSON format posthog-js expects.
51
- *
52
- * When `distinct_id === $device_id`, posthog-js treats the user as anonymous.
53
- *
54
- * @param anonymousId - The anonymous distinct ID to serialize
55
- * @returns JSON string suitable for the PostHog cookie value
56
- */
57
- export function serializePostHogCookie(anonymousId: string): string {
58
- const now = Date.now()
59
- const sessionId = uuidv7()
60
- return JSON.stringify({
61
- distinct_id: anonymousId,
62
- $device_id: anonymousId,
63
- $user_state: 'anonymous',
64
- $sesid: [now, sessionId, now],
65
- })
66
- }
67
-
68
- /**
69
- * Reads and parses the PostHog cookie from a cookie store.
70
- *
71
- * Compatible with Next.js `cookies()`, `request.cookies`, and any object
72
- * with a `get(name)` method that returns `{ value: string } | undefined`.
73
- */
74
- export function readPostHogCookie(cookies: CookieStore, apiKey: string): PostHogCookieState | null {
75
- const cookieName = getPostHogCookieName(apiKey)
76
- const cookie = cookies.get(cookieName)
77
- return cookie ? parsePostHogCookie(cookie.value) : null
78
- }
79
-
80
- /**
81
- * Converts cookie state into PostHog properties (e.g. `$session_id`, `$device_id`).
82
- */
83
- export function cookieStateToProperties(state: PostHogCookieState | null): Record<string, string> | undefined {
84
- if (!state) {
85
- return undefined
86
- }
87
- const props: Record<string, string> = {}
88
- if (state.sessionId) {
89
- props.$session_id = state.sessionId
90
- }
91
- if (state.deviceId) {
92
- props.$device_id = state.deviceId
93
- }
94
- return Object.keys(props).length > 0 ? props : undefined
95
- }
96
-
97
- /**
98
- * Parses a PostHog cookie value and extracts identity information.
99
- *
100
- * The cookie value is a JSON object containing `distinct_id` and `$user_state`.
101
- * A user is considered identified if `$user_state` is `'identified'`.
102
- *
103
- * @param cookieValue - The raw cookie string value
104
- * @returns Parsed identity state, or null if the cookie is missing/invalid
105
- */
106
- export function parsePostHogCookie(cookieValue: string): PostHogCookieState | null {
107
- if (!cookieValue) {
108
- return null
109
- }
110
-
111
- try {
112
- const parsed = JSON.parse(cookieValue)
113
- if (!parsed || typeof parsed !== 'object' || !parsed.distinct_id) {
114
- return null
115
- }
116
-
117
- // $sesid is stored as [lastActivityTimestamp, sessionId, sessionStartTimestamp]
118
- const sesid = isArray(parsed.$sesid) ? parsed.$sesid[1] : undefined
119
-
120
- return {
121
- distinctId: String(parsed.distinct_id),
122
- isIdentified: parsed.$user_state === 'identified',
123
- sessionId: typeof sesid === 'string' ? sesid : undefined,
124
- deviceId: typeof parsed.$device_id === 'string' ? parsed.$device_id : undefined,
125
- }
126
- } catch {
127
- return null
128
- }
129
- }
130
-
131
- export interface ConsentCookieConfig {
132
- consent_persistence_name?: string | null
133
- opt_out_capturing_cookie_prefix?: string | null
134
- }
135
-
136
- const CONSENT_PREFIX = '__ph_opt_in_out_'
137
-
138
- export function getConsentCookieName(apiKey: string, config?: ConsentCookieConfig): string {
139
- if (config?.consent_persistence_name) {
140
- return config.consent_persistence_name
141
- }
142
- if (config?.opt_out_capturing_cookie_prefix) {
143
- return config.opt_out_capturing_cookie_prefix + apiKey
144
- }
145
- return CONSENT_PREFIX + apiKey
146
- }
147
-
148
- export interface ConsentConfig extends ConsentCookieConfig {
149
- opt_out_capturing_by_default?: boolean
150
- }
151
-
152
- export function isOptedOut(cookies: CookieStore, apiKey: string, config?: ConsentConfig): boolean {
153
- const cookieName = getConsentCookieName(apiKey, config)
154
- const cookie = cookies.get(cookieName)
155
-
156
- if (cookie) {
157
- return isNoLike(cookie.value)
158
- }
159
-
160
- // No consent cookie means pending — defer to config
161
- return config?.opt_out_capturing_by_default ?? false
162
- }
1
+ // Cookie helpers were lifted to `@posthog/core` so all SDKs (posthog-node,
2
+ // posthog-js-lite, etc.) can share them. This module re-exports the public
3
+ // surface for any internal `@posthog/next` imports — keep using these from
4
+ // `./shared/cookie` within next/, or import directly from `@posthog/core`.
5
+ export {
6
+ cookieStateToProperties,
7
+ cookieStoreFromHeader,
8
+ getConsentCookieName,
9
+ getPostHogCookieName,
10
+ isOptedOut,
11
+ parsePostHogCookie,
12
+ readPostHogCookie,
13
+ serializePostHogCookie,
14
+ } from '@posthog/core'
15
+ export type { ConsentConfig, ConsentCookieConfig, CookieStore, PostHogCookieState } from '@posthog/core'