Skip to main content

UID2 Client-Server Integration Guide for Mobile

This guide is for mobile app publishers who want to integrate with UID2 by generating UID2 tokens server-side via a Public Operator or Private Operator and then passing the tokens and user identities into their mobile apps, which will in turn pass the tokens for bidstream use.  

This is called client-server integration because some integration steps are client-side and some are server-side.

If you want to integrate with UID2 via client-side only changes (that is, all integration changes required are within the mobile apps), refer to the UID2 Client-Side Integration Guide for Mobile instead.

This page provides a high-level overview of integration steps and links to additional documentation.

UID2 provides mobile SDKs for Android and iOS. Each SDK has the following features:

  • Takes in a UID2 identity (a UID2 token and associated values) and persists it in local file storage.
  • Automatically refreshes UID2 tokens.
note

This guide uses the group term UID2 mobile SDKs to include both the UID2 SDK for Android and the UID2 SDK for iOS.

You'll need to complete the following steps:

  1. Complete the UID2 account setup.
  2. Implement server-side token generation.
  3. Add the UID2 mobile SDK to your mobile app.
  4. Configure the UID2 mobile SDK.
  5. Check that the token was successfully generated and then pass it for bidstream use.
  6. Optionally, integrate the UID2 GMA/IMA Plugin for GAM Secure Signal integration.

Mobile SDK Version

This guide provides instructions for using version 1.2.0 or higher of either of these UID2 mobile SDKs:

  • UID2 SDK for Android
  • UID2 SDK for iOS

For instructions for installing the correct SDK/version into your mobile app, see Add the UID2 Mobile SDK to Your Mobile App.

Complete the UID2 Account Setup

To set up your account, follow the steps described in Account Setup.

When account setup is complete, you'll receive your unique API key and client secret. These values are unique to you, and it's important to keep them secure. For details, see API Key and Client Secret.

Client-Server Mobile Integration Data Flow Overview

The following diagram shows the data flow that the publisher must implement for UID2 client-server mobile integration.

This example uses the UID2 SDK for Android in the client-side mobile app and the UID2 SDK for Java on the server side.

Mobile Client-Server Integration Example

Implement Server-Side Token Generation

The first step of UID2 integration is to be able to generate the UID2 token on your server. Then, you can pass the token into your mobile apps for sending to the RTB bidstream.

There are two approaches to generating UID2 tokens on the server side by providing directly identifying information (DII) (email address or phone number):

  • Integration with an SDK
  • Direct integration to API endpoints

Options are summarized in the following table.

Integration Solution Generate TokenRefresh Token
UID2 SDK for Java✅ 
UID2 SDK for Python
Direct integration (API endpoints with custom code)

Whatever integration option you choose to generate the identity (UID2 token and associated values), you'll need to implement one of the following:

  • Call the POST /token/generate endpoint.

    The identity output that you need for the rest of this guide is the content inside the body section of a successful endpoint response. For an example, see Successful Response.

  • Use one of the Publisher Client classes, in one of the UID2 server-side SDKs. These classes simplify the request into a single method call.

    For instructions, see UID2 SDK for Java, Publisher Basic Usage or UID2 SDK for Python, Usage for Publishers.

    If you're using an SDK option, the Identity response that you need for the rest of this guide is the output of the applicable method, as follows:

    tokenGenerateResponse.getIdentityJsonString()
important

The endpoint and SDK API returns opt-out status if the DII you are generating the token for has been opted out of UID2. If this happens, save the information and do not call the token generation endpoint for the same DII again. 

You will need to pass the Identity response into the mobile app: see Configure the UID2 Mobile SDK.

warning

For security reasons, the API key and secret used in token generation must be called server-side. Do not store these values inside a mobile app. For details, see Security of API Key and Client Secret.

Server-Side Token Refresh

Token refresh is automatically enabled inside the UID2 mobile SDKs; you don't need to manage it explicitly on the server side.

You might decide to do server-side token refresh if you want to keep your changes in the mobile apps as simple as possible.

If you want to manage token refresh on the server side and not the client/mobile side, you can do so using one of the following:

Then, pass the newly refreshed Identity value to the mobile app by following the rest of this guide.

Add the UID2 Mobile SDK to Your Mobile App

For installation instructions, refer to one of the following:

At this point, you are ready to use the UID2 Identity generated server-side in the mobile SDK.

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, provide the following URL in your call to UID2Manager initialization:

