@meowwolf/cookie-consent-utils
v1.2.0
Published
A collection of utilities for managing cookie consent. Used to manage cookie consent across meow wolf sites.
Readme
Cookie Consent Utils
A lightweight collection of TypeScript utility functions for managing privacy consent across Meow Wolf sites, built around Cloudflare Zaraz's Consent API, the Global Privacy Control (GPC) signal, and Segment.
Features
- Integrates with Segment (
@segment/analytics-nextand@segment/analytics-consent-tools) to delay loading until Zaraz consent is resolved - Automatically synchronizes consent category preferences with Segment tracking calls
- Displays the Zaraz privacy consent modal on demand
- Honors the browser's Global Privacy Control (GPC) signal, opting users out automatically when enabled
- Synchronizes consent state with the Zaraz Consent API on page load
- Automatically reloads the page on consent choice updates so changes take effect immediately
Installation
npm install @meowwolf/cookie-consent-utilsThis package is published as compiled JavaScript with bundled type
declarations (dist/index.d.ts), so no separate @types package is needed.
Quick start
import {
handlePrivacyOnClick,
loadSegmentWithConsent,
} from '@meowwolf/cookie-consent-utils';
// Initialize Segment tracking with Zaraz consent management.
window.analytics = loadSegmentWithConsent('YOUR_SEGMENT_WRITE_KEY');
// Wire up a "Privacy Settings" link/button to open the Zaraz consent modal.
document
.querySelector('#privacy-settings-link')
?.addEventListener('click', handlePrivacyOnClick);
// Track page views or events as usual; outgoing calls are buffered and
// dispatched according to user consent once Zaraz is ready.
window.analytics.page();How it works
- Segment integration:
loadSegmentWithConsentcreates anAnalyticsBrowserinstance wrapped with Zaraz consent management using@segment/analytics-consent-tools. Segment loading is delayed until the Zaraz Consent API is ready (zarazConsentAPIReady). - Consent synchronization: When Zaraz is ready, the library synchronizes consent state with Zaraz (opting users in by default if no choices exist yet, or denying consent if GPC is active).
- Consent cookie & categories: Zaraz stores the user's consent choices in a
cookie named
zaraz-consent. Category preferences are extracted from this cookie and automatically attached to Segment's consent context. - GPC takes precedence: Whenever
navigator.globalPrivacyControlistrue, the library treats the user as opted out, regardless of what is stored in the consent cookie. - Consent updates: When the user updates their choices in the Zaraz modal
(
zarazConsentChoicesUpdated), the new category preferences are saved and the page is reloaded so changes take effect immediately. window.zaraz: The Zaraz Consent API (window.zaraz) is injected at runtime by the Cloudflare Zaraz script and is not bundled with this package. Functions that touchwindow.zarazno-op safely if it is not present yet.
API Reference
loadSegmentWithConsent(writeKeyOrSettings: string | AnalyticsBrowserSettings, options?: InitOptions, consentOptions?: ConsentOptions): AnalyticsBrowser
Creates an AnalyticsBrowser instance wrapped with Zaraz consent management and begins loading Segment:
- Delays Segment and device-mode destination initialization until Zaraz is ready.
- Automatically stamps outgoing events with the user's consent category preferences.
- Honors GPC signals and listens for consent preference changes.
- Safe to use synchronously in React components/hooks as well as vanilla JS (buffered pre-initialization calls are preserved and dispatched once consent resolves).
- If Zaraz doesn't report ready within 3 seconds, loads Segment with
JaAU: false(required cookies only) and callsconsentOptions.onZarazUnavailable({ timeoutMs, zarazDefined })once, so the site can report it to error monitoring:
window.analytics = loadSegmentWithConsent('YOUR_SEGMENT_WRITE_KEY', undefined, {
onZarazUnavailable: (details) => Sentry.captureMessage('Zaraz consent API not ready', { level: 'warning', extra: details }),
})handlePrivacyOnClick(e: Event): void
Click handler for privacy-related elements (e.g. a "Privacy Settings" link). Prevents the default action and opens the Zaraz consent modal (window.zaraz.consent.modal = true).
<a href="/privacy" onClick={handlePrivacyOnClick}>Privacy Settings</a>Requirements
- These utilities are designed to run in a browser context (they read
document.cookieandwindow/navigatorglobals) and assume the Cloudflare Zaraz script is loaded on the page for consent-modal and consent-sync features to take effect.
Development
This project uses pnpm. Common scripts:
pnpm install # install dependencies
pnpm run build # compile TypeScript to dist/
pnpm run lint # lint with ESLint
pnpm test # run the test suite (Vitest)
pnpm run test:watch # run tests in watch mode
pnpm run test:coverage # run tests with coverage
pnpm release # interactively bump version and create a release branchSee CONTRIBUTING.md for local setup, branching model, and the release process, and DEPLOYMENT.md for details on the CI/publish pipeline.
License
ISC
