scm-spain / boros-CMP

GDPR CMP (Consent Managment Provider) implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

window.__cmp not ready after bootstrap initialization

alextremp opened this issue · comments

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

  • Import the project
  • Call the init method
  • Call any window.__cmp command

EXPECTED RESULT

  • The command is consumed with the window.__cmp function

ACTUAL RESULT

  • There's a random error (race condition) indicating that window.__cmp is not a function

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));
  })
})
`

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

  • Using .then as response of init method (we must fix the error of missing return in bootstrap)
  • Adding a window.document Listener for "cmpReady" event for legacy code

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( ...
})

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

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