diff --git a/src/index.test.ts b/src/index.test.ts index e50089e..ceaf79d 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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 = {} diff --git a/src/index.ts b/src/index.ts index d6eae3e..caae6fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -57,10 +57,12 @@ export type Product = { coupon: number | string } -const mapEcommerceData = (ecommerce: EcommerceType) => { +function mapEcommerceData( + ecommerce: EcommerceType +): Record | null { const transformedProductData: Record = {} - if (!ecommerce.products) { - return + if (!ecommerce || !ecommerce.products) { + return null } else { ecommerce.products.forEach((product, index) => [ @@ -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 | null = + mapEcommerceData(ecommerce) for (const key in ecommerceData) { cleanPayload[key] = ecommerceData[key] } @@ -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 }