---
title: PostHog
description: Learn how to sync c15t consent with PostHog.
lastModified: 2025-09-19
canary: true
---
Posthog allows for both cookieless and cookie-based tracking. This means that you can sync c15t with Posthog, allowing you to continue using Posthog even when users have not given consent.

## PostHog SDK Implementation

This is the recommended approach if you're using the Posthog JS SDK, this is commonly used in React projects.

1. **Initialize Posthog** When you initialize posthog make sure to set cookieless\_mode to on\_reject.

   ```ts
   posthog.init("phc_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", {
     api_host: "https://eu.i.posthog.com",
     defaults: "2025-05-24",
     cookieless_mode: 'on_reject'
   })

   posthog.has_opted_out_capturing() // Avoids accidental tracking without consent till c15t has loaded
   ```

2. **Adding the script to c15t** JavaScript

   ```ts
   import { configureConsentManager } from 'c15t';
   import { posthog } from 'posthog-js';

   configureConsentManager({
     // ... 
     callbacks: {
       onConsentSet({ preferences }) {
         if (preferences.measurement) {
           posthog.opt_in_capturing();
         } else {
           posthog.opt_out_capturing();
         }
       }
     }
   });
   ```

   React

   ```tsx
   import { posthog } from 'posthog-js';
   import { ConsentManagerProvider } from '@c15t/react';

   export function App({ children }: { children: React.ReactNode }) {
     return (
       <ConsentManagerProvider
         options={{
           callbacks: {
               onConsentSet({ preferences }) {
                 if (preferences.measurement) {
                   posthog.opt_in_capturing();
                 } else {
                   posthog.opt_out_capturing();
                 }
               }
             }
         }}
       >
         {children}
       </ConsentManagerProvider>
     );
   }
   ```

   Next.js + App Router

   ```tsx
   'use client';
   import { posthog } from 'posthog-js';
   import { ClientSideOptionsProvider } from '@c15t/nextjs/client';

   export function ConsentManagerClient({ children }: { children: React.ReactNode }) { 
     return (
       <ClientSideOptionsProvider
      <ClientSideOptionsProvider
         callbacks={{
            onConsentSet({ preferences }) {
                if (preferences.measurement) {
                  posthog.opt_in_capturing();
                } else {
                  posthog.opt_out_capturing();
                }
            }
         }}
      >
       >
         {children}
       </ClientSideOptionsProvider>
     );
   }
   ```

   Next.js + Pages Router

   ```tsx
   import { posthog } from 'posthog-js';
   import { ConsentManagerProvider } from '@c15t/nextjs';

   export function App({ children }: { children: React.ReactNode }) {
   return (
   <ConsentManagerProvider
   options={{
   // ... other options
   callbacks: {
     onConsentSet({ preferences }) {
       if (preferences.measurement) {
         posthog.opt_in_capturing();
       } else {
         posthog.opt_out_capturing();
       }
     }
   }
   }}
   >
   {children}
   </ConsentManagerProvider>
   );
   }
   ```

## PostHog Script Implementation

If you want to load posthog via a script tag it's recommended to use this approach.

By default c15t will always load the script regardless of consent. This is because the script has built-in functionality to opt into and out of tracking based on consent.

1. **Initialize Posthog** This script does not load the Posthog script, it only syncs the consent state with Posthog via the Posthog JS SDK.

   This is due to Posthog's use of cookieless tracking. However, you need to set cookieless\_mode to on\_reject when initializing Posthog.

   ```ts
   posthog.init("phc_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", {
     api_host: "https://eu.i.posthog.com",
     defaults: "2025-05-24",
     // PostHog will not set any cookies until the user has given consent
     cookieless_mode: 'on_reject'
   })
   ```

2. **Adding the script to c15t** JavaScript

   ```ts
   import { configureConsentManager } from 'c15t';
   import { posthog } from '@c15t/scripts/posthog';

   configureConsentManager({
     // ... 
     scripts: [
       posthog({ 
         id: 'phc_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
         apiHost: 'https://eu.i.posthog.com',
         defaults: '2025-05-24', 
         options: { person_profiles: 'identified_only' }
       }),
      ],
   });
   ```

   React

   ```tsx
   import { posthog } from '@c15t/scripts/posthog';
   import { ConsentManagerProvider } from '@c15t/react';

   export function App({ children }: { children: React.ReactNode }) {
     return (
       <ConsentManagerProvider
         options={{
           scripts: [
             posthog({ 
                 id: 'phc_foobar', 
                 apiHost: 'https://eu.i.posthog.com',
                 defaults: '2025-05-24', 
                 options: { person_profiles: 'identified_only' }
               }),
           ],
         }}
       >
         {children}
       </ConsentManagerProvider>
     );
   }
   ```

   Next.js + App Router

   ```tsx
   'use client';
   import { posthog } from '@c15t/scripts/posthog';
   import { ClientSideOptionsProvider } from '@c15t/nextjs/client';

   export function ConsentManagerClient({ children }: { children: React.ReactNode }) { 
     return (
       <ClientSideOptionsProvider
         scripts={[
           posthog({ 
               id: 'phc_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
               apiHost: 'https://eu.i.posthog.com',
               defaults: '2025-05-24', 
               options: { person_profiles: 'identified_only' }
             }),
         ]}
       >
         {children}
       </ClientSideOptionsProvider>
     );
   }
   ```

   Next.js + Pages Router

   ```tsx
   import { posthog } from '@c15t/scripts/posthog';
   import { ConsentManagerProvider } from '@c15t/nextjs';

   export function App({ children }: { children: React.ReactNode }) {
     <ConsentManagerProvider
       options={{
         // ... other options
         scripts: [
           posthog({ 
             id: 'phc_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
             apiHost: 'https://eu.i.posthog.com',
             defaults: '2025-05-24', 
             options: { person_profiles: 'identified_only' }
           }),
         ],
       }}
     >
       {children}
     </ConsentManagerProvider>
   }
   ```

