0.1.20 • Published 9 months ago

@consentry/next v0.1.20

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@consentry/next

React + Next.js integration for the Consentry consent manager. Provides a plug-and-play Consent Manager Provider and dynamic script loader based on user preferences.


✨ Overview

@consentry/next is the official Next.js wrapper for @consentry/core. It offers:

  • ✅ A React Context provider for managing cookie consent
  • ✅ Automatic script filtering and injection via <Script />
  • ✅ Google Analytics consent synchronization (gtag)
  • ✅ Support for external consent.config files

📦 Installation

npm install @consentry/next @consentry/core

⚙️ Setup

1. Create a consent.config.ts file in your app root:

import { defineConsentConfig } from "@consentry/core";

export default defineConsentConfig({
  debug: true,
  defaults: {
    functional: true,
    performance: false,
    advertising: false,
    social: false,
  },
  scripts: [
    {
      id: "ga4",
      category: "performance",
      strategy: "afterInteractive",
      src: "https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX",
      vendor: "Google Analytics",
    },
    {
      id: "fb-pixel",
      category: "advertising",
      content: "console.log('Facebook Pixel loaded')",
      vendor: "Facebook",
    }
  ]
});

2. Wrap your app with the ConsentManagerProvider

// app/layout.tsx or _app.tsx

import { ConsentManagerProvider } from "@consentry/next";

export default function RootLayout({ children }) {
  return <ConsentManagerProvider>{children}</ConsentManagerProvider>;
}

3. Use the hook to manage preferences

import { useConsentManager } from "@consentry/next";

const { cookiePreferences, setCategoryConsent } = useConsentManager();

return (
  <button onClick={() => setCategoryConsent("performance", true)}>
    Enable Performance Cookies
  </button>
);

🧠 Features

ConsentManagerProvider

  • Initializes state from localStorage or cookies
  • Applies default values from consent.config.ts
  • Syncs changes to gtag() for analytics/ads
  • Injects scripts using <Scripts />

useConsentManager() hook

Returns:

{
  cookiePreferences,
  setCookiePreferences,
  setCategoryConsent,
  hasConsentedTo,
  hasConsentedOnce
}

<Scripts /> component

Automatically injects only allowed scripts based on config + preferences.


📄 License

MIT — Copyright © 2025 Mustafa ONAL

0.1.20

9 months ago

0.1.19

10 months ago

0.1.17

10 months ago

0.1.16

10 months ago

0.1.15

10 months ago

0.1.14

10 months ago

0.1.13

10 months ago

0.1.12

10 months ago

0.1.11

10 months ago

0.1.10

10 months ago

0.1.9

10 months ago

0.1.8

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago