Iubenda logo
Start generating

Documentation

Table of Contents

Cache, Speed and Other Optimization of the iubenda Privacy Controls and Cookie Solution

Here are our tips on how to further optimize the performance of our Privacy Controls and Cookie Solution:

  1. Include the Privacy Controls and Cookie Solution scripts directly in the document’s HTML, don’t use a tag manager or another script
  2. If you’ve enabled the TCF option, you should include the Privacy Controls and Cookie Solution inside the body, not the head
  3. Preload the js files as soon as possible in the head
  4. The Privacy Controls and Cookie Solution should be embedded after at least one text or image tag to avoid influencing First Contentful Paint. However, keep in mind that, if you’ve enabled the TCF option, the lower you place the Privacy Controls and Cookie Solution scripts, the more delayed the ads will be activated.
  5. safe.js and safe-tcf-v2.js impact PageSpeed score. Ideally, you should use them only if you can write server side code (see our php example):
    • include safe.js only when the _iub_cs_xxx cookie exists
    • include safe-tcf-v2.js only when the euconsent-v2 cookie exists
  6. Decrease inlineDelay value (integer, milliseconds, default 500) to reduce the total time of activation. Keep in mind that inlineDelay works only with the “-inline” block, so it is reduced only to that kind of snippet. Finally, don’t set it under 300.

The Privacy Controls and Cookie Solution is configured by default to avoid running when we detect spiders or bots – therefore, there are no SEO effects whatsoever when using it.

Direct loading

In addition to the above suggestions, you may want to use a specific version of the iubenda Privacy Controls and Cookie Solution. To do that, in your snippet, instead of:

<script type="text/javascript" src="//cdn.iubenda.com/cs/iubenda_cs.js" charset="UTF-8" async></script>

You’ll have:

<script type="text/javascript" src="//cdn.iubenda.com/cs/versions/iubenda_cs-1.30.3.js" charset="UTF-8" async></script>

The “1.30.3” value must be replaced with the number of the version to be used. Available versions are those released from version 1.29.0 onwards (check the changelog for a listing of all released Privacy Controls and Cookie Solution versions. To use a version earlier than 1.29.0, read here).

