Tracking User Identity
Objectiv enables you to easily configure a user's identity during modeling with any identifier (e.g. a unique internal hashed ID, or an email address), and apply that to all users' behavior retroactively. This also allows you to track their behavior across sessions, platforms, devices, etc.
To enable this, the Tracker should be instructed to track the user identity explicitly in an IdentityContext.
All that's required is to track at least 1 event within the session that carries the
IdentityContext, e.g. on tracker
initialization or login; see the examples below. On all platforms there’s an IdentityContextPlugin
to
facilitate adding the IdentityContext.
IdentityContextPlugin example
In this example we assume to have a cookie called backend_user
holding an identifier of the currently logged-in user.
import { DebugTransport } from "@objectiv/transport-debug";
import { IdentityContextPlugin } from "@objectiv/plugin-identity-context";
import { ReactTracker } from "@objectiv/tracker-react";
import Cookies from 'js-cookie';
const userId = Cookies.get('backend_user');
const tracker = new ReactTracker({
applicationId: 'app-id',
transport: new DebugTransport(),
plugins: [
new IdentityContextPlugin({
id: 'backend',
value: userId
})
],
});
makeIdentityContext example
In this example we append the identity of the logged-in user when triggering a success event from a hypothetical Login button.
import { makeIdentityContext } from "@objectiv/tracker-core";
import { useSuccessEventTracker } from "@objectiv/tracker-react";
const trackSuccessEvent = useSuccessEventTracker();
trackSuccessEvent({
message: 'Logged in successfully',
globalContexts: [
new makeIdentityContext({
id: 'authentication',
value: response.user_id
})
]
});