UniConsent Tag for Web

Quick setup with the single UniConsent tag (recommended)

Add one script tag with your license key on every page, as high in the <head> as possible. The tag sets the Google Consent Mode default status, defines unicReady and unicUpdate immediately, and then loads the full CMP in the background.

<script src="https://cmp.uniconsent.com/v2/cmp.js" data-license="YOUR_LICENSE"></script>

You can find your license key at the Install & Setup section.

  • The single tag replaces the stub tag, the Consent Mode default status tag and the cmp.js loader described below. Do not add them again.
  • It sets a Consent Mode default of all storage denied with a 3 second wait_for_update. If you set your own Consent Mode defaults, place them after the tag or they will be overridden.
  • The tag ignores itself if the CMP script is already on the page.
  • Use unicReady and unicUpdate to run your code once consent is final. See the Consent Ready API.

Already installed UniConsent with the classic two-part tag? It keeps working. See the Classic tag setup section at the bottom of this page.

You can turn off Show Privacy Settings Button and add the following link on your page.

<a href="#" onclick='__unicapi("openunic");return false;'>Consent Settings</a>

You can target a DIV on your web pages with CSS selector, UniConsent automatically insert the Consent Settings Link within the DIV based on GEO location and your enabled features.

<div id="uniconsent-config"></div>

You can put the following place holder DIV at footer of your web pages, UniConsent automatically insert the Consent Settings Link within the place holder based on GEO location and your enabled features.

<div id="uniconsent-config"></div>

Install with WordPress and Drupal plugin:

You can find the license key at the Install & Setup section.

For Wordpress WP Rocket users

Avoid lazyload CMP tags:

You must add data-nowprocket to all the scripts:

<script data-nowprocket data-cfasync="false" src="https://cmp.uniconsent.com/v2/cmp.js" data-license="YOUR_LICENSE"></script>

You also have to exclude inline JavaScript from combining:

  1. Log in to your WordPress Admin dashboard > Settings > WP Rocket.
  2. Click the File Optimization tab, and scroll down to the JavaScript Files section.
  3. Minify JavaScript files > Exclude JavaScript Files: Enter cmp.uniconsent.com.

If you require WP Consent API, add the following tag on pages:

<script data-nowprocket data-cfasync="false" async src="http://cmp.uniconsent.com/v2/wp-consent-api.min.js"></script>

Find more about How to enable WP Consent API.

For Cloudflare Rocket Loader users

You must add data-cfasync="false" to the scripts:

<script data-nowprocket data-cfasync="false" src="https://cmp.uniconsent.com/v2/cmp.js" data-license="YOUR_LICENSE"></script>

Install via Google Tag Manager

You can find instructions about how to install UniConsent via Google Tag Manager at https://www.uniconsent.com/docs/tutorials/gtm-integration.

Performance tip: preconnect to the CMP domain

Add the following tags at the top of your pages, in the <head> before the UniConsent tag. They let the browser resolve DNS and open the connection to cmp.uniconsent.com early, so the CMP script starts downloading sooner and the consent banner appears faster.

<link rel='dns-prefetch' href='//cmp.uniconsent.com' />
<link rel="preconnect" href="https://cmp.uniconsent.com" crossorigin>

If you serve the CMP from a first-party CMP domain, use your custom domain instead of cmp.uniconsent.com.

Why the tag is synchronous by default

The single tag is loaded without async on purpose. It runs before anything else on the page, so the Google Consent Mode default status, the IAB TCF, USP and GPP stubs, and the unicReady and unicUpdate functions are all in place before your Google tags, ad tags or your own code run. The tag itself is small and cached, and the full CMP is loaded in the background, so the impact on page load is minimal. Combined with the preconnect tags above, this gives the fastest banner display with no risk of tags firing before the consent defaults are set.

If you prefer to load the tag with async

You can add async to the tag. Keep the following in mind:

  • Your inline code may run before the tag has been executed, so unicReady and unicUpdate may not exist yet. Add the two-line queue guard shown below so early calls are replayed once the tag loads.
  • The Consent Mode default status may be set after your Google tags have loaded. Either set the Consent Mode default status yourself before your Google tags, or load your Google tags from a unicReady callback.
  • The same applies to the IAB TCF stub. With async, __tcfapi may not exist yet when your ad tags or Prebid call it. Either add the stub tag before your ad tags, or load them from a unicReady callback.

Integrating with the API when loading async

<script async src="https://cmp.uniconsent.com/v2/cmp.js" data-license="YOUR_LICENSE"></script>
<script>
  window.unicReady  = window.unicReady  || function (cb) { (window.unicReady.q  = window.unicReady.q  || []).push(cb); };
  window.unicUpdate = window.unicUpdate || function (cb) { (window.unicUpdate.q = window.unicUpdate.q || []).push(cb); };

  unicReady(function (status) {
    // consent is final: load your tags here
  });

  unicUpdate(function (status) {
    // every consent change
  });
</script>

You can also listen to the unic:ready and unic:update DOM events on window, registered before the tag. See the Consent Ready API for the status object and full details.

Next

