0.0.2 • Published 5 months ago

google-cmp-addon v0.0.2

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

🏠 Homepage

About

If you use google adsense and the new google "Privacy & Message" CMP feature and need to add custom purposes - This is for you. This library adds the missing functionality to en- and disable custom services (Google maps, analytics, ...) according to the user selected purposes. It works just like any other CMP. Configure and match the purpose and let the library do the rest

NOTE: This library is still BETA and experimental since Google is changing the way their CMP works - Besides its still full of bugs!

Getting started

Javascript usage

To use the widget on your website, simply embed the js as well as a valid config.

```html
<!-- make sure the config gets after the libary code -->
<script defer type="text/javascript" src="/path/to/js"></script>
<script defer type="text/javascript" src="/path/to/config.js"></script>

The config file should be in the format:

window.gcmp.init({
  debug: true,
  reloadAfterUserAction: true,
  onLoad: function () {
  },
  onUi: function () {
  },
  purposes: [{
    purpose: 2,
    selectors: '[data-consent="affiliate-ad"]',
    cookies: [/^UI$/i, /^PI$/i, /^BT$/i]
  }, {
    purpose: 10,
    selectors: '[data-name="analytics"],[data-consent="analytics"]',
    cookies: [/^ga/i, /^_ga/i, /^_gid/i],
  }, {
    purpose: 7,
    selectors: '[data-name="externalmedia"],[data-consent="externalmedia"]',
  }, {
    purpose: 7,
    selectors: '[data-name="security"],[data-consent="security"]',
  }]
});

React usage

npm install google-cmp-addon
yarn install google-cmp-addon

Use with react provider

import React from 'react';
import {GcmpProvider} from "google-cmp-addon";

export const App = () => {
  const config = {
    // your config here - see above´s javascript usage
  }
  return (<GcmpProvider config={config}>
    Hello React
  </GcmpProvider>)
}

See all the options and configurations here. Please also consult the IAB official documentation: IAB And the google JavaScript API https://developers.google.com/funding-choices/fc-api-docs.

propertytypedefaultDescription
debugBooleanfalseEnable debug mode
tcfVersionNumber2.2The TCF version to use
waitForDomBooleanfalseWaits for the DOM to be fully loaded to run
initWithGoogleBooleantrueRun when goggle has issued ready event (default)
rootDocument | Element;DocumentDefine the root element from where to look for placeholders
purposesPurposes[][]Requires the purpose object (see below)
embedOptionsEmbedOptionsundefinedOptional options for embedded objects
elementOptionsElementOptionsundefinedOptional options for elements
reloadAfterUserActionbooleanfalseHard reload after user has confirmed purpose choices in google UI
onInit(app: App) => voidundefinedFires when the google CONSENT_DATA_READY and IAB window.__tcfapi.addEventListener() has been fired
onLoad(app: App) => voidundefinedFires when the google CONSENT_DATA_READY and IAB window.__tcfapi.addEventListener() has been fired
onUi(app: App) => voidundefinedFires when the google UI is mounted in the parent DOM
onAdStatus(status: number) => voidundefinedFires when the google AD_BLOCK_DATA_READY has fired

The purpose object has the following configurations:

propertytypedefaultDescription
purposeNumberExpects the a number between 1-11 - according to the google IAB purposes
mode"hide" | "remove" | "disable"undefinedThe mode to use on elements where "hide" hides the element, "remove" removes it from DOM and "disable" tries to disable it
tagsString[]undefinedA list of allowed html tag names
selectorsStringExpects the HTML selectors to target
cookiesRegExp[]undefinedArray of regexp expression for cookie names to remove
localStorageRegExp[]undefinedArray of regexp expression to remove entries from local storage
sessionStorageRegExp[]undefinedArray of regexp expression to remove entries from session storage
onRenderonRender?: (consent: boolean, nodes: NodeListOf,app: App,) => void | falseundefinedHook in custom render function for this purpose
<script type="text/plain"
        data-type="text/javascript"
        data-consent="optimizely"
        data-src="https://cdn.optimizely.com/js/10196010078.js">
</script>

The same method also works for images, stylesheets and other elements with a src or type attribute.

Configure

Install

yarn install

Usage

yarn run dev

Run tests

yarn run test

Author

👤 Frank Mueller set@cooki.me

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2024 Frank Mueller <set@cooki.me>. This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator

0.0.2

5 months ago

0.0.1

5 months ago