UID2Manager.init(
context = this,
serverUrl = "https://operator-integ.uidapi.com"
)
note

Bear in mind the following differences between environments:

  • Tokens from the UID2 integration environment are not valid for passing to the bidstream.
  • You'll have a different set of API key and client secret values for each environment (integration and production). Be sure to use the correct values for each environment.

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:

UID2Manager.init(
context = this,
serverUrl = "https://global.prod.uidapi.com"
)

Configure the UID2 Mobile SDK

After you've instantiated UID2Manager correctly in your mobile app, you'll need to pass a UID2 identity generated server-side (see Implement server-side token generation), and then pass it into the mobile app using the setIdentity method, as shown in the following:

UID2Manager.getInstance().setIdentity()

Token Storage

After you call the setIdentity method, the UID2 identity is persisted in local file storage.

warning

The format of the file stored in the local file storage, or the filename itself, could change without notice. We recommend that you do not read or update the file directly.

Pass Generated Token for Bidstream Use

To retrieve the token, in your mobile app, call the following:

UID2Manager.getInstance().getAdvertisingToken()

If a successful identity was added into the UID2Manager, this method returns a string such as the following:

AgAAAQFt3aNLXKXEyWS8Tpezcymk1Acv3n+ClOHLdAgqR0kt0Y+pQWSOVaW0tsKZI4FOv9K/rZH9+c4lpm2DBpmFJqjdF6FAaAzva5vxDIX/67UOspsYtiwxH73zU7Fj8PhVf1JcpsxUHRHzuk3vHF+ODrM13A8NAVlO1p0Wkb+cccIIhQ==

You can use this identity to pass downstream for sending in the RTB bidstream.

If the getAdvertisingToken() method call returns null, there was no identity or valid token generated. Some possible reasons for this, and some things you could do to troubleshoot, are as follows:

  • The identity is invalid. In this scenario there are a couple of options:
    • Check to see whether there are any errors from the previous setIdentity() call.
    • Check the status of the identity, using UID2Manager.getInstance().getIdentityStatus() for Android or UID2Manager.shared.identityStatus for iOS.
  • You could enable logging to get more information: see Enable Logging.
  • The advertising token inside the UID2 identity has expired, and the refresh token has also expired, so the SDK cannot refresh the token.

If there is no identity, follow the instructions in Implement Server-Side Token Generation again, generate a new identity, and pass the result into your mobile app's UID2Manager again.

When to Pass a new UID2 Identity/Token into the SDK

The best way to determine whether a new UID2 identity is required by the UID2 SDK again is to call the getAdvertisingToken() method in all cases:

UID2Manager.getInstance().getAdvertisingToken()

On startup/resumption of the app, if getAdvertisingToken() returns null, it is time to generate new identity on the server by following the instructions in Implement Server-Side Token Generation. Then, pass the result into the mobile app’s UID2Manager again: see Configure the UID2 mobile SDK.

Enable Logging

The UID2 SDK can generate logs, which could help in debugging any issues during UID2 integration work. To enable logging, do the following:

// During UID2Manager initialization:
UID2Manager.init(
context = this,
isLoggingEnabled = true
)

Enable Automatic Token Refresh in Mobile App/Client Side

By default, after a valid UID2 identity has been passed into the UID2Manager, it performs automatic token refresh. If for any reason token refresh was disabled, you can enable it with the following method call:

Android Java:

UID2Manager.getInstance().setAutomaticRefreshEnabled(false)

Android Kotlin:

UID2Manager.getInstance().automaticRefreshEnabled = false

Optional: UID2 GMA/IMA Plugin for GAM Secure Signal integration

If you intend to generate UID2 tokens to send to the Google GMA SDK or the Google IMA SDK, assuming you have followed the instructions in this guide, you must also add the UID2 GMA/IMA plugins into your mobile app. For instructions, refer to the applicable plug-in guide:

You do not need to explicitly make the getAdvertisingToken() method call to retrieve the advertising tokens and pass them into Google GMA/IMA SDK manually. The UID2 GMA/IMA plugins manage this for you.

All you need to do is make sure that getAdvertisingToken() returns a non-null string object:

UID2Manager.getInstance().getAdvertisingToken()

If the token exists, the Google GMA/IMA plug-ins can retrieve it automatically via the UID2 GMA/IMA plugins.