|
| 1 | +# UID2 Prebid.js Client-Server Integration Example |
| 2 | + |
| 3 | +This example demonstrates how a content publisher can use [UID2](https://unifiedid.com/docs/intro) and [Prebid.js](http://prebid.org/) to generate UID2 tokens on the server side and pass them to Prebid for use in header bidding auctions. |
| 4 | + |
| 5 | +For the client-side Prebid.js integration example, see [../client-side](../client-side). |
| 6 | + |
| 7 | +> **NOTE:** While the server side of this example is implemented in JavaScript using Node.js, it is not a requirement. You can use any technology of your choice and refer to this example for illustration of the functionality that needs to be implemented. |
| 8 | +
|
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- Node.js (version 20.x or later recommended) |
| 12 | +- UID2 API credentials (API Key and Client Secret) |
| 13 | +- A local UID2 Operator instance **OR** access to the UID2 integration environment |
| 14 | + |
| 15 | +## Run Locally for Testing |
| 16 | + |
| 17 | +### 1. Set Up Environment Variables |
| 18 | + |
| 19 | +Create a `.env` file in the **root of the uid2-examples repository** (not in this folder) with the following variables: |
| 20 | + |
| 21 | +```bash |
| 22 | +# UID2 Operator configuration |
| 23 | +UID2_BASE_URL=http://localhost:8080 |
| 24 | +UID2_API_KEY=your-api-key-here |
| 25 | +UID2_CLIENT_SECRET=your-client-secret-here |
| 26 | + |
| 27 | +# Port for this example (optional, defaults to 3000) |
| 28 | +PORT=3005 |
| 29 | +``` |
| 30 | + |
| 31 | +**For local operator testing:** |
| 32 | +- Use `UID2_BASE_URL=http://localhost:8080` |
| 33 | +- Use API credentials from your local operator's configuration |
| 34 | +- Ensure your local UID2 operator is running |
| 35 | + |
| 36 | +**For integration environment testing:** |
| 37 | +- Use `UID2_BASE_URL=https://operator-integ.uidapi.com` |
| 38 | +- Use your integration environment API credentials from the UID2 Portal |
| 39 | + |
| 40 | +### 2. Install Dependencies |
| 41 | + |
| 42 | +```bash |
| 43 | +npm install |
| 44 | +``` |
| 45 | + |
| 46 | +### 3. Start the Server |
| 47 | + |
| 48 | +```bash |
| 49 | +npm start |
| 50 | +``` |
| 51 | + |
| 52 | +You should see: |
| 53 | + |
| 54 | +``` |
| 55 | +UID2 Prebid Client-Server example listening at http://localhost:3005 |
| 56 | +Make sure you have set the following environment variables: |
| 57 | + - UID2_API_KEY |
| 58 | + - UID2_CLIENT_SECRET |
| 59 | + - UID2_BASE_URL (optional, defaults to integration environment) |
| 60 | +``` |
| 61 | + |
| 62 | +### 4. Test the Application |
| 63 | + |
| 64 | +1. Open your browser to `http://localhost:3005` |
| 65 | +2. Enter an email address in the input field |
| 66 | +3. Click **"Generate UID2"** |
| 67 | +4. You should see: |
| 68 | + - "Ready for Targeted Advertising: **yes**" |
| 69 | + - The UID2 advertising token displayed |
| 70 | + - The button changes to "Clear UID2" |
| 71 | + |
| 72 | +### 5. Verify Prebid Integration |
| 73 | + |
| 74 | +Open the browser console (F12) and run: |
| 75 | + |
| 76 | +```javascript |
| 77 | +pbjs.getUserIds() |
| 78 | +``` |
| 79 | + |
| 80 | +You should see output like: |
| 81 | + |
| 82 | +```javascript |
| 83 | +{ |
| 84 | + uid2: { |
| 85 | + id: "AdvertisingTokenA...", // Your UID2 token |
| 86 | + refresh_token: "...", |
| 87 | + refresh_from: 1234567890, |
| 88 | + refresh_expires: 1234567890, |
| 89 | + // ... |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +This confirms that Prebid.js has received and stored the UID2 token. |
| 95 | + |
| 96 | +### 6. Test Token Persistence |
| 97 | + |
| 98 | +Refresh the page - the UID2 token should persist (loaded from localStorage) and Prebid should still have access to it via `pbjs.getUserIds()`. |
| 99 | + |
| 100 | +## Environment Variables |
| 101 | + |
| 102 | +| Variable | Required | Description | |
| 103 | +| :------------------- | :------- | :---------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 104 | +| `UID2_BASE_URL` | Yes | The base URL of the UID2 service. For local testing: `http://localhost:8080`. For integration: `https://operator-integ.uidapi.com`. | |
| 105 | +| `UID2_API_KEY` | Yes | Your UID2 API key. Must have the GENERATOR role. | |
| 106 | +| `UID2_CLIENT_SECRET` | Yes | Your UID2 client secret corresponding to the API key. | |
| 107 | +| `PORT` | No | Port for the Express server (default: 3000). Set to 3005 to avoid conflicts with other examples. | |
| 108 | + |
| 109 | +## How It Works |
| 110 | + |
| 111 | +This example implements the [UID2 Client-Server Integration Guide for Prebid.js](https://unifiedid.com/docs/guides/integration-prebid-client-server). |
| 112 | + |
| 113 | +### Server Side (`server.js`) |
| 114 | + |
| 115 | +1. Receives email from the client via `/login` endpoint |
| 116 | +2. Encrypts the email and sends it to the UID2 `/v2/token/generate` API |
| 117 | +3. Decrypts the response and extracts the UID2 identity |
| 118 | +4. Returns the identity (advertising token, refresh token, etc.) to the client as JSON |
| 119 | + |
| 120 | +### Client Side (`public/index.html`) |
| 121 | + |
| 122 | +1. Calls the `/login` endpoint with the user's email |
| 123 | +2. Receives the UID2 identity from the server |
| 124 | +3. Stores the identity in localStorage |
| 125 | +4. Configures Prebid.js with the UID2 token using `pbjs.setConfig()` |
| 126 | +5. Prebid.js includes the UID2 in subsequent bid requests |
| 127 | + |
| 128 | +## Troubleshooting |
| 129 | + |
| 130 | +### "Request failed with status code 401" |
| 131 | + |
| 132 | +- Verify your `UID2_API_KEY` and `UID2_CLIENT_SECRET` are correct |
| 133 | +- Ensure your API key has the **GENERATOR** role |
| 134 | +- Check that your credentials match the environment (local operator vs. integration) |
| 135 | + |
| 136 | +### "Request failed with status code 404" or "500" |
| 137 | + |
| 138 | +- **Local operator:** Ensure your UID2 operator is running at `localhost:8080` |
| 139 | +- **Local operator:** Verify operator configuration accepts v2 encrypted requests |
| 140 | +- **Integration environment:** Switch `UID2_BASE_URL` to `https://operator-integ.uidapi.com` |
| 141 | + |
| 142 | +### Token doesn't persist across refresh |
| 143 | + |
| 144 | +- Check browser localStorage for `__uid2_advertising_token` key |
| 145 | +- Verify no browser extensions are clearing storage |
| 146 | +- Check browser console for JavaScript errors |
| 147 | + |
| 148 | +### Prebid doesn't have the UID2 token |
| 149 | + |
| 150 | +Run `pbjs.getUserIds()` in console: |
| 151 | +- If empty or missing `uid2`, check console for Prebid errors |
| 152 | +- Verify `setPrebidConfig()` is being called after token generation |
| 153 | +- Check that `prebid.js` loaded correctly (check Network tab) |
| 154 | + |
| 155 | +## Additional Resources |
| 156 | + |
| 157 | +- [UID2 Client-Server Integration Guide for Prebid.js](https://unifiedid.com/docs/guides/integration-prebid-client-server) |
| 158 | +- [UID2 SDK for JavaScript](https://unifiedid.com/docs/sdks/client-side-identity) |
| 159 | +- [Prebid.js Documentation](https://docs.prebid.org/) |
| 160 | +- [UID2 Portal](https://unifiedid.com/docs/portal/overview) |
| 161 | + |
0 commit comments