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

Add missing evt param, ensure random number is an int #5

Merged
merged 1 commit into from
Oct 18, 2023
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
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const { payload: initialPayload, name } = event
const { ecommerce: payload } = initialPayload

const data: any = {

Check warning on line 12 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (18.x)

Unexpected any. Specify a different type
ec: 'ecommerce',
gc: payload.currency,
gv: payload.revenue || payload.total || payload.value,
Expand All @@ -27,7 +27,7 @@
payload.name ||
payload.product_id ||
payload.sku ||
payload.products?.map((p: any) => p.name).join()

Check warning on line 30 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (18.x)

Unexpected any. Specify a different type
data.ev = payload.price || payload.total || payload.value
data.gv = payload.price || data.gv
break
Expand All @@ -48,7 +48,7 @@
payload.checkout_id ||
payload.product_id ||
payload.name ||
payload.products?.map((p: any) => p.name).join()

Check warning on line 51 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (18.x)

Unexpected any. Specify a different type
data.ev =
payload.revenue || payload.total || payload.value || payload.price
}
Expand All @@ -58,6 +58,7 @@
const getStandardParams = (
event: MCEvent,
settings: ComponentSettings,
evt: 'pageLoad' | 'custom',
ec: boolean
) => {
const { payload: initialPayload } = event
Expand All @@ -71,9 +72,10 @@

const returnPayload = omitNullish({
...payload,
evt,
ti: payload.ti || settings.ti,
tl: event.client.title || '',
rn: (+(Math.random() * 1000000)).toString(),
rn: (+Math.floor(Math.random() * 1000000)).toString(), // ensure it's just a 6-digit integer
sw: event.client.screenWidth,
sh: event.client.screenHeight,
lg: (event.client.language || '').split(',')[0].trim(),
Expand All @@ -89,15 +91,14 @@
}

const handleEvent =
(settings: ComponentSettings, ec = false) =>
(settings: ComponentSettings, evt: 'custom' | 'pageLoad', ec = false) =>
async (event: MCEvent) => {
const payload = !ec
? getStandardParams(event, settings, ec)
? getStandardParams(event, settings, evt, ec)
: {
...getStandardParams(event, settings, ec),
...getStandardParams(event, settings, evt, ec),
...getECParams(event),
}

// Handle Bing click id
// Click id can come from two places:
// 1- The URL on landing page after clicking on a Microsoft Ad
Expand Down Expand Up @@ -135,7 +136,7 @@
}

export default async function (manager: Manager, settings: ComponentSettings) {
manager.addEventListener('pageview', handleEvent(settings))
manager.addEventListener('event', handleEvent(settings))
manager.addEventListener('ecommerce', handleEvent(settings, true))
manager.addEventListener('pageview', handleEvent(settings, 'pageLoad'))
manager.addEventListener('event', handleEvent(settings, 'custom'))
manager.addEventListener('ecommerce', handleEvent(settings, 'custom', true))
}
Loading