Golden Signals
Read ContextSDK's on-device Golden Signals to enrich ad requests, either instantly or after waiting for a full sensor window.
Golden Signals are a snapshot of the device's current context - motion, device state, and the outputs of ContextSDK's on-device models - returned as a plain dictionary you can attach to an ad request. Everything is computed on device.
Setting up the AdTech build on Android
The Android AdTech build is a separate artifact, com.contextsdk:contextsdk-adtech, served from the same Maven repository as the standard SDK (com.contextsdk:contextsdk):
Add the ContextSDK Maven repository in your project-level settings.gradle.kts:
dependencyResolutionManagement {
repositories {
// …
// Add the ContextSDK Maven repo:
maven {
url = uri("https://storage.googleapis.com/fc4073e9-contextsdk-maven/")
}
}
}Add the ContextSDK AdTech dependency in your module-level build.gradle.kts:
dependencies {
// …
implementation("com.contextsdk:contextsdk-adtech:<version>")
}Add the ContextSDK Maven repository in your project-level settings.gradle:
dependencyResolutionManagement {
repositories {
// …
// Add the ContextSDK maven repo:
maven {
url 'https://storage.googleapis.com/fc4073e9-contextsdk-maven/'
}
}
}Add the ContextSDK AdTech dependency in your module-level build.gradle:
dependencies {
// …
implementation 'com.contextsdk:contextsdk-adtech:<version>'
}Replace <version> with the latest AdTech release.
The AdTech build is set up with an API key rather than a license key. Call setupWithApiKey from your Application. ContextSDK then observes every activity in your app by itself and collects motion data while any of them is in the foreground, an ad network's full-screen ad included - there is nothing to call per activity:
Call it once. Only the first call takes effect: any later call logs a warning and is ignored, including its API key and configuration.
setupWithApiKey returns whether the API key is activated for your app. The key is verified online, so the value reflects the last check on this device, and is true until the first check completes. If a check fails, ContextSDK checks again on the next app start, for example after your app has been added to the key. A repeated call is ignored but still returns this value. It returns false when called outside your app's main process, and when setup failed.
Choosing between the two methods
There are two ways to read the signals, and the difference is only in how they treat the motion sensor:
goldenSignals()
Returns immediately, reading whatever the accelerometer has buffered at that instant.
fetchGoldenSignals()
Waits until a full sensor window has been collected, then calls you back.
The distinction matters because the accelerometer buffer starts out empty at every app launch and every return to the foreground, and takes a few seconds to fill. Ad requests tend to cluster in exactly that window - interstitial and rewarded preloads usually fire moments after launch or resume - so goldenSignals() called there returns a thinner dictionary, with motion-derived signals and any model outputs that depend on them missing or based on a partial window.
Use goldenSignals() when you cannot afford to wait at all. Use fetchGoldenSignals() when you would rather wait a moment for complete data - it is the better default for a bid request that isn't already at its deadline.
Reading the signals instantly
Every parameter is optional - see Tagging the request. From Java, pass all three, using null for an unused trigger or bidID and new CustomSignals() when you have no custom signals.
On Android, goldenSignals() runs synchronously on the calling thread, including the on-device models, so call it off the main thread if your bid path allows. If setupWithApiKey hasn't been called yet, it logs an error and returns an empty map.
Waiting for a full sensor window
fetchGoldenSignals() calls your callback once, on the main thread, with a status and the signals:
The status tells you how complete the sensor data behind the dictionary is:
.ok
OK
A full sensor window was collected. This is the case the method exists to guarantee.
.timeout
TIMEOUT
The call ran out of time before the sensor delivered a full window. The dictionary holds the instantly-available data instead - the same thing goldenSignals() would have returned.
.sensorUnavailable
SENSOR_UNAVAILABLE
The device has no usable motion sensor, or you're running in the iOS simulator or an Android emulator. Motion signals are flagged invalid and the model-driven results that depend on them are absent. On Android this status is reported straight away, without waiting for the timeout.
What a non-OK status does mean is that the dictionary is less complete. Anything derived from motion needs a usable window to be computed from, so on a timeout or an unavailable sensor the motion signals and the model-driven results that consume them (u_, m_ and mv_ keys) may be based on a partial window or missing altogether. Read keys defensively rather than assuming a fixed set - which is good practice anyway, since the exposed set is configured remotely.
The call is bounded: it gives up after twice the sensor window configured for your app - for example 6 seconds for a 3-second window - and reports a timeout, so it will never block an ad request indefinitely.
On Android, your callback is delivered on the main thread. A call made before setupWithApiKey logs an error and invokes your callback right away with SENSOR_UNAVAILABLE and an empty map, still on the main thread: immediately when you called from the main thread, otherwise posted to it.
Tagging the request
Both methods take the same optional parameters, and all of them are worth setting:
trigger- where in your flow you're reading the signals, for example"before_bid_request"or"before_ad_show". Use a distinct value per call site.bidID- your identifier for the bid. Setting it lets us connect the several contexts captured across one bid's lifecycle, which is what makes stage-by-stage analysis possible.customSignals- anything you know about this request that we can't measure on device. See Attaching custom signals.
The dictionary also comes back with a ctxId key: a short identifier for this specific context. Keep it alongside your own records if you want to join your reporting back to a single Golden Signals read.
Attaching custom signals
Both methods accept custom signals for that one call - the ad format you're about to request, how many impressions this session has already shown, whatever else describes the request:
These are layered on top of any global signals you've set - with setGlobalCustomSignal(id:value:) on iOS, or ContextSDK.globalCustomSignals on Android - and a per-call signal wins over a global one with the same id. See Custom Signals for the rules an id and value must follow, and for how to set globals.
They are used in two places: they're recorded on the context event we log for this call, so your bid data and ours can be joined on ctxId, and they're available to the on-device models as features - so a model trained on one of your custom signals can act on it. With fetchGoldenSignals() they're attached whatever the status, because they describe the bid request, not the sensor window.
What's in the dictionary
Keys are stable strings and values are heterogeneous, so read each key as the type that signal carries. On Android the value types are the Kotlin equivalents - Boolean, Int, Float, String, List<String> and List<Float> for signals, a Double or List<Float> for model outputs:
ctxId
A String identifying this context.
contextDuration
An Int: the sensor window length in seconds, when enabled for your app.
c<number>
An individual signal. The type follows the signal - Bool, Int, Float, Double, Decimal, String, [String] or [Float].
u_<name>
A Float probability from an on-device activity model. iOS only.
m_<name>
An on-device model output: a Double score, or a [Float] vector for a moment embedding.
mv_<name>
A String version of the model that produced the matching m_ embedding.
Keys that aren't available for a given call are omitted rather than returned empty, so check for a key's presence rather than assuming a fixed set. Which signals and model outputs are exposed is configured remotely per app, so the set can change without an app update.
On Android the map also iterates in a stable order: ctxId, contextDuration, the c signals, then the model outputs.
Last updated
Was this helpful?