@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/coreUsage
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
| Option | Required | Default | Description |
|---|---|---|---|
siteId | Yes | - | Your site ID from the Consentify dashboard |
apiKey | No | - | API key (ck_ prefix) for authenticated event posting |
endpoint | No | https://consentify.dev/api | Base URL; adapter POSTs to ${endpoint}/consent/events |
How It Works
- On enable: if consent is already
decided, posts the current state immediately - On change: subscribes to
instance.subscribe()and posts events when choices change - Deduplication: only posts when the JSON-stringified choices or policy version actually change (no duplicate events on noise)
- Visitor tracking: generates a visitor hash (
crypto.randomUUID()preferred) stored inlocalStorageunderconsentify_visitor - Action derivation: auto-classifies into
accept_all,reject_all, orcustomizebased 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