diff --git a/src/index.ts b/src/index.ts index 225b00f..c30895f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,11 @@ 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. -const getUserId = (event: MCEvent) => { - const { client } = event - let userId = event.payload.user_id || client.get('user_id') +// 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 userId = event.payload.user_id if (!userId) { - userId = crypto.randomUUID() - client.set('user_id', userId, { scope: 'infinite' }) + return null } return userId } @@ -53,10 +51,13 @@ 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, + }), event_properties: { url: client.url }, user_properties: {}, groups: {},