Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to set the Network User Id (close #1236) #1245

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-tracker-core",
"comment": "Add setNetworkUserId() function",
"type": "none"
}
],
"packageName": "@snowplow/browser-tracker-core"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-tracker",
"comment": "Add setNetworkUserId() function",
"type": "none"
}
],
"packageName": "@snowplow/browser-tracker"
}
11 changes: 10 additions & 1 deletion libraries/browser-tracker-core/src/tracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ export function Tracker(
configSessionContext = trackerConfiguration.contexts?.session ?? false,
toOptoutByCookie: string | boolean,
onSessionUpdateCallback = trackerConfiguration.onSessionUpdateCallback,
manualSessionUpdateCalled = false;
manualSessionUpdateCalled = false,
networkUserId: string | undefined;

if (trackerConfiguration.hasOwnProperty('discoverRootDomain') && trackerConfiguration.discoverRootDomain) {
configCookieDomain = findRootDomain(configCookieSameSite, configCookieSecure);
Expand All @@ -336,6 +337,7 @@ export function Tracker(
core.addPayloadPair('lang', browserLanguage);
core.addPayloadPair('res', resolution);
core.addPayloadPair('cd', colorDepth);
setNetworkUserId(networkUserId);

/*
* Initialize tracker
Expand Down Expand Up @@ -1149,6 +1151,11 @@ export function Tracker(
activityTrackingConfig.configurations[actionKey] = undefined;
}

function setNetworkUserId(networkUserId: string | undefined): void {
if (networkUserId) {
core.addPayloadPair('tnuid', networkUserId);
}
}
const apiMethods = {
getDomainSessionIndex: function () {
return memorizedVisitCount;
Expand Down Expand Up @@ -1311,6 +1318,8 @@ export function Tracker(
},

clearUserData: clearUserDataAndCookies,

setNetworkUserId,
};

return {
Expand Down
6 changes: 6 additions & 0 deletions libraries/browser-tracker-core/src/tracker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,12 @@ export interface BrowserTracker {
* @param configuration - The plugin to add
*/
addPlugin: (configuration: BrowserPluginConfiguration) => void;

/**
* Set the newtork user id
* @param networkUserId - The custom network user id
*/
setNetworkUserId: (networkUserId: string) => void;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions trackers/browser-tracker/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,15 @@ export function addPlugin(configuration: BrowserPluginConfiguration, trackers?:
t.addPlugin(configuration);
});
}

/**
* Set the business-defined network user ID for this user.
*
* @param networkUserId - The business-defined network user ID
* @param trackers - The tracker identifiers which will be configured
*/
export function setNetworkUserId(networkUserId: string, trackers?: Array<string>) {
dispatchToTrackers(trackers, (t) => {
t.setNetworkUserId(networkUserId);
});
}
Loading