[C] Consentify

@consentify/cloud

Cloud adapter that posts consent events to Consentify SaaS for analytics.

@consentify/cloud is a thin adapter that bridges @consentify/core with the Consentify SaaS backend. It subscribes to consent state changes and posts events to your analytics dashboard.

Installation

npm install @consentify/cloud @consentify/core

Usage

import { createConsentify } from '@consentify/core';
import { enableCloud } from '@consentify/cloud';

const consentify = createConsentify({
  policy: { categories: ['analytics', 'marketing'] as const },
});

const dispose = enableCloud(consentify, {
  siteId: 'your-site-id',
  apiKey: 'ck_your_api_key',              // optional
  endpoint: 'https://consentify.dev/api', // optional
});

// Later, to stop cloud sync:
dispose();

enableCloud returns an unsubscribe function. It is a no-op during SSR (returns () => {} if window is undefined).

Options

OptionRequiredDefaultDescription
siteIdYes-Your site ID from the Consentify dashboard
apiKeyNo-API key (ck_ prefix) for authenticated event posting
endpointNohttps://consentify.dev/apiBase URL; adapter POSTs to ${endpoint}/consent/events

How It Works

  1. On enable: if consent is already decided, posts the current state immediately
  2. On change: subscribes to instance.subscribe() and posts events when choices change
  3. Deduplication: only posts when the JSON-stringified choices or policy version actually change (no duplicate events on noise)
  4. Visitor tracking: generates a visitor hash (crypto.randomUUID() preferred) stored in localStorage under consentify_visitor
  5. Action derivation: auto-classifies into accept_all, reject_all, or customize based on non-necessary categories

Event Payload

POSTed to ${endpoint}/consent/events:

{
  "siteId": "uuid",
  "action": "accept_all",
  "categories": { "analytics": true, "marketing": true },
  "visitorHash": "uuid",
  "policyVersion": "a1b2c3d4"
}

If apiKey is provided, it is sent via the X-API-Key header.

Silent Failure

Network errors are swallowed. Cloud sync never blocks consent operations or throws to the user. Consent continues to work locally even if the API is unreachable.

Next.js Setup

enableCloud guards window internally, so it is safe to call at module scope in SSR frameworks:

// lib/consentify.ts
import { createConsentify } from '@consentify/core';
import { enableCloud } from '@consentify/cloud';

export const consentify = createConsentify({
  policy: { categories: ['analytics', 'marketing'] as const },
});

enableCloud(consentify, {
  siteId: process.env.NEXT_PUBLIC_CONSENTIFY_SITE_ID!,
  apiKey: process.env.NEXT_PUBLIC_CONSENTIFY_API_KEY,
});

Add to .env.local:

NEXT_PUBLIC_CONSENTIFY_SITE_ID=your-site-id
NEXT_PUBLIC_CONSENTIFY_API_KEY=ck_your_api_key

On this page