@easy-cookie/posthog 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Keeps [PostHog](https://posthog.com) in sync with easy-cookie consent, with no page reload. It follows the pattern MythicVoices uses in production:
4
4
 
5
- - **Before a choice:** PostHog captures nothing.
5
+ - **Before a choice:** PostHog captures nothing (default), or counts visits without cookies with `beforeChoice: 'cookieless'`. The adapter waits for the runtime to be ready before acting, so a returning visitor is never opted out by mistake.
6
6
  - **After a rejection:** cookieless mode only. PostHog sets no cookies and no storage, and creates no person profiles. Autocapture, exceptions, web vitals and heatmaps are dropped.
7
7
  - **After an acceptance:** normal capture with cookies, then `identify()` for logged-in users.
8
8
 
package/dist/index.cjs CHANGED
@@ -30,21 +30,31 @@ function connectPostHog(posthog, api, options = {}) {
30
30
  const category = options.category ?? "measurement";
31
31
  const granted = () => options.service ? api.isServiceGranted(options.service) : api.isGranted(category);
32
32
  let last = null;
33
+ const status = () => {
34
+ const explicit = posthog.get_explicit_consent_status?.();
35
+ if (explicit === "granted") return "in";
36
+ if (explicit === "denied") return "out";
37
+ if (explicit === "pending") return "pending";
38
+ if (posthog.has_opted_in_capturing?.()) return "in";
39
+ if (posthog.has_opted_out_capturing?.()) return "out";
40
+ return "pending";
41
+ };
33
42
  const apply = () => {
43
+ if (!api.getConfig()) return;
34
44
  if (!api.get()) {
35
45
  if (options.beforeChoice === "cookieless") {
36
46
  if (last !== "out") {
37
- if (!posthog.has_opted_out_capturing?.()) posthog.opt_out_capturing();
47
+ if (status() !== "out") posthog.opt_out_capturing();
38
48
  last = "out";
39
49
  }
40
- } else if (posthog.has_opted_in_capturing?.()) {
50
+ } else if (status() === "in") {
41
51
  posthog.clear_opt_in_out_capturing?.();
42
52
  }
43
53
  return;
44
54
  }
45
55
  if (granted()) {
46
56
  if (last !== "in") {
47
- if (!posthog.has_opted_in_capturing?.()) posthog.opt_in_capturing({ captureEventName: false });
57
+ if (status() !== "in") posthog.opt_in_capturing({ captureEventName: false });
48
58
  if (options.sessionReplay) posthog.startSessionRecording?.();
49
59
  last = "in";
50
60
  }
@@ -52,11 +62,11 @@ function connectPostHog(posthog, api, options = {}) {
52
62
  if (id && posthog.identify && posthog.get_distinct_id?.() !== id) posthog.identify(id);
53
63
  } else if (last !== "out") {
54
64
  if (options.sessionReplay) posthog.stopSessionRecording?.();
55
- if (!posthog.has_opted_out_capturing?.()) posthog.opt_out_capturing();
65
+ if (status() !== "out") posthog.opt_out_capturing();
56
66
  last = "out";
57
67
  }
58
68
  };
59
- const events = ["replay", "consent", "change"];
69
+ const events = ["ready", "replay", "consent", "change"];
60
70
  const unsubs = events.map((ev) => api.on(ev, apply));
61
71
  apply();
62
72
  return () => unsubs.forEach((u) => u());
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { EasyCookieApi, EasyCookieEvent } from '@easy-cookie/core/shared';\n\n/**\n * The few posthog-js methods the adapter uses. Typed loosely on purpose, so\n * `posthog-js` stays an optional peer dependency and any compatible client works.\n */\nexport interface PostHogLike {\n opt_in_capturing(options?: { captureEventName?: string | null | false; [key: string]: unknown }): void;\n opt_out_capturing(): void;\n has_opted_in_capturing?(): boolean;\n has_opted_out_capturing?(): boolean;\n clear_opt_in_out_capturing?(): void;\n startSessionRecording?(...args: unknown[]): void;\n stopSessionRecording?(): void;\n identify?(distinctId?: string, ...rest: unknown[]): void;\n get_distinct_id?(): string;\n}\n\n/** Minimal shape of a PostHog event passed to `before_send`. */\nexport interface PostHogEventLike {\n event: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- matches posthog-js `Properties`\n properties?: Record<string, any>;\n}\n\nexport interface ConnectPostHogOptions {\n /** Category that allows PostHog cookies/profiles. Default `measurement`. */\n category?: string;\n /** Service id: when set, the service's own consent decides instead of the category. */\n service?: string;\n /**\n * Return the logged-in user id (or undefined). Called after opt-in, so the\n * identified person profile is created only with consent.\n */\n identify?: () => string | undefined | null;\n /**\n * Before the visitor's first choice:\n * - `'pending'` (default): capture nothing. A stale opt-in left in storage by an\n * earlier setup is cleared, so nothing is captured with cookies.\n * - `'cookieless'`: count visits without cookies (same as after a rejection).\n * Only if your privacy policy covers cookieless measurement without consent.\n */\n beforeChoice?: 'pending' | 'cookieless';\n /** Start session recording on opt-in and stop it on opt-out. Default false. */\n sessionReplay?: boolean;\n}\n\n/**\n * Keep PostHog's capture state in sync with the visitor's consent, without reloads.\n *\n * PostHog must be initialised by the host with `cookieless_mode: 'on_reject'`\n * (see {@link posthogInitOptions}): before a choice it captures nothing, after a\n * rejection it keeps counting anonymously with no cookies or storage, and after\n * an acceptance it switches to normal cookie-based capture.\n *\n * On `replay`, `consent` and `change` the adapter calls `opt_in_capturing()` or\n * `opt_out_capturing()`. Returns an unsubscribe function.\n */\nexport function connectPostHog(posthog: PostHogLike, api: EasyCookieApi, options: ConnectPostHogOptions = {}): () => void {\n const category = options.category ?? 'measurement';\n const granted = () => (options.service ? api.isServiceGranted(options.service) : api.isGranted(category));\n let last: 'in' | 'out' | null = null;\n\n const apply = () => {\n if (!api.get()) {\n if (options.beforeChoice === 'cookieless') {\n if (last !== 'out') {\n if (!posthog.has_opted_out_capturing?.()) posthog.opt_out_capturing();\n last = 'out';\n }\n } else if (posthog.has_opted_in_capturing?.()) {\n // Pending: an opt-in stored by a previous banner must not keep capturing.\n posthog.clear_opt_in_out_capturing?.();\n }\n return;\n }\n if (granted()) {\n if (last !== 'in') {\n // No `$opt_in` event: the consent itself is recorded by easy-cookie.\n if (!posthog.has_opted_in_capturing?.()) posthog.opt_in_capturing({ captureEventName: false });\n if (options.sessionReplay) posthog.startSessionRecording?.();\n last = 'in';\n }\n const id = options.identify?.();\n if (id && posthog.identify && posthog.get_distinct_id?.() !== id) posthog.identify(id);\n } else if (last !== 'out') {\n if (options.sessionReplay) posthog.stopSessionRecording?.();\n if (!posthog.has_opted_out_capturing?.()) posthog.opt_out_capturing();\n last = 'out';\n }\n };\n\n const events: EasyCookieEvent[] = ['replay', 'consent', 'change'];\n const unsubs = events.map((ev) => api.on(ev, apply));\n // The stored choice may have been replayed before we subscribed.\n apply();\n return () => unsubs.forEach((u) => u());\n}\n\n/** Events dropped by {@link consentBeforeSend} while consent is missing. */\nexport const NON_ESSENTIAL_EVENTS: readonly string[] = [\n '$autocapture',\n '$rageclick',\n '$dead_click',\n '$exception',\n '$web_vitals',\n '$$heatmap',\n '$heatmap',\n];\n\nexport interface ConsentBeforeSendOptions {\n /** Category checked by the default `isGranted`. Default `measurement`. */\n category?: string;\n /**\n * Consent check. Defaults to the runtime when `api` is given, otherwise to the\n * state published by the head script (`window.__easyCookieHead.granted`).\n */\n isGranted?: () => boolean;\n api?: Pick<EasyCookieApi, 'isGranted'> | null;\n /** Event names to drop without consent. Default {@link NON_ESSENTIAL_EVENTS}. */\n drop?: readonly string[];\n}\n\nfunction headGranted(category: string): boolean {\n const head = (globalThis as { __easyCookieHead?: { granted?: string[]; valid?: boolean } }).__easyCookieHead;\n return !!head?.valid && !!head.granted?.includes(category);\n}\n\n/**\n * `before_send` filter: without consent, drop autocapture, exceptions, web vitals\n * and heatmap events, so cookieless mode only sends the essentials (pageviews,\n * explicit captures). Pass it as `before_send` or use {@link posthogInitOptions}.\n */\nexport function consentBeforeSend(options: ConsentBeforeSendOptions = {}) {\n const category = options.category ?? 'measurement';\n const drop = new Set(options.drop ?? NON_ESSENTIAL_EVENTS);\n const isGranted =\n options.isGranted ?? (() => (options.api ? options.api.isGranted(category) : headGranted(category)));\n return <T extends PostHogEventLike | null>(event: T): T | null => {\n if (!event) return event;\n if (drop.has(event.event) && !isGranted()) return null;\n return event;\n };\n}\n\nexport interface PostHogInitOptions {\n cookieless_mode: 'on_reject';\n persistence: 'localStorage+cookie';\n person_profiles: 'identified_only';\n before_send: ReturnType<typeof consentBeforeSend>;\n [key: string]: unknown;\n}\n\n/**\n * Recommended `posthog.init()` options for easy-cookie:\n * - `cookieless_mode: 'on_reject'` (requires \"Cookieless server hash mode\" enabled\n * in the PostHog project settings);\n * - `persistence: 'localStorage+cookie'` (only used after opt-in);\n * - `person_profiles: 'identified_only'`;\n * - a consent-aware `before_send` filter.\n *\n * Spread your own options after it: `posthog.init(key, { ...posthogInitOptions(), api_host })`.\n */\nexport function posthogInitOptions(options: ConsentBeforeSendOptions = {}): PostHogInitOptions {\n return {\n cookieless_mode: 'on_reject',\n persistence: 'localStorage+cookie',\n person_profiles: 'identified_only',\n before_send: consentBeforeSend(options),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA0DO,SAAS,eAAe,SAAsB,KAAoB,UAAiC,CAAC,GAAe;AACxH,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,UAAU,MAAO,QAAQ,UAAU,IAAI,iBAAiB,QAAQ,OAAO,IAAI,IAAI,UAAU,QAAQ;AACvG,MAAI,OAA4B;AAEhC,QAAM,QAAQ,MAAM;AAClB,QAAI,CAAC,IAAI,IAAI,GAAG;AACd,UAAI,QAAQ,iBAAiB,cAAc;AACzC,YAAI,SAAS,OAAO;AAClB,cAAI,CAAC,QAAQ,0BAA0B,EAAG,SAAQ,kBAAkB;AACpE,iBAAO;AAAA,QACT;AAAA,MACF,WAAW,QAAQ,yBAAyB,GAAG;AAE7C,gBAAQ,6BAA6B;AAAA,MACvC;AACA;AAAA,IACF;AACA,QAAI,QAAQ,GAAG;AACb,UAAI,SAAS,MAAM;AAEjB,YAAI,CAAC,QAAQ,yBAAyB,EAAG,SAAQ,iBAAiB,EAAE,kBAAkB,MAAM,CAAC;AAC7F,YAAI,QAAQ,cAAe,SAAQ,wBAAwB;AAC3D,eAAO;AAAA,MACT;AACA,YAAM,KAAK,QAAQ,WAAW;AAC9B,UAAI,MAAM,QAAQ,YAAY,QAAQ,kBAAkB,MAAM,GAAI,SAAQ,SAAS,EAAE;AAAA,IACvF,WAAW,SAAS,OAAO;AACzB,UAAI,QAAQ,cAAe,SAAQ,uBAAuB;AAC1D,UAAI,CAAC,QAAQ,0BAA0B,EAAG,SAAQ,kBAAkB;AACpE,aAAO;AAAA,IACT;AAAA,EACF;AAEA,QAAM,SAA4B,CAAC,UAAU,WAAW,QAAQ;AAChE,QAAM,SAAS,OAAO,IAAI,CAAC,OAAO,IAAI,GAAG,IAAI,KAAK,CAAC;AAEnD,QAAM;AACN,SAAO,MAAM,OAAO,QAAQ,CAAC,MAAM,EAAE,CAAC;AACxC;AAGO,IAAM,uBAA0C;AAAA,EACrD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAeA,SAAS,YAAY,UAA2B;AAC9C,QAAM,OAAQ,WAA8E;AAC5F,SAAO,CAAC,CAAC,MAAM,SAAS,CAAC,CAAC,KAAK,SAAS,SAAS,QAAQ;AAC3D;AAOO,SAAS,kBAAkB,UAAoC,CAAC,GAAG;AACxE,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,OAAO,IAAI,IAAI,QAAQ,QAAQ,oBAAoB;AACzD,QAAM,YACJ,QAAQ,cAAc,MAAO,QAAQ,MAAM,QAAQ,IAAI,UAAU,QAAQ,IAAI,YAAY,QAAQ;AACnG,SAAO,CAAoC,UAAuB;AAChE,QAAI,CAAC,MAAO,QAAO;AACnB,QAAI,KAAK,IAAI,MAAM,KAAK,KAAK,CAAC,UAAU,EAAG,QAAO;AAClD,WAAO;AAAA,EACT;AACF;AAoBO,SAAS,mBAAmB,UAAoC,CAAC,GAAuB;AAC7F,SAAO;AAAA,IACL,iBAAiB;AAAA,IACjB,aAAa;AAAA,IACb,iBAAiB;AAAA,IACjB,aAAa,kBAAkB,OAAO;AAAA,EACxC;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { EasyCookieApi, EasyCookieEvent } from '@easy-cookie/core/shared';\n\n/**\n * The few posthog-js methods the adapter uses. Typed loosely on purpose, so\n * `posthog-js` stays an optional peer dependency and any compatible client works.\n */\nexport interface PostHogLike {\n opt_in_capturing(options?: { captureEventName?: string | null | false; [key: string]: unknown }): void;\n opt_out_capturing(): void;\n has_opted_in_capturing?(): boolean;\n has_opted_out_capturing?(): boolean;\n clear_opt_in_out_capturing?(): void;\n /** posthog-js ≥ 1.2xx: 'granted' | 'denied' | 'pending' (reliable with cookieless_mode). */\n get_explicit_consent_status?(): string;\n startSessionRecording?(...args: unknown[]): void;\n stopSessionRecording?(): void;\n identify?(distinctId?: string, ...rest: unknown[]): void;\n get_distinct_id?(): string;\n}\n\n/** Minimal shape of a PostHog event passed to `before_send`. */\nexport interface PostHogEventLike {\n event: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- matches posthog-js `Properties`\n properties?: Record<string, any>;\n}\n\nexport interface ConnectPostHogOptions {\n /** Category that allows PostHog cookies/profiles. Default `measurement`. */\n category?: string;\n /** Service id: when set, the service's own consent decides instead of the category. */\n service?: string;\n /**\n * Return the logged-in user id (or undefined). Called after opt-in, so the\n * identified person profile is created only with consent.\n */\n identify?: () => string | undefined | null;\n /**\n * Before the visitor's first choice:\n * - `'pending'` (default): capture nothing. A stale opt-in left in storage by an\n * earlier setup is cleared, so nothing is captured with cookies.\n * - `'cookieless'`: count visits without cookies (same as after a rejection).\n * Only if your privacy policy covers cookieless measurement without consent.\n */\n beforeChoice?: 'pending' | 'cookieless';\n /** Start session recording on opt-in and stop it on opt-out. Default false. */\n sessionReplay?: boolean;\n}\n\n/**\n * Keep PostHog's capture state in sync with the visitor's consent, without reloads.\n *\n * PostHog must be initialised by the host with `cookieless_mode: 'on_reject'`\n * (see {@link posthogInitOptions}): before a choice it captures nothing (or counts\n * cookieless with `beforeChoice: 'cookieless'`), after a\n * rejection it keeps counting anonymously with no cookies or storage, and after\n * an acceptance it switches to normal cookie-based capture.\n *\n * On `replay`, `consent` and `change` the adapter calls `opt_in_capturing()` or\n * `opt_out_capturing()`. Returns an unsubscribe function.\n */\nexport function connectPostHog(posthog: PostHogLike, api: EasyCookieApi, options: ConnectPostHogOptions = {}): () => void {\n const category = options.category ?? 'measurement';\n const granted = () => (options.service ? api.isServiceGranted(options.service) : api.isGranted(category));\n let last: 'in' | 'out' | null = null;\n\n // With cookieless_mode, has_opted_out_capturing() is already true while consent\n // is pending, so prefer the explicit consent status when the client has it.\n const status = (): 'in' | 'out' | 'pending' => {\n const explicit = posthog.get_explicit_consent_status?.();\n if (explicit === 'granted') return 'in';\n if (explicit === 'denied') return 'out';\n if (explicit === 'pending') return 'pending';\n if (posthog.has_opted_in_capturing?.()) return 'in';\n if (posthog.has_opted_out_capturing?.()) return 'out';\n return 'pending';\n };\n\n const apply = () => {\n // Until run() has read the stored choice, \"no choice\" is not known yet:\n // acting now could opt out (and reset) a returning visitor who accepted.\n if (!api.getConfig()) return;\n if (!api.get()) {\n if (options.beforeChoice === 'cookieless') {\n if (last !== 'out') {\n if (status() !== 'out') posthog.opt_out_capturing();\n last = 'out';\n }\n } else if (status() === 'in') {\n // Pending: an opt-in stored by a previous banner must not keep capturing.\n posthog.clear_opt_in_out_capturing?.();\n }\n return;\n }\n if (granted()) {\n if (last !== 'in') {\n // No `$opt_in` event: the consent itself is recorded by easy-cookie.\n if (status() !== 'in') posthog.opt_in_capturing({ captureEventName: false });\n if (options.sessionReplay) posthog.startSessionRecording?.();\n last = 'in';\n }\n const id = options.identify?.();\n if (id && posthog.identify && posthog.get_distinct_id?.() !== id) posthog.identify(id);\n } else if (last !== 'out') {\n if (options.sessionReplay) posthog.stopSessionRecording?.();\n if (status() !== 'out') posthog.opt_out_capturing();\n last = 'out';\n }\n };\n\n const events: EasyCookieEvent[] = ['ready', 'replay', 'consent', 'change'];\n const unsubs = events.map((ev) => api.on(ev, apply));\n // The stored choice may have been replayed before we subscribed.\n apply();\n return () => unsubs.forEach((u) => u());\n}\n\n/** Events dropped by {@link consentBeforeSend} while consent is missing. */\nexport const NON_ESSENTIAL_EVENTS: readonly string[] = [\n '$autocapture',\n '$rageclick',\n '$dead_click',\n '$exception',\n '$web_vitals',\n '$$heatmap',\n '$heatmap',\n];\n\nexport interface ConsentBeforeSendOptions {\n /** Category checked by the default `isGranted`. Default `measurement`. */\n category?: string;\n /**\n * Consent check. Defaults to the runtime when `api` is given, otherwise to the\n * state published by the head script (`window.__easyCookieHead.granted`).\n */\n isGranted?: () => boolean;\n api?: Pick<EasyCookieApi, 'isGranted'> | null;\n /** Event names to drop without consent. Default {@link NON_ESSENTIAL_EVENTS}. */\n drop?: readonly string[];\n}\n\nfunction headGranted(category: string): boolean {\n const head = (globalThis as { __easyCookieHead?: { granted?: string[]; valid?: boolean } }).__easyCookieHead;\n return !!head?.valid && !!head.granted?.includes(category);\n}\n\n/**\n * `before_send` filter: without consent, drop autocapture, exceptions, web vitals\n * and heatmap events, so cookieless mode only sends the essentials (pageviews,\n * explicit captures). Pass it as `before_send` or use {@link posthogInitOptions}.\n */\nexport function consentBeforeSend(options: ConsentBeforeSendOptions = {}) {\n const category = options.category ?? 'measurement';\n const drop = new Set(options.drop ?? NON_ESSENTIAL_EVENTS);\n const isGranted =\n options.isGranted ?? (() => (options.api ? options.api.isGranted(category) : headGranted(category)));\n return <T extends PostHogEventLike | null>(event: T): T | null => {\n if (!event) return event;\n if (drop.has(event.event) && !isGranted()) return null;\n return event;\n };\n}\n\nexport interface PostHogInitOptions {\n cookieless_mode: 'on_reject';\n persistence: 'localStorage+cookie';\n person_profiles: 'identified_only';\n before_send: ReturnType<typeof consentBeforeSend>;\n [key: string]: unknown;\n}\n\n/**\n * Recommended `posthog.init()` options for easy-cookie:\n * - `cookieless_mode: 'on_reject'` (requires \"Cookieless server hash mode\" enabled\n * in the PostHog project settings);\n * - `persistence: 'localStorage+cookie'` (only used after opt-in);\n * - `person_profiles: 'identified_only'`;\n * - a consent-aware `before_send` filter.\n *\n * Spread your own options after it: `posthog.init(key, { ...posthogInitOptions(), api_host })`.\n */\nexport function posthogInitOptions(options: ConsentBeforeSendOptions = {}): PostHogInitOptions {\n return {\n cookieless_mode: 'on_reject',\n persistence: 'localStorage+cookie',\n person_profiles: 'identified_only',\n before_send: consentBeforeSend(options),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6DO,SAAS,eAAe,SAAsB,KAAoB,UAAiC,CAAC,GAAe;AACxH,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,UAAU,MAAO,QAAQ,UAAU,IAAI,iBAAiB,QAAQ,OAAO,IAAI,IAAI,UAAU,QAAQ;AACvG,MAAI,OAA4B;AAIhC,QAAM,SAAS,MAAgC;AAC7C,UAAM,WAAW,QAAQ,8BAA8B;AACvD,QAAI,aAAa,UAAW,QAAO;AACnC,QAAI,aAAa,SAAU,QAAO;AAClC,QAAI,aAAa,UAAW,QAAO;AACnC,QAAI,QAAQ,yBAAyB,EAAG,QAAO;AAC/C,QAAI,QAAQ,0BAA0B,EAAG,QAAO;AAChD,WAAO;AAAA,EACT;AAEA,QAAM,QAAQ,MAAM;AAGlB,QAAI,CAAC,IAAI,UAAU,EAAG;AACtB,QAAI,CAAC,IAAI,IAAI,GAAG;AACd,UAAI,QAAQ,iBAAiB,cAAc;AACzC,YAAI,SAAS,OAAO;AAClB,cAAI,OAAO,MAAM,MAAO,SAAQ,kBAAkB;AAClD,iBAAO;AAAA,QACT;AAAA,MACF,WAAW,OAAO,MAAM,MAAM;AAE5B,gBAAQ,6BAA6B;AAAA,MACvC;AACA;AAAA,IACF;AACA,QAAI,QAAQ,GAAG;AACb,UAAI,SAAS,MAAM;AAEjB,YAAI,OAAO,MAAM,KAAM,SAAQ,iBAAiB,EAAE,kBAAkB,MAAM,CAAC;AAC3E,YAAI,QAAQ,cAAe,SAAQ,wBAAwB;AAC3D,eAAO;AAAA,MACT;AACA,YAAM,KAAK,QAAQ,WAAW;AAC9B,UAAI,MAAM,QAAQ,YAAY,QAAQ,kBAAkB,MAAM,GAA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has_opted_out_capturing?(): boolean;
15
15
  clear_opt_in_out_capturing?(): void;
16
+ /** posthog-js ≥ 1.2xx: 'granted' | 'denied' | 'pending' (reliable with cookieless_mode). */
17
+ get_explicit_consent_status?(): string;
16
18
  startSessionRecording?(...args: unknown[]): void;
17
19
  stopSessionRecording?(): void;
18
20
  identify?(distinctId?: string, ...rest: unknown[]): void;
@@ -48,7 +50,8 @@ interface ConnectPostHogOptions {
48
50
  * Keep PostHog's capture state in sync with the visitor's consent, without reloads.
49
51
  *
50
52
  * PostHog must be initialised by the host with `cookieless_mode: 'on_reject'`
51
- * (see {@link posthogInitOptions}): before a choice it captures nothing, after a
53
+ * (see {@link posthogInitOptions}): before a choice it captures nothing (or counts
54
+ * cookieless with `beforeChoice: 'cookieless'`), after a
52
55
  * rejection it keeps counting anonymously with no cookies or storage, and after
53
56
  * an acceptance it switches to normal cookie-based capture.
54
57
  *
package/dist/index.d.ts CHANGED
@@ -13,6 +13,8 @@ interface PostHogLike {
13
13
  has_opted_in_capturing?(): boolean;
14
14
  has_opted_out_capturing?(): boolean;
15
15
  clear_opt_in_out_capturing?(): void;
16
+ /** posthog-js ≥ 1.2xx: 'granted' | 'denied' | 'pending' (reliable with cookieless_mode). */
17
+ get_explicit_consent_status?(): string;
16
18
  startSessionRecording?(...args: unknown[]): void;
17
19
  stopSessionRecording?(): void;
18
20
  identify?(distinctId?: string, ...rest: unknown[]): void;
@@ -48,7 +50,8 @@ interface ConnectPostHogOptions {
48
50
  * Keep PostHog's capture state in sync with the visitor's consent, without reloads.
49
51
  *
50
52
  * PostHog must be initialised by the host with `cookieless_mode: 'on_reject'`
51
- * (see {@link posthogInitOptions}): before a choice it captures nothing, after a
53
+ * (see {@link posthogInitOptions}): before a choice it captures nothing (or counts
54
+ * cookieless with `beforeChoice: 'cookieless'`), after a
52
55
  * rejection it keeps counting anonymously with no cookies or storage, and after
53
56
  * an acceptance it switches to normal cookie-based capture.
54
57
  *
package/dist/index.js CHANGED
@@ -3,21 +3,31 @@ function connectPostHog(posthog, api, options = {}) {
3
3
  const category = options.category ?? "measurement";
4
4
  const granted = () => options.service ? api.isServiceGranted(options.service) : api.isGranted(category);
5
5
  let last = null;
6
+ const status = () => {
7
+ const explicit = posthog.get_explicit_consent_status?.();
8
+ if (explicit === "granted") return "in";
9
+ if (explicit === "denied") return "out";
10
+ if (explicit === "pending") return "pending";
11
+ if (posthog.has_opted_in_capturing?.()) return "in";
12
+ if (posthog.has_opted_out_capturing?.()) return "out";
13
+ return "pending";
14
+ };
6
15
  const apply = () => {
16
+ if (!api.getConfig()) return;
7
17
  if (!api.get()) {
8
18
  if (options.beforeChoice === "cookieless") {
9
19
  if (last !== "out") {
10
- if (!posthog.has_opted_out_capturing?.()) posthog.opt_out_capturing();
20
+ if (status() !== "out") posthog.opt_out_capturing();
11
21
  last = "out";
12
22
  }
13
- } else if (posthog.has_opted_in_capturing?.()) {
23
+ } else if (status() === "in") {
14
24
  posthog.clear_opt_in_out_capturing?.();
15
25
  }
16
26
  return;
17
27
  }
18
28
  if (granted()) {
19
29
  if (last !== "in") {
20
- if (!posthog.has_opted_in_capturing?.()) posthog.opt_in_capturing({ captureEventName: false });
30
+ if (status() !== "in") posthog.opt_in_capturing({ captureEventName: false });
21
31
  if (options.sessionReplay) posthog.startSessionRecording?.();
22
32
  last = "in";
23
33
  }
@@ -25,11 +35,11 @@ function connectPostHog(posthog, api, options = {}) {
25
35
  if (id && posthog.identify && posthog.get_distinct_id?.() !== id) posthog.identify(id);
26
36
  } else if (last !== "out") {
27
37
  if (options.sessionReplay) posthog.stopSessionRecording?.();
28
- if (!posthog.has_opted_out_capturing?.()) posthog.opt_out_capturing();
38
+ if (status() !== "out") posthog.opt_out_capturing();
29
39
  last = "out";
30
40
  }
31
41
  };
32
- const events = ["replay", "consent", "change"];
42
+ const events = ["ready", "replay", "consent", "change"];
33
43
  const unsubs = events.map((ev) => api.on(ev, apply));
34
44
  apply();
35
45
  return () => unsubs.forEach((u) => u());
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { EasyCookieApi, EasyCookieEvent } from '@easy-cookie/core/shared';\n\n/**\n * The few posthog-js methods the adapter uses. Typed loosely on purpose, so\n * `posthog-js` stays an optional peer dependency and any compatible client works.\n */\nexport interface PostHogLike {\n opt_in_capturing(options?: { captureEventName?: string | null | false; [key: string]: unknown }): void;\n opt_out_capturing(): void;\n has_opted_in_capturing?(): boolean;\n has_opted_out_capturing?(): boolean;\n clear_opt_in_out_capturing?(): void;\n startSessionRecording?(...args: unknown[]): void;\n stopSessionRecording?(): void;\n identify?(distinctId?: string, ...rest: unknown[]): void;\n get_distinct_id?(): string;\n}\n\n/** Minimal shape of a PostHog event passed to `before_send`. */\nexport interface PostHogEventLike {\n event: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- matches posthog-js `Properties`\n properties?: Record<string, any>;\n}\n\nexport interface ConnectPostHogOptions {\n /** Category that allows PostHog cookies/profiles. Default `measurement`. */\n category?: string;\n /** Service id: when set, the service's own consent decides instead of the category. */\n service?: string;\n /**\n * Return the logged-in user id (or undefined). Called after opt-in, so the\n * identified person profile is created only with consent.\n */\n identify?: () => string | undefined | null;\n /**\n * Before the visitor's first choice:\n * - `'pending'` (default): capture nothing. A stale opt-in left in storage by an\n * earlier setup is cleared, so nothing is captured with cookies.\n * - `'cookieless'`: count visits without cookies (same as after a rejection).\n * Only if your privacy policy covers cookieless measurement without consent.\n */\n beforeChoice?: 'pending' | 'cookieless';\n /** Start session recording on opt-in and stop it on opt-out. Default false. */\n sessionReplay?: boolean;\n}\n\n/**\n * Keep PostHog's capture state in sync with the visitor's consent, without reloads.\n *\n * PostHog must be initialised by the host with `cookieless_mode: 'on_reject'`\n * (see {@link posthogInitOptions}): before a choice it captures nothing, after a\n * rejection it keeps counting anonymously with no cookies or storage, and after\n * an acceptance it switches to normal cookie-based capture.\n *\n * On `replay`, `consent` and `change` the adapter calls `opt_in_capturing()` or\n * `opt_out_capturing()`. Returns an unsubscribe function.\n */\nexport function connectPostHog(posthog: PostHogLike, api: EasyCookieApi, options: ConnectPostHogOptions = {}): () => void {\n const category = options.category ?? 'measurement';\n const granted = () => (options.service ? api.isServiceGranted(options.service) : api.isGranted(category));\n let last: 'in' | 'out' | null = null;\n\n const apply = () => {\n if (!api.get()) {\n if (options.beforeChoice === 'cookieless') {\n if (last !== 'out') {\n if (!posthog.has_opted_out_capturing?.()) posthog.opt_out_capturing();\n last = 'out';\n }\n } else if (posthog.has_opted_in_capturing?.()) {\n // Pending: an opt-in stored by a previous banner must not keep capturing.\n posthog.clear_opt_in_out_capturing?.();\n }\n return;\n }\n if (granted()) {\n if (last !== 'in') {\n // No `$opt_in` event: the consent itself is recorded by easy-cookie.\n if (!posthog.has_opted_in_capturing?.()) posthog.opt_in_capturing({ captureEventName: false });\n if (options.sessionReplay) posthog.startSessionRecording?.();\n last = 'in';\n }\n const id = options.identify?.();\n if (id && posthog.identify && posthog.get_distinct_id?.() !== id) posthog.identify(id);\n } else if (last !== 'out') {\n if (options.sessionReplay) posthog.stopSessionRecording?.();\n if (!posthog.has_opted_out_capturing?.()) posthog.opt_out_capturing();\n last = 'out';\n }\n };\n\n const events: EasyCookieEvent[] = ['replay', 'consent', 'change'];\n const unsubs = events.map((ev) => api.on(ev, apply));\n // The stored choice may have been replayed before we subscribed.\n apply();\n return () => unsubs.forEach((u) => u());\n}\n\n/** Events dropped by {@link consentBeforeSend} while consent is missing. */\nexport const NON_ESSENTIAL_EVENTS: readonly string[] = [\n '$autocapture',\n '$rageclick',\n '$dead_click',\n '$exception',\n '$web_vitals',\n '$$heatmap',\n '$heatmap',\n];\n\nexport interface ConsentBeforeSendOptions {\n /** Category checked by the default `isGranted`. Default `measurement`. */\n category?: string;\n /**\n * Consent check. Defaults to the runtime when `api` is given, otherwise to the\n * state published by the head script (`window.__easyCookieHead.granted`).\n */\n isGranted?: () => boolean;\n api?: Pick<EasyCookieApi, 'isGranted'> | null;\n /** Event names to drop without consent. Default {@link NON_ESSENTIAL_EVENTS}. */\n drop?: readonly string[];\n}\n\nfunction headGranted(category: string): boolean {\n const head = (globalThis as { __easyCookieHead?: { granted?: string[]; valid?: boolean } }).__easyCookieHead;\n return !!head?.valid && !!head.granted?.includes(category);\n}\n\n/**\n * `before_send` filter: without consent, drop autocapture, exceptions, web vitals\n * and heatmap events, so cookieless mode only sends the essentials (pageviews,\n * explicit captures). Pass it as `before_send` or use {@link posthogInitOptions}.\n */\nexport function consentBeforeSend(options: ConsentBeforeSendOptions = {}) {\n const category = options.category ?? 'measurement';\n const drop = new Set(options.drop ?? NON_ESSENTIAL_EVENTS);\n const isGranted =\n options.isGranted ?? (() => (options.api ? options.api.isGranted(category) : headGranted(category)));\n return <T extends PostHogEventLike | null>(event: T): T | null => {\n if (!event) return event;\n if (drop.has(event.event) && !isGranted()) return null;\n return event;\n };\n}\n\nexport interface PostHogInitOptions {\n cookieless_mode: 'on_reject';\n persistence: 'localStorage+cookie';\n person_profiles: 'identified_only';\n before_send: ReturnType<typeof consentBeforeSend>;\n [key: string]: unknown;\n}\n\n/**\n * Recommended `posthog.init()` options for easy-cookie:\n * - `cookieless_mode: 'on_reject'` (requires \"Cookieless server hash mode\" enabled\n * in the PostHog project settings);\n * - `persistence: 'localStorage+cookie'` (only used after opt-in);\n * - `person_profiles: 'identified_only'`;\n * - a consent-aware `before_send` filter.\n *\n * Spread your own options after it: `posthog.init(key, { ...posthogInitOptions(), api_host })`.\n */\nexport function posthogInitOptions(options: ConsentBeforeSendOptions = {}): PostHogInitOptions {\n return {\n cookieless_mode: 'on_reject',\n persistence: 'localStorage+cookie',\n person_profiles: 'identified_only',\n before_send: consentBeforeSend(options),\n };\n}\n"],"mappings":";AA0DO,SAAS,eAAe,SAAsB,KAAoB,UAAiC,CAAC,GAAe;AACxH,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,UAAU,MAAO,QAAQ,UAAU,IAAI,iBAAiB,QAAQ,OAAO,IAAI,IAAI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AkB,UAAoC,CAAC,GAAG;AACxE,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,OAAO,IAAI,IAAI,QAAQ,QAAQ,oBAAoB;AACzD,QAAM,YACJ,QAAQ,cAAc,MAAO,QAAQ,MAAM,QAAQ,IAAI,UAAU,QAAQ,IAAI,YAAY,QAAQ;AACnG,SAAO,CAAoC,UAAuB;AAChE,QAAI,CAAC,MAAO,QAAO;AACnB,QAAI,KAAK,IAAI,MAAM,KAAK,KAAK,CAAC,UAAU,EAAG,QAAO;AAClD,WAAO;AAAA,EACT;AACF;AAoBO,SAAS,mBAAmB,UAAoC,CAAC,GAAuB;AAC7F,SAAO;AAAA,IACL,iBAAiB;AAAA,IACjB,aAAa;AAAA,IACb,iBAAiB;AAAA,IACjB,aAAa,kBAAkB,OAAO;AAAA,EACxC;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { EasyCookieApi, EasyCookieEvent } from '@easy-cookie/core/shared';\n\n/**\n * The few posthog-js methods the adapter uses. Typed loosely on purpose, so\n * `posthog-js` stays an optional peer dependency and any compatible client works.\n */\nexport interface PostHogLike {\n opt_in_capturing(options?: { captureEventName?: string | null | false; [key: string]: unknown }): void;\n opt_out_capturing(): void;\n has_opted_in_capturing?(): boolean;\n has_opted_out_capturing?(): boolean;\n clear_opt_in_out_capturing?(): void;\n /** posthog-js ≥ 1.2xx: 'granted' | 'denied' | 'pending' (reliable with cookieless_mode). */\n get_explicit_consent_status?(): string;\n startSessionRecording?(...args: unknown[]): void;\n stopSessionRecording?(): void;\n identify?(distinctId?: string, ...rest: unknown[]): void;\n get_distinct_id?(): string;\n}\n\n/** Minimal shape of a PostHog event passed to `before_send`. */\nexport interface PostHogEventLike {\n event: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- matches posthog-js `Properties`\n properties?: Record<string, any>;\n}\n\nexport interface ConnectPostHogOptions {\n /** Category that allows PostHog cookies/profiles. Default `measurement`. */\n category?: string;\n /** Service id: when set, the service's own consent decides instead of the category. */\n service?: string;\n /**\n * Return the logged-in user id (or undefined). Called after opt-in, so the\n * identified person profile is created only with consent.\n */\n identify?: () => string | undefined | null;\n /**\n * Before the visitor's first choice:\n * - `'pending'` (default): capture nothing. A stale opt-in left in storage by an\n * earlier setup is cleared, so nothing is captured with cookies.\n * - `'cookieless'`: count visits without cookies (same as after a rejection).\n * Only if your privacy policy covers cookieless measurement without consent.\n */\n beforeChoice?: 'pending' | 'cookieless';\n /** Start session recording on opt-in and stop it on opt-out. Default false. */\n sessionReplay?: boolean;\n}\n\n/**\n * Keep PostHog's capture state in sync with the visitor's consent, without reloads.\n *\n * PostHog must be initialised by the host with `cookieless_mode: 'on_reject'`\n * (see {@link posthogInitOptions}): before a choice it captures nothing (or counts\n * cookieless with `beforeChoice: 'cookieless'`), after a\n * rejection it keeps counting anonymously with no cookies or storage, and after\n * an acceptance it switches to normal cookie-based capture.\n *\n * On `replay`, `consent` and `change` the adapter calls `opt_in_capturing()` or\n * `opt_out_capturing()`. Returns an unsubscribe function.\n */\nexport function connectPostHog(posthog: PostHogLike, api: EasyCookieApi, options: ConnectPostHogOptions = {}): () => void {\n const category = options.category ?? 'measurement';\n const granted = () => (options.service ? api.isServiceGranted(options.service) : api.isGranted(category));\n let last: 'in' | 'out' | null = null;\n\n // With cookieless_mode, has_opted_out_capturing() is already true while consent\n // is pending, so prefer the explicit consent status when the client has it.\n const status = (): 'in' | 'out' | 'pending' => {\n const explicit = posthog.get_explicit_consent_status?.();\n if (explicit === 'granted') return 'in';\n if (explicit === 'denied') return 'out';\n if (explicit === 'pending') return 'pending';\n if (posthog.has_opted_in_capturing?.()) return 'in';\n if (posthog.has_opted_out_capturing?.()) return 'out';\n return 'pending';\n };\n\n const apply = () => {\n // Until run() has read the stored choice, \"no choice\" is not known yet:\n // acting now could opt out (and reset) a returning visitor who accepted.\n if (!api.getConfig()) return;\n if (!api.get()) {\n if (options.beforeChoice === 'cookieless') {\n if (last !== 'out') {\n if (status() !== 'out') posthog.opt_out_capturing();\n last = 'out';\n }\n } else if (status() === 'in') {\n // Pending: an opt-in stored by a previous banner must not keep capturing.\n posthog.clear_opt_in_out_capturing?.();\n }\n return;\n }\n if (granted()) {\n if (last !== 'in') {\n // No `$opt_in` event: the consent itself is recorded by easy-cookie.\n if (status() !== 'in') posthog.opt_in_capturing({ captureEventName: false });\n if (options.sessionReplay) posthog.startSessionRecording?.();\n last = 'in';\n }\n const id = options.identify?.();\n if (id && posthog.identify && posthog.get_distinct_id?.() !== id) posthog.identify(id);\n } else if (last !== 'out') {\n if (options.sessionReplay) posthog.stopSessionRecording?.();\n if (status() !== 'out') posthog.opt_out_capturing();\n last = 'out';\n }\n };\n\n const events: EasyCookieEvent[] = ['ready', 'replay', 'consent', 'change'];\n const unsubs = events.map((ev) => api.on(ev, apply));\n // The stored choice may have been replayed before we subscribed.\n apply();\n return () => unsubs.forEach((u) => u());\n}\n\n/** Events dropped by {@link consentBeforeSend} while consent is missing. */\nexport const NON_ESSENTIAL_EVENTS: readonly string[] = [\n '$autocapture',\n '$rageclick',\n '$dead_click',\n '$exception',\n '$web_vitals',\n '$$heatmap',\n '$heatmap',\n];\n\nexport interface ConsentBeforeSendOptions {\n /** Category checked by the default `isGranted`. Default `measurement`. */\n category?: string;\n /**\n * Consent check. Defaults to the runtime when `api` is given, otherwise to the\n * state published by the head script (`window.__easyCookieHead.granted`).\n */\n isGranted?: () => boolean;\n api?: Pick<EasyCookieApi, 'isGranted'> | null;\n /** Event names to drop without consent. Default {@link NON_ESSENTIAL_EVENTS}. */\n drop?: readonly string[];\n}\n\nfunction headGranted(category: string): boolean {\n const head = (globalThis as { __easyCookieHead?: { granted?: string[]; valid?: boolean } }).__easyCookieHead;\n return !!head?.valid && !!head.granted?.includes(category);\n}\n\n/**\n * `before_send` filter: without consent, drop autocapture, exceptions, web vitals\n * and heatmap events, so cookieless mode only sends the essentials (pageviews,\n * explicit captures). Pass it as `before_send` or use {@link posthogInitOptions}.\n */\nexport function consentBeforeSend(options: ConsentBeforeSendOptions = {}) {\n const category = options.category ?? 'measurement';\n const drop = new Set(options.drop ?? NON_ESSENTIAL_EVENTS);\n const isGranted =\n options.isGranted ?? (() => (options.api ? options.api.isGranted(category) : headGranted(category)));\n return <T extends PostHogEventLike | null>(event: T): T | null => {\n if (!event) return event;\n if (drop.has(event.event) && !isGranted()) return null;\n return event;\n };\n}\n\nexport interface PostHogInitOptions {\n cookieless_mode: 'on_reject';\n persistence: 'localStorage+cookie';\n person_profiles: 'identified_only';\n before_send: ReturnType<typeof consentBeforeSend>;\n [key: string]: unknown;\n}\n\n/**\n * Recommended `posthog.init()` options for easy-cookie:\n * - `cookieless_mode: 'on_reject'` (requires \"Cookieless server hash mode\" enabled\n * in the PostHog project settings);\n * - `persistence: 'localStorage+cookie'` (only used after opt-in);\n * - `person_profiles: 'identified_only'`;\n * - a consent-aware `before_send` filter.\n *\n * Spread your own options after it: `posthog.init(key, { ...posthogInitOptions(), api_host })`.\n */\nexport function posthogInitOptions(options: ConsentBeforeSendOptions = {}): PostHogInitOptions {\n return {\n cookieless_mode: 'on_reject',\n persistence: 'localStorage+cookie',\n person_profiles: 'identified_only',\n before_send: consentBeforeSend(options),\n };\n}\n"],"mappings":";AA6DO,SAAS,eAAe,SAAsB,KAAoB,UAAiC,CAAC,GAAe;AACxH,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,UAAU,MAAO,QAAQ,UAAU,IAAI,iBAAiB,QAAQ,OAAO,IAAI,IAAI,UAAU,QAAQ;AACvG,MAAI,OAA4B;AAIhC,QAAM,SAAS,MAAgC;AAC7C,UAAM,WAAW,QAAQ,8BAA8B;AACvD,QAAI,aAAa,UAAW,QAAO;AACnC,QAAI,aAAa,SAAU,QAAO;AAClC,QAAI,aAAa,UAAW,QAAO;AACnC,QAAI,QAAQ,yBAAyB,EAAG,QAAO;AAC/C,QAAI,QAAQ,0BAA0B,EAAG,QAAO;AAChD,WAAO;AAAA,EACT;AAEA,QAAM,QAAQ,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AAQ,YAAY;AACrC,QAAM,OAAO,IAAI,IAAI,QAAQ,QAAQ,oBAAoB;AACzD,QAAM,YACJ,QAAQ,cAAc,MAAO,QAAQ,MAAM,QAAQ,IAAI,UAAU,QAAQ,IAAI,YAAY,QAAQ;AACnG,SAAO,CAAoC,UAAuB;AAChE,QAAI,CAAC,MAAO,QAAO;AACnB,QAAI,KAAK,IAAI,MAAM,KAAK,KAAK,CAAC,UAAU,EAAG,QAAO;AAClD,WAAO;AAAA,EACT;AACF;AAoBO,SAAS,mBAAmB,UAAoC,CAAC,GAAuB;AAC7F,SAAO;AAAA,IACL,iBAAiB;AAAA,IACjB,aAAa;AAAA,IACb,iBAAiB;AAAA,IACjB,aAAa,kBAAkB,OAAO;AAAA,EACxC;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cookie/posthog",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "PostHog adapter for easy-cookie: cookieless on_reject mode and opt-in/out synced with consent.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -28,7 +28,7 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@easy-cookie/core": "^0.1.1"
31
+ "@easy-cookie/core": "^0.1.2"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "posthog-js": ">=1.200.0"