## Types

### PosthogConsentOptions

| Property | Type                     | Description                                                                                                                              | Default                     |  Required  |
| :------- | :----------------------- | :--------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------- | :--------: |
| id       | string                   | Your posthog id, begins with 'phc\_'.                                                                                                    | -                           | ✅ Required |
| apiHost  | string                   | Your posthog api host.                                                                                                                   | 'https\://eu.i.posthog.com' | ✅ Required |
| defaults | string                   | The defaults for the posthog script.                                                                                                     | -                           | ✅ Required |
| options  | Record\<string, unknown> | Other optional options for the posthog script.                                                                                           | -                           | ✅ Required |
| script   | Script                   | Override or extend the default script values.&#xA;&#xA;Default values:&#xA;- \`id\`: 'posthog-consent'&#xA;- \`category\`: 'measurement' | -                           |  Optional  |

### Script

| Property                   | Type                               | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | Default |  Required  |
| :------------------------- | :--------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------ | :--------: |
| id                         | string                             | Unique identifier for the script                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | -       | ✅ Required |
| src                        | string                             | URL of the script to load                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | -       |  Optional  |
| textContent                | string                             | Inline JavaScript code to execute                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | -       |  Optional  |
| category                   | HasCondition\<AllConsentNames>     | Consent category or condition required to load this script                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | -       | ✅ Required |
| callbackOnly               | boolean                            | Whether this is a callback-only script that doesn't need to load an external resource.&#xA;When true, no script tag will be added to the DOM, only callbacks will be executed.&#xA;&#xA;This is useful for:&#xA;- Managing consent for libraries already loaded on the page&#xA;- Enabling/disabling tracking features based on consent changes&#xA;- Running custom code when consent status changes without loading external scripts&#xA;&#xA;Example use cases:&#xA;- Enabling/disabling Posthog tracking&#xA;- Configuring Google Analytics consent mode&#xA;- Managing cookie consent for embedded content | false   |  Optional  |
| persistAfterConsentRevoked | boolean                            | Whether the script should persist after consent is revoked.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | false   |  Optional  |
| alwaysLoad                 | boolean                            | Whether the script should always load regardless of consent state.&#xA;&#xA;This is useful for scripts like Google Tag Manager or PostHog that manage&#xA;their own consent state internally. The script will load immediately and&#xA;never be unloaded based on consent changes.&#xA;&#xA;Note: When using this option, you are responsible for ensuring the script&#xA;itself respects user consent preferences through its own consent management.                                                                                                                                                          | false   |  Optional  |
| fetchPriority              | "high" \| "low" \| "auto"          | Priority hint for browser resource loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | -       |  Optional  |
| attributes                 | Record\<string, string>            | Additional attributes to add to the script element                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | -       |  Optional  |
| async                      | boolean                            | Whether to use async loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | -       |  Optional  |
| defer                      | boolean                            | Whether to defer script loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | -       |  Optional  |
| nonce                      | string                             | Content Security Policy nonce                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| anonymizeId                | boolean                            | Whether to use an anonymized ID for the script element, this helps ensure the script is not blocked by ad blockers                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | true    |  Optional  |
| onBeforeLoad               | (info: ScriptCallbackInfo) => void | Callback executed before the script is loaded                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| onLoad                     | (info: ScriptCallbackInfo) => void | Callback executed when the script loads successfully                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | -       |  Optional  |
| onDelete                   | (info: ScriptCallbackInfo) => void | Callback executed when the script is being unloaded/removed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | -       |  Optional  |
| onError                    | (info: ScriptCallbackInfo) => void | Callback executed if the script fails to load                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| onConsentChange            | (info: ScriptCallbackInfo) => void | Callback executed whenever the consent store is changed.&#xA;This callback only applies to scripts already loaded.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | -       |  Optional  |
