Skip to content

Javascript library for providing users with a choice, extendable by plugins

Notifications You must be signed in to change notification settings

RootStudio/Root-Consent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

import RootConsent from 'root-consent';

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

HTML

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

JQuery

<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.

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

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

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

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

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

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.

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:

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:

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:

Event Payload
root-consent.setup -
root-consent.display -
root-consent.hide -
root-consent.approve -
root-consent.deny -
root-consent.plugin.[NAME].load options
root-consent.plugin.[NAME].approve {instance, name}
root-consent.plugin.[NAME].deny {instance, name}
root-consent.plugin.[NAME].loaded instance, name
root-consent.plugin.[NAME].registered instance, name

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.

About

Javascript library for providing users with a choice, extendable by plugins

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published