Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop falling back to legacy targeting cache #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions lib/core/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,4 @@ describe("LocalStorage", () => {
expect(store.setSite());
expect(store.getSite()).toBeNull();
});

test("auto fixes v1 targeting as v2 base on audience key presence", () => {
const store = new LocalStorage(randomConfig());
window.localStorage.setItem(store.targetingV1Key, JSON.stringify({ k1: ["v1"], k2: ["v2"] }));

expect(store.getTargeting()).toEqual({
user: [],
audience: [
{ provider: "optable.co", keyspace: "k1", ids: [{ id: "v1" }], rtb_segtax: 5001 },
{ provider: "optable.co", keyspace: "k2", ids: [{ id: "v2" }], rtb_segtax: 5001 },
],
});

store.setTargeting({ audience: [] });
expect(store.getTargeting()).toEqual({
audience: [],
});
});
});
33 changes: 1 addition & 32 deletions lib/core/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ function toBinary(str: string): string {

class LocalStorage {
private passportKey: string;
private targetingV1Key: string;
private targetingKey: string;
private siteKey: string;

private storage: LocalStorageProxy;

constructor(private config: ResolvedConfig) {
const sfx = btoa(toBinary(`${this.config.host}/${this.config.site}`));
// Legacy targeting key
this.targetingV1Key = "OPTABLE_TGT_" + sfx;

this.passportKey = "OPTABLE_PASS_" + sfx;
this.targetingKey = "OPTABLE_V2_TGT_" + sfx;
this.siteKey = "OPTABLE_SITE_" + sfx;
Expand All @@ -34,36 +30,9 @@ class LocalStorage {
return this.storage.getItem(this.passportKey);
}

getV1Targeting(): TargetingResponse | null {
const raw = this.storage.getItem(this.targetingV1Key);
const parsed = raw ? JSON.parse(raw) : null;
if (!parsed) {
return null;
}

const audiences = Object.entries(parsed).map(([keyspace, values]) => {
return {
provider: "optable.co",
keyspace,
// 5001 is Optable Private Member Defined Audiences
// See: https://github.com/InteractiveAdvertisingBureau/openrtb/pull/81
//
// Starting v2 this is returned in the targeting payload directly
rtb_segtax: 5001,
ids: [].concat(...[values as any]).map((id: any) => ({ id: String(id) })),
};
});

return {
user: [],
audience: audiences,
};
}

getTargeting(): TargetingResponse | null {
const raw = this.storage.getItem(this.targetingKey);
const parsed = raw ? JSON.parse(raw) : null;
return parsed ? parsed : this.getV1Targeting();
return raw ? JSON.parse(raw) : null;
}

setPassport(passport: string) {
Expand Down