Skip to main content

@uniformdev/context

This package provides the core functionality for Uniform's classification, personalization, and testing capabilities.

Functions

createLinearDecay

Creates a linear decay function that can be assigned to the Context. Linear decay involves reducing a score by the same amount each day the decay runs.

The following describes how decay works over time and with continuing engagement. Consider the following settings:

  • Scores will decay by 10% each day (decay rate).
  • The most a score will decay in a day is 95% (decay cap).
  • Decay will start after 1 day (grace period).
DayNew activityDecayTotal scoreNotes
11000100No decay due to grace period.
201090Decay is 10% of the previous day's score (100).
30981Decay is 10% of the previous day's score (90).
4208.192.9Decay is 10% of the previous day's score (81). Decay does not apply to the new activity score.
509.2983.61Decay is 10% of the previous day's score (92.9).
- Arguments
optionsLinearDecayOptions (optional)
Options that control how linear decay works.
+ show child attributes
- Return
DecayFunctionThe decay function.
Example
import { 
Context,
createLinearDecay,
} from '@uniformdev/context';

const manifest = { project: {} };
const options = { decayRate: 0.1 };

const context = new Context({
manifest,
consent: true,
decay: createLinearDecay(options),
});
info

For more information, see the linear decay activation guide.

enableConsoleLogDrain

Creates a plugin that writes log messages to the console.
- Arguments
The logger will only write messages at or greater than this level of severity.
optionsConsoleDebugLogDrainOptions (optional)
Additional settings that control the logger.
+ show child attributes
- Return
ContextPluginThe plugin.
Example
import { 
Context,
enableDebugConsoleLogDrain,
} from '@uniformdev/context';

const manifest = { project: {} };
const level = "debug";
const options = { enableOnServer: false };

const context = new Context({
manifest,
consent: true,
plugins: [
createConsoleLogDrain(level, options),
],
});
info

For more information, see the console log plugin guide.

enableContextDevTools

Creates a plugin that causes the Context to expose visitor classification data that the Context DevTools can display.
- Arguments
None
- Return
ContextPluginThe plugin.
Example
import { Context, enableContextDevTools } from '@uniformdev/context';

const manifest = { project: {} };

const context = new Context({
manifest,
consent: true,
plugins: [
enableContextDevTools(),
],
});
info

For more information, see the Context DevTools plugin guide.

getEnrichmentVectorKey

Generates the name of an enrichment key based on the enrichment category and enrichment value. This key is needed if you are writing enrichment values programmatically.
- Arguments
categorystring
The enrichment category id.
valuestring
The enrichment value id.
- Return
stringThe name of the enrichment key.

Types

Context

Represents the Uniform tracker.
- Events
log
Emitted when a log message is emitted from the Context.
personalizationResult
Emitted when personalization variants have been selected.
+ show event data members
For more information, see example in our developer guide.
quirksUpdated

Emitted when quirk data changes.

  • The event is not fired if an update does not result in at least one change to a quirk value.

Each key on the event object is a string that identifies the name of the quirk. The value for each key is a string.

For more information, see example in our developer guide.
scoresUpdated

Emitted when the scores are updated.

  • The event is not fired if an update does not result in any score changes.
  • The result is merged between session and permanent data.

Each key on the event object is a string that identifies the name of the visitor dimension. The value for each key is a number.

For more information, see example in our developer guide.
testResult
Emitted when an A/B test runs.
+ show event data members
For more information, see example in our developer guide.
- Members
eventsobject (read-only)
Object used to add and remove listeners for the events emitted by the Context.
+ show child attributes
forget
getServerToClientTransitionState
log
manifest
personalize
quirks
Gets the current visitor's quirks values.
scores
Gets the current visitor's dimension score vector.
storageVisitorDataStore (read-only)
test(options)
Executes an A/B test with a given set of variants, showing the visitor's assigned variant (or selecting one to assign, if none is set yet).
  • optionsTestOptions - Options that control how the A/B testing process runs.
    + show child attributes
Returns: void
update(newData)
Updates the Context with new data of any sort, such as new URLs, cookies, quirks, and enrichments.
  • newDataVisitorData - New data used to updated the Context.
    + show child attributes
