JavaScript 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')
  • getUConsent

Get the consent signal of your custom vendors.

__unicapi(
  'getUConsent',
  2,
  (uConsent, success) => {
    if (success) {
      // do something with tcData
    } else {
      // do something else
    }
  },
  [1, 2, 3]
) // vendors Ids
  • getUCData

Get the consent signal data of your custom vendors.

__unicapi(
  'getUCData',
  2,
  (ucData, success) => {
    if (success) {
      // do something with tcData
    } else {
      // do something else
    }
  },
  [1, 2, 3]
) // vendors Ids
  • getConsentId

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

__unicapi('getConsentId', 2, (consentId) => {
  console.log(consentId);
});

// or

UnicI.getConsentId();

The auditing consent ID can also be retrieved from the user's local storage __unic -> __unid.

  • 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') {
    // 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)

Example:

__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": "1YN" /* string; not applicable: "1--" */ /* number; 1 applies, 0 doesn't apply, -1 not set */
}

Make sure your ad stack get the consent from users before kicking off.

Example:

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

Example:

;(function waitCMP() {
  var readyCMP
  if (!readyCMP && window['dataLayer']) {
    window['dataLayer'].forEach(function (event) {
      if (event['event'] === 'unic_data') {
        readyCMP = 1
        console.log(event)
        // your other codes;
      }
    })
  }
  if (!readyCMP) {
    setTimeout(waitCMP, 100)
  }
})()

You can use the following Javascript API to access the Google Consent Mode Data:

(function waitCMP() {
  var readyCMP
  if (!readyCMP && window['dataLayer']) {
    window['dataLayer'].forEach(function (event) {
      if (event['event'] === 'unic_data') {
        readyCMP = 1
        console.log(event['CONSENT_MODE'])
      }
    })
  }
  if (!readyCMP) {
    setTimeout(waitCMP, 100)
  }
})()

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 bellow 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