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
  • 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
    }
  }
});
  • 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)

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

### Delay the ads loading until the TCF consent is given or has given

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

Example:

```javascript
__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