Skip to content

Commit 932ed9e

Browse files
committed
feat(prebid): enhance analytics with parsed user agent and device
Integrates the 'bowser' library to parse the raw user agent string into a structured object, providing richer browser and OS information. Includes OpenRTB2 device details in the auction end event payload. Improves robustness of domain and EID extraction using nullish coalescing to prevent errors from missing properties. Corrects a typo from 'cmp' to 'cpm' in the bid won event payload.
1 parent 2bba55a commit 932ed9e

3 files changed

Lines changed: 28 additions & 36 deletions

File tree

lib/addons/prebid/analytics.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import type { WitnessProperties } from "../../edge/witness";
44
import type OptableSDK from "../../sdk";
55

6+
import * as Bowser from "bowser";
7+
68
declare const SDK_WRAPPER_VERSION: string;
79

810
declare global {
@@ -168,17 +170,9 @@ class OptablePrebidAnalytics {
168170
auctionId,
169171
timeout,
170172
bidderRequests: bidderRequests.map((br: any) => {
171-
const {
172-
bidderCode,
173-
bidderRequestId,
174-
ortb2: {
175-
site: { domain },
176-
user: {
177-
ext: { eids },
178-
},
179-
},
180-
bids = [],
181-
} = br;
173+
const { bidderCode, bidderRequestId, bids = [] } = br;
174+
const domain = br.ortb2.site?.domain ?? "unknown";
175+
const eids = br.ortb2.user?.eids ?? [];
182176

183177
// Optable EIDs
184178
const optableEIDS = eids.filter((e: { inserter: string }) => e.inserter === "optable.co");
@@ -313,6 +307,8 @@ class OptablePrebidAnalytics {
313307
};
314308
this.log("bidWon filtered event", filteredEvent);
315309

310+
this.log("bidWon event", event);
311+
316312
const auction = this.auctions.get(event.auctionId);
317313
if (!auction) {
318314
this.log("Missing 'auctionEnd' event. Skipping.");
@@ -356,17 +352,9 @@ class OptablePrebidAnalytics {
356352
let totalBids = 0;
357353

358354
const requests = bidderRequests.map((br: any) => {
359-
const {
360-
bidderCode,
361-
bidderRequestId,
362-
ortb2: {
363-
site: { domain },
364-
user: {
365-
ext: { eids },
366-
},
367-
},
368-
bids = [],
369-
} = br;
355+
const { bidderCode, bidderRequestId, bids = [] } = br;
356+
const domain = br.ortb2.site?.domain ?? "unknown";
357+
const eids = br.ortb2.user?.eids ?? [];
370358

371359
// Optable EIDs
372360
const optableEIDS = eids.filter((e: { inserter: string }) => e.inserter === "optable.co");
@@ -377,6 +365,7 @@ class OptablePrebidAnalytics {
377365
bidderCode,
378366
bidderRequestId,
379367
domain,
368+
device: br.ortb2.device,
380369
optableTargetingDone: optableEIDS.length > 0,
381370
optableMatchers,
382371
optableSources,
@@ -408,22 +397,11 @@ class OptablePrebidAnalytics {
408397
br.optableMatchers.forEach((m: unknown) => oMatchersSet.add(m));
409398
br.optableSources.forEach((s: unknown) => oSourcesSet.add(s));
410399

411-
return {
412-
optableTargetingDone: br.optableTargetingDone,
413-
bidderCode: br.bidderCode,
414-
bids: br.bids.map((b: any) => {
415-
adUnitCode = adUnitCode || b.adUnitCode;
416-
417-
if (b.cpm != null) totalBids += 1;
418-
419-
return { floorMin: b.floorMin, cpm: b.cpm, size: b.size, bidId: b.bidId };
420-
}),
421-
};
400+
return br;
422401
}),
423402
auctionId,
424403
adUnitCode,
425404
totalRequests: bidderRequests.length,
426-
totalBids,
427405
optableSampling: this.config.samplingRate || 1,
428406
optableTargetingDone: oMatchersSet.size || oSourcesSet.size,
429407
optableMatchers: Array.from(oMatchersSet),
@@ -438,14 +416,15 @@ class OptablePrebidAnalytics {
438416
" CPM",
439417
bidderCode: bidWonEvent.bidderCode,
440418
adUnitCode: bidWonEvent.adUnitCode,
441-
cpm: bidWonEvent.cmp,
419+
cpm: bidWonEvent.cpm,
442420
},
443421
missed,
444422
url: `${window.location.hostname}${window.location.pathname}`,
445423
tenant: this.config.tenant!,
446424
// eslint-disable-next-line no-undef
447425
optableWrapperVersion: SDK_WRAPPER_VERSION || "unknown",
448-
userAgent: window.navigator.userAgent,
426+
userAgentRaw: window.navigator.userAgent,
427+
userAgent: Bowser.parse(window.navigator.userAgent) as unknown as Record<string, any>,
449428
};
450429

451430
// Log summary with bid counts

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"dependencies": {
3636
"@babel/runtime": "^7.27.0",
3737
"@optable/web-sdk": "^0.40.0",
38+
"bowser": "^2.12.1",
3839
"iab-openrtb": "^1.0.1",
3940
"js-sha256": "^0.11.0",
4041
"regenerator-runtime": "^0.13.7"

0 commit comments

Comments
 (0)