Skip to content

Commit

Permalink
run client.set if payload includes user_id and fix bug in getEventData
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-astra-via committed May 27, 2024
1 parent 3afc882 commit 732e0ea
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check failure on line 6 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Delete `;`
let userId = event.payload.user_id || client.get('user_id');

Check failure on line 7 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

'userId' is never reassigned. Use 'const' instead

Check failure on line 7 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Delete `;`
if (!userId) {
return null
return null;

Check failure on line 9 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Delete `;`
}
if (event.payload.user_id) {
client.set('user_id', userId, { scope: 'infinite' });

Check failure on line 12 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Delete `;`
}
return userId
return userId;

Check failure on line 14 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Delete `;`
}

// 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.
Expand Down Expand Up @@ -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: {},
Expand Down

0 comments on commit 732e0ea

Please sign in to comment.