Programmatic classification
The Uniform tracker is responsible for classifying the visitor based on their behavior.
The Uniform tracker#
You need to add the tracker to your front-end application and be able to get a reference to it when you need to use it.
Add the tracker#
The Context type represents the Uniform tracker. The tracker must be added to the front-end application before any classification can happen.
Add the following packages to your app:
@uniformdev/context @uniformdev/context-reactAdd the following code to the
_app.js
file:import { UniformContext } from '@uniformdev/context-react'; import { Context } from '@uniformdev/context'; const context = new Context({ consent: true, manifest: { project: {}, }, }); function MyApp({ Component, pageProps }) { return ( <UniformContext context={context}> <Component {...pageProps} /> </UniformContext> ); }info
This example includes a hard-coded manifest. Your code should use the manifest for your Uniform project. For information on how to retrieve the manifest, see the manifest guide.
Get reference to the tracker#
Before you can use the tracker, you must get a reference to it.
To get a reference to the tracker from inside a React component, the hook useUniformContext
is used.
Write tracker data#
The Uniform SDK provides the ability for you to add logic to your application that explicitly captures visitor activity for the purpose of classifying the visitor.
Enrichments#
If you are using Uniform Canvas, any enrichments that are assigned to a component are tracked automatically. However, if you aren't using Canvas, you must track enrichments in your application.
You can use the Uniform tracker to track enrichments. The following example demonstrates how to add a button that can track enrichments.
tip
This code requires the manifest has an enrichment category colorPreference
with an enrichment value red
.
You can also use front-end specific components to track enrichments.
You can use a React hook to track enrichments.
info
This code requires the manifest has an enrichment category colorPreference
with an enrichment value red
.
Events#
An event can trigger a signal. You can use the Uniform tracker to write events. The following example demonstrates how to add a button that can write events.
Quirks#
A quirk can trigger a signal. You can use the Uniform tracker to write quirk values.
Client-side#
The following example demonstrates how to add a button that can write quirks.
Server-side#
You can set quirks in code that runs during server-side rendering.
Add the following file to the root of your application:
createUniformContext.js
import { Context, enableContextDevTools } from "@uniformdev/context"; import { NextCookieTransitionDataStore } from "@uniformdev/context-next"; import manifest from "./contextManifest.json"; export function createUniformContext(serverContext) { return new Context({ defaultConsent: true, manifest, transitionStore: new NextCookieTransitionDataStore({ serverContext, }), plugins: [enableContextDevTools()], }); }Add the following to your
_document.js
or_document.tsx
file:import Document, { Html, Head, Main, NextScript } from "next/document"; import { enableNextSsr } from "@uniformdev/context-next"; import { createUniformContext } from "../createUniformContext"; class MyDocument extends Document { static async getInitialProps(ctx) { const context = createUniformContext(ctx); await context.update({ quirks: { location: "YOUR LOCATION", }, }); enableNextSsr(ctx, context); return await Document.getInitialProps(ctx); } render() { return ( <Html lang="en"> <Head /> <body> <Main /> <NextScript /> </body> </Html> ); } } export default MyDocument;About this step
This code demonstrates how to set the value of a quirk named
location
.Add the following to your
_app.js
or_app.tsx
file:import { UniformContext } from "@uniformdev/context-react"; import { createUniformContext } from "../createUniformContext"; export default function App(props) { const { Component, pageProps, serverUniformContext } = props; const context = createUniformContext(serverUniformContext); return ( <UniformContext context={serverUniformContext ?? context}> <Component {...pageProps} /> </UniformContext> ); }
Read tracker data#
The Uniform tracker enables you to read the classification data for the current visitor.
Quirks#
You can read the quirks that are currently set on the visitor.
You can use a React hook to read the quirks currently set on the visitor. This example creates a component that displays the visitor's current quirks.
Scores#
You can read the visitor dimension (audiences, enrichments, intents, and signals) scores that are current set on the visitor.
You can use a React hook to read the scores currently set on the visitor. This example creates a component that displays the visitor's current scores.
Handle tracker events#
You can define event handlers for the events that are emitted from the Uniform tracker during the classification process.
Quirks updated#
Coming soon
Scores updated#
Coming soon
Set score decay#
The Uniform tracker automatically reduces visitor dimension scores over time through a process called decay. See the score decay guide for information on how to control score decay.
Clear tracker data#
This example demonstrates how to implement a "forget me" button.