Unlike the default link (//cdn.iubenda.com/cs/iubenda_cs.js), the link //cdn.iubenda.com/cs/versions/iubenda_cs-1.30.3.js has a very extended cache configuration as it points to a specific version which does not require future modifications.

If you decide to lock the iubenda Privacy Controls and Cookie Solution at a specific version, we suggest that you manually update it periodically so that you may continue to benefit from our updates.

By applying the edit described above to the Privacy Controls and Cookie Solution, the js resource will load a new file containing the desired version.

It’s a best practice, also adopted by Google for the loading of Google Analytics, as a way to achieve a compromise between the need to update the Privacy Controls and Cookie Solution and the loading/caching speed.

The js file is in fact a file with a brief cache of few bytes, which in turn calls the real Privacy Controls and Cookie Solution, whose cache is much more extensive.

Direct loading of versions prior to 1.29.0

If Google PageSpeed provided you with a suggestion similar to the following, we can help you solve the problem:

Look through the browser’s cache: If there is a maximum age for statistic resources in the HTTP headers or if an expiry date is set, the browser will load the the resources previously downloaded from the local disk instead of from the network.

For versions prior to 1.29.0 (but later than 1.3.11), to bypass iubenda_cs.js, you’ll have to get the hash from the desired version. This way you’ll be able to:

  • Eliminate an intermediary passage (resulting however in an almost negligible gain of a few milliseconds)
  • Access a version of the file with a very long cache

For example, let’s take version “1.23.2”: by visiting https://cdn.iubenda.com/cs/versions/iubenda_cs-1.23.2.js, you’ll get e.src="//cdn.iubenda.com/cookie_solution/iubenda_cs/core-03369fd53df171f29d94bfed207eedaa.js" which is the URL of the file with the hash of the desired version.

So, in your Privacy Controls and Cookie Solution snippet, instead of:

<script type="text/javascript" src="//cdn.iubenda.com/cs/iubenda_cs.js" charset="UTF-8" async></script>

You’ll have:

<script>
  _iub.csConfigLegacy = !1;
</script>
<script type="text/javascript" src="//cdn.iubenda.com/cookie_solution/iubenda_cs/core-03369fd53df171f29d94bfed207eedaa.js" charset="UTF-8" async></script>

IAB Transparency and Consent Framework

If you’ve enabled IAB Transparency and Consent Framework (TCF) compatibility for the customization of advertising tracking preferences, and would like to make the most of the cache, you need to take the value of _iub.GVL2 from https://cdn.iubenda.com/cs/iubenda_cs.js and add it to your script.

So, continuing with the previous example, instead of:

<script src="//cdn.iubenda.com/cs/iubenda_cs.js" async></script>

You’ll have:

<script>
  _iub.csConfigLegacy = !1, _iub.GVL2 = _iub.GVL2 || 54;
</script>
<script src="//cdn.iubenda.com/cookie_solution/iubenda_cs/core-03369fd53df171f29d94bfed207eedaa.js" async></script>

However, please note that you’ll have to update the value of _iub.GVL2 on a weekly basis (so that it matches the value included in https://cdn.iubenda.com/cs/iubenda_cs.js), as the Global Vendor List is regularly updated.

Because of this, we advise against direct loading for the TCF. Instead, we suggest using the inline activator and not directly blocking the vendor scripts (see our examples).

Inline activator – Direct inclusion of the activator inside the page

It’s also possible to include the part of the code relative to the scripts, directly in-page (inline); this code is known as the inline activator. Scripts can be activated through the inline activator even if the iubenda_cs.js primary resource is generically unavailable or in error.

The inline activator for safe.js is available at:

The inline activator for safe-tcf-v2.js is available at:

For more information and code samples, see How to Configure Your Privacy Controls and Cookie Solution (Advanced Guide).

Optimization examples

Without TCF

HTML

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <!-- Preload resources beforehand -->
    <link rel="preload" href="//cdn.iubenda.com/cs/iubenda_cs.js" as="script">
    
    <!-- The ID (e.g. 12345) should be the same as cookiePolicyId -->
    <link rel="preload" href="//www.iubenda.com/cookie-solution/confs/js/12345.js" as="script">

    <!-- other stuff in head tag -->
</head>

<body>
    <script>
        var _iub = _iub || [];
        _iub.csConfiguration = {
            "lang": "en",
            "siteId": 67890, //use your siteId
            "cookiePolicyId": 12345, //use your cookiePolicyId
            "enableTcf": true,
            "inlineDelay": 300, //default value: 500
            // ...
        };
    </script>
    
    <!-- inline activator - safe.js (optional) -->
    <script type="text/javascript">
        //<![CDATA[
            //copy content from cdn.iubenda.com/cs/safe.js (current channel) and paste here
        //]]>
    </script>

    <script type="text/javascript" src="//cdn.iubenda.com/cs/iubenda_cs.js" charset="UTF-8" async></script>
</body>

</html>

PHP

<?php
  $cookie_policy_id = 12345; //use your cookiePolicyId
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <!-- Preload resources beforehand -->
    <link rel="preload" href="//cdn.iubenda.com/cs/iubenda_cs.js" as="script">
    <link rel="preload" href="//www.iubenda.com/cookie-solution/confs/js/<?php echo $cookie_policy_id; ?>.js" as="script">

    <!-- other stuff in head tag -->
</head>

<body>

    <script>
        var _iub = _iub || [];
        _iub.csConfiguration = {
            "lang": "en",
            "siteId": 67890, //use your siteId
            "cookiePolicyId": <?php echo $cookie_policy_id; ?>, 
            "inlineDelay": 300, //default value: 500
            // ...
        };
    </script>

    <!-- Only include safe.js if the consent cookie is found -->
    <?php if (isset($_COOKIE["_iub_cs-{$cookie_policy_id}"])) { ?>
    <script>
        /* inline safe.js */
    </script>
    <?php } ?>
    
    <script type="text/javascript" src="//cdn.iubenda.com/cs/iubenda_cs.js" charset="UTF-8" async></script>
</body>

</html>

With TCF enabled

HTML – TCF and inline activator

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <!-- Preload resources beforehand -->
    <link rel="preload" href="//cdn.iubenda.com/cs/tcf/stub-v2.js" as="script">
    <link rel="preload" href="//cdn.iubenda.com/cs/tcf/safe-tcf-v2.js" as="script">
    <link rel="preload" href="//cdn.iubenda.com/cs/iubenda_cs.js" as="script">
    
    <!-- The ID (e.g. 12345) should be the same as cookiePolicyId -->
    <link rel="preload" href="//www.iubenda.com/cookie-solution/confs/js/12345.js" as="script">

    <!-- other stuff in head tag -->
</head>

<body>
    <script>
        var _iub = _iub || [];
        _iub.csConfiguration = {
            "lang": "en",
            "siteId": 67890, //use your siteId
            "cookiePolicyId": 12345, //use your cookiePolicyId
            "enableTcf": true,
            "inlineDelay": 300, //default value: 500
            // ...
        };
    </script>
    <script type="text/javascript" src="//cdn.iubenda.com/cs/tcf/stub-v2.js"></script>
    <script type="text/javascript" src="//cdn.iubenda.com/cs/tcf/safe-tcf-v2.js"></script>
    
    <!-- inline activator - safe.js -->
    <script type="text/javascript">
        //<![CDATA[
            //copy content from cdn.iubenda.com/cs/safe.js (current channel) and paste here
        //]]>
    </script>

    <script type="text/javascript" src="//cdn.iubenda.com/cs/iubenda_cs.js" charset="UTF-8" async></script>
</body>

</html>

PHP – TCF and inline activator

<?php
  $cookie_policy_id = 12345; //use your cookiePolicyId
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <!-- Preload resources beforehand -->
    <link rel="preload" href="//cdn.iubenda.com/cs/tcf/stub-v2.js" as="script">
    
    <!-- Only preload safe-tcf-v2.js if the euconsent-v2 cookie is found -->
    <?php if (isset($_COOKIE['euconsent-v2'])) { ?>
    <link rel="preload" href="//cdn.iubenda.com/cs/tcf/safe-tcf-v2.js" as="script">
    <?php } ?>
    
    <link rel="preload" href="//cdn.iubenda.com/cs/iubenda_cs.js" as="script">
    <link rel="preload" href="//www.iubenda.com/cookie-solution/confs/js/<?php echo $cookie_policy_id; ?>.js" as="script">

    <!-- other stuff in head tag -->
</head>

<body>

    <script>
        var _iub = _iub || [];
        _iub.csConfiguration = {
            "lang": "en",
            "siteId": 67890, //use your siteId
            "cookiePolicyId": <?php echo $cookie_policy_id; ?>, 
            "enableTcf": true,
            "inlineDelay": 300, //default value: 500
            // ...
        };
    </script>

    <script type="text/javascript" src="//cdn.iubenda.com/cs/tcf/stub-v2.js"></script>
    
    <!-- Only include safe-tcf-v2.js if the TCF cookie euconsent-v2 is found -->
    <?php if (isset($_COOKIE['euconsent-v2'])) { ?>
    <script type="text/javascript" src="//cdn.iubenda.com/cs/tcf/safe-tcf-v2.js"></script>
    <?php } ?>
    
    <!-- Only include safe.js if the consent cookie is found -->
    <?php if (isset($_COOKIE["_iub_cs-{$cookie_policy_id}"])) { ?>
    <script>
        /* inline safe.js */
    </script>
    <?php } ?>
    
    <script type="text/javascript" src="//cdn.iubenda.com/cs/iubenda_cs.js" charset="UTF-8" async></script>
</body>

</html>

See also