# root-consent

> Provides a configurable way of obtaining explicit consent from users for tracking purposes

Latest version **1.0.6** (published 2023-05-23) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install root-consent
pnpm add root-consent
yarn add root-consent
bun add root-consent
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2023-05-23 |
| First published | 2019-05-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 109.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Byron Fitzgerald |
| Maintainers | rootstudio |

## Links

- npm: https://www.npmjs.com/package/root-consent
- npm.io page: https://npm.io/package/root-consent

## Dependencies (2)

- [node-sass](https://npm.io/package/node-sass.md) ^6.0.1
- [custom-event-polyfill](https://npm.io/package/custom-event-polyfill.md) ^0.3.0

## Recent versions

- 1.0.6 (latest) — 2023-05-23
- 1.0.5 — 2021-11-19
- 1.0.4 — 2021-11-19
- 1.0.3 — 2020-06-11
- 1.0.2 — 2019-07-15
- 1.0.1 — 2019-05-29
- 1.0.0 — 2019-05-29

## README

# Root Consent

This library can be used to provide GDPR compliant consent screen that allows users to opt-in explicitly for data collection. Also has the option to set the consent as always approved

## Installation

The library can be installed via NPM or included in your page:

* NPM: `npm install root-consent`
* Download Latest Release

#### ES6

```js
import RootConsent from 'root-consent';

const consent = new rootConsent( 'body', {
    delay: 500,
} );
```

#### HTML

```html
<script src="./js/root-consent.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const consent = rootConsent('body', {
        	delay: 500
        });
    });
</script>
```

#### JQuery

```html
<script src="./js/jquery.root-consent.js"></script>
<script>
    $(document).ready(function() {
        $('body').rootConsent({
        	delay: 500
        });
    });
</script>
```

## Options

The library is designed to be customisable, you can configure the text, theme and positioning on the page.

<table>
	<tr>
		<th>Property</th>
		<th>Type</th>
		<th>Default</th>
	</tr>
	<tr>
		<td>messageTitle</td>
		<td>String</td>
		<td>Your Right to Privacy</td>
	</tr>
	<tr>
		<td>messageText</td>
		<td>String</td>
		<td>This website uses analytics software - do you wish to be included in the collected data? Find out more in our Privacy Policy.</td>
	</tr>
	<tr>
		<td>approveLabel</td>
		<td>String</td>
		<td>I accept</td>
	</tr>
	<tr>
		<td>denyLabel</td>
		<td>String</td>
		<td>No thanks</td>
	</tr>
	<tr>
		<td>storageKey</td>
		<td>String</td>
		<td>root-consent</td>
	</tr>
	<tr>
		<td>delay</td>
		<td>Integer</td>
		<td>1000</td>
	</tr>
	<tr>
		<td>expiryFrame</td>
		<td>Integer</td>
		<td>(1000 * 60 * 60 * 24)</td>
	</tr>
	<tr>
		<td>expiry</td>
		<td>Integer</td>
		<td>30</td>
	</tr>
	<tr>
		<td>position</td>
		<td>top, top-right, top-left, bottom, bottom-right, bottom-left, intrusive</td>
		<td>bottom</td>
	</tr>
	<tr>
		<td>theme</td>
		<td>light, dark</td>
		<td>dark</td>
	</tr>
	<tr>
		<td>alwaysApproved</td>
		<td>boolean</td>
		<td>false</td>
	</tr>
</table>

## Plugins

This library makes no assumptions on which analytics package or tracking software is used on your site. The control of such things is left up to you through the extendable plugin system.

Plugins can be registered asynchronously like so:
#### ES6

```js
import RootConsent from 'root-consent';
import Example from 'path/toexample';

const consent = new rootConsent('body', {
	delay: 500
});

consent.registerPlugin('example', {
    //Options object here
});
```

#### HTML
```html
<script src="./js/root-consent.js"></script>
<script>
        document.addEventListener('DOMContentLoaded', function() {

            var Consent = rootConsent('body', {
                //Options object here
            });

            Consent.registerPlugin('example', {
                //Options object here
            });
        });
</script>
<script src="./js/plugins/example.js" async></script>
```

You can pass any options through to the plugin by adding a second parameter to the `registerPlugin` method. This will be passed through to the `onLoad()` method within the plugin instance.
```js
import RootConsent from 'root-consent';
import Plugin1 from 'path/to/plugin1';
import Plugin2 from 'path/to/plugin2';

const consent = new rootConsent('body', {
	delay: 500
});

consent.registerMultiple([
    {
        name: 'Plugin1'
        options: {}
    },
    {
        name: 'Plugin2'
        options: {}
    }
]);
```
Its also possible to register multiple plugins at one

### Developing Plugins

Adding a new plugin is straight forward, each one has three methods that must be callable publicly: `onLoad`, `onApprove` and `onDeny`. These handle the different states of consent managed by the library.

Here is a simple example:

```js
import providePlugin from 'root-consent/src/js/utils/provide-plugin';

class Example {

    // Set any default config options here
    constructor () {
    }
    
    onLoad() {
        console.log('PLUGIN REGISTERED');
    };

    onApprove() {
        console.log('PLUGIN CONSENT GIVEN');
    };

    onDeny() {
        console.log('PLUGIN CONSENT DENIED');
    };
}

providePlugin('example', new Example());
```

The `providePlugin` utility is a helper utility to correctly dispatch the loaded event so the main library can complete the plugin registration. The same functionality can be replicated using:

```js
document.addEventListener( `root-consent.plugin.${pluginName}.load`, ( ev ) => {
    pluginInstance.onLoad( ev.detail );

    // Catches plugins that load before RootConsent has been called
    fireEvent( document, `root-consent.plugin.${pluginName}.loaded`, {
        instance: pluginInstance,
        name: pluginName
    } );
} )

fireEvent( document, `root-consent.plugin.${pluginName}.registered`, {
    instance: pluginInstance,
    name: pluginName
} );
```

**Note:** you should ensure the plugin name used for registering matches the plugin name passed to the provide event.

## Events

The library fires events on the associated DOM Node as actions are completed:

<table>
	<tr>
		<th>Event</th>
		<th>Payload</th>
	</tr>
	<tr>
		<td>root-consent.setup</td>
		<td>-</td>
	</tr>
	<tr>
		<td>root-consent.display</td>
		<td>-</td>
	</tr>
	<tr>
		<td>root-consent.hide</td>
		<td>-</td>
	</tr>
	<tr>
		<td>root-consent.approve</td>
		<td>-</td>
	</tr>
	<tr>
		<td>root-consent.deny</td>
		<td>-</td>
	</tr>
	<tr>
		<td>root-consent.plugin.[NAME].load</td>
		<td>options</td>
	</tr>
	<tr>
		<td>root-consent.plugin.[NAME].approve</td>
		<td>{instance, name}</td>
	</tr>
	<tr>
		<td>root-consent.plugin.[NAME].deny</td>
		<td>{instance, name}</td>
	</tr>
	<tr>
		<td>root-consent.plugin.[NAME].loaded</td>
		<td>instance, name</td>
	</tr>
	<tr>
		<td>root-consent.plugin.[NAME].registered</td>
		<td>instance, name</td>
	</tr>
</table>

## License

The MIT License (MIT)

Copyright (c) 2018 Root Studio

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---
_Source: https://npm.io/package/root-consent · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
