gtm4wp_woocommerce_enhanced_ecom_product_click

The Google Tag Manager for WordPress gtm4wp woocommerce enhanced ecom product click function.

Description

gtm4wp_woocommerce_enhanced_ecom_product_click(); 

Usage

  1. if ( !function_exists( 'gtm4wp_woocommerce_enhanced_ecom_product_click' ) ) { 
  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_enhanced_ecom_product_click(); 
  7.  

Defined (1)

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

/integration/woocommerce.php  
  1. function gtm4wp_woocommerce_enhanced_ecom_product_click() { 
  2. global $woocommerce, $gtm4wp_datalayer_name; 
  3.  
  4. $( '.products li:not(.product-category) a:not(.add_to_cart_button), .widget-product-item' ).click(function( event ) { 
  5. if ( 'undefined' == typeof google_tag_manager ) { 
  6. return true; 
  7.  
  8. var _productdata = $( this ).closest( '.product' ); 
  9.  
  10. if ( _productdata.length > 0 ) { 
  11. var productdata = _productdata.find( '.gtm4wp_productdata' ); 
  12.  
  13. } else { 
  14. var _productdata = $( this ).closest( 'ul.products li' ); 
  15.  
  16. if ( _productdata.length > 0 ) { 
  17. var productdata = _productdata.find( '.gtm4wp_productdata' ); 
  18.  
  19. } else { 
  20. var productdata = jQuery( this ); 
  21.  
  22.  
  23. if ( ( 'undefined' == typeof productdata.data( 'gtm4wp_product_id' ) ) || ( '' == productdata.data( 'gtm4wp_product_id' ) ) ) { 
  24. return true; 
  25.  
  26. // only act on links pointing to the product detail page 
  27. if ( productdata.data( 'gtm4wp_product_url' ) != $( this ).attr( 'href' ) ) { 
  28. return true; 
  29.  
  30. var ctrl_key_pressed = event.ctrlKey; 
  31.  
  32. event.preventDefault(); 
  33. if ( ctrl_key_pressed ) { 
  34. // we need to open the new tab/page here so that popup blocker of thebrowserdoesn't block our code 
  35. var _productpage = window.open( 'about:blank', '_blank' ); 
  36.  
  37. ". $gtm4wp_datalayer_name .".push({ 
  38. 'event': 'gtm4wp.productClickEEC',  
  39. 'ecommerce': { 
  40. 'currencyCode': '".get_woocommerce_currency()."',  
  41. 'click': { 
  42. 'actionField': {'list': productdata.data( 'gtm4wp_productlist_name' )},  
  43. 'products': [{ 
  44. id productdata.data( 'gtm4wp_product_id' ),  
  45. 'name': productdata.data( 'gtm4wp_product_name' ),  
  46. 'price': productdata.data( 'gtm4wp_product_price' ),  
  47. 'category': productdata.data( 'gtm4wp_product_cat' ),  
  48. 'position': productdata.data( 'gtm4wp_product_listposition' ) 
  49. }] 
  50. },  
  51. 'eventCallback': function() { 
  52. if ( ctrl_key_pressed && _productpage ) { 
  53. _productpage.location.href= productdata.data( 'gtm4wp_product_url' ); 
  54. } else { 
  55. document.location.href = productdata.data( 'gtm4wp_product_url' ) 
  56. },  
  57. 'eventTimeout': 2000 
  58. }); 
  59. }); 
  60. ");