diff --git a/packages/analytics-js-integrations/__tests__/integrations/FacebookPixel/browser.test.js b/packages/analytics-js-integrations/__tests__/integrations/FacebookPixel/browser.test.js index 12ef39cbc2..f8a72c3f87 100644 --- a/packages/analytics-js-integrations/__tests__/integrations/FacebookPixel/browser.test.js +++ b/packages/analytics-js-integrations/__tests__/integrations/FacebookPixel/browser.test.js @@ -242,6 +242,69 @@ describe('Facebook Pixel Track event', () => { }); }); + test('Testing Ecommerce Track Events with content_type in properties', () => { + facebookPixel.track({ + message: { + context: {}, + event: 'Order Completed', + properties: { + content_type: 'product_group', + customProp: 'testProp', + checkout_id: 'random_id', + event_id: 'purchaseId', + order_id: 'transactionId', + value: 35.0, + shipping: 4.0, + coupon: 'APPARELSALE', + currency: 'GBP', + products: [ + { + product_id: 'abc', + category: 'Merch', + price: 3.0, + quantity: 2, + currency: 'GBP', + value: 6.0, + typeOfProduct: 'Food', + }, + ], + }, + }, + }); + expect(window.fbq.mock.calls[0][3]).toEqual({ + checkout_id: 'random_id', + content_ids: ['abc'], + content_type: 'product_group', + currency: 'GBP', + content_name: undefined, + value: 0, + contents: [ + { + id: 'abc', + quantity: 2, + item_price: 3, + }, + ], + num_items: 1, + coupon: 'APPARELSALE', + customProp: 'testProp', + event_id: 'purchaseId', + order_id: 'transactionId', + products: [ + { + product_id: 'abc', + category: 'Merch', + price: 3.0, + quantity: 2, + currency: 'GBP', + value: 6.0, + typeOfProduct: 'Food', + }, + ], + shipping: 4, + }); + }); + test('Testing Track Custom Events', () => { facebookPixel.track({ message: { diff --git a/packages/analytics-js-integrations/src/integrations/FacebookPixel/browser.js b/packages/analytics-js-integrations/src/integrations/FacebookPixel/browser.js index a8c697e220..da50f3a024 100644 --- a/packages/analytics-js-integrations/src/integrations/FacebookPixel/browser.js +++ b/packages/analytics-js-integrations/src/integrations/FacebookPixel/browser.js @@ -133,7 +133,8 @@ class FacebookPixel { contentName, product_id: productId, product_name: productName, - delivery_category: deliveryCategory + delivery_category: deliveryCategory, + content_type: contentType, } = properties; let { value, category, currency } = properties; @@ -162,11 +163,16 @@ class FacebookPixel { const derivedEventID = getEventId(rudderElement.message); if (event === 'Product List Viewed') { - const { contentIds, contentType, contents } = getProductListViewedEventParams(properties); + const { + contentIds, + contentType: defaultContentType, + contents, + } = getProductListViewedEventParams(properties); const productInfo = { content_ids: contentIds, - content_type: getContentType(rudderElement, contentType, this.categoryToContent), + content_type: + contentType || getContentType(rudderElement, defaultContentType, this.categoryToContent), contents, content_category: eventHelpers.getCategory(category), content_name: contentName, @@ -189,7 +195,8 @@ class FacebookPixel { const productInfo = { content_ids: contentIds, - content_type: getContentType(rudderElement, 'product', this.categoryToContent), + content_type: + contentType || getContentType(rudderElement, 'product', this.categoryToContent), content_name: eventHelpers.getProdName(productName, name), content_category: eventHelpers.getCategory(category), currency, @@ -208,8 +215,12 @@ class FacebookPixel { value: productInfo.value, }); } else if (event === 'Order Completed') { - const contentType = getContentType(rudderElement, 'product', this.categoryToContent); - const { contents, contentIds } = getProductsContentsAndContentIds(products, quantity, price, deliveryCategory); + const { contents, contentIds } = getProductsContentsAndContentIds( + products, + quantity, + price, + deliveryCategory, + ); // ref: https://developers.facebook.com/docs/meta-pixel/implementation/marketing-api#purchase // "trackSingle" feature is : @@ -217,7 +228,8 @@ class FacebookPixel { const productInfo = { content_ids: contentIds, - content_type: contentType, + content_type: + contentType || getContentType(rudderElement, 'product', this.categoryToContent), currency, value: revValue, contents, @@ -258,7 +270,8 @@ class FacebookPixel { const productInfo = { content_ids: contentIds, - content_type: getContentType(rudderElement, 'product', this.categoryToContent), + content_type: + contentType || getContentType(rudderElement, 'product', this.categoryToContent), content_category: contentCategory, currency, value: revValue, @@ -277,20 +290,20 @@ class FacebookPixel { value: revValue, }); } else if (eventHelpers.isCustomEventNotMapped(standardTo, legacyTo, event)) { - payload.value = revValue; - window.fbq('trackSingleCustom', this.pixelId, event, payload, { - eventID: derivedEventID, - }); - } else { + payload.value = revValue; + window.fbq('trackSingleCustom', this.pixelId, event, payload, { + eventID: derivedEventID, + }); + } else { logger.info('Not standard event & no custom mapping available'); - payload.value = revValue; - payload.currency = currency; - this.makeTrackSignalCalls(this.pixelId, event, standardTo, derivedEventID, payload); - this.makeTrackSignalCalls(this.pixelId, event, legacyTo, derivedEventID, { - currency, - value: revValue, - }); - } + payload.value = revValue; + payload.currency = currency; + this.makeTrackSignalCalls(this.pixelId, event, standardTo, derivedEventID, payload); + this.makeTrackSignalCalls(this.pixelId, event, legacyTo, derivedEventID, { + currency, + value: revValue, + }); + } } makeTrackSignalCalls(pixelId, event, array, derivedEventID, payload) {