Skip to content

Commit

Permalink
fix ecommerce function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-astra-via committed Nov 27, 2023
1 parent 67e47d5 commit 9f4130b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
39 changes: 13 additions & 26 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,21 @@ describe('Pinterest MC sends correct request', () => {
const baseHost = `${baseHostname}:${port}`
const baseOrigin = `https://${baseHost}`
const baseHref = `${baseOrigin}/`
const searchParams = new URLSearchParams()
// const searchParams = new URLSearchParams()

const mockEvent = new Event('pagevisit', {}) as MCEvent
mockEvent.name = 'Pinterest Test'
mockEvent.payload = { timestamp: 1670409810, event: 'pagevisit', tid: 'xyz' }
mockEvent.client = {
url: {
href: baseHref,
origin: baseOrigin,
protocol: 'http:',
username: '',
password: '',
host: baseHost,
hostname: baseHostname,
port: port,
pathname: '/',
search: '',
searchParams: searchParams,
hash: '',
const mockEvent: MCEvent = {
payload: { timestamp: 1670409810, event: 'pagevisit', tid: 'xyz' },
client: {
url: new URL(baseHref),
title: 'Zaraz "Test" /t Page',
timestamp: 1670409810,
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
language: 'en-GB',
referer: `${baseOrigin}/somewhere-else.html`,
ip: baseHostname,
emitter: 'browser',
},
title: 'Zaraz "Test" /t Page',
timestamp: 1670409810,
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
language: 'en-GB',
referer: `${baseOrigin}/somewhere-else.html`,
ip: baseHostname,
emitter: 'browser',
}

const settings: ComponentSettings = {}
Expand Down
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ export type Product = {
coupon: number | string
}

const mapEcommerceData = (ecommerce: EcommerceType) => {
function mapEcommerceData(
ecommerce: EcommerceType
): Record<string, string | number> | null {
const transformedProductData: Record<string, string | number> = {}
if (!ecommerce.products) {
return
if (!ecommerce || !ecommerce.products) {
return null
} else {
ecommerce.products.forEach((product, index) =>
[
Expand Down Expand Up @@ -133,7 +135,8 @@ export const getRequestBody = (
requestBody.event = eventTypes[eventType]
}

const ecommerceData: any = mapEcommerceData(ecommerce) // not sure how to get read of this any!
const ecommerceData: Record<string, string | number> | null =
mapEcommerceData(ecommerce)
for (const key in ecommerceData) {
cleanPayload[key] = ecommerceData[key]
}
Expand All @@ -142,6 +145,7 @@ export const getRequestBody = (
// event data is created, note that it also holds the ecommerce parameters
requestBody['ed'] = JSON.stringify(cleanPayload)
}
console.log('`@#@#@#@###@#@ requestbody:', requestBody)
return requestBody
}

Expand Down

0 comments on commit 9f4130b

Please sign in to comment.