gtm4wp_woocommerce_datalayer_filter_items

The Google Tag Manager for WordPress gtm4wp woocommerce datalayer filter items function.

Description

gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ); 

Parameters (1)

0. $datalayer
The datalayer.

Usage

  1. if ( !function_exists( 'gtm4wp_woocommerce_datalayer_filter_items' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-tag-manager-for-wordpress/integration/woocommerce.php'; 
  3.  
  4. // The datalayer. 
  5. $datalayer = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = gtm4wp_woocommerce_datalayer_filter_items($datalayer); 
  9.  

Defined (1)

The function is defined in the following location(s).

/integration/woocommerce.php  
  1. function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) { 
  2. global $woocommerce, $gtm4wp_options, $wp_query, $gtm4wp_datalayer_name, $gtm4wp_product_counter; 
  3.  
  4. if ( is_product_category() || is_product_tag() || is_front_page() || is_shop() ) { 
  5. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) { 
  6. $dataLayer["ecomm_prodid"] = array(); 
  7. $dataLayer["ecomm_pagetype"] = ( is_front_page() ? "home" : "category" ); 
  8. $dataLayer["ecomm_totalvalue"] = 0; 
  9. } else if ( is_product() ) { 
  10. if ( ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) || ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) ) { 
  11. $postid = get_the_ID(); 
  12. $product = wc_get_product( $postid ); 
  13. $product_id = $product->get_id(); 
  14.  
  15. $_product_cats = get_the_terms( $product->id, 'product_cat' ); 
  16. if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) { 
  17. $product_cat = array_pop( $_product_cats ); 
  18. $product_cat = $product_cat->name; 
  19. } else { 
  20. $product_cat = ""; 
  21.  
  22. if ( "variable" != $product->product_type ) { 
  23. $product_price = $product->get_price(); 
  24. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] ) { 
  25. $product_sku = $product->get_sku(); 
  26. if ( "" != $product_sku ) { 
  27. $product_id = $product_sku; 
  28.  
  29. $_temp_productdata = array( 
  30. "name" => gtm4wp_woocommerce_html_entity_decode( get_the_title() ),  
  31. "id" => $product_id,  
  32. "price" => $product_price,  
  33. "category" => $product_cat,  
  34. ); 
  35. $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "productdetail" ); 
  36.  
  37. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) { 
  38. $remarketing_id = (string)$product_id; 
  39.  
  40. $dataLayer["ecomm_prodid"] = $remarketing_id; 
  41. $dataLayer["ecomm_pagetype"] = "product"; 
  42. $dataLayer["ecomm_totalvalue"] = (float)$eec_product_array[ "price" ]; 
  43.  
  44. if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) { 
  45. $dataLayer["ecommerce"] = array( 
  46. "detail" => array( 
  47. "products" => array($eec_product_array) 
  48. ); 
  49. } else { 
  50. $dataLayer["ecomm_prodid"] = array(); 
  51. $dataLayer["ecomm_pagetype"] = "product"; 
  52. $dataLayer["ecomm_totalvalue"] = 0; 
  53.  
  54. var gtm4wp_product_detail_data = { 
  55. name: '" . gtm4wp_woocommerce_html_entity_decode( get_the_title() ) . "',  
  56. id 0,  
  57. price: 0,  
  58. category: '" . $product_cat . "',  
  59. variant: '' 
  60. }; 
  61.  
  62. jQuery(document).on( 'found_variation', function( event, product_variation ) { 
  63. var current_product_detail_data = gtm4wp_product_detail_data; 
  64. current_product_detail_data.id = product_variation.variation_id; 
  65. current_product_detail_data.price = product_variation.display_price; 
  66.  
  67. var _tmp = []; 
  68. for( var attrib_key in product_variation.attributes ) { 
  69. _tmp.push( product_variation.attributes[ attrib_key ] ); 
  70. current_product_detail_data.variant = _tmp.join(', '); 
  71.  
  72. ". $gtm4wp_datalayer_name .".push({ 
  73. 'event': 'gtm4wp.changeDetailViewEEC',  
  74. 'ecommerce': { 
  75. 'currencyCode': '".get_woocommerce_currency()."',  
  76. 'detail': current_product_detail_data,  
  77. 'ecomm_prodid': current_product_detail_data.id,  
  78. 'ecomm_pagetype': 'product',  
  79. 'ecomm_totalvalue': 0 
  80. }); 
  81. }); 
  82.  
  83. jQuery( '.variations select' ).trigger( 'change' ); 
  84. "); 
  85. } else if ( is_cart() ) { 
  86. if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) { 
  87. $( document ).on( 'click', '[name=update_cart]', function() { 
  88. $( '.product-quantity input.qty' ).each(function() { 
  89. var _original_value = $( this ).prop( 'defaultValue' ); 
  90.  
  91. var _current_value = parseInt( $( this ).val() ); 
  92. if ( isNaN( _current_value ) ) { 
  93. _current_value = _original_value; 
  94.  
  95. if ( _original_value != _current_value ) { 
  96. var productdata = $( this ).closest( '.cart_item' ).find( '.remove' ); 
  97.  
  98. if ( _original_value < _current_value ) { 
  99. ". $gtm4wp_datalayer_name .".push({ 
  100. 'event': 'gtm4wp.addProductToCartEEC',  
  101. 'ecommerce': { 
  102. 'currencyCode': '".get_woocommerce_currency()."',  
  103. 'add': { 
  104. 'products': [{ 
  105. 'name': productdata.data( 'gtm4wp_product_name' ),  
  106. id productdata.data( 'gtm4wp_product_id' ),  
  107. 'price': productdata.data( 'gtm4wp_product_price' ),  
  108. 'category': productdata.data( 'gtm4wp_product_cat' ),  
  109. 'variant': productdata.data( 'gtm4wp_product_variant' ),  
  110. 'quantity': _current_value - _original_value 
  111. }] 
  112. }); 
  113. } else { 
  114. ". $gtm4wp_datalayer_name .".push({ 
  115. 'event': 'gtm4wp.removeFromCartEEC',  
  116. 'ecommerce': { 
  117. 'currencyCode': '".get_woocommerce_currency()."',  
  118. 'remove': { 
  119. 'products': [{ 
  120. 'name': productdata.data( 'gtm4wp_product_name' ),  
  121. id productdata.data( 'gtm4wp_product_id' ),  
  122. 'price': productdata.data( 'gtm4wp_product_price' ),  
  123. 'category': productdata.data( 'gtm4wp_product_cat' ),  
  124. 'variant': productdata.data( 'gtm4wp_product_variant' ),  
  125. 'quantity': _original_value - _current_value 
  126. }] 
  127. }); 
  128. }); 
  129. }); 
  130. "); 
  131.  
  132. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) { 
  133. $products = $woocommerce->cart->get_cart(); 
  134. $product_ids = array(); 
  135. foreach( $products as $oneproduct ) { 
  136. $remarketing_id = ( isset( $oneproduct[ "variation_id" ] ) && ($oneproduct[ "variation_id" ]>0) ) ? $oneproduct[ "variation_id" ] : $oneproduct[ "product_id" ]; 
  137. $product_sku = $oneproduct["data"]->get_sku(); 
  138. if ( ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] ) && ( "" != $product_sku ) ) { 
  139. $remarketing_id = $product_sku; 
  140.  
  141. $product_ids[] = $remarketing_id; 
  142.  
  143. $dataLayer["ecomm_prodid"] = $product_ids; 
  144. $dataLayer["ecomm_pagetype"] = "cart"; 
  145. if ( ! $woocommerce->cart->prices_include_tax ) { 
  146. $cart_total = $woocommerce->cart->cart_contents_total; 
  147. } else { 
  148. $cart_total = $woocommerce->cart->cart_contents_total + $woocommerce->cart->tax_total; 
  149. $dataLayer["ecomm_totalvalue"] = (float)$cart_total; 
  150. } else if ( is_order_received_page() ) { 
  151. $order_id = empty( $_GET[ "order" ] ) ? ( $GLOBALS[ "wp" ]->query_vars[ "order-received" ] ? $GLOBALS[ "wp" ]->query_vars[ "order-received" ] : 0 ) : absint( $_GET[ "order" ] ); 
  152. $order_id_filtered = apply_filters( 'woocommerce_thankyou_order_id', $order_id ); 
  153. if ( "" != $order_id_filtered ) { 
  154. $order_id = $order_id_filtered; 
  155.  
  156. $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET[ "key" ] ) ? "" : wc_clean( $_GET[ "key" ] ) ); 
  157.  
  158. if ( $order_id > 0 ) { 
  159. $order = new WC_Order( $order_id ); 
  160. if ( $order->order_key != $order_key ) 
  161. unset( $order ); 
  162.  
  163. if ( 1 == get_post_meta( $order_id, '_ga_tracked', true ) ) { 
  164. unset( $order ); 
  165.  
  166. if ( isset( $order ) ) { 
  167. if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKCLASSICEC ] ) { 
  168. $dataLayer["transactionId"] = $order->get_order_number(); 
  169. $dataLayer["transactionDate"] = date("c"); 
  170. $dataLayer["transactionType"] = "sale"; 
  171. $dataLayer["transactionAffiliation"] = gtm4wp_woocommerce_html_entity_decode( get_bloginfo( 'name' ) ); 
  172. $dataLayer["transactionTotal"] = $order->get_total(); 
  173. $dataLayer["transactionShipping"] = $order->get_total_shipping(); 
  174. $dataLayer["transactionTax"] = $order->get_total_tax(); 
  175. $dataLayer["transactionPaymentType"] = $order->payment_method_title; 
  176. $dataLayer["transactionCurrency"] = get_woocommerce_currency(); 
  177. $dataLayer["transactionShippingMethod"] = $order->get_shipping_method(); 
  178. $dataLayer["transactionPromoCode"] = implode( ", ", $order->get_used_coupons() ); 
  179.  
  180. if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) { 
  181. $dataLayer["ecommerce"] = array( 
  182. "currencyCode" => get_woocommerce_currency(),  
  183. "purchase" => array( 
  184. "actionField" => array( 
  185. "id" => $order->get_order_number(),  
  186. "affiliation" => gtm4wp_woocommerce_html_entity_decode( get_bloginfo( 'name' ) ),  
  187. "revenue" => $order->get_total(),  
  188. "tax" => $order->get_total_tax(),  
  189. "shipping" => $order->get_total_shipping(),  
  190. "coupon" => implode( ", ", $order->get_used_coupons() ) 
  191. ); 
  192.  
  193. $_products = array(); 
  194. $_sumprice = 0; 
  195. $_product_ids = array(); 
  196.  
  197. if ( $order->get_items() ) { 
  198. foreach ( $order->get_items() as $item ) { 
  199. if ( version_compare( $woocommerce->version, "3.0", ">=" ) ) { 
  200. $product = $item->get_product(); 
  201. } else { 
  202. $product = $order->get_product_from_item( $item ); 
  203.  
  204. $product_id = $product->get_id(); 
  205. $_product_cats = get_the_terms($product->id, 'product_cat'); 
  206. if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) { 
  207. $product_cat = array_pop( $_product_cats ); 
  208. $product_cat = $product_cat->name; 
  209. } else { 
  210. $product_cat = ""; 
  211.  
  212. $remarketing_id = $product_id; 
  213. $product_sku = $product->get_sku(); 
  214. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) { 
  215. $remarketing_id = $product_sku; 
  216.  
  217. $product_price = $order->get_item_total( $item ); 
  218. $_temp_productdata = array( 
  219. "id" => $remarketing_id,  
  220. "name" => $item['name'],  
  221. "sku" => $product_sku ? __( 'SKU:', 'duracelltomi-google-tag-manager' ) . ' ' . $product_sku : $product_id,  
  222. "category" => $product_cat,  
  223. "price" => $product_price,  
  224. "currency" => get_woocommerce_currency(),  
  225. "quantity" => $item['qty'] 
  226. ); 
  227.  
  228. if ( "variation" == $product->get_type() ) { 
  229. $_temp_productdata[ "variant" ] = implode(", ", $product->get_variation_attributes()); 
  230.  
  231. $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "purchase" ); 
  232. $_products[] = $eec_product_array; 
  233.  
  234. $_sumprice += $product_price * $eec_product_array[ "quantity" ]; 
  235. $_product_ids[] = $remarketing_id; 
  236.  
  237. if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKCLASSICEC ] ) { 
  238. $dataLayer["transactionProducts"] = $_products; 
  239. $dataLayer["event"] = "gtm4wp.orderCompleted"; 
  240.  
  241. if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) { 
  242. $dataLayer["ecommerce"]["purchase"]["products"] = $_products; 
  243. $dataLayer["event"] = "gtm4wp.orderCompletedEEC"; 
  244.  
  245. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) { 
  246. $dataLayer["ecomm_prodid"] = $_product_ids; 
  247. $dataLayer["ecomm_pagetype"] = "purchase"; 
  248. $dataLayer["ecomm_totalvalue"] = (float)$_sumprice; 
  249.  
  250. update_post_meta( $order_id, '_ga_tracked', 1 ); 
  251. } else if ( is_checkout() ) { 
  252. if ( ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) || ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) ) { 
  253. $gtm4wp_checkout_products = array(); 
  254. $gtm4wp_checkout_products_remarketing = array(); 
  255. $gtm4wp_totalvalue = 0; 
  256.  
  257. foreach( $woocommerce->cart->get_cart() as $cart_item_id => $cart_item_data) { 
  258. $product = apply_filters( 'woocommerce_cart_item_product', $cart_item_data["data"], $cart_item_data, $cart_item_id ); 
  259.  
  260. $product_id = $product->get_id(); 
  261.  
  262. $_product_cats = get_the_terms($product->id, 'product_cat'); 
  263. if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) { 
  264. $product_cat = array_pop( $_product_cats ); 
  265. $product_cat = $product_cat->name; 
  266. } else { 
  267. $product_cat = ""; 
  268.  
  269. $remarketing_id = $product_id; 
  270. $product_sku = $product->get_sku(); 
  271. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) { 
  272. $remarketing_id = $product_sku; 
  273.  
  274. $_temp_productdata = array( 
  275. "id" => $remarketing_id,  
  276. "name" => $product->post->post_title,  
  277. "price" => $product->get_price(),  
  278. "category" => $product_cat,  
  279. "quantity" => $cart_item_data["quantity"] 
  280. ); 
  281.  
  282. if ( "variation" == $product->get_type() ) { 
  283. $_temp_productdata[ "variant" ] = implode(", ", $product->get_variation_attributes()); 
  284.  
  285. $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "checkout" ); 
  286. $gtm4wp_checkout_products[] = $eec_product_array; 
  287.  
  288. $gtm4wp_checkout_products_remarketing[] = $remarketing_id; 
  289. $gtm4wp_totalvalue += $eec_product_array[ "quantity" ] * $eec_product_array[ "price" ]; 
  290. } // end foreach cart item 
  291.  
  292. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) { 
  293. $dataLayer["ecomm_prodid"] = $gtm4wp_checkout_products_remarketing; 
  294. $dataLayer["ecomm_pagetype"] = "cart"; 
  295. $dataLayer["ecomm_totalvalue"] = (float)$gtm4wp_totalvalue; 
  296.  
  297. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) { 
  298. $dataLayer["ecommerce"] = array( 
  299. "checkout" => array( 
  300. "actionField" => array( 
  301. "step" => 1 
  302. ),  
  303. "products" => $gtm4wp_checkout_products 
  304. ); 
  305.  
  306. $( 'form[name=checkout]' ).on( 'submit', function() { 
  307. var _checkout_option = []; 
  308.  
  309. var _shipping_el = $( '#shipping_method input:checked' ); 
  310. if ( _shipping_el.length > 0 ) { 
  311. _checkout_option.push( 'Shipping: ' + _shipping_el.val() ); 
  312.  
  313. var _payment_el = $( '.payment_methods input:checked' ); 
  314. if ( _payment_el.length > 0 ) { 
  315. _checkout_option.push( 'Payment: ' + _payment_el.val() ); 
  316.  
  317. if ( _checkout_option.length > 0 ) { 
  318. ". $gtm4wp_datalayer_name .".push({ 
  319. 'event': 'gtm4wp.checkoutOptionECC',  
  320. 'ecommerce': { 
  321. 'checkout_option': { 
  322. 'actionField': { 
  323. 'step': 1,  
  324. 'option': _checkout_option.join( ', ' ) 
  325. }); 
  326. });"); 
  327. } else { 
  328. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) { 
  329. $dataLayer["ecomm_pagetype"] = "other"; 
  330.  
  331. if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) { 
  332. $( document ).on( 'click', '.mini_cart_item a.remove, .product-remove a.remove', function() { 
  333. var productdata = $( this ); 
  334.  
  335. var qty = 0; 
  336. var qty_element = $( this ).closest( '.cart_item' ).find( '.product-quantity input.qty' ); 
  337. if ( 0 == qty_element.length ) { 
  338. qty_element = $( this ).closest( '.mini_cart_item' ).find( '.quantity' ); 
  339. if ( qty_element.length > 0 ) { 
  340. qty = parseInt( qty_element.text() ); 
  341.  
  342. if ( isNaN( qty ) ) { 
  343. qty = 0; 
  344. } else { 
  345. qty = qty_element.val(); 
  346.  
  347. if ( 0 == qty ) { 
  348. return true; 
  349.  
  350. ". $gtm4wp_datalayer_name .".push({ 
  351. 'event': 'gtm4wp.removeFromCartEEC',  
  352. 'ecommerce': { 
  353. 'remove': { 
  354. 'products': [{ 
  355. 'name': productdata.data( 'gtm4wp_product_name' ),  
  356. id productdata.data( 'gtm4wp_product_id' ),  
  357. 'price': productdata.data( 'gtm4wp_product_price' ),  
  358. 'category': productdata.data( 'gtm4wp_product_cat' ),  
  359. 'variant': productdata.data( 'gtm4wp_product_variant' ),  
  360. 'quantity': qty 
  361. }] 
  362. }); 
  363. }); 
  364. "); 
  365.  
  366. return $dataLayer;