Skip to content

fix(analytics): skip PostHog when the browser sends DNT or GPC - #144

Merged
scttbnsn merged 1 commit into
devfrom
fix/respect-dnt-gpc
Nov 18, 2026
Merged

scttbnsn merged 1 commit into
devfrom
fix/respect-dnt-gpc

Conversation

@scttbnsn

@scttbnsn scttbnsn commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Visitors who send Do Not Track or Global Privacy Control were still being tracked. In posthog-js 1.447.2, is_capturing() returns true under cookieless_mode: "always" before the consent check runs, so respect_dnt never kicks in.

browserRequestsNoTracking in lib/posthog-privacy.ts reads navigator.doNotTrack, msDoNotTrack, window.doNotTrack and navigator.globalPrivacyControl. When any of them is set, instrumentation-client.ts skips posthog.init, the error listeners and every pageview, and before_send drops the event as a second check. respect_dnt: true stays in the config.

Unit tests cover each signal, the negatives and throwing getters, and a source contract keeps init and before_send behind the guard. Locally, check, test:posthog (37/37), build, test:posthog:browser and Qlty all pass.

Summary by CodeRabbit

  • Privacy
    • Analytics tracking now respects browser Do Not Track and Global Privacy Control signals.
    • When an opt-out signal is detected, analytics initialization, pageview capture, and event sending are skipped.
  • Tests
    • Added coverage for recognized privacy signals, unrecognized values, and signals that cannot be read.

posthog-js is_capturing() returns true under cookieless_mode always before it checks respect_dnt, so DNT and GPC visitors were still tracked. Skip posthog.init, the error listeners and pageviews when the browser opts out, and drop events in before_send as a second guard.
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The frontend now detects browser DNT and GPC signals and uses them to control PostHog initialization, event submission, and router-transition pageviews. Signal reads tolerate malformed inputs and throwing getters. Tests cover recognized signals, invalid values, and tracking guards.

Changes

PostHog privacy opt-out

Layer / File(s) Summary
Detect browser privacy signals
frontend/lib/posthog-privacy.ts, frontend/test/posthog.test.ts
The new helper checks supported DNT and GPC values. Tests cover recognized, absent, malformed, and throwing signal values.
Apply opt-out to PostHog tracking
frontend/instrumentation-client.ts, frontend/test/posthog-source.test.mjs
PostHog initialization and router-transition pageviews require tracking not to be opted out. The before_send hook rechecks signals and drops events when tracking is disabled.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Browser
  participant Instrumentation as instrumentation-client.ts
  participant Privacy as browserRequestsNoTracking
  participant PostHog
  Browser->>Instrumentation: provide navigator and window
  Instrumentation->>Privacy: check privacy signals
  Privacy-->>Instrumentation: return opt-out status
  Instrumentation->>PostHog: initialize when configured and opted in
  Instrumentation->>PostHog: capture router-transition pageview when opted in
  PostHog->>Privacy: recheck signals in before_send
  Privacy-->>PostHog: return opt-out status
Loading

Possibly related PRs

Suggested reviewers: biggest-littlest

Merge Risk: Minimal · up to 7662f

This change stops PostHog initialization, event capture, and pageviews when the browser sends Do Not Track or Global Privacy Control signals. No actionable merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: PostHog is skipped when the browser sends a DNT or GPC signal. The raw summary shows the new browserRequestsNoTracking helper, the opt-out guard around posthog.init an…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: PostHog is skipped when the browser sends a DNT or GPC signal. The raw summary shows the new browserRequestsNoTracking helper, the opt-out guard around posthog.init and router pageviews, and the before_send recheck, all matching that title. The title is specific and does not use vague terms.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Biome (2.6.3)

frontend/instrumentation-client.ts

Biome could not lint this file: nested root configuration. Check the repository's Biome configuration and plugins.

frontend/lib/posthog-privacy.ts

Biome could not lint this file: nested root configuration. Check the repository's Biome configuration and plugins.

frontend/test/posthog-source.test.mjs

Biome could not lint this file: nested root configuration. Check the repository's Biome configuration and plugins.

  • 1 others

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between f8ec8ad and 7662fcb.

Files selected for processing: 4

  • frontend/instrumentation-client.ts
  • frontend/lib/posthog-privacy.ts
  • frontend/test/posthog-source.test.mjs
  • frontend/test/posthog.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@scttbnsn

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@scttbnsn
scttbnsn merged commit 940440c into dev Nov 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant