@@ -35,9 +35,11 @@ jQuery( document ).ready(
3535 return ;
3636 }
3737
38- pluginGtmServerSide . pushAddToCart (
39- pluginGtmServerSide . removePrefixes ( el . dataset )
40- ) ;
38+ let gtmData = pluginGtmServerSide . getGtmItemData ( el . dataset ) ;
39+ let customData = pluginGtmServerSide . getCustomItemData ( el . dataset ) ;
40+
41+ pluginGtmServerSide . pushAddToCart ( gtmData ) ;
42+ pluginGtmServerSide . pushSelectItem ( gtmData , customData ) ;
4143 }
4244 ) ;
4345
@@ -54,9 +56,11 @@ jQuery( document ).ready(
5456 return ;
5557 }
5658
57- pluginGtmServerSide . pushAddToCart (
58- pluginGtmServerSide . removePrefixes ( $el . data ( ) )
59- ) ;
59+ let gtmData = pluginGtmServerSide . getGtmItemData ( $el . data ( ) ) ;
60+ let customData = pluginGtmServerSide . getCustomItemData ( $el . data ( ) ) ;
61+
62+ pluginGtmServerSide . pushAddToCart ( gtmData ) ;
63+ pluginGtmServerSide . pushSelectItem ( gtmData , customData ) ;
6064 }
6165 ) ;
6266
@@ -98,7 +102,7 @@ jQuery( document ).ready(
98102 }
99103
100104 pluginGtmServerSide . removeFromCart (
101- pluginGtmServerSide . removePrefixes ( $thisbutton . data ( ) )
105+ pluginGtmServerSide . getGtmItemData ( $thisbutton . data ( ) )
102106 ) ;
103107 }
104108 ) ;
@@ -117,7 +121,7 @@ jQuery( document ).ready(
117121 }
118122
119123 pluginGtmServerSide . removeFromCart (
120- pluginGtmServerSide . removePrefixes ( $el . data ( ) )
124+ pluginGtmServerSide . getGtmItemData ( $el . data ( ) )
121125 ) ;
122126 }
123127 ) ;
@@ -129,7 +133,7 @@ var pluginGtmServerSide = {
129133 var item = this . convertInputsToObject (
130134 $elForm . find ( '[name^=gtm_]' )
131135 ) ;
132- item = this . removePrefixes ( item ) ;
136+ item = this . getGtmItemData ( item ) ;
133137
134138 var $elQty = $elForm . find ( '[name=quantity]' ) ;
135139 if ( $elQty . length ) {
@@ -143,7 +147,7 @@ var pluginGtmServerSide = {
143147 var item = this . convertInputsToObject (
144148 $elForm . find ( '[name^=gtm_]' )
145149 ) ;
146- item = this . removePrefixes ( item ) ;
150+ item = this . getGtmItemData ( item ) ;
147151
148152 var $elQty = $elForm . find ( '[name=quantity]' ) ;
149153 if ( $elQty . length ) {
@@ -242,7 +246,7 @@ var pluginGtmServerSide = {
242246 }
243247
244248 if ( originalValue < currentValue ) {
245- var item = $this . removePrefixes ( elDataset . dataset ) ;
249+ var item = $this . getGtmItemData ( elDataset . dataset ) ;
246250 item [ 'quantity' ] = currentValue - originalValue ;
247251
248252 $this . pushAddToCart ( item ) ;
@@ -253,22 +257,42 @@ var pluginGtmServerSide = {
253257 } ,
254258
255259 /**
256- * Remove field prefixes .
260+ * Return gtm custom data .
257261 *
258262 * @param object items List items.
259263 * @returns object
260264 */
261- removePrefixes : function ( items ) {
262- var item = { } ;
265+ getGtmItemData : function ( items ) {
266+ return this . _getItemData ( items , 'gtm_' ) ;
267+ } ,
268+
269+ /**
270+ * Return gtm custom data.
271+ *
272+ * @param object items List items.
273+ * @returns object
274+ */
275+ getCustomItemData : function ( items ) {
276+ return this . _getItemData ( items , 'custom_gtm_' ) ;
277+ } ,
278+
279+ /**
280+ * Return item data.
281+ *
282+ * @param object items List items.
283+ * @returns object
284+ */
285+ _getItemData : function ( items , prefix ) {
286+ var result = { } ;
263287 for ( var key in items ) {
264- if ( 0 !== key . indexOf ( 'gtm_' ) ) {
288+ if ( 0 !== key . indexOf ( prefix ) ) {
265289 continue ;
266290 }
267291
268- var itemKey = key . replace ( 'gtm_' , '' )
269- item [ itemKey ] = items [ key ] ;
292+ var itemKey = key . replace ( prefix , '' )
293+ result [ itemKey ] = items [ key ] ;
270294 }
271- return item ;
295+ return result ;
272296 } ,
273297
274298 /**
@@ -317,6 +341,39 @@ var pluginGtmServerSide = {
317341 * @param mixed item List items.
318342 */
319343 pushAddToCart : function ( item ) {
344+ this . _pushToDataLayer ( item , 'add_to_cart' , 'product' )
345+ } ,
346+
347+ /**
348+ * Push add_to_cart to dataLayer.
349+ *
350+ * @param object item List items.
351+ * @param object custom Custom data.
352+ */
353+ pushSelectItem : function ( item , custom ) {
354+ if ( custom ?. pagetype ) { // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
355+ this . _pushToDataLayer (
356+ item ,
357+ 'select_item' ,
358+ custom . pagetype ,
359+ {
360+ collection_id : custom ?. collection_id , // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
361+ item_list_name : custom ?. item_list_name , // phpcs:ignore WordPress.WhiteSpace.OperatorSpacing.NoSpaceBefore, WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter
362+ }
363+ )
364+ }
365+ } ,
366+
367+ /**
368+ * Push to dataLayer.
369+ *
370+ * @param object originalItem List items.
371+ * @param string event Event name.
372+ * @param string pagetype Page type name.
373+ * @param object customEcommerce Ecommerce data.
374+ */
375+ _pushToDataLayer : function ( originalItem , event , pagetype , customEcommerce = { } ) {
376+ item = Object . assign ( { } , originalItem ) ;
320377 if ( item . item_id ) {
321378 item = [ item ] ;
322379 }
@@ -332,14 +389,16 @@ var pluginGtmServerSide = {
332389 items . push ( item_loop ) ;
333390 }
334391
392+ let eventDataEcommerce = {
393+ 'currency' : varGtmServerSide . currency ,
394+ 'value' : value . toFixed ( 2 ) ,
395+ 'items' : items ,
396+ }
397+
335398 var eventData = {
336- 'event' : this . getDataLayerEventName ( 'add_to_cart' ) ,
337- 'ecomm_pagetype' : 'product' ,
338- 'ecommerce' : {
339- 'currency' : varGtmServerSide . currency ,
340- 'value' : value . toFixed ( 2 ) ,
341- 'items' : items ,
342- } ,
399+ 'event' : this . getDataLayerEventName ( event ) ,
400+ 'ecomm_pagetype' : pagetype ,
401+ 'ecommerce' : Object . assign ( { } , eventDataEcommerce , customEcommerce ) ,
343402 } ;
344403 if ( varGtmServerSide . user_data ) {
345404 eventData . user_data = { } ;
0 commit comments