If you like to integrate your third-party JavaScript tags, please go to the Third-party JavaScript Tags Integration section.

Verify the Setup

You can monitor the developer console for warning messages or use the UniConsent Consent Validator to ensure that Consent Mode, IAB TCF, and IAB GPP data are available and correctly passing on your webpages.

Regularly check the implementation audit status on your dashboard.

Classic tag setup (existing installations)

This section applies to websites that installed UniConsent before the single tag was available. The classic two-part tag keeps working and there is no need to migrate. To switch, remove all of the tags below and add the single tag from the top of this page.

Each of your websites has a unique javascript tag to be installed on every page.

You can find the UniConsent tag at the Install & Setup section.

There are two parts of the tags. The first part is a stub that should be embedded at the top of your page. The second part can be loaded with async. There is a minimum performance impact on your site.

Stub codes

You can also choose to load the stub code with the following javascript tag:

<script src="https://cmp.uniconsent.com/v2/stub.min.js"></script>

You can also replace the following code with the above stub tag to improve page speed:

<script type="text/javascript">
!function(){var e,t,a;e="__tcfapiLocator",t=[],(a=window.frames[e])||(function t(){var n=window.document,i=!!a;if(!i)if(n.body){var o=n.createElement("iframe");o.style.cssText="display:none",o.name=e,n.body.appendChild(o)}else setTimeout(t,50);return!i}(),window.__tcfapi=function(){for(var e,a=[],n=0;n<arguments.length;n++)a[n]=arguments[n];if(!a.length)return t;if("setGdprApplies"===a[0])a.length>3&&2===parseInt(a[1],10)&&"boolean"==typeof a[3]&&(e=a[3],"function"==typeof a[2]&&a[2]("set",!0));else if("ping"===a[0]){var i={gdprApplies:e,cmpLoaded:!1,cmpStatus:"stub"};"function"==typeof a[2]&&a[2](i,!0)}else t.push(a)},window.addEventListener("message",(function(e){var t="string"==typeof e.data,a={};try{a=t?JSON.parse(e.data):e.data}catch(e){}var n=a.__tcfapiCall;if(n)try{window.__tcfapi(n.command,n.version,(function(a,i){var o={__tcfapiReturn:{returnValue:a,success:i,callId:n.callId}};t&&(o=JSON.stringify(o)),e.source&&"function"==typeof e.source.postMessage&&e.source.postMessage(o,"*")}),n.parameter)}catch(e){}}),!1))}();
!function(){var e,a,t;e="__uspapiLocator",a=[],(t=window.frames[e])||(function a(){var n=window.document,s=!!t;if(!t)if(n.body){var i=n.createElement("iframe");i.style.cssText="display:none",i.name=e,n.body.appendChild(i)}else setTimeout(a,50);return!s}(),window.__uspapi=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];if(!e.length)return a;"ping"===e[0]?"function"==typeof e[2]&&e[2]({cmpLoaded:!1,cmpStatus:"stub"},!0):a.push(e)},window.addEventListener("message",(function(e){var a="string"==typeof e.data,t={};try{t=a?JSON.parse(e.data):e.data}catch(e){}var n=t&&t.__uspapiCall;if(n)try{window.__uspapi(n.command,n.version,(function(t,s){var i={__uspapiReturn:{returnValue:t,success:s,callId:n.callId}};a&&(i=JSON.stringify(i)),e.source&&"function"==typeof e.source.postMessage&&e.source.postMessage(i,"*")}),n.parameter)}catch(e){}}),!1))}();
window.__gpp_addFrame=function(e){if(!window.frames[e])if(document.body){var t=document.createElement("iframe");t.style.cssText="display:none",t.name=e,document.body.appendChild(t)}else window.setTimeout(window.__gpp_addFrame,50,e)},window.__gpp_stub=function(){var e=arguments;if(__gpp.queue=__gpp.queue||[],__gpp.events=__gpp.events||[],!e.length||1==e.length&&"queue"==e[0])return __gpp.queue;if(1==e.length&&"events"==e[0])return __gpp.events;var t=e[0],p=e.length>1?e[1]:null,s=e.length>2?e[2]:null;if("ping"===t)p&&p({gppVersion:"1.1",cmpStatus:"stub",cmpDisplayStatus:"hidden",signalStatus:"not ready",supportedAPIs:["2:tcfeuv2","5:tcfcav1","6:uspv1","7:usnatv1","8:uscav1","9:usvav1","10:uscov1","11:usutv1","12:usctv1"],cmpId:0,sectionList:[],applicableSections:[-1],gppString:"",parsedSections:{}},!0);else if("addEventListener"===t){"lastId"in __gpp||(__gpp.lastId=0),__gpp.lastId++;var n=__gpp.lastId;__gpp.events.push({id:n,callback:p,parameter:s}),p({eventName:"listenerRegistered",listenerId:n,data:!0,pingData:{gppVersion:"1.1",cmpStatus:"stub",cmpDisplayStatus:"hidden",signalStatus:"not ready",supportedAPIs:["2:tcfeuv2","5:tcfcav1","6:uspv1","7:usnatv1","8:uscav1","9:usvav1","10:uscov1","11:usutv1","12:usctv1"],cmpId:0,sectionList:[],applicableSections:[-1],gppString:"",parsedSections:{}}},!0)}else if("removeEventListener"===t){for(var a=!1,i=0;i<__gpp.events.length;i++)if(__gpp.events[i].id==s){__gpp.events.splice(i,1),a=!0;break}p({eventName:"listenerRemoved",listenerId:s,data:a,pingData:{gppVersion:"1.1",cmpStatus:"stub",cmpDisplayStatus:"hidden",signalStatus:"not ready",supportedAPIs:["2:tcfeuv2","5:tcfcav1","6:uspv1","7:usnatv1","8:uscav1","9:usvav1","10:uscov1","11:usutv1","12:usctv1"],cmpId:0,sectionList:[],applicableSections:[-1],gppString:"",parsedSections:{}}},!0)}else"hasSection"===t?p(!1,!0):"getSection"===t||"getField"===t?p(null,!0):__gpp.queue.push([].slice.apply(e))},window.__gpp_msghandler=function(e){var t="string"==typeof e.data;try{var p=t?JSON.parse(e.data):e.data}catch(e){p=null}if("object"==typeof p&&null!==p&&"__gppCall"in p){var s=p.__gppCall;try{window.__gpp(s.command,(function(p,n){var a={__gppReturn:{returnValue:p,success:n,callId:s.callId}};e.source&&"function"==typeof e.source.postMessage&&e.source.postMessage(t?JSON.stringify(a):a,"*")}),"parameter"in s?s.parameter:null,"version"in s?s.version:"1.1")}catch(e){}}},"__gpp"in window&&"function"==typeof window.__gpp||(window.__gpp=window.__gpp_stub,window.addEventListener("message",window.__gpp_msghandler,!1),window.__gpp_addFrame("__gppLocator"));
</script>

The above stub code or tag support the following API: IAB TCF API, IAB GPP API, IAB USP API.

You can add this at the top of your pages if you are not using Google Tag Manager but like to enable Consent Mode V2:

<script src="https://cmp.uniconsent.com/v2/stubgcm.min.js"></script>

or add the following code at the top of your pages:

<script>
window.gtag||(window.dataLayer=window.dataLayer||[],window.gtag=function(){window.dataLayer.push(arguments)}),window.gtag("set","developer_id.dZTcxZD",!0),window.gtag("consent","default",{ad_storage:"denied",functionality_storage:"denied",personalization_storage:"denied",analytics_storage:"denied",ad_user_data:"denied",ad_personalization:"denied",security_storage:"granted",wait_for_update:3e3}),window.gtag("set","ads_data_redaction",!0),window.gtag("set","url_passthrough",!1);
</script>

Find more about Google Consent Mode setup at https://www.uniconsent.com/docs/tutorials/google-consent-mode

WP Rocket and Cloudflare Rocket Loader users with the classic tag

Add data-nowprocket and data-cfasync="false" to both scripts. The WP Rocket exclusion settings above also apply.

<script data-nowprocket data-cfasync="false" src="https://cmp.uniconsent.com/v2/stub.min.js"></script>
<script data-nowprocket data-cfasync="false" async src='https://cmp.uniconsent.com/v2/YOUR_ID/cmp.js'></script>
  1. Google Consent Mode Enabled, but Default Status Stub Code is Missing

    Issue: You might encounter an error message in the Validator or developer console:

    Error: Consent Mode Default Status is not added.

    Solution: Add the Default Status stub code at the top of your pages to resolve this issue. You can follow the instructions here: Consent Mode Default Status Tag Installation.

  2. Google Consent Mode Enabled, but Default Status Stub Code is Added After Google Tags

    Issue: If the Default Status stub code is added after your Google tags, you may see the following error message in the Validator or developer console:

    Error: Your Google tags are loaded before Consent Mode Default Status and CMP.

    Solution: Ensure the Default Status stub code is added at the very top of your pages, before any Google tags. Follow the guide here: Consent Mode Default Status Tag Installation.

  3. Google Consent Mode Enabled via Google Tag Manager, but Not All Google Tags Are Managed in Google Tag Manager

    Issue: When some Google tags are not managed in Google Tag Manager, you might see errors similar to those described above.

    Solution: Either add the Default Status stub code at the top of your pages or ensure all your Google tags are managed within Google Tag Manager. Refer to the guide: Consent Mode Default Status Tag Installation.

  4. IAB TCF Stub Code is Missing

    Issue: The IAB TCF stub code is not included, leading to potential errors.

    Solution: Add the IAB TCF stub code at the top of your pages. Follow the instructions here: Stub Codes Installation.

  5. IAB GPP Stub Code is Missing

    Issue: The IAB GPP stub code is missing from your implementation.

    Solution: Include the IAB GPP stub code at the top of your pages. You can find the relevant guide here: Stub Codes Installation.

  6. USP API Stub Code is Missing

    Issue: The IAB USP stub code is not present.

    Solution: To resolve this, add the IAB USP stub code at the top of your pages. Detailed instructions are available here: Stub Codes Installation.