Cluster 15 · Product Analytics · opt_out_capturing_by_default

PostHog & Segment Pre-Consent Guide: Cookieless Memory Mode & Consent Manager

Configure PostHog (`opt_out_capturing_by_default: true` / `persistence: 'memory'`) and Segment Analytics.js (`@segment/consent-manager`) so zero tracking cookies fire before consent.

100% Browser-Side · Zero User-Data Upload · Strict CSP

Compliance & Pre-Consent Control Specification

Control / Signal Compliant Baseline Engineering & Privacy Rationale
PostHog Opt-Out opt_out_capturing_by_default: true Suppresses all autocapture & event HTTP beacons until `posthog.opt_in_capturing()` runs
PostHog Cookieless persistence: 'memory' Eliminates `ph_*` cookies & `localStorage` persistence completely prior to consent
Segment Consent Load analytics.load() inside consent wrapper Hold Segment destinations & `ajs_anonymous_id` cookies until consent preferences resolve
GPC Compliance Honor navigator.globalPrivacyControl Automatically disable ad-sharing destinations (Meta/Google Ads) in Segment when GPC=true

PostHog Pre-Consent Safe Initialization (TypeScript)

posthog-consent.ts
import posthog from "posthog-js";

export function initPostHogSafe(apiKey: string, apiHost: string) {
  posthog.init(apiKey, {
    api_host: apiHost,
    persistence: "memory",
    opt_out_capturing_by_default: true,
    respect_dnt: true
  });
}

export function handleAnalyticsConsent(granted: boolean) {
  if (granted && navigator.globalPrivacyControl !== true) {
    posthog.set_config({ persistence: "localStorage+cookie" });
    posthog.opt_in_capturing();
  } else {
    posthog.opt_out_capturing();
    posthog.reset();
  }
}

Segment Analytics.js Pre-Consent Gating Pattern

segment-consent-gate.ts
export function bootSegmentWithConsent(writeKey: string, consent: { analytics: boolean; marketing: boolean }) {
  if (!consent.analytics && !consent.marketing) return;
  const gpc = typeof navigator !== "undefined" && navigator.globalPrivacyControl === true;
  // Pass destination-level integration flags to Segment AnalyticsBrowser.load:
  window.analytics?.load(writeKey, {
    integrations: {
      All: Boolean(consent.analytics),
      "Facebook Pixel": Boolean(consent.marketing && !gpc),
      "Google Ads (Gtag)": Boolean(consent.marketing && !gpc)
    }
  });
}

Implementation Steps & Key Insights

  1. Initialize PostHog with `opt_out_capturing_by_default: true` and `persistence: 'memory'` so `ph_*` cookies and `localStorage` stay empty.
  2. Only call `posthog.opt_in_capturing()` and upgrade `persistence` when the user accepts Analytics consent.
  3. For Segment, wrap `analytics.load()` inside your CMP callback and pass destination-level integration booleans (blocking ad pixels when `navigator.globalPrivacyControl === true`).
  4. Verify zero `ph_*` or `ajs_*` cookies appear on fresh page load via `consent.runglab.dev`.
  • PostHog's `persistence: 'memory'` mode prevents touching cookies or `localStorage` altogether — avoiding ePrivacy storage triggers when strictly anonymized.
  • Default Segment snippets write `ajs_anonymous_id` to cookies & `localStorage` immediately on `analytics.load()` unless wrapped by a consent manager.
  • When users revoke consent, call `posthog.opt_out_capturing()` AND `posthog.reset()` to clear cached IDs.

Common Pre-Consent Pitfalls

  • Setting `opt_out_capturing_by_default: true` while forgetting `persistence: 'memory'` (which may still inspect/write storage flags depending on SDK version).
  • Calling `analytics.page()` immediately on mount before Segment consent rules resolve.
  • Letting Segment proxy ad-sharing destinations while `Sec-GPC: 1` is active.
Verify Live: Audit your production site on consent.runglab.dev or grab the $39 ConsentCheck Compliance Pack. Not legal advice.

More GDPR, Consent Mode v2 & GPC Implementation Guides

← Back to GDPR & Consent Mode v2 Hub · ConsentCheck Live Audit → · $39 Compliance Pack → · Cluster 4: Security Headers → · Cluster 11: PCI & SRI →

Frequently Asked Questions

Does PostHog `persistence: 'memory'` use cookies?

No — `'memory'` keeps session state strictly in JavaScript RAM for the current page view and writes zero browser cookies or `localStorage` entries.

How do I avoid setting `ajs_anonymous_id` cookies with Segment?

Delay `analytics.load(writeKey)` until your CMP confirms consent, or disable cookies in Segment options until opt-in.

How can I audit my live site for leaked `ph_*` or `ajs_*` cookies?

Run a free live scan at https://consent.runglab.dev/?ref=tool-consent-cluster.