### Install and Build SDK
Source: https://github.com/adzerk/adzerk-decision-sdk-js/blob/master/README.md
Commands to install dependencies, build the project, and execute the test suite.
```bash
npm install
npm run build
npm run test
```
--------------------------------
### Install SDK via NPM
Source: https://github.com/adzerk/adzerk-decision-sdk-js/blob/master/README.md
Use this command to install the SDK in a Node.js environment. Requires Node.js v20 or higher.
```shell
npm install --save @adzerk/decision-sdk
```
--------------------------------
### Fetch an Ad Decision
Source: https://github.com/adzerk/adzerk-decision-sdk-js/blob/master/README.md
Initializes the client and requests an ad decision based on placements and user keys.
```javascript
import { Client } from "@adzerk/decision-sdk";
// Demo network, site, and ad type IDs; find your own via the Adzerk UI!
let client = new Client({ networkId: 23, siteId: 667480 });
let request = {
placements: [{ adTypes: [5] }],
user: { key: "abc" },
keywords: ["keyword1", "keyword2"]
};
client.decisions.get(request).then(response => {
console.dir(response, { depth: null });
});
```
--------------------------------
### Include SDK via CDN
Source: https://github.com/adzerk/adzerk-decision-sdk-js/blob/master/README.md
Include the SDK directly in HTML files using a script tag. Choose between the latest version or a specific fixed version.
```html
```
```html
```
--------------------------------
### Configure Custom Logging
Source: https://github.com/adzerk/adzerk-decision-sdk-js/blob/master/README.md
Integrates a custom logging function into the SDK client.
```javascript
import { Client } from "@adzerk/decision-sdk";
const logger = (lvl, msg, meta) =>
console.log(`[${lvl}] ${msg} [${JSON.stringify(meta)}]\n`);
let client = new Client({ logger });
```
--------------------------------
### Record Impressions and Clicks
Source: https://github.com/adzerk/adzerk-decision-sdk-js/blob/master/README.md
Fires tracking pixels for impressions and clicks using URLs returned from an ad decision.
```javascript
// Impression pixel; fire when user sees the ad
client.pixels.fire({ url: decision.impressionUrl });
// Click pixel; fire when user clicks on the ad
// status: HTTP status code
// location: click target URL
client.pixels.fire({ url: decision.clickUrl }).then(r => {
console.log(`status ${r["status"]}; location: ${r["location"]}`);
});
```
--------------------------------
### Use Decision Explainer
Source: https://github.com/adzerk/adzerk-decision-sdk-js/blob/master/README.md
Fetches an ad decision with an explanation of why specific ads were chosen or rejected.
```javascript
import { Client } from "@adzerk/decision-sdk";
const apiKey = process.env.ADZERK_API_KEY;
// Demo network, site, and ad type IDs; find your own via the Adzerk UI!
let client = new Client({ networkId: 23, siteId: 667480 });
let request = {
placements: [{ adTypes: [5] }],
user: { key: "abc" },
keywords: ["keyword1", "keyword2"]
};
const options = {
includeExplanation: true,
apiKey
};
client.decisions.get(request, options).then(response => {
console.dir(response, { depth: null });
});
```
```json
{
"div0": {
"placement": {},
"buckets": [],
"rtb_log": [],
"results": []
}
}
```
--------------------------------
### Set UserDB Custom Properties
Source: https://github.com/adzerk/adzerk-decision-sdk-js/blob/master/README.md
Updates a user record in UserDB with custom key-value properties.
```javascript
import { Client } from "@adzerk/decision-sdk";
// Demo network ID; find your own via the Adzerk UI!
let client = new Client({ networkId: 23 });
let props = {
favoriteColor: "blue",
favoriteNumber: 42,
favoriteFoods: ["strawberries", "chocolate"]
};
client.userDb.setCustomProperties("abc", props);
```
--------------------------------
### Read UserDB Record
Source: https://github.com/adzerk/adzerk-decision-sdk-js/blob/master/README.md
Retrieves a user record from UserDB using a specific user key.
```javascript
import { Client } from "@adzerk/decision-sdk";
// Demo network ID; find your own via the Adzerk UI!
let client = new Client({ networkId: 23 });
client.userDb.read("abc").then(response => console.log(response));
```
--------------------------------
### Forget UserDB Record
Source: https://github.com/adzerk/adzerk-decision-sdk-js/blob/master/README.md
Removes a user record from UserDB. Requires an API key.
```javascript
import { Client } from "@adzerk/decision-sdk";
const apiKey = process.env.ADZERK_API_KEY;
// Demo network ID and API key; find your own via the Adzerk UI!
let client = new Client({ networkId: 23, apiKey });
client.userDb.forget("abc");
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.