Revision control

Copy as Markdown

# Using the Mozilla Ads Client (MAC)
An integration has three steps: create one client, request ads for your placements, then show them and report what the user did with them. Privacy (OHTTP), caching and telemetry are options on top.
Every example is given in Kotlin, Swift and JavaScript, one after the other. The [method reference](#method-reference) and the [type index](#type-index) are at the end.
---
## 1. Create the client
Build one client with `MozAdsClientBuilder` and keep it for the life of the app: a singleton, or something similar, so that multiple clients do not exist at once.
```kotlin
val client = MozAdsClientBuilder()
.environment(MozAdsEnvironment.PROD)
.cacheConfig(cache)
.telemetry(telemetry)
.build()
```
```swift
let client = MozAdsClientBuilder()
.environment(environment: .prod)
.cacheConfig(cacheConfig: cache)
.telemetry(telemetry: telemetry)
.build()
```
```javascript
const client = MozAdsClientBuilder()
.environment(MozAdsEnvironment.Prod)
.cacheConfig(cache)
.telemetry(telemetry)
.build();
```
Every setting is optional:
| Setting | What it does | More |
| ------------- | --------------------------------------------------------------------------------------------------------------------- | -------------------------------------- |
| `environment` | Which MARS environment to talk to. Defaults to Prod, and outside a dev build it can only ever be Prod. | |
| `cacheConfig` | Where and how much to cache. Without it, nothing is cached. | [Cache responses](#5-cache-responses) |
| `telemetry` | Where the client's metrics go. Without it, none are recorded. | [Record telemetry](#6-record-telemetry) |
| `userAgent` | The browser's user agent, used by OHTTP requests when the preflight returns none. | [Keep requests private](#4-keep-requests-private-with-ohttp) |
<details>
<summary><code>MozAdsClientBuilder</code></summary>
```kotlin
class MozAdsClientBuilder {
fun environment(environment: MozAdsEnvironment): MozAdsClientBuilder
fun cacheConfig(cacheConfig: MozAdsCacheConfig): MozAdsClientBuilder
fun telemetry(telemetry: MozAdsTelemetry): MozAdsClientBuilder
fun userAgent(userAgent: String): MozAdsClientBuilder
fun build(): MozAdsClient
}
```
```swift
class MozAdsClientBuilder {
func environment(environment: MozAdsEnvironment) -> MozAdsClientBuilder
func cacheConfig(cacheConfig: MozAdsCacheConfig) -> MozAdsClientBuilder
func telemetry(telemetry: MozAdsTelemetry) -> MozAdsClientBuilder
func userAgent(userAgent: String) -> MozAdsClientBuilder
func build() -> MozAdsClient
}
```
```javascript
MozAdsClientBuilder() // → MozAdsClientBuilder
builder.environment(environment) // MozAdsEnvironment → MozAdsClientBuilder
builder.cacheConfig(cacheConfig) // MozAdsCacheConfig → MozAdsClientBuilder
builder.telemetry(telemetry) // MozAdsTelemetry → MozAdsClientBuilder
builder.userAgent(userAgent) // string → MozAdsClientBuilder
builder.build() // → MozAdsClient
```
</details>
---
## 2. Request ads
There are three kinds of ad, each with its own method:
| Kind | Method | You ask for | You get, per placement |
| ---------------------- | ----------------- | -------------------- | ---------------------- |
| Tile | `requestTileAds` | one ad per placement | one `MozAdsTile` |
| Image (e.g. billboard) | `requestImageAds` | one ad per placement | one `MozAdsImage` |
| Sponsored story (spoc) | `requestSpocAds` | a count per placement | a list of `MozAdsSpoc` |
Each request lists the placements to fill, by `placementId`. Ids must be unique within one request. The result is a map keyed by `placementId`, and it leaves out placements that got no ad, so an empty slot is simply absent.
```kotlin
val tiles = client.requestTileAds(listOf(
MozAdsPlacementRequest(placementId = "newtab_tile_1", iabContent = null),
MozAdsPlacementRequest(placementId = "newtab_tile_2", iabContent = null),
))
val stories = client.requestSpocAds(listOf(
MozAdsPlacementRequestWithCount(count = 3, placementId = "newtab_stories", iabContent = null),
))
```
```swift
let tiles = try client.requestTileAds(mozAdRequests: [
MozAdsPlacementRequest(placementId: "newtab_tile_1", iabContent: nil),
MozAdsPlacementRequest(placementId: "newtab_tile_2", iabContent: nil),
])
let stories = try client.requestSpocAds(mozAdRequests: [
MozAdsPlacementRequestWithCount(count: 3, placementId: "newtab_stories", iabContent: nil),
])
```
```javascript
const tiles = client.requestTileAds([
MozAdsPlacementRequest({ placementId: "newtab_tile_1", iabContent: null }),
MozAdsPlacementRequest({ placementId: "newtab_tile_2", iabContent: null }),
]);
const stories = client.requestSpocAds([
MozAdsPlacementRequestWithCount({ count: 3, placementId: "newtab_stories", iabContent: null }),
]);
```
Every method throws on failure.
<details>
<summary><code>MozAdsPlacementRequest</code>, <code>MozAdsPlacementRequestWithCount</code></summary>
```kotlin
data class MozAdsPlacementRequest(
val placementId: String,
val iabContent: MozAdsIABContent?
)
data class MozAdsPlacementRequestWithCount(
val count: Int,
val placementId: String,
val iabContent: MozAdsIABContent?
)
```
```swift
struct MozAdsPlacementRequest {
let placementId: String
let iabContent: MozAdsIABContent?
}
struct MozAdsPlacementRequestWithCount {
let count: UInt32
let placementId: String
let iabContent: MozAdsIABContent?
}
```
```javascript
/**
* @typedef {Object} MozAdsPlacementRequest
* @property {string} placementId
* @property {MozAdsIABContent|null} iabContent
*/
/**
* @typedef {Object} MozAdsPlacementRequestWithCount
* @property {number} count - Number of spoc ads to request for this placement.
* @property {string} placementId
* @property {MozAdsIABContent|null} iabContent
*/
```
</details>
### Target by content
A placement can carry the IAB category of the page it sits on, in `iabContent`: a taxonomy version and one or more category ids from the [IAB Content Taxonomy](https://www.iab.com/guidelines/content-taxonomy/).
<details>
<summary><code>MozAdsIABContent</code>, <code>MozAdsIABContentTaxonomy</code></summary>
```kotlin
data class MozAdsIABContent(
val taxonomy: MozAdsIABContentTaxonomy,
val categoryIds: List<String>
)
enum class MozAdsIABContentTaxonomy {
IAB1_0,
IAB2_0,
IAB2_1,
IAB2_2,
IAB3_0
}
```
```swift
struct MozAdsIABContent {
let taxonomy: MozAdsIABContentTaxonomy
let categoryIds: [String]
}
enum MozAdsIABContentTaxonomy {
case iab1_0
case iab2_0
case iab2_1
case iab2_2
case iab3_0
}
```
```javascript
/**
* @typedef {Object} MozAdsIABContent
* @property {MozAdsIABContentTaxonomy} taxonomy
* @property {string[]} categoryIds
*/
/**
* @enum {string}
*/
const MozAdsIABContentTaxonomy = {
Iab1_0: "Iab1_0",
Iab2_0: "Iab2_0",
Iab2_1: "Iab2_1",
Iab2_2: "Iab2_2",
Iab3_0: "Iab3_0"
};
```
</details>
> The generated bindings name enum variants differently per language: screaming snake-case in Kotlin, lower camel-case in Swift, and depending on the environment in JavaScript.
### Request options
Each request method takes an optional `MozAdsRequestOptions`:
| Option | What it does |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `flags` | Request-level flags forwarded verbatim as the `flags` object on the wire, e.g. `contextual_placement: true`. Empty: not sent. |
| `relevance` | Relevance scores calculated on the device. Empty: not sent. |
| `ohttp` | Send this request through OHTTP. Defaults to `false`. See [Keep requests private](#4-keep-requests-private-with-ohttp). |
| `cachePolicy` | How this request uses the cache. See [Cache responses](#5-cache-responses). |
<details>
<summary><code>MozAdsRequestOptions</code></summary>
```kotlin
data class MozAdsRequestOptions(
val cachePolicy: MozAdsRequestCachePolicy?,
val flags: Map<String, Boolean> = emptyMap(),
val ohttp: Boolean = false,
val relevance: Map<String, Double> = emptyMap()
)
```
```swift
struct MozAdsRequestOptions {
let cachePolicy: MozAdsRequestCachePolicy?
let flags: [String: Bool] // default: [:]
let ohttp: Bool // default: false
let relevance: [String: Double] // default: [:]
}
```
```javascript
/**
* @typedef {Object} MozAdsRequestOptions
* @property {MozAdsRequestCachePolicy|null} cachePolicy
* @property {Object.<string, boolean>} flags - default: {}
* @property {boolean} ohttp - default: false
* @property {Object.<string, number>} relevance - default: {}
*/
```
</details>
---
## 3. Show the ad, then report back
Every ad carries what you need to render it and where to send its events:
| Field | Tile | Image | Spoc | What it is |
| --------------------- | :--: | :---: | :--: | -------------------------------------------- |
| `url` | ✓ | ✓ | ✓ | Destination URL. |
| `imageUrl` | ✓ | ✓ | ✓ | Creative asset URL. |
| `format` | ✓ | ✓ | ✓ | Ad format, e.g. `"tile"`, `"skyscraper"`, `"spoc"`. |
| `blockKey` | ✓ | ✓ | ✓ | The block key generated for the advertiser. |
| `callbacks` | ✓ | ✓ | ✓ | Where to send impressions, clicks and reports. |
| `name` | ✓ | | | Tile name. |
| `altText` | | ✓ | | Alt text, if available. |
| `title`, `excerpt` | | | ✓ | Story title and excerpt. |
| `domain`, `sponsor` | | | ✓ | Domain of the story and sponsor name. |
| `sponsoredByOverride` | | | ✓ | Optional override for the sponsor name. |
| `caps` | | | ✓ | Frequency cap: a `capKey` and a `day`. |
| `ranking` | | | ✓ | `priority`, `personalizationModels` scores and an overall `itemScore`. |
<details>
<summary><code>MozAdsTile</code>, <code>MozAdsImage</code>, <code>MozAdsSpoc</code> and their parts</summary>
```kotlin
data class MozAdsTile(
val blockKey: String,
val callbacks: MozAdsCallbacks,
val format: String,
val imageUrl: String,
val name: String,
val url: String
)
data class MozAdsImage(
val altText: String?,
val blockKey: String,
val callbacks: MozAdsCallbacks,
val format: String,
val imageUrl: String,
val url: String
)
data class MozAdsSpoc(
val blockKey: String,
val callbacks: MozAdsCallbacks,
val caps: MozAdsSpocFrequencyCaps,
val domain: String,
val excerpt: String,
val format: String,
val imageUrl: String,
val ranking: MozAdsSpocRanking,
val sponsor: String,
val sponsoredByOverride: String?,
val title: String,
val url: String
)
data class MozAdsSpocFrequencyCaps(
val capKey: String,
val day: Int
)
data class MozAdsSpocRanking(
val priority: Int,
val personalizationModels: Map<String, Int>,
val itemScore: Double
)
```
```swift
struct MozAdsTile {
let blockKey: String
let callbacks: MozAdsCallbacks
let format: String
let imageUrl: String
let name: String
let url: String
}
struct MozAdsImage {
let altText: String?
let blockKey: String
let callbacks: MozAdsCallbacks
let format: String
let imageUrl: String
let url: String
}
struct MozAdsSpoc {
let blockKey: String
let callbacks: MozAdsCallbacks
let caps: MozAdsSpocFrequencyCaps
let domain: String
let excerpt: String
let format: String
let imageUrl: String
let ranking: MozAdsSpocRanking
let sponsor: String
let sponsoredByOverride: String?
let title: String
let url: String
}
struct MozAdsSpocFrequencyCaps {
let capKey: String
let day: UInt32
}
struct MozAdsSpocRanking {
let priority: UInt32
let personalizationModels: [String: UInt32]
let itemScore: Double
}
```
```javascript
/**
* @typedef {Object} MozAdsTile
* @property {string} blockKey
* @property {MozAdsCallbacks} callbacks
* @property {string} format
* @property {string} imageUrl
* @property {string} name
* @property {string} url
*/
/**
* @typedef {Object} MozAdsImage
* @property {string|null} altText
* @property {string} blockKey
* @property {MozAdsCallbacks} callbacks
* @property {string} format
* @property {string} imageUrl
* @property {string} url
*/
/**
* @typedef {Object} MozAdsSpoc
* @property {string} blockKey
* @property {MozAdsCallbacks} callbacks
* @property {MozAdsSpocFrequencyCaps} caps
* @property {string} domain
* @property {string} excerpt
* @property {string} format
* @property {string} imageUrl
* @property {MozAdsSpocRanking} ranking
* @property {string} sponsor
* @property {string|null} sponsoredByOverride
* @property {string} title
* @property {string} url
*/
/**
* @typedef {Object} MozAdsSpocFrequencyCaps
* @property {string} capKey
* @property {number} day
*/
/**
* @typedef {Object} MozAdsSpocRanking
* @property {number} priority
* @property {Object.<string, number>} personalizationModels
* @property {number} itemScore
*/
```
</details>
### Report impressions, clicks and reports
Send each event to the URL the ad came with, in `ad.callbacks`: an impression once the ad is seen on screen, a click when the user opens it, and a report, with a reason, when the user flags it. `report` can be absent.
```kotlin
client.recordImpression(ad.callbacks.impression)
client.recordClick(ad.callbacks.click)
ad.callbacks.report?.let { client.reportAd(it, MozAdsReportReason.NOT_INTERESTED) }
```
```swift
try client.recordImpression(impressionUrl: ad.callbacks.impression)
try client.recordClick(clickUrl: ad.callbacks.click)
if let report = ad.callbacks.report {
try client.reportAd(reportUrl: report, reason: .notInterested)
}
```
```javascript
client.recordImpression(ad.callbacks.impression);
client.recordClick(ad.callbacks.click);
if (ad.callbacks.report) client.reportAd(ad.callbacks.report, MozAdsReportReason.NotInterested);
```
The report reasons are: inappropriate, not interested, seen too many times, scam, misleading, offensive. Each callback method takes an optional `MozAdsCallbackOptions`, whose only option is `ohttp` (next section).
<details>
<summary><code>MozAdsCallbacks</code>, <code>MozAdsReportReason</code>, <code>MozAdsCallbackOptions</code></summary>
```kotlin
data class MozAdsCallbacks(
val click: String,
val impression: String,
val report: String?
)
enum class MozAdsReportReason {
INAPPROPRIATE,
NOT_INTERESTED,
SEEN_TOO_MANY_TIMES,
SCAM,
MISLEADING,
OFFENSIVE
}
data class MozAdsCallbackOptions(
val ohttp: Boolean = false
)
```
```swift
struct MozAdsCallbacks {
let click: String
let impression: String
let report: String?
}
enum MozAdsReportReason {
case inappropriate
case notInterested
case seenTooManyTimes
case scam
case misleading
case offensive
}
struct MozAdsCallbackOptions {
let ohttp: Bool // default: false
}
```
```javascript
/**
* @typedef {Object} MozAdsCallbacks
* @property {string} click
* @property {string} impression
* @property {string|null} report
*/
/**
* @enum {string}
*/
const MozAdsReportReason = {
Inappropriate: "Inappropriate",
NotInterested: "NotInterested",
SeenTooManyTimes: "SeenTooManyTimes",
Scam: "Scam",
Misleading: "Misleading",
Offensive: "Offensive"
};
/**
* @typedef {Object} MozAdsCallbackOptions
* @property {boolean} ohttp - default: false
*/
```
</details>
---
## 4. Keep requests private with OHTTP
Set `ohttp: true` on a request or a callback to send it through Oblivious HTTP: a relay sees who asked but not what, and MARS sees what was asked but not who.
```kotlin
// Request ads over OHTTP
val ads = client.requestTileAds(placements, MozAdsRequestOptions(ohttp = true))
// Record a click over OHTTP
client.recordClick(ad.callbacks.click, MozAdsCallbackOptions(ohttp = true))
// Record an impression over OHTTP
client.recordImpression(ad.callbacks.impression, MozAdsCallbackOptions(ohttp = true))
```
```swift
// Request ads over OHTTP
let ads = try client.requestTileAds(mozAdRequests: placements, options: MozAdsRequestOptions(ohttp: true))
// Record a click over OHTTP
try client.recordClick(clickUrl: ad.callbacks.click, options: MozAdsCallbackOptions(ohttp: true))
// Record an impression over OHTTP
try client.recordImpression(impressionUrl: ad.callbacks.impression, options: MozAdsCallbackOptions(ohttp: true))
```
```javascript
// Request ads over OHTTP
const ads = client.requestTileAds(placements, { ohttp: true });
// Record a click over OHTTP
client.recordClick(ad.callbacks.click, { ohttp: true });
// Record an impression over OHTTP
client.recordImpression(ad.callbacks.impression, { ohttp: true });
```
> OHTTP must be configured at the viaduct level before use. When `ohttp` is `true`, the client first makes a direct preflight request for the geo-location and user-agent headers, then injects them into the MARS request. If the preflight returns no user agent, the client sends the builder's `userAgent`, else viaduct's global default user agent, else the request fails.
---
## 5. Cache responses
The client can keep MARS responses in a SQLite-backed HTTP cache, which saves network traffic and latency on repeated requests. Give the builder a `MozAdsCacheConfig` to turn it on:
| Field | What it is |
| ------------------------ | ---------------------------------------------------------------------------------- |
| `dbPath` | Path to the SQLite file. Required, and the client must be allowed to write to it. |
| `defaultCacheTtlSeconds` | Default time to live of an entry. Defaults to 300 seconds (5 minutes). |
| `maxSizeMib` | Maximum cache size. Defaults to 10 MiB. |
```kotlin
val cache = MozAdsCacheConfig(
dbPath = "/tmp/ads_cache.sqlite",
defaultCacheTtlSeconds = 600L, // 10 min
maxSizeMib = 20L // 20 MiB
)
val client = MozAdsClientBuilder()
.cacheConfig(cache)
.build()
```
```swift
let cache = MozAdsCacheConfig(
dbPath: "/tmp/ads_cache.sqlite",
defaultCacheTtlSeconds: 600, // 10 min
maxSizeMib: 20 // 20 MiB
)
let client = MozAdsClientBuilder()
.cacheConfig(cacheConfig: cache)
.build()
```
```javascript
const cache = MozAdsCacheConfig({
dbPath: "/tmp/ads_cache.sqlite",
defaultCacheTtlSeconds: 600, // 10 min
maxSizeMib: 20 // 20 MiB
});
const client = MozAdsClientBuilder()
.cacheConfig(cache)
.build();
```
<details>
<summary><code>MozAdsCacheConfig</code></summary>
```kotlin
data class MozAdsCacheConfig(
val dbPath: String,
val defaultCacheTtlSeconds: Long?,
val maxSizeMib: Long?
)
```
```swift
struct MozAdsCacheConfig {
let dbPath: String
let defaultCacheTtlSeconds: UInt64?
let maxSizeMib: UInt64?
}
```
```javascript
/**
* @typedef {Object} MozAdsCacheConfig
* @property {string} dbPath
* @property {number|null} defaultCacheTtlSeconds
* @property {number|null} maxSizeMib
*/
```
</details>
### Per request
By default a request is answered from the cache when it can be (cache first), with the client's default time to live. A `cachePolicy` in `MozAdsRequestOptions` changes both for one request:
| Field | What it does |
| ------------ | ------------------------------------------------------------------------------------------------------------- |
| `mode` | Cache first: return a cached response if there is one, otherwise fetch and store. Network first: always fetch, then store. |
| `ttlSeconds` | Time to live for this response. Not set: the client default. `0`: do not cache it. |
```kotlin
// Always fetch from network but only cache for 60 seconds
val options = MozAdsRequestOptions(
cachePolicy = MozAdsRequestCachePolicy(mode = MozAdsCacheMode.NETWORK_FIRST, ttlSeconds = 60L)
)
val placements = client.requestImageAds(configs, options = options)
```
```swift
// Always fetch from network but only cache for 60 seconds
let options = MozAdsRequestOptions(
cachePolicy: MozAdsRequestCachePolicy(mode: .networkFirst, ttlSeconds: 60)
)
let placements = try client.requestImageAds(mozAdRequests: configs, options: options)
```
```javascript
// Always fetch from network but only cache for 60 seconds
const options = MozAdsRequestOptions({
cachePolicy: MozAdsRequestCachePolicy({ mode: MozAdsCacheMode.NetworkFirst, ttlSeconds: 60 })
});
const placements = client.requestImageAds(configs, options);
```
<details>
<summary><code>MozAdsRequestCachePolicy</code>, <code>MozAdsCacheMode</code></summary>
```kotlin
data class MozAdsRequestCachePolicy(
val mode: MozAdsCacheMode,
val ttlSeconds: Long?
)
enum class MozAdsCacheMode {
CACHE_FIRST,
NETWORK_FIRST
}
```
```swift
struct MozAdsRequestCachePolicy {
let mode: MozAdsCacheMode
let ttlSeconds: UInt64?
}
enum MozAdsCacheMode {
case cacheFirst
case networkFirst
}
```
```javascript
/**
* @typedef {Object} MozAdsRequestCachePolicy
* @property {MozAdsCacheMode} mode
* @property {number|null} ttlSeconds
*/
/**
* @enum {string}
*/
const MozAdsCacheMode = {
CacheFirst: "CacheFirst",
NetworkFirst: "NetworkFirst"
};
```
</details>
### How long an entry lives
Each response is stored with an effective time to live, taken from the first of:
1. the request's `ttlSeconds` (`MozAdsRequestCachePolicy`);
2. the response's `Cache-Control: max-age=N` header;
3. the client's `defaultCacheTtlSeconds` (`MozAdsCacheConfig`).
A time to live of 0 seconds (e.g. `Cache-Control: max-age=0`) means the response is not cached. Whatever its source, it is capped at 7 days.
Entries leave the cache three ways:
- **Expiry (automatic):** at the start of each send, the cache deletes rows older than the time to live. This is a coarse, global freshness window.
- **Size (automatic):** after storing a new response, the cache deletes the oldest rows until it is back under `maxSizeMib`. With small entries and short lifetimes this should be rare.
- **Manually:** `client.clearCache()` deletes everything.
---
## 6. Record telemetry
To get the client's metrics, give the builder an implementation of `MozAdsTelemetry` that forwards each call to your telemetry system. Telemetry is process-wide and meant to be set up once: building a client with a telemetry instance makes it the destination for metrics from every client in the process, replacing any instance installed before it. It is dropped when any client shuts down. Until an instance is installed, no metrics are recorded.
```kotlin
import mozilla.appservices.adsclient.MozAdsTelemetry
import org.mozilla.appservices.ads_client.GleanMetrics.AdsClient
class AdsClientTelemetry : MozAdsTelemetry {
override fun recordBuildCacheError(label: String, value: String) {
AdsClient.buildCacheError[label].set(value)
}
override fun recordClientError(label: String, value: String) {
AdsClient.clientError[label].set(value)
}
override fun recordClientOperationTotal(label: String) {
AdsClient.clientOperationTotal[label].add()
}
override fun recordDeserializationError(label: String, value: String) {
AdsClient.deserializationError[label].set(value)
}
override fun recordHttpCacheOutcome(label: String, value: String) {
AdsClient.httpCacheOutcome[label].set(value)
}
}
```
```swift
import MozillaRustComponents
import Glean
public final class AdsClientTelemetry: MozAdsTelemetry {
public func recordBuildCacheError(label: String, value: String) {
AdsClientMetrics.buildCacheError[label].set(value)
}
public func recordClientError(label: String, value: String) {
AdsClientMetrics.clientError[label].set(value)
}
public func recordClientOperationTotal(label: String) {
AdsClientMetrics.clientOperationTotal[label].add()
}
public func recordDeserializationError(label: String, value: String) {
AdsClientMetrics.deserializationError[label].set(value)
}
public func recordHttpCacheOutcome(label: String, value: String) {
AdsClientMetrics.httpCacheOutcome[label].set(value)
}
}
```
```javascript
class AdsClientTelemetry {
recordBuildCacheError(label, value) {
// Bind to your telemetry system
}
recordClientError(label, value) {
// Bind to your telemetry system
}
recordClientOperationTotal(label) {
// Bind to your telemetry system
}
recordDeserializationError(label, value) {
// Bind to your telemetry system
}
recordHttpCacheOutcome(label, value) {
// Bind to your telemetry system
}
}
```
<details>
<summary><code>MozAdsTelemetry</code></summary>
```kotlin
interface MozAdsTelemetry {
fun recordBuildCacheError(label: String, value: String)
fun recordClientError(label: String, value: String)
fun recordClientOperationTotal(label: String)
fun recordDeserializationError(label: String, value: String)
fun recordHttpCacheOutcome(label: String, value: String)
}
```
```swift
protocol MozAdsTelemetry {
func recordBuildCacheError(label: String, value: String)
func recordClientError(label: String, value: String)
func recordClientOperationTotal(label: String)
func recordDeserializationError(label: String, value: String)
func recordHttpCacheOutcome(label: String, value: String)
}
```
```javascript
/**
* @typedef {Object} MozAdsTelemetry
* @property {function(string, string): void} recordBuildCacheError
* @property {function(string, string): void} recordClientError
* @property {function(string): void} recordClientOperationTotal
* @property {function(string, string): void} recordDeserializationError
* @property {function(string, string): void} recordHttpCacheOutcome
*/
```
</details>
---
## Method reference
All of them are on `MozAdsClient`, and all of them throw on failure.
```kotlin
fun clearCache()
fun recordClick(clickUrl: String, options: MozAdsCallbackOptions? = null)
fun recordImpression(impressionUrl: String, options: MozAdsCallbackOptions? = null)
fun reportAd(reportUrl: String, reason: MozAdsReportReason, options: MozAdsCallbackOptions? = null)
fun requestImageAds(mozAdRequests: List<MozAdsPlacementRequest>, options: MozAdsRequestOptions? = null): Map<String, MozAdsImage>
fun requestSpocAds(mozAdRequests: List<MozAdsPlacementRequestWithCount>, options: MozAdsRequestOptions? = null): Map<String, List<MozAdsSpoc>>
fun requestTileAds(mozAdRequests: List<MozAdsPlacementRequest>, options: MozAdsRequestOptions? = null): Map<String, MozAdsTile>
```
```swift
func clearCache() throws
func recordClick(clickUrl: String, options: MozAdsCallbackOptions? = nil) throws
func recordImpression(impressionUrl: String, options: MozAdsCallbackOptions? = nil) throws
func reportAd(reportUrl: String, reason: MozAdsReportReason, options: MozAdsCallbackOptions? = nil) throws
func requestImageAds(mozAdRequests: [MozAdsPlacementRequest], options: MozAdsRequestOptions? = nil) throws -> [String: MozAdsImage]
func requestSpocAds(mozAdRequests: [MozAdsPlacementRequestWithCount], options: MozAdsRequestOptions? = nil) throws -> [String: [MozAdsSpoc]]
func requestTileAds(mozAdRequests: [MozAdsPlacementRequest], options: MozAdsRequestOptions? = nil) throws -> [String: MozAdsTile]
```
```javascript
client.clearCache() // → void
client.recordClick(clickUrl, options?) // → void
client.recordImpression(impressionUrl, options?) // → void
client.reportAd(reportUrl, reason, options?) // → void
client.requestImageAds(mozAdRequests, options?) // → Object.<string, MozAdsImage>
client.requestSpocAds(mozAdRequests, options?) // → Object.<string, Array.<MozAdsSpoc>>
client.requestTileAds(mozAdRequests, options?) // → Object.<string, MozAdsTile>
```
## Type index
| Type | Where |
| --------------------------------- | ------------------------------------------------------------- |
| `MozAdsCacheConfig` | [Cache responses](#5-cache-responses) |
| `MozAdsCacheMode` | [Cache responses › Per request](#per-request) |
| `MozAdsCallbackOptions` | [Report impressions, clicks and reports](#report-impressions-clicks-and-reports) |
| `MozAdsCallbacks` | [Report impressions, clicks and reports](#report-impressions-clicks-and-reports) |
| `MozAdsClient` | [Create the client](#1-create-the-client), [Method reference](#method-reference) |
| `MozAdsClientBuilder` | [Create the client](#1-create-the-client) |
| `MozAdsEnvironment` | [Create the client](#1-create-the-client) |
| `MozAdsIABContent` | [Target by content](#target-by-content) |
| `MozAdsIABContentTaxonomy` | [Target by content](#target-by-content) |
| `MozAdsImage` | [Show the ad](#3-show-the-ad-then-report-back) |
| `MozAdsPlacementRequest` | [Request ads](#2-request-ads) |
| `MozAdsPlacementRequestWithCount` | [Request ads](#2-request-ads) |
| `MozAdsReportReason` | [Report impressions, clicks and reports](#report-impressions-clicks-and-reports) |
| `MozAdsRequestCachePolicy` | [Cache responses › Per request](#per-request) |
| `MozAdsRequestOptions` | [Request options](#request-options) |
| `MozAdsSpoc` | [Show the ad](#3-show-the-ad-then-report-back) |
| `MozAdsSpocFrequencyCaps` | [Show the ad](#3-show-the-ad-then-report-back) |
| `MozAdsSpocRanking` | [Show the ad](#3-show-the-ad-then-report-back) |
| `MozAdsTelemetry` | [Record telemetry](#6-record-telemetry) |
| `MozAdsTile` | [Show the ad](#3-show-the-ad-then-report-back) |