Skip to content

Commit

Permalink
Update prebid demos to use targeting keyname as segments provider dis…
Browse files Browse the repository at this point in the history
…criminator (#29)

* Update prebid demos to use targeting keyname as segments provider discriminator

* Don't prettify demos

* Expose a helper to fill prebid user data on the SDK
  • Loading branch information
zapo authored Feb 11, 2021
1 parent fe6f8fb commit 4106a5b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 76 deletions.
7 changes: 5 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.prettierrc

# build outputs
browser/dist
lib/dist
demos/vanilla/nocookies/targeting/prebid*.js
demos/vanilla/targeting/prebid*.js

# demos
demos
22 changes: 4 additions & 18 deletions demos/vanilla/targeting/prebid-us-east-16.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,10 @@

pbjs.que.push(function () {
optable.cmd.push(function () {
const tdata = optable.instance.targetingFromCache();

if (tdata) {
const segments = [];
for (const [key, values] of Object.entries(tdata)) {
for (const [idx, value] of Object.entries(values)) {
segments.push({
name: key,
value: value,
});
}
}

if (segments.length > 0) {
const sbh = optable.instance.sandbox.host + "/" + optable.instance.sandbox.site;
pbjs.setConfig({ fpd: { user: { data: [{ id: sbh, name: sbh, segment: segments }] } } });
console.log("[OptableSDK] pbjs.setConfig({ fpd: ... })");
}
const pbdata = optable.instance.prebidUserDataFromCache();
if (pbdata.length > 0) {
pbjs.setConfig({ fpd: { user: { data: pbdata } } });
console.log("[OptableSDK] pbjs.setConfig({ fpd: ... })");
}

pbjs.setConfig({
Expand Down
22 changes: 4 additions & 18 deletions demos/vanilla/targeting/prebid-us-east-16.html.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,10 @@
pbjs.que.push(function () {
optable.cmd.push(function () {
const tdata = optable.instance.targetingFromCache();
if (tdata) {
const segments = [];
for (const [key, values] of Object.entries(tdata)) {
for (const [idx, value] of Object.entries(values)) {
segments.push({
name: key,
value: value,
});
}
}
if (segments.length > 0) {
const sbh = optable.instance.sandbox.host + "/" + optable.instance.sandbox.site;
pbjs.setConfig({ fpd: { user: { data: [{ id: sbh, name: sbh, segment: segments }] } } });
console.log("[OptableSDK] pbjs.setConfig({ fpd: ... })");
}
const pbdata = optable.instance.prebidUserDataFromCache();
if (pbdata.length > 0) {
pbjs.setConfig({ fpd: { user: { data: pbdata } } });
console.log("[OptableSDK] pbjs.setConfig({ fpd: ... })");
}
pbjs.setConfig({
Expand Down
22 changes: 4 additions & 18 deletions demos/vanilla/targeting/prebid.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,10 @@

pbjs.que.push(function () {
optable.cmd.push(function () {
const tdata = optable.instance.targetingFromCache();

if (tdata) {
const segments = [];
for (const [key, values] of Object.entries(tdata)) {
for (const [idx, value] of Object.entries(values)) {
segments.push({
name: key,
value: value,
});
}
}

if (segments.length > 0) {
const sbh = optable.instance.sandbox.host + "/" + optable.instance.sandbox.site;
pbjs.setConfig({ fpd: { user: { data: [{ id: sbh, name: sbh, segment: segments }] } } });
console.log("[OptableSDK] pbjs.setConfig({ fpd: ... })");
}
const pbdata = optable.instance.prebidUserDataFromCache();
if (pbdata.length > 0) {
pbjs.setConfig({ fpd: { user: { data: pbdata } } });
console.log("[OptableSDK] pbjs.setConfig({ fpd: ... })");
}

pbjs.setConfig({
Expand Down
22 changes: 4 additions & 18 deletions demos/vanilla/targeting/prebid.html.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,10 @@
pbjs.que.push(function () {
optable.cmd.push(function () {
const tdata = optable.instance.targetingFromCache();
if (tdata) {
const segments = [];
for (const [key, values] of Object.entries(tdata)) {
for (const [idx, value] of Object.entries(values)) {
segments.push({
name: key,
value: value,
});
}
}
if (segments.length > 0) {
const sbh = optable.instance.sandbox.host + "/" + optable.instance.sandbox.site;
pbjs.setConfig({ fpd: { user: { data: [{ id: sbh, name: sbh, segment: segments }] } } });
console.log("[OptableSDK] pbjs.setConfig({ fpd: ... })");
}
const pbdata = optable.instance.prebidUserDataFromCache();
if (pbdata.length > 0) {
pbjs.setConfig({ fpd: { user: { data: pbdata } } });
console.log("[OptableSDK] pbjs.setConfig({ fpd: ... })");
}
pbjs.setConfig({
Expand Down
35 changes: 34 additions & 1 deletion lib/edge/targeting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,38 @@ function TargetingClearCache(config: OptableConfig) {
ls.clearTargeting();
}

export { Targeting, TargetingFromCache, TargetingClearCache, TargetingKeyValues };
type PrebidUserSegment = { name: string; value: string };
type PrebidUserSegmentProvider = { id: string; name: string; segment: PrebidUserSegment[] };
type PrebidUserData = PrebidUserSegmentProvider[];

function PrebidUserDataFromCache(config: OptableConfig): PrebidUserData {
const tdata = TargetingFromCache(config);
const result = [];

for (const [key, values] of Object.entries(tdata || {})) {
const segments = values.map((value) => ({
name: key,
value: value,
}));

if (segments.length > 0) {
result.push({
id: key,
name: key,
segment: segments,
});
}
}

return result;
}

export {
Targeting,
TargetingFromCache,
TargetingClearCache,
TargetingKeyValues,
PrebidUserDataFromCache,
PrebidUserData,
};
export default Targeting;
6 changes: 5 additions & 1 deletion lib/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { OptableConfig } from "./config";
import type { TargetingKeyValues } from "./edge/targeting";
import { PrebidUserData, PrebidUserDataFromCache, TargetingKeyValues } from "./edge/targeting";
import type { WitnessProperties } from "./edge/witness";
import type { ProfileTraits } from "./edge/profile";
import { Identify } from "./edge/identify";
Expand Down Expand Up @@ -30,6 +30,10 @@ class OptableSDK {
TargetingClearCache(this.sandbox);
}

prebidUserDataFromCache(): PrebidUserData {
return PrebidUserDataFromCache(this.sandbox);
}

witness(event: string, properties: WitnessProperties = {}): Promise<void> {
return Witness(this.sandbox, event, properties);
}
Expand Down

0 comments on commit 4106a5b

Please sign in to comment.