Commit 32583d0
Bosko Milekic
feat: add ctxSegments() and ctxTargetingKeyValues() for contextual targeting
Introduce a contextual-classification path in the SDK: fetch the DCN's
taxonomy/category classifications for a page URL, cache them on the SDK
instance, and expose a helper that turns the cached response into
GAM-ready targeting key-values.
SDK (lib/sdk.ts, lib/edge/contextual_segments.ts):
- New OptableSDK.ctxSegments(url?) — POSTs to /v1beta1/contextual and
returns ContextualSegmentsResponse { classifications: { categories: [
{ taxonomy, id, score, ... } ] } }. url defaults to
window.location.href. Response is cached on the instance; calling
again refreshes the cache. Pass-through behavior (no validation /
normalization), matching Targeting() at the edge layer.
- initContextual: true now also fires ctxSegments() at init time
(fire-and-forget, alongside the pageview witness), so the cache is
populated automatically before any consumer reads it.
- New OptableSDK.ctxTargetingKeyValues(taxonomyKeys?) reads the cached
response and returns Record<string, string[]> of category ids grouped
by taxonomy, ready for googletag.pubads().setTargeting(). Without a
map, keys are the raw taxonomy values; with a map, keys are renamed
and only the listed taxonomies are emitted (filter + rename). Ids are
deduped, order preserved.
Tests (lib/sdk.test.ts, lib/test/handlers.ts):
- MSW handler for /v1beta1/contextual.
- Breaking-change tests for ctxSegments and ctxTargetingKeyValues.
- Behavioral coverage: happy path, window.location.href default, empty
/ missing / malformed responses, categories spanning multiple
taxonomies, cache population, default keying, rename, filter of
unmapped taxonomies, and that initContextual: true triggers a
contextual request.
Docs & demos (README.md, demos/**):
- Document the Contextual Segments API in README: ctxSegments() caching
semantics, the initContextual precondition for ctxTargetingKeyValues,
the explicit ctxSegments().then(loadGAM) alternative, key
rename/allow-list, and the requirement that the URL be classified by
the DCN.
- Two new vanilla demo pages —
demos/vanilla/targeting/ctx_segments.html.tpl and
demos/vanilla/nocookies/targeting/ctx_segments.html.tpl — that call
ctxSegments(), render categories grouped by taxonomy with score bars,
show the derived GAM key-values, and display the raw JSON response.
- Register both templates in the Makefile demo-html target and link
them from the Audience Targeting section of demos/index.html and
demos/index-nocookies.html.1 parent 7b0d27e commit 32583d0
10 files changed
Lines changed: 1064 additions & 1 deletion
File tree
- demos
- vanilla
- nocookies/targeting
- targeting
- lib
- edge
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
| 63 | + | |
62 | 64 | | |
63 | 65 | | |
64 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| |||
148 | 150 | | |
149 | 151 | | |
150 | 152 | | |
151 | | - | |
| 153 | + | |
152 | 154 | | |
153 | 155 | | |
154 | 156 | | |
| |||
404 | 406 | | |
405 | 407 | | |
406 | 408 | | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
407 | 494 | | |
408 | 495 | | |
409 | 496 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
134 | 142 | | |
135 | 143 | | |
136 | 144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
141 | 149 | | |
142 | 150 | | |
143 | 151 | | |
| |||
0 commit comments