Returns: void

ContextOptions

Represents options that can be specified when creating the Context.
- Members
decayDecayFunction (optional)
Allows decaying of scores over time based on time between visits. If not specified, scores do not decay over time.
defaultConsentboolean (optional)
By default, the Context stores visitor activity in-memory, only. This activity data is lost when the browser leaves the page. When set to true, visitor activity is stored in the visitor's browser.
For more information, see our compliance and consent guide.
manifestManifestV2
Settings the Context uses to perform processes such as classification and testing.
onLogMessage(message)
Called when a log message is emitted from the data store.
Returns: void
onServerTransitionReceived(state)

Called when server-to-client transfer state is loaded and contains server-side computed scores. These scores are used as a temporary shim for the current scores on the client side until score computation is completed the first time (which occurs when the current url is fed into the tracker).

Because the feed of the URL may be marginally delayed (for example in React it is in an effect so it is a second render), one render might be done with no scores unless we dropped the server scores in temporarily, resulting in a flash of unpersonalized content.

  • stateServerToClientTransitionState
Returns: void
transitionDataStoreTransitionDataStore (optional)
Used to transfer server or edge-side execution (e.g. personalization) state to the client. This member is not applicable when client-side execution is used.
visitLifespannumber (optional)
Duration of a visit. The Context determines a new visit has started when this number of milliseconds has elapsed without performing any updates.

ContextPlugin

Represents a plugin for the Context.
- Members
logDrainLogDrain (optional)
The component responsible for handling logging events emitted by the Context.
init(context)
Initializes the plugin, usually by assigning event handlers to the Context.
  • contextContext - The tracker to which the plugin is being initialized for.
Returns: void
info

For information on how to create your own plugin, see the custom plugins guide.

DecayFunction

The function that implements the score decay process.
- Parameters
optionsDecayOptions
Options that are passed to the decay function when the decay process is started.
+ show child attributes
- Return
booleanIf any decay was applied, true is returned. If no decay was applied, false is returned.
info

For information on how to create your own decay function, see the custom decay functions guide.

LogDrain

Represents a component that can handle logging messages (i.e. a logger).
- Arguments
The message or messages to write to the log.

LogMessageSingle

info

Coming soon.

LogMessageGroup

info

Coming soon.

ManifestV2

Represents the settings the Context uses to perform processes such as classification and testing.
- Members
projectobject
Project described by the manifest.
+ show child attributes
Example
{
  "project": {
    "pz": {
      "enr": {
        "1": {
          "cap": 100
        }
      }
    },
    "test": {
      "firstTest": {}
    }
  }
}

OutputSeverity

When a logger is created, an output severity level is assigned. The logger only outputs messages written at least as severe as this the severity level on the logger.
- Options
none
No messages will be logged. This effectively disables logging.
debug
All messages will be logged.
info
Messages with the severity of info, warn or error will be logged.
warn
Messages with the severity of warn or error will be logged.
error
Messages with the severity of error will be logged.

Severity

Each message that is sent to the logger has a severity level. Whether the logger writes the message to the log depends on the output severity assigned to the logger.

- Options
debug
Use to capture very precise details that can be helpful during debugging or troubleshooting.
info
Use to capture meaningful but expected activity.
warn
Use to capture activity that a developer should pay attention to.
error
Use to capture details about something that is preventing or will prevent the system from working as expected.

VisitorDataStore

Provides access to storage for visitor data.
- Events
consentUpdated
Emitted when storage consent is changed.
+ show event data members
controlGroupUpdated
Emitted when visitor control group membership is changed.
+ show event data members
quirksUpdated

Emitted when quirk data changes.

  • The event is not fired if an update does not result in at least one change to a quirk value.
+ show event data members
scoresUpdated

Emitted when the scores are updated.

  • The event is not fired if an update does not result in any score changes.
  • This is fired for any update, whether from integrated or transition storage.
+ show event data members
testsUpdated
Fired when test variant selection is updated.
+ show event data members
- Members
data
decayEnabled
delete
eventsobject (read-only)
Object used to add and remove listeners for the events emitted by the VisitorDataStore.
+ show child attributes
options
updateData