Skip to content

Commit 9aa7f2e

Browse files
authored
edge: Fix targeting to support cascading and start migrating toward object param (#208)
1 parent 02e7cb3 commit 9aa7f2e

4 files changed

Lines changed: 18 additions & 8 deletions

File tree

lib/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getConsent, inferRegulation } from "./core/regs/consent";
22
import type { CMPApiConfig, Consent } from "./core/regs/consent";
33

4-
type Experiment = "tokenize-v2";
4+
type Experiment = "tokenize-v2" | "targeting-cascade";
55

66
type InitConsent = {
77
// A "cmpapi" configuration indicating that consent should be gathered from CMP apis.

lib/edge/targeting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type TargetingResponse = {
2727
ortb2: { user: ortb2.User };
2828
};
2929

30-
async function Targeting(config: ResolvedConfig, ...ids: readonly string[]): Promise<TargetingResponse> {
30+
async function Targeting(config: ResolvedConfig, ids: readonly string[]): Promise<TargetingResponse> {
3131
const searchParams = new URLSearchParams(ids.map((id) => ["id", id]));
3232
const path = "/v2/targeting?" + searchParams.toString();
3333

lib/sdk.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,10 @@ describe("behavior testing of", () => {
395395
})
396396
);
397397

398-
const targetingWithParam = await sdk.targeting("someId", "someOtherId");
399-
expect(targetingWithParam).toBeDefined();
398+
await expect(sdk.targeting({ ids: ["someId", "someOtherId"] })).rejects.toMatch(/targeting-cascade/);
399+
400+
sdk.dcn.experiments = ["targeting-cascade"];
401+
const targetingWithParam = await sdk.targeting({ ids: ["someId", "someOtherId"] });
400402

401403
expect(fetchSpy).toHaveBeenLastCalledWith(
402404
expect.objectContaining({

lib/sdk.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ import { Profile } from "./edge/profile";
2020
import { sha256 } from "js-sha256";
2121
import { Tokenize, TokenizeResponse } from "./edge/tokenize";
2222

23+
type TargetingRequest =
24+
| string
25+
| {
26+
ids?: string[];
27+
};
28+
2329
class OptableSDK {
2430
public static version = buildInfo.version;
2531

@@ -54,13 +60,15 @@ class OptableSDK {
5460
return Uid2Token(this.dcn, id);
5561
}
5662

57-
async targeting(...ids: string[]): Promise<TargetingResponse> {
58-
if (!ids.length) {
59-
ids = ["__passport__"];
63+
async targeting(request: TargetingRequest = "__passport__"): Promise<TargetingResponse> {
64+
const ids = typeof request === "string" ? [request] : request?.ids || [];
65+
66+
if (ids.length > 1 && !this.dcn.experiments.includes("targeting-cascade")) {
67+
throw "Targeting multiple IDs is only available with the 'targeting-cascade' experiment enabled.";
6068
}
6169

6270
await this.init;
63-
return Targeting(this.dcn, ...ids);
71+
return Targeting(this.dcn, ids);
6472
}
6573

6674
targetingFromCache(): TargetingResponse | null {

0 commit comments

Comments
 (0)