scm-spain / boros-CMP

GDPR CMP (Consent Managment Provider) implementation
MIT License
5 stars 1 forks source link

window.__cmp not ready after bootstrap initialization #55

Closed alextremp closed 6 years ago

alextremp commented 6 years ago

SUMMARY When the boros-cmp is imported and the init method is called it's expected to be able to use the window.__cmp function, but there's a race condition as the initialization runs in a Promise.

STEPS TO REPRODUCE

EXPECTED RESULT

ACTUAL RESULT

rafamarquezv commented 6 years ago

Hi,

Are you subscribed to cmpReady event?

You should use this way to listen to the event Boros is dispatching:


boroscmp.init()

window.document.addEventListener("cmpReady", (event) =>{
 // call the command you want as follows...
  window.__cmp('ping', null, (data, success) => {
    console.log(JSON.stringify(success, null, 2));
    console.log(JSON.stringify(data, null, 2));
  })
})
`
victuxbb commented 6 years ago

Definitely we should create documentation about the two possible ways of knowing when BorosCmp is ready:

rafamarquezv commented 6 years ago

To initialize Boros CMP using default configuration values:

import boroscmp from '@schibstedspain/boros-cmp'

boroscmp.init()
    .then(()=>{
        // do your stuff ...
    })

To initialize Boros CMP using customized configuration values:

import boroscmp from '@schibstedspain/boros-cmp'

const customConfig = {
  gdpr: {
    gdprApplies: true,
    storeConsentGlobally: false
  },
  consent: {
    consentScreen: 1,
    consentLanguage: 'es'
  }
}

boroscmp.init({config: customConfig})
    .then(()=>{
        // do your stuff ...
    })

Notice that init method is returning a Promise but you can subscribe to cmpReady event instead:

import boroscmp from '@schibstedspain/boros-cmp'

boroscmp.init()

window.document.addEventListener("cmpReady", (event) =>{
  // do your stuff ...
  // like calling window.__cmp( ...
})
rafamarquezv commented 6 years ago

Solved in version -> @schibstedspain/boros-cmp@1.0.3

https://www.npmjs.com/package/@schibstedspain/boros-cmp