|
| 1 | +# UID2 Prebid.js Client-Server Integration Example |
| 2 | + |
| 3 | +This example demonstrates how to integrate [UID2 with Prebid.js using client-server integration](https://unifiedid.com/docs/guides/integration-prebid-client-server), where UID2 tokens are generated on the server side and passed to Prebid for use in header bidding auctions. |
| 4 | + |
| 5 | +> **NOTE:** This example uses Prebid.js v8. |
| 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 | +## Run with Docker (Recommended) |
| 10 | + |
| 11 | +### 1. Choose Your Testing Environment |
| 12 | + |
| 13 | +You have two options for testing: |
| 14 | + |
| 15 | +**Option A: Local UID2 Operator** (for local development) |
| 16 | +**Option B: Integration Environment** (for testing against a live UID2 service) |
| 17 | + |
| 18 | +### 2. Set Up Environment Variables |
| 19 | + |
| 20 | +Create a `.env` file in the **root of the uid2-examples repository** (NOT in this folder) with the following: |
| 21 | + |
| 22 | +```bash |
| 23 | +UID2_BASE_URL=http://host.docker.internal:8080 |
| 24 | +UID2_API_KEY=your-api-key-here |
| 25 | +UID2_CLIENT_SECRET=your-client-secret-here |
| 26 | +``` |
| 27 | + |
| 28 | +**Important:** The `.env` file must be at `/uid2-examples/.env`, not `/uid2-examples/web-integrations/prebid-integrations/client-server/.env`. |
| 29 | + |
| 30 | +See the instructions below for your chosen environment to get the correct values. |
| 31 | + |
| 32 | +### 3. Build and Run |
| 33 | + |
| 34 | +From the **root of the uid2-examples repository**, run: |
| 35 | + |
| 36 | +```bash |
| 37 | +docker-compose up --build prebid-client-server |
| 38 | +``` |
| 39 | + |
| 40 | +The application will be available at **`http://localhost:3052`** |
| 41 | + |
| 42 | +To stop, press `Ctrl+C` or run: |
| 43 | + |
| 44 | +```bash |
| 45 | +docker-compose down |
| 46 | +``` |
| 47 | + |
| 48 | +## Testing Environment Setup |
| 49 | + |
| 50 | +### Option A: Local UID2 Operator |
| 51 | + |
| 52 | +This option runs a local instance of the UID2 Operator on your machine. |
| 53 | + |
| 54 | +#### 1. Clone and Set Up the UID2 Operator |
| 55 | + |
| 56 | +```bash |
| 57 | +git clone https://github.com/IABTechLab/uid2-operator.git |
| 58 | +cd uid2-operator |
| 59 | +``` |
| 60 | + |
| 61 | +#### 2. Configure the Operator |
| 62 | + |
| 63 | +Edit `conf/local-config.json` and ensure this key is set: |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + "enable_v2_encryption": true |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +#### 3. Get Your Credentials |
| 72 | + |
| 73 | +Open `src/main/resources/clients/clients.json` and find a client entry with the **"GENERATOR"** role. For example: |
| 74 | + |
| 75 | +```json |
| 76 | +{ |
| 77 | + "key": "UID2-C-L-124-H8VwqX.l2G4TCuUWYAqdqkeG/UqtFoPEoXirKn4kHWxc=", |
| 78 | + "secret": "NcMgi6Y8C80SlxvV7pYlfcvEIo+2b0508tYQ3pKK8HM=", |
| 79 | + "name": "Publisher", |
| 80 | + "roles": ["GENERATOR"] |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +#### 4. Update Your .env File |
| 85 | + |
| 86 | +In the **root of the uid2-examples repository** (at `/uid2-examples/.env`), add: |
| 87 | + |
| 88 | +```bash |
| 89 | +UID2_BASE_URL=http://host.docker.internal:8080 |
| 90 | +UID2_API_KEY=UID2-C-L-124-H8VwqX.l2G4TCuUWYAqdqkeG/UqtFoPEoXirKn4kHWxc= |
| 91 | +UID2_CLIENT_SECRET=NcMgi6Y8C80SlxvV7pYlfcvEIo+2b0508tYQ3pKK8HM= |
| 92 | +``` |
| 93 | + |
| 94 | +#### 5. Start the Operator |
| 95 | + |
| 96 | +Follow the [UID2 Operator README](https://github.com/IABTechLab/uid2-operator) instructions to start the operator. It will run on `http://localhost:8080`. |
| 97 | + |
| 98 | +### Option B: Integration Environment |
| 99 | + |
| 100 | +This option uses the hosted UID2 integration environment (deployed version). |
| 101 | + |
| 102 | +#### 1. Get Your Credentials |
| 103 | + |
| 104 | +Contact your UID2 representative or use the [UID2 Portal](https://unifiedid.com/docs/portal/overview) to obtain integration environment credentials. |
| 105 | + |
| 106 | +#### 2. Update Your .env File |
| 107 | + |
| 108 | +In the **root of the uid2-examples repository** (at `/uid2-examples/.env`), add: |
| 109 | + |
| 110 | +```bash |
| 111 | +UID2_BASE_URL=https://operator-integ.uidapi.com |
| 112 | +UID2_API_KEY=your-integ-api-key |
| 113 | +UID2_CLIENT_SECRET=your-integ-client-secret |
| 114 | +``` |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +## Testing the Application |
| 119 | + |
| 120 | +### 1. Generate a UID2 Token |
| 121 | + |
| 122 | +1. Open `http://localhost:3052` in your browser |
| 123 | +2. Enter an email address |
| 124 | +3. Click **"Generate UID2"** |
| 125 | + |
| 126 | +**Expected result:** |
| 127 | +- "Ready for Targeted Advertising: **yes**" |
| 128 | +- The UID2 advertising token is displayed |
| 129 | +- Button changes to "Clear UID2" |
| 130 | + |
| 131 | +### 2. Verify Prebid Integration |
| 132 | + |
| 133 | +Open the browser console (F12) and run: |
| 134 | + |
| 135 | +```javascript |
| 136 | +pbjs.getUserIds() |
| 137 | +``` |
| 138 | + |
| 139 | +You should see: |
| 140 | + |
| 141 | +```javascript |
| 142 | +{ |
| 143 | + uid2: { |
| 144 | + id: "AdvertisingTokenA...", |
| 145 | + refresh_token: "...", |
| 146 | + // ... |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### 3. Test Token Persistence |
| 152 | + |
| 153 | +Refresh the page - the token should persist (loaded from localStorage). |
| 154 | + |
| 155 | +### 4. Test Opt-Out |
| 156 | + |
| 157 | +Enter an opted-out email and click "Generate UID2". |
| 158 | + |
| 159 | +**Test emails:** |
| 160 | +- **Local operator: ** `[email protected]` |
| 161 | +- **Integration environment: ** `[email protected]` |
| 162 | + |
| 163 | +**Expected result:** |
| 164 | +- "Ready for Targeted Advertising: **no**" |
| 165 | +- "UID2 Advertising Token: **This email has opted out.**" |
| 166 | +- Console shows: `UID2 status: optout` |
| 167 | + |
| 168 | +## Environment Variables |
| 169 | + |
| 170 | +| Variable | Description | Example | |
| 171 | +| :------------------- | :----------------------------------------------------------------------------------------------- | :--------------------------------------- | |
| 172 | +| `UID2_BASE_URL` | The UID2 Operator endpoint. Use `http://localhost:8080` for local or the integration URL. | `http://localhost:8080` | |
| 173 | +| `UID2_API_KEY` | Your UID2 API key with GENERATOR role. | `UID2-C-L-124-H8VwqX...` | |
| 174 | +| `UID2_CLIENT_SECRET` | Your UID2 client secret. | `NcMgi6Y8C80SlxvV7pYlfcvEIo+2b0508...` | |
| 175 | +| `UID2_STORAGE_KEY` | Your localStorage key for storing UID2 tokens. | `__uid2_advertising_token` | |
| 176 | + |
| 177 | +**Note:** For Docker, use `http://host.docker.internal:8080` instead of `http://localhost:8080` to access services on your host machine. |
| 178 | + |
| 179 | +## How It Works |
| 180 | + |
| 181 | +This example implements the [UID2 Client-Server Integration Guide for Prebid.js](https://unifiedid.com/docs/guides/integration-prebid-client-server). |
| 182 | + |
| 183 | +### Server Side (`server.js`) |
| 184 | + |
| 185 | +1. Receives email from the client via `/login` endpoint |
| 186 | +2. Encrypts the email and sends it to the UID2 `/v2/token/generate` API |
| 187 | +3. Decrypts the response and extracts the UID2 identity |
| 188 | +4. Returns the identity to the client as JSON |
| 189 | + |
| 190 | +### Client Side (`public/index.html`) |
| 191 | + |
| 192 | +1. Calls the `/login` endpoint with the user's email |
| 193 | +2. Receives the UID2 identity from the server |
| 194 | +3. Stores the identity in localStorage |
| 195 | +4. Configures Prebid.js with the UID2 token using `pbjs.setConfig()` |
| 196 | +5. Prebid.js includes the UID2 in bid requests |
| 197 | + |
| 198 | +## Troubleshooting |
| 199 | + |
| 200 | +### "Request failed with status code 401" |
| 201 | + |
| 202 | +- Verify your `UID2_API_KEY` and `UID2_CLIENT_SECRET` are correct |
| 203 | +- Ensure your API key has the **GENERATOR** role |
| 204 | +- Check that credentials match your environment (local vs. integration) |
| 205 | + |
| 206 | +### "Request failed with status code 500" |
| 207 | + |
| 208 | +**For local operator:** |
| 209 | +- Verify the operator is running at `localhost:8080`; the output should indicate 'OK'. |
| 210 | +- Check `enable_v2_encryption: true` is set in `local-config.json` |
| 211 | +- Review operator logs for errors |
| 212 | + |
| 213 | +**For Docker:** |
| 214 | +- Ensure `UID2_BASE_URL` uses `host.docker.internal:8080` not `localhost:8080` |
| 215 | + |
| 216 | +### Prebid Doesn't Have the UID2 |
| 217 | + |
| 218 | +Run `pbjs.getUserIds()` in console: |
| 219 | +- If empty or missing `uid2`, check console for Prebid errors |
| 220 | +- Verify Prebid.js loaded correctly (check Network tab) |
| 221 | +- Ensure `setPrebidConfig()` is being called after token generation |
| 222 | + |
| 223 | +## Additional Resources |
| 224 | + |
| 225 | +- [UID2 Client-Server Integration Guide for Prebid.js](https://unifiedid.com/docs/guides/integration-prebid-client-server) |
| 226 | +- [UID2 Operator Repository](https://github.com/IABTechLab/uid2-operator) |
| 227 | +- [UID2 Portal](https://unifiedid.com/docs/portal/overview) |
| 228 | +- [Prebid.js Documentation](https://docs.prebid.org/) |
0 commit comments