Client-side integration guide for JavaScript
This guide is for all participants who want to integrate with UID2 and generate UID2 tokens (advertising tokens) using only JavaScript client-side changes on their website with minimum effort.
This approach is used by the following participant types:
- Publishers: Most notably, this workflow is for publishers wanting to send UID2 tokens into the bidstream.
- Advertisers and Data Providers: In addition, advertisers and data providers would use this for adding a UID2 token to their tracking pixels (see Tokenized sharing in pixels).
This guide does not apply to publishers who want to use a Private Operator, or who want to generate tokens server-side. Those publishers should follow the Client-server integration guide for JavaScript.
UID2 provides an SDK for JavaScript (see SDK for JavaScript reference guide) with the following features:
- UID2 token generation
- Automatic refreshing of UID2 tokens
- Automatic storage of UID2 tokens in the browser
To implement, you'll need to complete the following steps:
- Complete UID2 account setup and configure account
- Add SDK for JavaScript to your site
- Configure the SDK for JavaScript
- Check that the token was successfully generated
SDK for JavaScript version
Support for client-side token generation is available in version 3.4.5 and above of the SDK.
The URL for the SDK is:
In the following code examples, the placeholder {{ UID2_JS_SDK_URL }} refers to this URL.
If you want to use a debug build of the SDK, use the following URL instead:
Integrating with single sign-on (SSO)
If you integrate with one or more SSO providers to offer SSO login, you might be able to retrieve the logged-in user's email address from the SSO provider to generate UID2 tokens.
For details, see Publisher integration with SSO providers.
Inspecting with UID Verify Chrome extension
You can use the UID Verify Chrome extension to inspect and debug your UID2 web integration directly in Chrome. UID Verify supports the SDK for JavaScript, Prebid.js, and Google Secure Signals.
Preparing DII for processing
It's critical that the input data, which you are converting to UID2, is in an acceptable format. If it isn't, you won't get the expected results. For example, you must normalize phone numbers to include the country code, as explained in Phone number normalization.
For details, see Preparing emails and phone numbers for processing.
To validate the full token generation pipeline end to end, confirming that tokens generated from your normalized, hashed, and encoded values are correct, use the UID2 Token Validator.
Complete UID2 account setup and configure account
To integrate with UID2, you'll need to have a UID2 account. If you haven't yet created an account, first follow the steps described on the Account setup page.
When initial account setup is complete, you'll receive instructions and a link to access the UID2 Portal, where you can create your credentials for the production environment and configure additional values that you'll need to provide. For details, see Getting started with the UID2 Portal.
For a client-side integration you'll need to set up these values, in the UID2 Portal on the Client-side integration page:
-
Subscription ID and Public Key: See Adding and managing key pairs
-
A list of domain names for the sites on which you'll be using this SDK: See Adding and managing root-level domains
-
Mobile App IDs (any that apply): See Adding and managing mobile app IDs
Only root-level domains are required for account setup. For example, if you're going to use SDK for JavaScript on example.com, shop.example.com, and example.org, you only need to provide the domain names example.com and example.org.
Add SDK for JavaScript to your site
The following code snippet provides an overview of the code you will need to add to your website. It also illustrates the different events that the SDK can trigger.
For a more detailed code snippet, see Example integration code and when to pass DII to the UID2 SDK.
For the UID2_JS_SDK_URL value, see SDK for JavaScript version.
<script async src="{{ UID2_JS_SDK_URL }}"></script>
<script>
// When the UID2 SDK is executed, it looks for these callbacks and invokes them.
window.__uid2 = window.__uid2 || {};
window.__uid2.callbacks = window.__uid2.callbacks || [];
window.__uid2.callbacks.push((eventType, payload) => {
switch (eventType) {
case "SdkLoaded":
// The SdkLoaded event occurs just once.
__uid2.init({});
break;
case "InitCompleted":
// The InitCompleted event occurs just once.
//
// If there is a valid UID2 token, it is in payload.identity.
break;
case "IdentityUpdated":
// The IdentityUpdated event happens when a UID2 token is generated or refreshed.
// payload.identity contains the resulting latest identity.
break;
}
});
</script>
For more information about the SDK, see SDK for JavaScript reference guide.
Using the UID2 integration environment
By default, the SDK is configured to work with the UID2 production environment https://prod.uidapi.com. If you want to use the UID2 integration environment instead (for credentials, see Getting your credentials), provide the following URL in your call to init:
__uid2.init({
baseUrl: "https://operator-integ.uidapi.com",
});
Tokens from the UID2 integration environment are not valid for passing to the bidstream. For the integration environment, you will have different Subscription ID and public key values. For information about getting credentials for each environment, see Getting your credentials.
Optional: Specifying the API base URL to reduce latency
By default, this SDK makes calls to a UID2 production environment server in the USA.
For information about how to choose the best URL for your use case, and a full list of valid base URLs, see Environments.
To specify a UID2 server that is not the default, you can change it in the init call:
__uid2.init({
baseUrl: "https://global.prod.uidapi.com",
});
Configure the SDK for JavaScript
UID2 provides the publisher with the following values required to use the client-side token generation feature:
- A Subscription ID
- A public key
You'll have one set of these values for your publisher integration environment, and a separate set for your production environment.
To configure the SDK, call one of the following methods, with an object containing the public key and Subscription ID that you received during account setup, as well as the user's hashed or unhashed DII (email address or phone number):
__uid2.setIdentityFromEmail__uid2.setIdentityFromEmailHash__uid2.setIdentityFromPhone__uid2.setIdentityFromPhoneHash
For __uid2.setIdentityFromEmailHash or __uid2.setIdentityFromPhoneHash, the emailHash or PhoneHash argument must be the Base64-encoded value. For details, see Email address hashing and encoding and Phone number hashing and encoding.
The following sections include coding examples for each scenario.
Once it's configured, the UID2 SDK takes care of the following:
- Generates a UID2 token for the user.
- Stores the token in the user's browser.
- Automatically refreshes the token as required while your site is open in the user's browser.
You can pass the user's DII to the UID2 SDK either hashed or unhashed. If you pass the DII unhashed, the UID2 SDK hashes it for you. If you want to pass the DII to the SDK already hashed, you must normalize it before hashing. For details, see Normalization and encoding.