Shows how to call ctxSegments() to classify a page URL against one or more contextual
taxonomies (e.g. the
IAB Content Taxonomy) and inspect the
categories the DCN returns for it.
// Classify the URL of the current page (defaults to window.location.href):
optable.instance.ctxSegments();
// Or classify an explicit URL:
optable.instance.ctxSegments("https://optable.co/");
The response is a ContextualSegmentsResponse of the form
{ classifications: { categories: [{ id, name, score, taxonomy }] } }.
Alternatively, configure the SDK with initContextual set to a callback. The SDK will
automatically call ctxSegments() for the URL of the current page on initialization, and
invoke the callback with the response as soon as it's available — no second call required:
optable.instance = new optable.SDK({
host: "ca.edge.optable.co",
site: "web-sdk-demo",
node: "optable",
initContextual: function (response) {
// Use the response, or read it later via optable.instance.ctxTargetingKeyValues().
console.log("contextual segments:", response);
},
});
Note: the URL requested must have been classified by the DCN. For the demo DCN used by
this page, the URL https://optable.co/ should have been classified, so you can try that.
Derived from the cached ctxSegments() response via
optable.instance.ctxTargetingKeyValues(), this object can be passed straight to Google Ad
Manager via googletag.pubads().setTargeting(key, values):
var loadGAM = function (tdata = {}) {
window.googletag = window.googletag || { cmd: [] };
googletag.cmd.push(function () {
for (const [key, values] of Object.entries(tdata)) {
googletag.pubads().setTargeting(key, values);
}
googletag.pubads().refresh();
});
};
ctxTargetingKeyValues() reads the response cached on the SDK instance, so the instance should
be initialized with the initContextual: true option. That way the contextual segments are
fetched during initialization, and the cache is likely to be populated by the time loadGAM()
runs:
loadGAM(optable.instance.ctxTargetingKeyValues());
If you want loadGAM() to run as soon as the contextual segments arrive — without making a
second ctxSegments() call — pass a callback to initContextual. The SDK fires the
contextual request automatically during initialization and invokes the callback with the response,
populating the cache before ctxTargetingKeyValues() reads from it:
optable.instance = new optable.SDK({
host: "ca.edge.optable.co",
site: "web-sdk-demo",
node: "optable",
initContextual: function (response) {
loadGAM(optable.instance.ctxTargetingKeyValues());
},
});
If you are not using initContextual at all, fetch the segments explicitly and pass the result
to loadGAM() once ctxSegments() resolves (falling back to an untargeted load on
error):
optable.cmd.push(function () {
optable.instance
.ctxSegments()
.then(loadGAM)
.catch((err) => {
loadGAM();
});
});
By default each taxonomy is emitted under its own value as the GAM key. Pass a map to
ctxTargetingKeyValues() to rename keys and allow-list which taxonomies are emitted — only
taxonomies present in the map are included:
// Emit only the "iab_ct_3_1" taxonomy, under the GAM key "ctx_iab":
loadGAM(optable.instance.ctxTargetingKeyValues({ iab_ct_3_1: "ctx_iab" }));