Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update content_type mapping logic for fb pixel #1628

Merged
merged 9 commits into from
Mar 1, 2024
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 @@
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 @@
const derivedEventID = getEventId(rudderElement.message);

if (event === 'Product List Viewed') {
const { contentIds, contentType, contents } = getProductListViewedEventParams(properties);
const {
contentIds,
contentType: defaultContentType,
contents,
} = getProductListViewedEventParams(properties);

Check warning on line 170 in packages/analytics-js-integrations/src/integrations/FacebookPixel/browser.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-integrations/src/integrations/FacebookPixel/browser.js#L170

Added line #L170 was not covered by tests

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 @@

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 @@
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 @@

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 @@
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
Loading