Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to wait until user accept/decline #481

Open
janstieler opened this issue Aug 30, 2023 · 2 comments
Open

How to wait until user accept/decline #481

janstieler opened this issue Aug 30, 2023 · 2 comments

Comments

@janstieler
Copy link

Hi,
how can I wait wit a JS function until the user have accepted or deccline the consent window?
I'm not shure if the watcher is the right tool for that. And if yes how have I to use it?
The manual is a little bit written on the diet way.

Best

@fjahn
Copy link

fjahn commented Aug 30, 2023

The easiest way is to write it directly into the config at a service's callback property:

const config = {
    services: [
        {
            name: 'googleAnalytics',
            callback(consented, service) {
                console.log(`Consent status for ${service.name}: ${consented}`)
            }
        }
    ]
}

If you need to do this in a specific place in code, you can use the ConsentManager to register a watcher:

const manager = klaro.getManager(config) // Use the same config object you used for initialization
manager.watch({
    update(config, eventName, consents) {
        if (eventName !== 'consents') return
        console.log(`Consent status for google analytics: ${consents.googleAnalytics}`)
    }
})

Code is untested and might need minor fixing, just writing this from memory.

@janstieler
Copy link
Author

@fjahn thank you very much! This helps me really!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants