Consent Ready API

Add the UniConsent tag once, with your license key. The tag defines two functions 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. See Install UniConsent Tags for Web for the full installation guide, including the classic two-part tag for existing installations.

unicReady(callback)

Fires once, when the consent state is final.

  • GDPR: after the user makes a choice, or a stored choice is restored.
  • CCPA / US state laws / other opt-out regions: as soon as the CMP loads.

Subscribing after it has already fired calls back immediately.

unicReady(function (status) {
  if (status.UNIC_P_1) {
    loadAnalytics();
  }
});

unicUpdate(callback)

Fires at the same time as unicReady, and again after every consent change (for example when the user reopens the settings and changes a purpose). Subscribing late calls back immediately with the current state, then on every later change. Saves that do not change anything are ignored.

unicUpdate(function (status) {
  applyConsent(status);
});

DOM events

The same data is dispatched on window as unic:ready and unic:update. Register listeners before the tag so they exist when the events fire.

window.addEventListener('unic:update', function (e) {
  applyConsent(e.detail);
});

Loading synchronously

Place your code anywhere after the tag. Nothing else is required.

<script src="https://cmp.uniconsent.com/v2/cmp.js" data-license="YOUR_LICENSE"></script>
<script>
  unicReady(function (status) { /* consent is final */ });
  unicUpdate(function (status) { /* every change */ });
</script>

Loading asynchronously

With async, your inline code may run before the tag. Add the two-line queue guard so calls made early are replayed once the tag loads.

<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 */ });
  unicUpdate(function (status) { /* every change */ });
</script>

Status object

FieldTypeMeaning
IS_LOADbooleanConsent is final and tags may load
LOCstringDetected user location
GEOstringRegion whose rules apply, N when none
EU, US, CA, BR, CN, NbooleanWhich region flag applies
NPA0 or 11 when only non-personalized ads should be served
customVendorsobjectCustom vendor id to boolean consent
UNIC_P_1UNIC_P_10booleanTCF purposes 1 to 10 (GDPR only)
UNIC_SF_1, UNIC_SF_2booleanTCF special features (GDPR only)
UNIC_EP_1UNIC_EP_4booleanExtended purposes (opt-out regions only)

Notes

  • unicReady and unicUpdate are reserved names on window. Do not define functions with these names for other purposes.
  • Callbacks are wrapped in try/catch. An error in one does not stop the others.
  • Both functions accept any number of subscribers.
  • The tag ignores itself if the CMP script is already on the page.
  • The tag sets a Google Consent Mode default of all storage denied with a 3 second wait_for_update. If you already set your own Consent Mode defaults, place them after the tag or they will be overridden.

Still have questions?

Contact us: support@uniconsent.com