npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@primarix/easy-consent

v0.0.5

Published

A lightweight consent management solution for Google Analytics and related services. This is a beta version and should be used with caution.

Readme

Easy Consent

Disclaimer

This npm package is provided “as is” and is in beta. No warranties, express or implied (including merchantability or fitness for a particular purpose). The author is not liable for any direct, indirect, incidental or consequential damages (data loss, lost profits, business interruption, etc.) arising from its use.

What it does

Implements Consent Mode for services like Google Analytics and Google Ads, allowing easy, configurable management of cookie permissions (analytics, advertising, functionality, personalization and more)

Installation

npm install @primarix/easy-consent

Quick Start

Basic Usage

import { EasyConsent } from '@primarix/easy-consent';

// Initialize with your Google Analytics ID
const consentManager = new EasyConsent('YOUR-GA-ID');

// Update single consent preference
consentManager.update('analytics_storage', 'granted');

// Update multiple consent preferences at once
consentManager.updateMultiple({
    analytics_storage: 'granted',
    ad_storage: 'denied',
    functionality_storage: 'granted'
});

// Accept all consent options
consentManager.acceptAll();

// Reject all consent options
consentManager.rejectAll();

// Check consent states
const allGranted = consentManager.isAllConsented();  // true if all options are granted
const allDenied = consentManager.isAllDenied();      // true if all options are denied

// Access current consent state
console.log(consentManager.state);  // Get complete consent configuration

// Check if user is new
console.log(consentManager.isNewUser);  // true for new users, false for returning users

// Listen for consent updates
window.addEventListener('consent-updated', (event) => {
    const { key, mode, state, timestamp } = event.detail;
    console.log('Consent updated:', { key, mode, state, timestamp });
});

React Integration

import { type Options, EasyConsent } from "@primarix/easy-consent";
import { useConsent } from "@primarix/easy-consent/reactHooks";

export interface ConsentProps {
    consent: EasyConsent
}

export const ConsentComp: React.FC<ConsentProps> = ({ consent }) => {
    const {
        choices,
        changeChoice,
        toggleMode,
        grantAll, 
        denyAll
    } = useConsent(consent);
    
    return (
        <ul className='w-[320px] bg-gray-800 rounded p-2'>
            {Object.keys(consent.state).map((item, index) => (
                <li key={index} className='flex items-center justify-between p-2 bg-blac gap-0.5'>
                    <p className='font-semibold text-white'>{item}</p>
                    <button 
                        onClick={changeChoice(item as Options, toggleMode(choices[item as Options]))}
                        className={`
                            cursor-pointer transition-all duration-400 text-white w-[90px] 
                            font-semibold p-1 rounded uppercase 
                            ${choices[item as Options] === "denied" ? "bg-red-600" : "bg-green-600"}
                        `}
                    >
                        {choices[item as Options]}
                    </button>
                </li>
            ))}
            <div className="flex flex-col gap-3 p-3">
                <button onClick={grantAll} className="text-white cursor-pointer bg-amber-700 py-1">
                    Accept all
                </button>
                <button onClick={denyAll} className="text-white cursor-pointer bg-amber-700 py-1">
                    Reject all
                </button>
            </div>
        </ul>
    );
}

Hook Functions Explained

The useConsent hook provides several functions to manage consent state:

choices

choices: Config
  • Purpose: Provides the current consent state
  • Usage: Access the current state of all consent options
  • Example: Display current consent status in UI
<p>Analytics: {choices.analytics_storage}</p>

changeChoice

changeChoice: (key: Options, value: Mode) => () => void
  • Purpose: Updates a single consent option
  • Usage: Change the state of a specific consent option
  • Example: Toggle a specific consent option
<button onClick={changeChoice('analytics_storage', 'granted')}>
    Allow Analytics
</button>

toggleMode

toggleMode: (mode: Mode) => Mode
  • Purpose: Toggles between 'granted' and 'denied' states
  • Usage: Switch the state of a consent option
  • Example: Toggle button state
<button onClick={() => {
    const newMode = toggleMode(choices.analytics_storage);
    changeChoice('analytics_storage', newMode)();
}}>
    Toggle Analytics
</button>

grantAll

grantAll: () => void
  • Purpose: Grants consent for all options
  • Usage: Accept all consent options at once
  • Example: "Accept All" button
<button onClick={grantAll}>Accept All</button>

denyAll

denyAll: () => void
  • Purpose: Denies consent for all options
  • Usage: Reject all consent options at once
  • Example: "Reject All" button
<button onClick={denyAll}>Reject All</button>

updateChoices

updateChoices: (new_values: Partial<Config>) => () => void
  • Purpose: Updates multiple consent options at once
  • Usage: Bulk update of consent preferences
  • Example: Update multiple options
<button onClick={updateChoices({
    analytics_storage: 'granted',
    ad_storage: 'denied'
})}>
    Update Preferences
</button>

API Reference

EasyConsent Class

class EasyConsent {
    constructor(id: string);
    update(key: Options, mode: Mode): void;
    updateMultiple(config: PartialConfig): void;
    acceptAll(): void;
    rejectAll(): void;
    isAllConsented(): boolean;
    isAllDenied(): boolean;
    readonly state: Config;
    readonly isNewUser: boolean;
}

React Hook

function useConsent(consent: EasyConsent): {
    choices: Config;
    changeChoice: (key: Options, value: Mode) => () => void;
    toggleMode: (mode: Mode) => Mode;
    grantAll: () => void;
    denyAll: () => void;
    updateChoices: (new_values: Partial<Config>) => () => void;
}

Types

type Mode = "denied" | "granted";

interface Config {
    ad_storage: Mode;
    analytics_storage: Mode;
    functionality_storage: Mode;
    personalization_storage: Mode;
    ad_user_data: Mode;
    ad_personalization: Mode;
    security_storage: Mode;
}

type Options = keyof Config;
type PartialConfig = Partial<Config>;

Features

  • 🎯 Google Analytics integration
  • 🔒 Secure cookie storage (SameSite=Lax, Secure)
  • ⚛️ React hooks support
  • 📊 Automatic page view tracking
  • 🔄 Real-time consent updates
  • 🎨 Customizable consent options
  • 📝 Full TypeScript support
  • 🚀 Event-based system
  • 👤 New user detection

Events

The package dispatches consent-updated events:

interface ConsentUpdateEventDetail {
    key: ConsentEventKey;
    mode: ConsentEventMode;
    state: Config;
    timestamp: string;
}

window.addEventListener('consent-updated', (event) => {
    const { key, mode, state, timestamp } = event.detail;
    console.log('Consent updated:', { key, mode, state, timestamp });
});

Error Handling

The package includes error handling for:

  • Cookie parsing
  • Google Analytics initialization
  • Consent updates
  • Bulk operations

Contributing

We welcome contributions! Please feel free to submit issues and pull requests.

License

MIT License

Beta Status

This package is currently in beta testing. Users are advised to:

  1. Test thoroughly in production
  2. Ensure GDPR compliance
  3. Use Google's consent mode testing tools
  4. Monitor for updates