Skip to content

Commit

Permalink
apply cleanup and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-astra-via committed Nov 20, 2023
1 parent 919d14a commit 2e00fbf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 69 deletions.
134 changes: 69 additions & 65 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import UAParser from 'ua-parser-js'
import {
checkEventName,
hashPayload,
enrichEventData,
getRemainingEventData,
getCustomData,
} from './utils'

Expand All @@ -26,17 +26,61 @@ export interface Product {
position: number | string
}

const getEcommercePayload = (event: MCEvent) => {
const { name } = event
let { payload } = event
payload = { ...payload, ...payload.ecommerce }
payload.name =
name === 'Product Added'
? 'add_to_cart'
: name === 'Order Completed'
? 'checkout'
: name
if (Array.isArray(payload.products)) {
payload.content_ids = payload.products
.map((product: Product) => product.product_id)
.join()
payload.content_name = payload.products
.map((product: Product) => product.name)
.join()
payload.content_category = payload.products
.map((product: Product) => product.category)
.join()
payload.content_brand = payload.products
.map((product: Product) => product.brand)
.join()
payload.contents = payload.products.map((product: Product) => ({
id: product.product_id,
item_price: product.price.toString(),
quantity: product.quantity,
}))
payload.num_items =
payload.quantity ||
payload.products.reduce((sum: number, product: Product) => {
if (typeof product.quantity === 'string') {
return sum + parseInt(product.quantity, 10)
} else if (typeof product.quantity === 'number') {
return sum + product.quantity
}
return sum
}, 0)
}

payload.value = payload.revenue || payload.total || payload.value
return payload
}

export const getEventData = async (
client: Client,
pageview: boolean,
payload: MCEvent['payload']
) => {
const parsedUserAgent = UAParser(client.userAgent)

const [hashedUserProperties, eventDataResult, customDataResult] =
const [hashedUserProperties, remainingEventData, customDataResult] =
await Promise.all([
hashPayload(payload),
enrichEventData(payload),
getRemainingEventData(payload),
getCustomData(payload),
])

Expand All @@ -52,7 +96,7 @@ export const getEventData = async (
device_model: parsedUserAgent.device.model,
os_version: parsedUserAgent.os.version,
language: payload.language || client.language.split(',')[0].substring(0, 2),
...eventDataResult,
...remainingEventData,
user_data: {
client_ip_address: payload.client_ip_address || client.ip.toString(),
client_user_agent: payload.client_user_agent || parsedUserAgent.ua,
Expand All @@ -66,71 +110,31 @@ export const getEventData = async (
return eventData
}

export default async function (manager: Manager, settings: ComponentSettings) {
const getEcommercePayload = (event: MCEvent) => {
const { name } = event
let { payload } = event
payload = { ...payload, ...payload.ecommerce }
payload.name =
name === 'Product Added'
? 'add_to_cart'
: name === 'Order Completed'
? 'checkout'
: name
if (Array.isArray(payload.products)) {
payload.content_ids = payload.products
.map((product: Product) => product.product_id)
.join()
payload.content_name = payload.products
.map((product: Product) => product.name)
.join()
payload.content_category = payload.products
.map((product: Product) => product.category)
.join()
payload.content_brand = payload.products
.map((product: Product) => product.brand)
.join()
payload.contents = payload.products.map((product: Product) => ({
id: product.product_id,
item_price: product.price.toString(),
quantity: product.quantity,
}))
payload.num_items =
payload.quantity ||
payload.products.reduce((sum: number, product: Product) => {
if (typeof product.quantity === 'string') {
return sum + parseInt(product.quantity, 10)
} else if (typeof product.quantity === 'number') {
return sum + product.quantity
}
return sum
}, 0)
}

payload.value = payload.revenue || payload.total || payload.value
return payload
const sendEvent = async (
eventData: Record<string, unknown>,
manager: Manager,
settings: ComponentSettings
) => {
const requestBody = {
data: [eventData],
}

const sendEvent = async (eventData: Record<string, unknown>) => {
const requestBody = {
data: [eventData],
}

const pinterestEndpoint = `https://api.pinterest.com/v5/ad_accounts/${settings.ad_account_id}/events`
const pinterestEndpoint = `https://api.pinterest.com/v5/ad_accounts/${settings.ad_account_id}/events`

await manager.fetch(pinterestEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${settings.conversion_token}`,
},
body: JSON.stringify(requestBody),
})
}
await manager.fetch(pinterestEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${settings.conversion_token}`,
},
body: JSON.stringify(requestBody),
})
}

export default async function (manager: Manager, settings: ComponentSettings) {
manager.addEventListener('pageview', async event => {
const eventData = await getEventData(event.client, true, event.payload)
sendEvent(eventData)
sendEvent(eventData, manager, settings)
})

manager.addEventListener('event', async event => {
Expand All @@ -141,13 +145,13 @@ export default async function (manager: Manager, settings: ComponentSettings) {
}
const eventData = await getEventData(event.client, false, event.payload)
if (eventData) {
sendEvent(eventData)
sendEvent(eventData, manager, settings)
}
})

manager.addEventListener('ecommerce', async event => {
const ecomPayload = getEcommercePayload(event)
const eventData = await getEventData(event.client, false, ecomPayload)
sendEvent(eventData)
sendEvent(eventData, manager, settings)
})
}
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function hashPayload(
return results
}

export async function enrichEventData(payload: MCEvent['payload']) {
export async function getRemainingEventData(payload: MCEvent['payload']) {
if (!payload) {
return
}
Expand All @@ -81,14 +81,14 @@ export async function enrichEventData(payload: MCEvent['payload']) {
'app_version',
'wifi',
]
const eventDataResult: { [key: string]: unknown } = {}
const remainingEventData: { [key: string]: unknown } = {}

for (const key of eventDataKeys) {
if (Object.prototype.hasOwnProperty.call(payload, key)) {
eventDataResult[key] = payload[key]
remainingEventData[key] = payload[key]
}
}
return eventDataResult
return remainingEventData
}

export async function getCustomData(payload: MCEvent['payload']) {
Expand Down

0 comments on commit 2e00fbf

Please sign in to comment.