@@ -9,6 +9,17 @@ function isObject(val) {
99 return val != null && typeof val === 'object' && Array . isArray ( val ) === false ;
1010}
1111
12+ var isobject = /*#__PURE__*/ Object . freeze ( {
13+ __proto__ : null ,
14+ 'default' : isObject
15+ } ) ;
16+
17+ function getCjsExportFromNamespace ( n ) {
18+ return n && n [ 'default' ] || n ;
19+ }
20+
21+ var isobject$1 = getCjsExportFromNamespace ( isobject ) ;
22+
1223/* eslint-disable no-undef */
1324// Copyright 2015 mParticle, Inc.
1425//
@@ -71,11 +82,14 @@ function isObject(val) {
7182 constructor = function ( ) {
7283 var self = this ,
7384 isInitialized = false ,
74- reportingService = null ;
85+ reportingService = null ,
86+ settings ,
87+ productAttributeMapping ;
7588
7689 self . name = name ;
7790
78- function initForwarder ( settings , service , testMode , trackerId , userAttributes , userIdentities ) {
91+ function initForwarder ( forwarderSettings , service , testMode , trackerId , userAttributes , userIdentities ) {
92+ settings = forwarderSettings ;
7993 reportingService = service ;
8094
8195 SupportedCommerceTypes = [
@@ -118,6 +132,8 @@ function isObject(val) {
118132 fbq . disablePushState = true ;
119133 }
120134 fbq ( 'init' , settings . pixelId , visitorData ) ;
135+
136+ loadMappings ( ) ;
121137
122138 isInitialized = true ;
123139
@@ -129,6 +145,12 @@ function isObject(val) {
129145 }
130146 }
131147
148+ function loadMappings ( ) {
149+ productAttributeMapping = settings . productAttributeMapping
150+ ? JSON . parse ( settings . productAttributeMapping . replace ( / & q u o t ; / g, '"' ) )
151+ : [ ] ;
152+ }
153+
132154 function processEvent ( event ) {
133155 var reportEvent = false ;
134156
@@ -217,13 +239,13 @@ function isObject(val) {
217239 else if ( event . ProductAction . ProductActionType == mParticle . ProductActionType . AddToCart ) {
218240 eventName = ADD_TO_CART_EVENT_NAME ;
219241 }
220- else {
242+ else {
221243 eventName = VIEW_CONTENT_EVENT_NAME ;
222244 }
223245
224246 }
225247 else if ( event . ProductAction . ProductActionType == mParticle . ProductActionType . Checkout ||
226- event . ProductAction . ProductActionType == mParticle . ProductActionType . Purchase ) {
248+ event . ProductAction . ProductActionType == mParticle . ProductActionType . Purchase ) {
227249
228250 eventName = event . ProductAction . ProductActionType == mParticle . ProductActionType . Checkout ? CHECKOUT_EVENT_NAME : PURCHASE_EVENT_NAME ;
229251
@@ -238,6 +260,18 @@ function isObject(val) {
238260 return sum ;
239261 } , 0 ) ;
240262 params [ 'num_items' ] = num_items ;
263+
264+ if ( event . ProductAction . TransactionId ) {
265+ params [ 'order_id' ] = event . ProductAction . TransactionId ;
266+ }
267+
268+ // Build contents array for Purchase events
269+ if ( event . ProductAction . ProductActionType == mParticle . ProductActionType . Purchase ) {
270+ var contents = buildProductContents ( event . ProductAction . ProductList ) ;
271+ if ( contents && contents . length > 0 ) {
272+ params [ 'contents' ] = contents ;
273+ }
274+ }
241275 }
242276 else if ( event . ProductAction . ProductActionType == mParticle . ProductActionType . RemoveFromCart ) {
243277 eventName = REMOVE_FROM_CART_EVENT_NAME ;
@@ -334,6 +368,60 @@ function isObject(val) {
334368 return null ;
335369 }
336370
371+ /**
372+ * Builds contents array for Facebook Pixel commerce events.
373+ * Creates a nested array of content items with product details.
374+ *
375+ * @param {Array } productList - Array of products from event.ProductAction.ProductList
376+ * @returns {Array } Array of content objects for Facebook Pixel
377+ */
378+ function buildProductContents ( productList ) {
379+ if ( ! productList || productList . length === 0 ) {
380+ return [ ] ;
381+ }
382+
383+ return productList
384+ . filter ( function ( product ) {
385+ return product && product . Sku ;
386+ } )
387+ . map ( function ( product ) {
388+ var contentItem = {
389+ id : product . Sku ,
390+ quantity : isNumeric ( product . Quantity ) ? product . Quantity : 1 ,
391+ name : product . Name ,
392+ brand : product . Brand ,
393+ category : product . Category ,
394+ variant : product . Variant ,
395+ item_price : isNumeric ( product . Price ) ? product . Price : null
396+ } ;
397+
398+ // Apply configured mappings to custom attributes
399+ productAttributeMapping . forEach ( function ( productMapping ) {
400+ if ( ! isobject$1 ( productMapping ) || ! productMapping . map || ! productMapping . value ) {
401+ return ;
402+ }
403+
404+ var sourceField = productMapping . map ;
405+ var facebookFieldName = productMapping . value ;
406+ var value = null ;
407+
408+ // Check for Product level field first
409+ if ( product . hasOwnProperty ( sourceField ) ) {
410+ value = product [ sourceField ] ;
411+ }
412+ // then check for Product.Attributes level field
413+ else if ( product . Attributes && product . Attributes [ sourceField ] ) {
414+ value = product . Attributes [ sourceField ] ;
415+ }
416+
417+ if ( value !== null && value !== undefined ) {
418+ contentItem [ facebookFieldName ] = value ;
419+ }
420+ } ) ;
421+ return contentItem ;
422+ } ) ;
423+ }
424+
337425 // https://developers.facebook.com/docs/marketing-api/conversions-api/deduplicate-pixel-and-server-events#event-deduplication-options
338426 function createEventId ( event ) {
339427 return {
@@ -355,12 +443,12 @@ function isObject(val) {
355443 return ;
356444 }
357445
358- if ( ! isObject ( config ) ) {
446+ if ( ! isobject$1 ( config ) ) {
359447 console . log ( '\'config\' must be an object. You passed in a ' + typeof config ) ;
360448 return ;
361449 }
362450
363- if ( isObject ( config . kits ) ) {
451+ if ( isobject$1 ( config . kits ) ) {
364452 config . kits [ name ] = {
365453 constructor : constructor
366454 } ;
0 commit comments