---
title: Segment
description: Load Segment Analytics.js with c15t and gate it behind measurement consent.
lastModified: 2026-05-10
icon: segment
---
Segment lets you collect analytics events in one place and forward them to downstream destinations. The `segment()` helper creates Segment's standard `window.analytics` queue, optionally queues the initial `page()` call, and loads Analytics.js when `measurement` consent is available.

## Integrate with c15t

**React**

```tsx
import { type ReactNode } from 'react';
import { ConsentManagerProvider } from '@c15t/react';
import { segment } from '@c15t/scripts/segment';

const scripts = [segment({ writeKey: 'abc123xyz456' })];

export function ConsentProvider({ children }: { children: ReactNode }) {
  return (
    <ConsentManagerProvider
      options={{
        mode: 'hosted',
        backendURL: 'https://your-instance.c15t.dev',
        scripts,
      }}
    >
      {children}
    </ConsentManagerProvider>
  );
}
```

**Next.js**

```tsx
'use client';

import { type ReactNode } from 'react';
import { ConsentManagerProvider } from '@c15t/nextjs';
import { segment } from '@c15t/scripts/segment';

const scripts = [segment({ writeKey: 'abc123xyz456' })];

export function ConsentProvider({ children }: { children: ReactNode }) {
  return (
    <ConsentManagerProvider
      options={{
        mode: 'hosted',
        backendURL: '/api/c15t',
        scripts,
      }}
    >
      {children}
    </ConsentManagerProvider>
  );
}
```

**JavaScript**

```ts
import { getOrCreateConsentRuntime } from 'c15t';
import { segment } from '@c15t/scripts/segment';

getOrCreateConsentRuntime({
  mode: 'hosted',
  backendURL: 'https://your-instance.c15t.dev',
  scripts: [segment({ writeKey: 'abc123xyz456' })],
});
```

## How c15t loads it

* **Category:** `measurement` (Analytics)
* **Loads when:** measurement consent is granted
* **On revocation:** unloaded - c15t removes the script from the DOM and clears Segment globals until consent is granted again.

## Configure the integration

By default the helper queues `analytics.page()` before the vendor bundle loads. If you want to handle page views yourself, set `trackPageView` to `false`.

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

segment({
  writeKey: 'abc123xyz456',
  trackPageView: false,
});
```

## Tracking events in your app

c15t gates the Segment script from loading until `measurement` consent is granted. Your application code that calls Segment's runtime API (`window.analytics.track`, `identify`, etc.) is **not** automatically gated - `window.analytics` does not exist until the script is loaded, so unguarded calls before consent throw.

Guard event calls by checking consent state. From React:

```tsx
import { useCallback } from 'react';
import { useConsentManager } from '@c15t/react';

function SignupExample() {
  const { has } = useConsentManager();

  const trackSignup = useCallback(() => {
    if (has('measurement')) {
      window.analytics?.track('Signup Completed', { plan: 'pro' });
    }
  }, [has]);
}
```

From plain JavaScript:

```ts
import { getOrCreateConsentRuntime } from 'c15t';

const { consentStore } = getOrCreateConsentRuntime();

if (consentStore.getState().has('measurement')) {
  window.analytics?.track('Signup Completed', { plan: 'pro' });
}
```

## Types

### SegmentOptions

| Property    | Value                                                   |
| :---------- | :------------------------------------------------------ |
| Type Name   | \`SegmentOptions\`                                      |
| Source Path | \`./packages/scripts/src/vendors/analytics/segment.ts\` |

\*AutoTypeTable: Could not extract \`SegmentOptions\` from \`./packages/scripts/src/vendors/analytics/segment.ts\`. Verify the path/name and that the file is included by your tsconfig.\*

### Script

| Property    | Value                                               |
| :---------- | :-------------------------------------------------- |
| Type Name   | \`Script\`                                          |
| Source Path | \`./packages/core/src/libs/script-loader/types.ts\` |

\*AutoTypeTable: Could not extract \`Script\` from \`./packages/core/src/libs/script-loader/types.ts\`. Verify the path/name and that the file is included by your tsconfig.\*
