Leading Consent Management Platform

Compliant with GDPR, CCPA, COPPA, LGPD, PECR, PDPA, PIPEDA, and more.

JavaScript API

If you installed UniConsent with the single tag (https://cmp.uniconsent.com/v2/cmp.js), two functions are available as soon as the tag runs:

  • unicReady(callback) fires once, when the consent state is final.
  • unicUpdate(callback) fires at the same time as unicReady, and again after every consent change.
unicReady(function (status) {
  if (status.UNIC_P_1) {
    loadAnalytics();
  }
});

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

See the full reference, including the status object, the DOM events and the async queue guard, at Consent Ready API.

UniConsent CMP v2 API

  • openunic

The openunic API is used to display the UniConsent Consent Management Platform (CMP) UI to users. This function should be called when you need to present the CMP interface to the user, allowing them to review and modify their consent preferences.

__unicapi('openunic')
  • agreeAll

The agreeAll API is used to indicate that the user has agreed to all consent options. This function should be triggered programmatically in your Consent Management Platform (CMP) custom UI once the user clicks the "Agree All" button in your first layer user interface (UI). This action signals that the user consents to all categories of data processing.

__unicapi('agreeAll')
  • rejectAll

The rejectAll API is used to indicate that the user has rejected all consent options. This function should be triggered programmatically in your Consent Management Platform (CMP) custom UI once the user clicks the "Reject All" button in your first layer user interface (UI). This action signals that the user does not consent to any categories of data processing.

__unicapi('rejectAll')
  • openOptions

The openOptions API is used to open the second layer user interface (UI) of the UniConsent Consent Management Platform (CMP). This second layer provides users with more detailed consent options and settings.

__unicapi('openOptions')
  • openVendorList

The openVendorList API is used to display the vendor list user interface (UI) within the UniConsent CMP. This UI provides users with detailed information about third-party vendors that may process their data.

__unicapi('openVendorList')
  • openCookiesList

The openCookiesList API is used to display the cookies list user interface (UI) within the UniConsent CMP. This UI provides users with detailed information about cookies used on the website.

__unicapi('openCookiesList')

Example HTML link:

<a class="font-bold underline cursor-pointer" href="javascript:void(0);" onclick="__unicapi('openCookiesList');">Cookies List</a>
  • getUConsent

Get the consent of your custom vendors, as vendor ID to true or false. It returns false when custom vendors are not enabled.

__unicapi('getUConsent', 2, (uConsent, success) => {
  if (success && uConsent && uConsent['1']) {
    // custom vendor 1 is allowed
  }
})
  • getUCData

Get the consent data of your custom vendors, with each vendor's consent in vendor.consents.

__unicapi('getUCData', 2, (ucData, success) => {
  if (success && ucData.vendor) {
    console.log(ucData.vendor.consents)
  }
})
  • getEConsent

On Consent Categories banners, get the categories the visitor allows, as category ID (1 to 4) to true, for example {"1": true, "2": true}. It does not follow IAB TCF choices: for a value that is set in every region, use UNIC_EP_1 to UNIC_EP_4 from unicReady and unicUpdate.

__unicapi('getEConsent', 2, (eConsent, success) => {
  if (success && eConsent && eConsent['2']) {
    // category 2 (Performance / Analytics) is allowed
  }
})
  • getCustomConsent

Get the consent status of your Custom Purposes.

__unicapi('getCustomConsent', 2, (data, success) => {
  if (success) {
    console.log(data);
    // Example: { "custom_analytics": true, "ab_testing": false }

    if (data['custom_analytics']) {
      // custom analytics consent is granted
    }
  }
});
  • setCustomConsent

Programmatically set the consent status of your Custom Purposes. The provided values are merged into the existing consent status and saved with the visitor's other choices.

__unicapi('setCustomConsent', 2, { "custom_analytics": true, "ab_testing": false });
  • getConsentId

Get the auditing consent ID for the user's consent status.

__unicapi('getConsentId', 2, (consentId) => {
  console.log(consentId);
});
  • getTCData (deprecated, use addEventListener)
__tcfapi(
  'getTCData',
  2,
  (tcData, success) => {
    if (success) {
      // do something with tcData
    } else {
      // do something else
    }
  },
  [1, 2, 3]
) // vendors Ids
  • ping
__tcfapi('ping', 2, (pingReturn) => {
  // do something with pingReturn
})
  • addEventListener
const callback = (tcData, success) => {
  if (success && (tcData.eventStatus === 'tcloaded' || tcData.eventStatus === 'useractioncomplete')) {
    // do something with tcData.tcString

    // remove the ourself to not get called more than once
    __tcfapi(
      'removeEventListener',
      2,
      (success) => {
        if (success) {
          // oh good...
        }
      },
      tcData.listenerId
    )
  } else {
    // do something else
  }
}

__tcfapi('addEventListener', 2, callback)
  • getVendorList
__tcfapi(
  'getVendorList',
  2,
  (gvl, success) => {
    if (success) {
      // do something with gvl
    } else {
      // do something else
    }
  },
  'LATEST'
)

You can find all the API at: IAB TCF Document.

U.S. Privacy User Signal Mechanism "USP API" (CCPA Compliance Mechanism)

  • getUSPData

Get the current USP consent data.

__uspapi('getUSPData', 1, (uspData, success) => {
  if (success) {
    // do something with uspData
  } else {
    // do something else
  }
})

Response uspData:

{
 "version": 1, /* number indicating the U.S. Privacy spec version */
 "uspString": "1YNN" /* string; not applicable: "1---" */
}
  • registerDeletion

Register a callback function that will be invoked when a user clicks the "Delete My Data" button in the CCPA/USP consent UI. This allows publishers to handle user data deletion requests as required by the California Consumer Privacy Act (CCPA).

__uspapi('registerDeletion', 1, (param) => {
  // Handle user data deletion request
  // For example: call your backend API to delete user data
  console.log('User requested data deletion');

  // Example: send a deletion request to your server
  fetch('/api/delete-user-data', {
    method: 'POST',
    body: JSON.stringify({ action: 'delete' })
  });
})

When the user clicks "Delete My Data", the following actions occur in sequence:

  1. The registered deletion callback is invoked, allowing the publisher to perform actual data deletion.
  2. The user's USP consent is set to opt-out (1YY), indicating the user does not consent to the sale of personal data.
  3. The USP consent string is saved to the usprivacy cookie.
  4. The consent UI is closed.
  • performDeletion

Programmatically trigger the data deletion flow. This calls the callback previously registered via registerDeletion and can be used to build a custom deletion UI.

__uspapi('performDeletion', 1, null, param)

Wait for the IAB TCF choice before starting ads

tcData.unicLoad is true once the visitor's IAB TCF choice is known: a stored choice at page load, or the choice just made, accept or reject. Your ad stack then reads the choice from the TC string. Where IAB TCF does not apply, it is always true.

__tcfapi('addEventListener', 2, function (tcData, success) {
  if (success && tcData.unicLoad === true) {
    if (!window._initAds) {
      window._initAds = true
      // your code to start the ads
    }
  }
})

Variables in the unic_data event

The unic_data event in the data layer carries these variables, which you can read in Google Tag Manager as Data Layer Variables. It is pushed at page load and again after every consent choice or change. In page code, unicReady and unicUpdate give the same consent values: see Consent Ready API.

VariableMeaning
UNIC_IS_LOADWhere IAB TCF applies, true only when the visitor allows storage or any custom vendor: false before a choice and after a reject. Always true where IAB TCF does not apply
GEORegion whose rules apply, for example EU, CA, USCA or USTX
LOCDetected location
GEO_IS_EU, GEO_IS_US, GEO_IS_USCA, GEO_IS_CA, GEO_IS_BR, GEO_IS_CN, GEO_IS_ZA, GEO_IS_APAC, GEO_IS_LATAM, GEO_IS_OTHERTrue for the visitor's location
US_STATEThe visitor's two-letter US state code, empty outside the US
GEO_IS_MSPATrue in a state covered by US State Privacy
TC_STRINGIAB TCF string
GPP_STRINGIAB GPP string
uspDataUS Privacy string
MSPA_STRINGUS State Privacy string (IAB GPP US National section)
GPCTrue when the browser sends Global Privacy Control
UNIC_P_1 … UNIC_P_10, UNIC_SF_1, UNIC_SF_2IAB TCF purposes and special features
UNIC_EP_1 … UNIC_EP_4Consent categories 1 to 4
UNIC_CP_{id}Your custom purposes, by purpose ID
customVendorsConsent for your custom vendors, by vendor ID
CONSENT_MODEGoogle Consent Mode values

unic_data is pushed to the data layer before unicUpdate runs, so in a unicUpdate callback the latest unic_data event has the current Google Consent Mode values (when Google Consent Mode is on):

unicUpdate(function () {
  var events = (window.dataLayer || []).filter(function (e) { return e && e.event === 'unic_data'; });
  var latest = events[events.length - 1];
  if (latest) {
    console.log(latest.CONSENT_MODE);
  }
});

How to Load Your Webpages with UniConsent CMP in an iFrame

To synchronize cookies with SameSite=None; Partitioned;, which may be required when your page is loaded in an iFrame (such as in Discord), you can add the following code below your CMP tag.

const callback = (tcData, success) => {
  if (success && tcData.eventStatus === "useractioncomplete") {
    __tcfapi(
      'removeEventListener',
      2,
      (success) => {
        if (success) {
            const date = new Date();
            date.setTime(date.getTime() + (300 * 24 * 60 * 60 * 1000));
            const expires = `expires=${date.toUTCString()}`;
            document.cookie = `euconsent-v2=${tcData.tcString}; ${expires}; path=/; Secure; SameSite=None; Partitioned`;
        }
      },
      tcData.listenerId
    )
  }
}
__tcfapi('addEventListener', 2, callback);

Still have questions?

Contact us: support@uniconsent.com

Stay ahead on privacy and consent

Privacy laws, consent and compliance updates, a few emails a month.

No spam, unsubscribe any time. Privacy