Skip to content

Commit

Permalink
feat: update content_type mapping logic for fb pixel (#1628)
Browse files Browse the repository at this point in the history
* feat: update content_type mapping logic for fb pixel

* chore: update tests
  • Loading branch information
sandeepdsvs authored Mar 1, 2024
1 parent ce97106 commit d81adde
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -208,16 +215,21 @@ 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 :
// https://developers.facebook.com/ads/blog/post/v2/2017/11/28/event-tracking-with-multiple-pixels-tracksingle/

const productInfo = {
content_ids: contentIds,
content_type: contentType,
content_type:
contentType || getContentType(rudderElement, 'product', this.categoryToContent),
currency,
value: revValue,
contents,
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit d81adde

Please sign in to comment.