Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr 12 #13

Merged
merged 5 commits into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -47,16 +45,19 @@
const getEventData = (
event: MCEvent,
pageview: boolean,
ecomPayload?: any

Check warning on line 48 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Unexpected any. Specify a different type
) => {
const { client } = event
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 Expand Up @@ -103,10 +104,10 @@
if (type === 'ecommerce') {
payload.event_type = name
payload.productId = payload.products
.map((product: any) => product.product_id)

Check warning on line 107 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Unexpected any. Specify a different type
.join()
payload.quantity ??= payload.products.reduce(
(sum: any, product: any) => sum + parseInt(product.quantity, 10),

Check warning on line 110 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Unexpected any. Specify a different type

Check warning on line 110 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Unexpected any. Specify a different type
0
)
payload.revenue = payload.revenue || payload.total || payload.value
Expand All @@ -133,7 +134,7 @@
})

// sendEvent function is the main functions to send a server side request
const sendEvent = async (eventData: any) => {

Check warning on line 137 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Unexpected any. Specify a different type
const requestBody = {
api_key: settings.api_key,
...(settings.min_id_length && {
Expand Down
Loading