GithubHelp home page GithubHelp logo

consent.js's Introduction

Get consent

A simple Javascript API to ask for and store consent.

Usage

First load consent.

window.consent = {
  ready() {return this._},
  _: new Promise((resolve, reject) => {
    const s = document.createElement('script')
    s.setAttribute('src', './consent.js')
    s.setAttribute('defer', '')
    s.addEventListener('load', resolve)
    s.addEventListener('error', reject)
    document.body.appendChild(s)
  }),
}

Basic consent example

<h1>First section</h1>
<h2 id="next-section">Second section</h2>
// Wait until consent granted, then go to next section
;(async function() {
  await consent.ready()
  await consent.granted('go-to-next-section')
  document.querySelector('#next-section').scrollIntoView()
}())

// Ask for consent
document.addEventListener('DOMContentLoaded', async () => {
  await consent.ready()

  consent.request('go-to-next-section')

  // Use confirm dialog as consent gate modal
  if (confirm('Can I show you the next section?')) {
    consent.grant('go-to-next-section', {expires: 0})
  } else {
    consent.deny('go-to-next-section', {expires: 0})
  }
})

Third-party script loading example

// Wait until consent granted, then load third-party scripts
;(async function() {
  await consent.ready()
  await consent.granted('load-third-party-scripts')

  const s = document.createElement('script')
  s.setAttribute('defer', '')
  s.setAttribute('src', `https://www.googletagmanager.com/gtag/js?id=${gtmId}`)
  document.body.appendChild(s)
  window.dataLayer = window.dataLayer || []
  function gtag(){dataLayer.push(arguments)}
  gtag('js', new Date())
  gtag('config', gtmId)
}())

// Ask for consent (if not already stated)
;(async function() {
  await consent.ready()

  if (!consent.status('load-third-party-scripts')) {
    // Create HTML consent gate modal
    const el = document.createElement('div')

    el.setAttribute('style', `
      position: absolute;
      top: 10px;
      left: 10px;
      background: #FFF;
      border: 3px solid #000;
    `)

    el.innerHTML = `
      <p>Grant consent for Google Analytics, Ads etc.?</p>

      <button type="button" onclick="consent.deny('load-third-party-scripts')">
        No
      </button>

      <button type="button" onclick="consent.grant('load-third-party-scripts')">
        Yes
      </button>
    `

    consent.request('load-third-party-scripts')

    document.body.appendChild(el)
  }
}())

consent.js's People

Contributors

colinmeinke avatar

Stargazers

Roman avatar timelyportfolio avatar

Watchers

 avatar timelyportfolio avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.