Skip to content

Commit a2a36d6

Browse files
authored
Add witness API, used to send events to sandbox. (#12)
1 parent 20a3f1f commit a2a36d6

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,32 @@ sdk
101101

102102
On success, the resulting key values are typically sent as part of a subsequent ad call. Therefore we recommend that you either call targeting() before each ad call, or in parallel periodically, caching the resulting key values which you then provide in ad calls.
103103

104+
### Witness API
105+
106+
To send real-time event data from the user's browser to the sandbox for eventual audience assembly, you can call the witness API as follows:
107+
108+
```js
109+
const onSuccess = () => console.log("Witness API success!");
110+
const onFailure = (err) => console.warn("Witness API error: ${err.message}");
111+
112+
const eventProperties = {
113+
property_one: "some_value",
114+
property_two: "some other value",
115+
};
116+
117+
sdk.witness("event.type.here", eventProperties).then(onSuccess).catch(onFailure);
118+
```
119+
120+
The specified event type and properties are associated with the logged event and which can be used for matching during audience assembly.
121+
122+
Note that event properties are string keyvalue pairs and have type `WitnessProperties`:
123+
124+
```typescript
125+
type WitnessProperties = {
126+
[key: string]: string;
127+
};
128+
```
129+
104130
## Usage (script tag)
105131

106132
For each [SDK release](https://github.com/Optable/optable-web-sdk/releases), a webpack generated browser bundle targeting the browsers list described by `npx browserslist "> 0.25%, not dead"` can be loaded on a web site via a `script` tag. As previously mentioned, your sandbox is configured to serve such a released browser bundle via the `https://sandbox.customer.com/static/web/sdk.js` URL by default.

lib/edge/witness.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { SandboxConfig } from "../config";
2+
import { fetch } from "../core/network";
3+
4+
type WitnessProperties = {
5+
[key: string]: string;
6+
};
7+
8+
function Witness(config: SandboxConfig, event: string, properties: WitnessProperties): Promise<void> {
9+
const evt = {
10+
event: event,
11+
properties: properties,
12+
};
13+
14+
return fetch("/witness", config, {
15+
method: "POST",
16+
headers: {
17+
"Content-Type": "application/json",
18+
},
19+
body: JSON.stringify(evt),
20+
});
21+
}
22+
23+
export { Witness, WitnessProperties };
24+
export default Witness;

lib/sdk.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { SandboxConfig } from "./config";
22
import type { TargetingKeyValues } from "./edge/targeting";
3+
import type { WitnessProperties } from "./edge/witness";
34
import type { AuthModalDOMConfig, MicroModalConfig } from "./ui/auth_modal";
45
import Identify from "./edge/identify";
56
import Targeting from "./edge/targeting";
7+
import Witness from "./edge/witness";
68
import AuthModal from "./ui/auth_modal";
79
import { sha256 } from "js-sha256";
810

@@ -28,6 +30,10 @@ class SDK {
2830
return Targeting(this.sandbox);
2931
}
3032

33+
witness(event: string, properties: WitnessProperties = {}): Promise<void> {
34+
return Witness(this.sandbox, event, properties);
35+
}
36+
3137
authenticator(DOMConfig?: AuthModalDOMConfig, MicroModalConfig?: MicroModalConfig): AuthModal {
3238
return new AuthModal(this.sandbox, DOMConfig, MicroModalConfig);
3339
}

0 commit comments

Comments
 (0)