diff --git a/common/changes/@snowplow/browser-tracker-core/feature-set_network_user_id_2023-10-09-16-16.json b/common/changes/@snowplow/browser-tracker-core/feature-set_network_user_id_2023-10-09-16-16.json new file mode 100644 index 000000000..4d5407c0f --- /dev/null +++ b/common/changes/@snowplow/browser-tracker-core/feature-set_network_user_id_2023-10-09-16-16.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@snowplow/browser-tracker-core", + "comment": "Add setNetworkUserId() function", + "type": "none" + } + ], + "packageName": "@snowplow/browser-tracker-core" +} \ No newline at end of file diff --git a/common/changes/@snowplow/browser-tracker/feature-set_network_user_id_2023-10-09-16-16.json b/common/changes/@snowplow/browser-tracker/feature-set_network_user_id_2023-10-09-16-16.json new file mode 100644 index 000000000..a18df5682 --- /dev/null +++ b/common/changes/@snowplow/browser-tracker/feature-set_network_user_id_2023-10-09-16-16.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@snowplow/browser-tracker", + "comment": "Add setNetworkUserId() function", + "type": "none" + } + ], + "packageName": "@snowplow/browser-tracker" +} \ No newline at end of file diff --git a/libraries/browser-tracker-core/src/tracker/index.ts b/libraries/browser-tracker-core/src/tracker/index.ts index a6443a932..9f70bbd84 100755 --- a/libraries/browser-tracker-core/src/tracker/index.ts +++ b/libraries/browser-tracker-core/src/tracker/index.ts @@ -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); @@ -336,6 +337,7 @@ export function Tracker( core.addPayloadPair('lang', browserLanguage); core.addPayloadPair('res', resolution); core.addPayloadPair('cd', colorDepth); + setNetworkUserId(networkUserId); /* * Initialize tracker @@ -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; @@ -1311,6 +1318,8 @@ export function Tracker( }, clearUserData: clearUserDataAndCookies, + + setNetworkUserId, }; return { diff --git a/libraries/browser-tracker-core/src/tracker/types.ts b/libraries/browser-tracker-core/src/tracker/types.ts index 0f5801c9d..dbc313609 100755 --- a/libraries/browser-tracker-core/src/tracker/types.ts +++ b/libraries/browser-tracker-core/src/tracker/types.ts @@ -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; } /** diff --git a/trackers/browser-tracker/src/api.ts b/trackers/browser-tracker/src/api.ts index e8eba06b6..b5d3c9a99 100644 --- a/trackers/browser-tracker/src/api.ts +++ b/trackers/browser-tracker/src/api.ts @@ -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) { + dispatchToTrackers(trackers, (t) => { + t.setNetworkUserId(networkUserId); + }); +}