Programmatic API

If you need more control over when and how tags are loaded, the Didomi SDK exposes a function to get an observable on the consent status for a given vendor. This allows you to listen to changes to the consent status and react to them to decide when to enable a vendor.

Here are a few use cases and how to implement them with the getObservableOnUserConsentStatusForVendor function. You can find more examples and documentation on the function in the Reference.

Enable a vendor when the user has allowed it

With this structure, your function gets called exactly once and only when the user has given consent to the vendor specifically. It could be immediately if the user has already given consent or later on after the user gives consent.

window.didomiOnReady = window.didomiOnReady || [];
window.didomiOnReady.push(function (Didomi) {
    if (Didomi.isConsentRequired()) {
        // Consent is required: your visitor is from the EU or you are an EU company
        // Only enable the vendor when consent is given
        Didomi.getObservableOnUserConsentStatusForVendor('vendor-id')
            .filter(function(status) { return status === true; }) // Filter out updates where status is not true
            .first() // Only get the first consent status update
            .subscribe(function (consentStatusForVendor) {
                // The user has given consent to the vendor
                // Enable it
            });
    } else {
        // Consent is not required, enable your vendor immediately
    }
});

If your tag is configured to only collect consent for visitors from the EU, you can enable all your tags for other visitors without waiting for the consent. Use the isConsentRequired() function to check if consent is required or not for the current visitor on the page.

With this structure, your function gets called every single time the consent status for the vendor changes. If the user has not given consent information yet, your function is called immediately with undefined as the consent status. If the consent status of the user changes multiple times on the page, the function will be called multiple times.

window.didomiOnReady = window.didomiOnReady || [];
window.didomiOnReady.push(function (Didomi) { 
    if (Didomi.isConsentRequired()) {
        // Consent is required: your visitor is from the EU or you are an EU company
        // Only enable the vendor when consent is given
        Didomi.getObservableOnUserConsentStatusForVendor('vendor-id')
            .subscribe(function (consentStatusForVendor) {
                // The consent status for the vendor has changed
                if (consentStatusForVendor === true) {
                    // The vendor has consent
                } else if (consentStatusForVendor === false) {
                    // The vendor does not have consent
                } else if (consentStatusForVendor === undefined) {
                    // The consent status for the vendor is unknown
                }
            });
    } else {
        // Consent is not required, enable your vendor immediately
    }
});

If your tag is configured to only collect consent for visitors from the EU, you can enable all your tags for other visitors without waiting for the consent. Use the isConsentRequired() function to check if consent is required or not for the current visitor on the page.

Last updated