Skip to main content

Analytics

Segment

Configure Segment

Copy the write key of your Segment JavaScript source. The helper builds the loader URL from it.

npm install @c15t/integrations@alpha
src/consent-scripts.ts
import { segment } from '@c15t/integrations/segment';

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

Register the scripts

Complete your framework quickstart first. Keep its Inth endpoint, policy, styles and consent UI. Remove the vendor's original script, SDK initializer or tag-manager entry, so the vendor loads only through c15t.

The vendor pages put the helper in src/consent-scripts.ts. If your framework quickstart already has a scripts array, such as the one in c15t.config.ts in the Next.js guide, add the helper to that array instead of creating a second file. The scripts export is a configuration, not an initializer. Add it to the c15t provider you already have, at the registration point for your framework below. These are edits to that provider, not a second provider.

Add the configuration to scripts in c15t.config.ts, next to next.config.ts:

import { defineConsentConfig } from 'c15t/next';
import { scripts } from './src/consent-scripts';

export default defineConsentConfig({ scripts });

Keep the rest of your config, such as mode and routePrefix, in the same call. ConsentRoot reads the config in the browser, so the layout keeps passing only state. App Router, Pages Router and static export all read the same file. See Next.js scripts and embeds.

Options

OptionDefaultBehavior
writeKeyRequiredSource write key. Empty or whitespace-only values log an error and the script does not load.
loadOptions{}Set as window.analytics._loadOptions before the loader runs. Analytics.js reads it on initialization.
trackPageViewtrueQueues one analytics.page() call before the loader. Set false to send page views yourself.
scriptUrlhttps://cdn.segment.com/analytics.js/v1/<writeKey>/analytics.min.jsLoader URL override.

Loading and revocation

segment uses the measurement category. Before measurement is allowed, c15t defines no window.analytics and loads nothing from Segment. When measurement becomes allowed, the helper creates the window.analytics queue with track, page, identify, group, alias and reset, sets the load options, queues the page view, then loads Analytics.js. On revocation the helper removes the script element and calls no Segment API.

Segment forwards events to every destination enabled on the source. A measurement grant does not cover a destination that needs marketing permission. Configure destination consent in Segment, or load that vendor with its own c15t helper instead.

Guard your own Segment calls

The helper sends the first page view. Calls your code makes, such as track and identify, need their own check. After revocation window.analytics still exists, so its presence does not mean measurement is allowed. Check the permission first, then that the queue exists:

src/track-signup.ts
export function trackSignup(measurementAllowed: boolean, plan: string) {
	if (!measurementAllowed || !window.analytics) return;
	window.analytics.track('Signup', { plan });
}

Pass the current measurement permission from your framework, for example useConsent('measurement') in React. Analytics.js sends no page view on client-side route changes, so call window.analytics.page() from your router behind the same check.

Verify Segment

After you allow measurement, analytics.min.js loads from cdn.segment.com with your write key in the path, and a page call goes to api.segment.io. Check that each browser destination on the source loads only under the permission it needs. Then revoke measurement. Before the reload, calling trackSignup sends nothing.

Test in a private window with an opt-in policy. Open DevTools Network, disable the cache and filter by the vendor's domain:

  1. Load the page. No request goes to the vendor before you choose.
  2. Click Reject, then reload. There is still no vendor request.
  3. Open Privacy settings and allow the helper's category. The vendor script loads without a page reload.
  4. Turn the category off again and save. c15t reloads the page, and the new page makes no vendor request.

c15t reloads on revocation because removing a script element does not stop code that already ran. The vendor's listeners, timers and queued events stay alive until the page unloads. If you set reloadOnConsentRevoked: false, stop the vendor yourself. Register a callback-only script whose onConsentChange calls the vendor's opt-out API, as shown in custom integrations, and check the permission before each of your own event calls. The reload does not delete cookies the vendor already set; see clear on revocation for your framework.

The helper sets vendor to its script ID, so once you declare that vendor a visitor can turn it off inside an allowed category. See vendor consent for your framework. The consent verification guide covers navigation, expiry and hosting checks.