Plugins
Capabilities can be added to the Uniform tracker using plugins. Plugins are added to the Uniform context object.
Standard plugins
Uniform comes with the following plugins. These plugins are not enabled by default. You must enable them by adding the required plugins to the Uniform context object.
Context DevTools
The Context DevTools plugin causes the Uniform tracker to expose visitor classification data that the Context DevTools can display. For information on how to configure and use this plugin, see the Context DevTools guide.
Add the following import at the top of the file:
import { enableContextDevTools } from "@uniformdev/context";
Find the code where the Uniform context object is created. It will look something like the following:
const context = new Context({
manifest,
});Add the following code:
const context = new Context({
manifest,
plugins: [
enableContextDevTools(),
]
});
Logging
The console log plugin causes the Uniform tracker to write to the console.
Add the following import at the top of the file:
import { createConsoleLogDrain } from "@uniformdev/context";
Find the code where the Uniform context object is created. It will look something like the following:
const context = new Context({
manifest,
});Add the following code:
const context = new Context({
manifest,
plugins: [
createConsoleLogDrain("debug"),
]
});About this stepThe example above sets the logging output severity level to
debug
. For information on other severity levels, see the output severity reference.
Custom plugins
The Uniform tracker emits events at various points as it controls classification, personalization, and other processes. You can assign listeners directly to the tracker to handle these events, but custom plugins offer a more reusable option.
This section provides examples of custom plugins.
Visitor scores
This example demonstrates how visitor scores can be sent
to a third-party API. In this example, the Uniform tracker
maintains an enrichment role
. Depending on the content the
visitor views, they may have scores for multiple roles. This
plug-in sends the name of the role with highest score to the
third-party API.
Find the code where the Uniform context object is created. It will look something like the following:
const context = new Context({
manifest,
});Add the following code to the file where the Uniform context object is created:
function enableSendScores() {
return {
init: (context) => {
const isRoleEnrichment = (key) => key.split("_")[0] == "role";
const getRoleName = (key) => key.split("_")[1];
//
//Returns the name of the role with the largest score.
const getRole = (scores) => {
let role = undefined;
Object.keys(scores).forEach(key => {
if (!isRoleEnrichment(key)) return;
const score = scores[key];
if (score > 0 && !role) {
role = key;
}
else if (score > scores[role]) {
role = key;
}
});
if (!role) return;
return getRoleName(role);
}
//
//Listen for the context event.
context.events.on("scoresUpdated", (scores) => {
const role = getRole(scores);
if (!role) return;
//
//TODO: make API call using the role value.
});
},
}
}About this stepBy convention, a function is used to define the plugin. For information on the contract for a Context plugin, see the ContextPlugin reference.
Add the following code:
const context = new Context({
manifest,
plugins: [
enableSendScores(),
]
});