Documentation

Table of Contents

How to migrate consents from a previous provider to the Cookie Solution

If you’re switching from another cookie management solution to ours, you may want to migrate the consents you’ve already collected. This is useful for ensuring that users who have already given their consent under the previous solution are not presented with the cookie banner, and the related request for consent, again.

There are two ways to do this, depending on whether the “old” platform provides a cookie (synchronous method) or requires a call to its API (asynchronous method).

Synchronous method

Before embedding the Cookie Solution you’ll need to define a synchronous function (e.g. isConsentGivenByOtherPlatform) to get the consent from the other platform:

<script type="text/javascript">
    function readLocalCookie(cookieName) {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) { 
            var e=cookies[i].split('=');
            if (e[0] === cookieName) {
                return e[1];
            }
        }
        return null;
    }

    function isConsentGivenByOtherPlatform() {
        var otherPlatformCookieName = 'cookie_from_other_platform'; // use the actual cookie name saved by the other platform 
        var prevConsent = readLocalCookie(otherPlatformCookieName); 
        if (prevConsent) { 
            return true; 
        } else { 
            return false; 
        } 
    }
</script>
Important

Don’t copy and paste the code shown above — it’s just sample code we’ve provided to help you understand the approach.

Next, add the Cookie Solution snippet, invoking the onBeforePreload callback:

<script type="text/javascript">
    var _iub = _iub || [];
    _iub.csConfiguration = {
        "lang": "en",
        "siteId": XXXXXX, // use your siteId
        "cookiePolicyId": YYYYYY, // use your cookiePolicyId
        "banner": {
            "acceptButtonDisplay": true,
            "customizeButtonDisplay": true,
            "position": "float-top-center"
        },
        "callback": {
            onBeforePreload: function() {
                if (typeof window.isConsentGivenByOtherPlatform === 'function' && isConsentGivenByOtherPlatform()) {
                    _iub.cs.api.storeConsent();
                }
            }
        }
    };
</script>
<script type="text/javascript" src="//cdn.iubenda.com/cs/iubenda_cs.js" charset="UTF-8" async></script>

The onBeforePreload callback checks if consent is already given on another platform and, if so, stores it calling the _iub.cs.api.storeConsent() method.

Asynchronous method

Before embedding the Cookie Solution code you’ll need to define a function that:

  1. accepts a callback;
  2. detects the consent;
  3. saves the consent in a global variable; and
  4. invokes the callback afterward.

Here’s a sample:

<script type="text/javascript">
    // fetch consent asynchronously from 3rd party 
    function getConsentByOtherPlatform(cb) {
        $.getJSON('http://otherplatform.com/api/get-consent', function(result) {
            if (result.given) {
                window.consentByOtherPlatform = true;
            } else {
                window.consentByOtherPlatform = false;
            }
            cb();
        });
    }

    function isConsentGivenByOtherPlatform() {
        return !!window.consentByOtherPlatform;
    }
</script>
Important

Don’t copy and paste the code shown above — it’s just sample code we’ve provided to help you understand the approach.

Next, add the Cookie Solution snippet, invoking the onBeforePreload callback:

<script type="text/javascript">
    var _iub = _iub || [];
    _iub.csConfiguration = {
        "lang": "en",
        "siteId": XXXXXX, // use your siteId
        "cookiePolicyId": YYYYYY, // use your cookiePolicyId
        "banner": {
            "acceptButtonDisplay": true,
            "customizeButtonDisplay": true,
            "position": "float-top-center"
        },
        "callback": {
            onBeforePreload: function() {
                if (typeof window.isConsentGivenByOtherPlatform === 'function' && isConsentGivenByOtherPlatform()) {
                    _iub.cs.api.storeConsent();
                }
            }
        }
    };
</script>
<script type="text/javascript">
    // ask for 3rd party consent before including the CS
    getConsentByOtherPlatform(function() {
        var s = document.createElement('script');
        s.src = '//cdn.iubenda.com/cs/iubenda_cs.js';
        document.head.appendChild(s);
    });
</script>

The onBeforePreload callback will check if consent is already given on another platform and, if so, stores it calling the _iub.cs.api.storeConsent() method.

See also

Still have questions?

Visit our support forum Email us