From 0207c2aeadc906bebf85dcdda8e58ed8da72c5a8 Mon Sep 17 00:00:00 2001 From: Jon Wu Date: Wed, 16 Nov 2022 09:53:12 -0800 Subject: [PATCH 1/2] Update ecommerce to accept unknown product keys Allows unknown (to Zaraz e-commerce) keys that could be valid for GA4, such as `'ca2'`, `'ca3'`, `'ca4'`, `'ca5'`, `'ds'`, `'af'`, `'lp'` to be added to the GA4 result. --- src/ecommerce.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ecommerce.ts b/src/ecommerce.ts index 636f908..db80dd9 100644 --- a/src/ecommerce.ts +++ b/src/ecommerce.ts @@ -97,8 +97,10 @@ const buildProductRequest = (item: { [k: string]: any }) => { for (const [id, value] of Object.entries(item)) { const result: { [k: string]: string } = {} const preppedValue = _prepareStringContent(value) - Object.prototype.hasOwnProperty.call(PRODUCT_DETAILS_MAPPING, id) && - (result[PRODUCT_DETAILS_MAPPING[id]] = preppedValue) + const productDetailResultKey = + Object.prototype.hasOwnProperty.call(PRODUCT_DETAILS_MAPPING, id) + ? PRODUCT_DETAILS_MAPPING[id] : id + result[productDetailResultKey] = preppedValue if (Object.prototype.hasOwnProperty.call(_listMapping, id)) { if (!Object.prototype.hasOwnProperty.call(result, _listMapping[id])) { result[_listMapping[id]] = preppedValue From acf87f27a6601d57f6fed7d681a7f8e728bac182 Mon Sep 17 00:00:00 2001 From: Jon Wu Date: Fri, 19 May 2023 00:28:50 -0700 Subject: [PATCH 2/2] Keep all keys when building ecommerce product Removes product details mapping, which is unnecessary now that unknown keys are accepted. --- src/ecommerce.ts | 31 +------------------------------ src/requestBuilder.ts | 7 ++----- 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/src/ecommerce.ts b/src/ecommerce.ts index db80dd9..32354eb 100644 --- a/src/ecommerce.ts +++ b/src/ecommerce.ts @@ -16,24 +16,6 @@ const EVENTS: { [k: string]: string } = { 'Order Refunded': 'refund', } -const PRODUCT_DETAILS: string[] = [ - 'cart_id', - 'product_id', - 'sku', - 'category', - 'name', - 'brand', - 'variant', - 'price', - 'quantity', - 'coupon', - 'position', - - 'affiliation', - 'discount', - 'currency', -] - // list of params that will be prefixed in the request with // ep for string values // epn for numbers @@ -117,15 +99,4 @@ const buildProductRequest = (item: { [k: string]: any }) => { return resultList.join('~') } -// product comes in standard format -// returns GA4's standard item -const mapProductToItem = (product: any) => { - const eventProductDescription = PRODUCT_DETAILS - const item: any = {} - for (const prop of eventProductDescription) { - product[prop] && (item[prop] = product[prop]) - } - return item -} - -export { EVENTS, mapProductToItem, PREFIX_PARAMS_MAPPING, buildProductRequest } +export { EVENTS, PREFIX_PARAMS_MAPPING, buildProductRequest } diff --git a/src/requestBuilder.ts b/src/requestBuilder.ts index da624b0..1236422 100644 --- a/src/requestBuilder.ts +++ b/src/requestBuilder.ts @@ -2,7 +2,6 @@ import { ComponentSettings, MCEvent } from '@managed-components/types' import { buildProductRequest, EVENTS, - mapProductToItem, PREFIX_PARAMS_MAPPING, } from './ecommerce' import { flattenKeys, isNumber } from './utils' @@ -169,14 +168,12 @@ const getFinalURL = ( if (ecommerceData.products) { // handle products list for (const [index, product] of (ecommerceData.products || []).entries()) { - const item = mapProductToItem(product) - prQueryParams = buildProductRequest(item) + prQueryParams = buildProductRequest(product) toolRequest[`pr${index + 1}`] = prQueryParams } } else { // handle single product data - const item = mapProductToItem(ecommerceData) - prQueryParams = buildProductRequest(item) + prQueryParams = buildProductRequest(ecommerceData) if (prQueryParams) toolRequest['pr1'] = prQueryParams } }