Skip to content

Commit

Permalink
Merge pull request #13 from managed-components/pr-12
Browse files Browse the repository at this point in the history
Pr 12 - do not generate and set user_id automaticaly
  • Loading branch information
ad-astra-via authored May 28, 2024
2 parents caabf79 + 4d55563 commit 72a00e4
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down Expand Up @@ -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: {},
Expand Down

0 comments on commit 72a00e4

Please sign in to comment.