gtm4wp_woocommerce_single_add_to_cart_tracking

The Google Tag Manager for WordPress gtm4wp woocommerce single add to cart tracking function.

Description

gtm4wp_woocommerce_single_add_to_cart_tracking(); 

Usage

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

Defined (1)

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

/integration/woocommerce.php  
  1. function gtm4wp_woocommerce_single_add_to_cart_tracking() { 
  2. global $product, $woocommerce, $gtm4wp_datalayer_name, $gtm4wp_options; 
  3.  
  4. if ( ! is_single() ) { 
  5. return; 
  6.  
  7. // exit early if there is nothing to do 
  8. if ( ( false === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKCLASSICEC ] ) && ( false === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) ) { 
  9. return; 
  10.  
  11. $_product_cats = get_the_terms($product->id, 'product_cat'); 
  12. if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) { 
  13. $product_cat = array_pop( $_product_cats ); 
  14. $product_cat = $product_cat->name; 
  15. } else { 
  16. $product_cat = ""; 
  17.  
  18. $product_id = $product->get_id(); 
  19. $remarketing_id = $product_id; 
  20. $product_sku = $product->get_sku(); 
  21. if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) { 
  22. $remarketing_id = $product_sku; 
  23.  
  24. $_temp_productdata = array( 
  25. "id" => $remarketing_id,  
  26. "name" => $product->post->post_title,  
  27. "sku" => $product_sku ? __( 'SKU:', 'duracelltomi-google-tag-manager' ) . ' ' . $product_sku : $product_id,  
  28. "category" => $product_cat,  
  29. "price" => $product->get_price(),  
  30. "currency" => get_woocommerce_currency() 
  31. ); 
  32. $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "addtocartsingle" ); 
  33.  
  34. if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKCLASSICEC ] ) { 
  35. $( '.single_add_to_cart_button' ).click(function() { 
  36. ". $gtm4wp_datalayer_name .".push({ 
  37. 'event': 'gtm4wp.addProductToCart',  
  38. 'productName': '". esc_js( $eec_product_array[ "name" ] ) ."',  
  39. 'productSKU': '". esc_js( $eec_product_array[ "sku" ] ) ."',  
  40. 'productID': '". esc_js( $eec_product_array[ "id" ] ) ."' 
  41. }); 
  42. }); 
  43. "); 
  44.  
  45. if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) { 
  46.  
  47. $( '.single_add_to_cart_button' ).click(function() { 
  48. var _product_form = jQuery( this ).closest( 'form.cart' ); 
  49. var _product_var_id = jQuery( '[name=variation_id]', _product_form ); 
  50.  
  51. if ( _product_var_id.length > 0 ) { 
  52. _product_var_id_val = _product_var_id.val(); 
  53. _product_form_variations = _product_form.data( 'product_variations' ); 
  54.  
  55. _product_form_variations.forEach( function( product_var ) { 
  56. if ( product_var.variation_id == _product_var_id_val ) { 
  57. _product_var_sku = product_var.sku; 
  58. if ( ! _product_var_sku ) { 
  59. _product_var_sku = _product_var_id_val; 
  60.  
  61. var _tmp = []; 
  62. for( var attrib_key in product_var.attributes ) { 
  63. _tmp.push( product_var.attributes[ attrib_key ] ); 
  64.  
  65. ". $gtm4wp_datalayer_name .".push({ 
  66. 'event': 'gtm4wp.addProductToCartEEC',  
  67. 'ecommerce': { 
  68. 'currencyCode': '".get_woocommerce_currency()."',  
  69. 'add': { 
  70. 'products': [{ 
  71. id " . ($gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] ? "_product_var_sku" : "_product_var_id_val") . ",  
  72. 'name': '". esc_js( $eec_product_array[ "name" ] ) ."',  
  73. 'price': product_var.display_price,  
  74. 'category': '". esc_js( $eec_product_array[ "category" ] ) ."',  
  75. 'variant': _tmp.join(', '),  
  76. 'quantity': jQuery( 'form.cart:first input[name=quantity]' ).val() 
  77. }] 
  78. }); 
  79.  
  80. }); 
  81. } else { 
  82. ". $gtm4wp_datalayer_name .".push({ 
  83. 'event': 'gtm4wp.addProductToCartEEC',  
  84. 'ecommerce': { 
  85. 'currencyCode': '".get_woocommerce_currency()."',  
  86. 'add': { 
  87. 'products': [{ 
  88. id '". esc_js( $eec_product_array[ "id" ] ) ."',  
  89. 'name': '". esc_js( $eec_product_array[ "name" ] ) ."',  
  90. 'price': '". esc_js( $eec_product_array[ "price" ] ) ."',  
  91. 'category': '". esc_js( $eec_product_array[ "category" ] ) ."',  
  92. 'quantity': jQuery( 'form.cart:first input[name=quantity]' ).val() 
  93. }] 
  94. }); 
  95. }); 
  96. ");