From 3afc88252ddf1930f7f7d3e0c69452447c0b98a9 Mon Sep 17 00:00:00 2001 From: Kody Jackson Date: Fri, 24 May 2024 09:25:01 -0500 Subject: [PATCH 1/5] If not set, don't set a value for user_id Better follow the guidance of [Amplitude](https://help.amplitude.com/hc/en-us/articles/115003135607-Track-unique-users). If you're working in an unauthenticated environment, you should not send a user id b/c it can conflict with added user ids + lead to join issues later. This'd also require updating the text in the dashboard which currently says one will be set if not. --- src/index.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 225b00f..d451c9f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,12 @@ import { ComponentSettings, Manager, MCEvent } from '@managed-components/types' import UAParser from 'ua-parser-js' -// Get the user ID stored in the client, if it does not exist, make a random one, save it in the client, and return it. +// Get the user ID stored in the client, if it does not exist, then do not set it. const getUserId = (event: MCEvent) => { const { client } = event let userId = event.payload.user_id || client.get('user_id') if (!userId) { - userId = crypto.randomUUID() - client.set('user_id', userId, { scope: 'infinite' }) + return null } return userId } @@ -53,10 +52,14 @@ export default async function (manager: Manager, settings: ComponentSettings) { const parsedUserAgent = UAParser(client.userAgent) const payload = ecomPayload ? ecomPayload : event.payload // eventData builds the eventData object to be used in the request body + const userId = getUserId(event); const eventData = { event_type: pageview ? 'pageview' : payload.event_type, - user_id: getUserId(event), + ...(userId && { + user_id: userId, + }) + user_id?: getUserId(event), event_properties: { url: client.url }, user_properties: {}, groups: {}, From 732e0eafe2bca6a0d3ca0fdaf2631e23715b9b55 Mon Sep 17 00:00:00 2001 From: Yair Dovrat Date: Mon, 27 May 2024 17:08:24 +0200 Subject: [PATCH 2/5] run client.set if payload includes user_id and fix bug in getEventData --- src/index.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index d451c9f..56ebbfc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,13 +2,16 @@ import { ComponentSettings, Manager, MCEvent } from '@managed-components/types' import UAParser from 'ua-parser-js' // Get the user ID stored in the client, if it does not exist, then do not set it. -const getUserId = (event: MCEvent) => { - const { client } = event - let userId = event.payload.user_id || client.get('user_id') +const getUserId = (event: MCEvent): string | null => { + const { client } = event; + let userId = event.payload.user_id || client.get('user_id'); if (!userId) { - return null + return null; + } + if (event.payload.user_id) { + client.set('user_id', userId, { scope: 'infinite' }); } - return userId + return userId; } // Get the device ID stored in the client, if it does not exist, make a random one, save it in the client, and return it. @@ -58,8 +61,7 @@ export default async function (manager: Manager, settings: ComponentSettings) { event_type: pageview ? 'pageview' : payload.event_type, ...(userId && { user_id: userId, - }) - user_id?: getUserId(event), + }), event_properties: { url: client.url }, user_properties: {}, groups: {}, From 4b298d174225161e8df5466a5909e4037f369da4 Mon Sep 17 00:00:00 2001 From: Yair Dovrat Date: Mon, 27 May 2024 17:10:50 +0200 Subject: [PATCH 3/5] prettier cleanup --- src/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 56ebbfc..adbf223 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,15 +3,15 @@ import UAParser from 'ua-parser-js' // Get the user ID stored in the client, if it does not exist, then do not set it. const getUserId = (event: MCEvent): string | null => { - const { client } = event; - let userId = event.payload.user_id || client.get('user_id'); + const { client } = event + let userId = event.payload.user_id || client.get('user_id') if (!userId) { - return null; + return null } if (event.payload.user_id) { - client.set('user_id', userId, { scope: 'infinite' }); + client.set('user_id', userId, { scope: 'infinite' }) } - return userId; + return userId } // Get the device ID stored in the client, if it does not exist, make a random one, save it in the client, and return it. @@ -55,7 +55,7 @@ export default async function (manager: Manager, settings: ComponentSettings) { const parsedUserAgent = UAParser(client.userAgent) const payload = ecomPayload ? ecomPayload : event.payload // eventData builds the eventData object to be used in the request body - const userId = getUserId(event); + const userId = getUserId(event) const eventData = { event_type: pageview ? 'pageview' : payload.event_type, From f38cc74df03a59c271c92b059391e153665a372a Mon Sep 17 00:00:00 2001 From: Yair Dovrat Date: Mon, 27 May 2024 17:12:22 +0200 Subject: [PATCH 4/5] use const for userID --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index adbf223..490d20f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import UAParser from 'ua-parser-js' // Get the user ID stored in the client, if it does not exist, then do not set it. const getUserId = (event: MCEvent): string | null => { const { client } = event - let userId = event.payload.user_id || client.get('user_id') + const userId = event.payload.user_id || client.get('user_id') if (!userId) { return null } From 4d55563e062a14f0cbf0ad3ad8b2a0a19f03731f Mon Sep 17 00:00:00 2001 From: Yair Dovrat Date: Tue, 28 May 2024 11:15:43 +0200 Subject: [PATCH 5/5] remove client.set of user_id --- src/index.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 490d20f..c30895f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,14 +3,10 @@ import UAParser from 'ua-parser-js' // Get the user ID stored in the client, if it does not exist, then do not set it. const getUserId = (event: MCEvent): string | null => { - const { client } = event - const userId = event.payload.user_id || client.get('user_id') + const userId = event.payload.user_id if (!userId) { return null } - if (event.payload.user_id) { - client.set('user_id', userId, { scope: 'infinite' }) - } return userId }