DuracellTomi's Google Tag Manager for WordPress - Version 1.6

Version Description

  • Fixed: do not block product list item clicks if ad blocker is enabled
  • Fixed: only track product clicks in product lists if link points to the product detail page URL
  • Fixed: PHP warning in backlogs 'Undefined variable: gtm4wp_options'
  • Added: product variation support in WooCommerce integration (enhanced ecommerce implementations should add the GTM event gtm4wp.changeDetailViewEEC to the ecommerce event trigger)
  • Updated: better WooCommerce 3.0 compatibility
Download this release

Release Info

Developer duracelltomi
Plugin Icon 128x128 DuracellTomi's Google Tag Manager for WordPress
Version 1.6
Comparing to
See all releases

Code changes from version 1.5.1 to 1.6

duracelltomi-google-tag-manager-for-wordpress.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /*
3
  Plugin Name: Google Tag Manager for Wordpress
4
- Version: 1.5.1
5
  Plugin URI: https://duracelltomi.com/google-tag-manager-for-wordpress/
6
  Description: The first Google Tag Manager plugin for WordPress with business goals in mind
7
  Author: Thomas Geiger
@@ -10,7 +10,7 @@ Text Domain: duracelltomi-google-tag-manager
10
  Domain Path: /languages
11
  */
12
 
13
- define( 'GTM4WP_VERSION', '1.5.1' );
14
  define( 'GTM4WP_PATH', plugin_dir_path( __FILE__ ) );
15
 
16
  $gtp4wp_plugin_url = plugin_dir_url( __FILE__ );
1
  <?php
2
  /*
3
  Plugin Name: Google Tag Manager for Wordpress
4
+ Version: 1.6
5
  Plugin URI: https://duracelltomi.com/google-tag-manager-for-wordpress/
6
  Description: The first Google Tag Manager plugin for WordPress with business goals in mind
7
  Author: Thomas Geiger
10
  Domain Path: /languages
11
  */
12
 
13
+ define( 'GTM4WP_VERSION', '1.6' );
14
  define( 'GTM4WP_PATH', plugin_dir_path( __FILE__ ) );
15
 
16
  $gtp4wp_plugin_url = plugin_dir_url( __FILE__ );
integration/woocommerce.php CHANGED
@@ -29,10 +29,10 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
29
  }
30
  } else if ( is_product() ) {
31
  if ( ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) || ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) ) {
32
- $prodid = get_the_ID();
 
 
33
 
34
- $product = get_product( $prodid );
35
- $product_price = $product->get_price();
36
  $_product_cats = get_the_terms( $product->id, 'product_cat' );
37
  if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) {
38
  $product_cat = array_pop( $_product_cats );
@@ -41,35 +41,77 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
41
  $product_cat = "";
42
  }
43
 
44
- if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] ) {
45
- $product_sku = $product->get_sku();
46
- if ( "" != $product_sku ) {
47
- $prodid = $product_sku;
 
 
 
48
  }
49
- }
50
 
51
- $_temp_productdata = array(
52
- "name" => gtm4wp_woocommerce_html_entity_decode( get_the_title() ),
53
- "id" => $prodid,
54
- "price" => $product_price,
55
- "category" => $product_cat,
56
- );
57
- $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "productdetail" );
58
 
59
- if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) {
60
- $remarketing_id = (string)$prodid;
61
 
62
- $dataLayer["ecomm_prodid"] = $remarketing_id;
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  $dataLayer["ecomm_pagetype"] = "product";
64
- $dataLayer["ecomm_totalvalue"] = (float)$eec_product_array[ "price" ];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  }
 
 
66
 
67
- if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) {
68
- $dataLayer["ecommerce"] = array(
69
- "detail" => array(
70
- "products" => array($eec_product_array)
71
- )
72
- );
73
  }
74
  }
75
  } else if ( is_cart() ) {
@@ -98,6 +140,7 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
98
  'id': productdata.data( 'gtm4wp_product_id' ),
99
  'price': productdata.data( 'gtm4wp_product_price' ),
100
  'category': productdata.data( 'gtm4wp_product_cat' ),
 
101
  'quantity': _current_value - _original_value
102
  }]
103
  }
@@ -114,6 +157,7 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
114
  'id': productdata.data( 'gtm4wp_product_id' ),
115
  'price': productdata.data( 'gtm4wp_product_price' ),
116
  'category': productdata.data( 'gtm4wp_product_cat' ),
 
117
  'quantity': _original_value - _current_value
118
  }]
119
  }
@@ -130,12 +174,10 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
130
  $products = $woocommerce->cart->get_cart();
131
  $product_ids = array();
132
  foreach( $products as $oneproduct ) {
133
- $remarketing_id = $oneproduct['product_id'];
134
- if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] ) {
135
- $product_sku = isset( $oneproduct['product_sku'] ) ? $oneproduct['product_sku'] : "";
136
- if ( "" != $product_sku ) {
137
- $remarketing_id = $product_sku;
138
- }
139
  }
140
 
141
  $product_ids[] = $remarketing_id;
@@ -206,49 +248,42 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
206
 
207
  if ( $order->get_items() ) {
208
  foreach ( $order->get_items() as $item ) {
209
- $_product = $order->get_product_from_item( $item );
210
-
211
- $variation_data = null;
212
- if (get_class($_product) == "WC_Product_Variation") {
213
- $variation_data = $_product->get_variation_attributes();
214
- }
215
-
216
- if ( isset( $variation_data ) ) {
217
-
218
- $_category = woocommerce_get_formatted_variation( $_product->variation_data, true );
219
 
 
 
 
 
 
220
  } else {
221
- $out = array();
222
- $categories = get_the_terms( $_product->id, 'product_cat' );
223
- if ( $categories ) {
224
- foreach ( $categories as $category ) {
225
- $out[] = $category->name;
226
- }
227
- }
228
-
229
- $_category = implode( " / ", $out );
230
  }
231
 
232
- $remarketing_id = $_product->id;
233
- $product_sku = $_product->get_sku();
234
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
235
  $remarketing_id = $product_sku;
236
  }
237
 
238
- $_prodprice = $order->get_item_total( $item );
239
  $_temp_productdata = array(
240
  "id" => $remarketing_id,
241
  "name" => $item['name'],
242
- "sku" => $product_sku ? __( 'SKU:', 'duracelltomi-google-tag-manager' ) . ' ' . $product_sku : $_product->id,
243
- "category" => $_category,
244
- "price" => $_prodprice,
245
  "currency" => get_woocommerce_currency(),
246
  "quantity" => $item['qty']
247
  );
 
 
 
 
 
248
  $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "purchase" );
249
  $_products[] = $eec_product_array;
250
 
251
- $_sumprice += $eec_product_array[ "price" ] * $eec_product_array[ "quantity" ];
252
  $_product_ids[] = $remarketing_id;
253
  }
254
  }
@@ -280,6 +315,8 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
280
  foreach( $woocommerce->cart->get_cart() as $cart_item_id => $cart_item_data) {
281
  $product = apply_filters( 'woocommerce_cart_item_product', $cart_item_data["data"], $cart_item_data, $cart_item_id );
282
 
 
 
283
  $_product_cats = get_the_terms($product->id, 'product_cat');
284
  if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) {
285
  $product_cat = array_pop( $_product_cats );
@@ -288,7 +325,7 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
288
  $product_cat = "";
289
  }
290
 
291
- $remarketing_id = $product->id;
292
  $product_sku = $product->get_sku();
293
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
294
  $remarketing_id = $product_sku;
@@ -301,6 +338,11 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
301
  "category" => $product_cat,
302
  "quantity" => $cart_item_data["quantity"]
303
  );
 
 
 
 
 
304
  $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "checkout" );
305
  $gtm4wp_checkout_products[] = $eec_product_array;
306
 
@@ -393,6 +435,7 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
393
  'id': productdata.data( 'gtm4wp_product_id' ),
394
  'price': productdata.data( 'gtm4wp_product_price' ),
395
  'category': productdata.data( 'gtm4wp_product_cat' ),
 
396
  'quantity': qty
397
  }]
398
  }
@@ -406,6 +449,8 @@ function gtm4wp_woocommerce_datalayer_filter_items( $dataLayer ) {
406
  }
407
 
408
  function gtm4wp_woocommerce_single_add_to_cart_tracking() {
 
 
409
  if ( ! is_single() ) {
410
  return;
411
  }
@@ -415,8 +460,6 @@ function gtm4wp_woocommerce_single_add_to_cart_tracking() {
415
  return;
416
  }
417
 
418
- global $product, $woocommerce, $gtm4wp_datalayer_name, $gtm4wp_options;
419
-
420
  $_product_cats = get_the_terms($product->id, 'product_cat');
421
  if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) {
422
  $product_cat = array_pop( $_product_cats );
@@ -425,7 +468,8 @@ function gtm4wp_woocommerce_single_add_to_cart_tracking() {
425
  $product_cat = "";
426
  }
427
 
428
- $remarketing_id = $product->id;
 
429
  $product_sku = $product->get_sku();
430
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
431
  $remarketing_id = $product_sku;
@@ -434,7 +478,7 @@ function gtm4wp_woocommerce_single_add_to_cart_tracking() {
434
  $_temp_productdata = array(
435
  "id" => $remarketing_id,
436
  "name" => $product->post->post_title,
437
- "sku" => $product_sku ? __( 'SKU:', 'duracelltomi-google-tag-manager' ) . ' ' . $product_sku : $product->id,
438
  "category" => $product_cat,
439
  "price" => $product->get_price(),
440
  "currency" => get_woocommerce_currency()
@@ -458,21 +502,61 @@ function gtm4wp_woocommerce_single_add_to_cart_tracking() {
458
 
459
  gtm4wp_woocommerce_addjs("
460
  $( '.single_add_to_cart_button' ).click(function() {
461
- ". $gtm4wp_datalayer_name .".push({
462
- 'event': 'gtm4wp.addProductToCartEEC',
463
- 'ecommerce': {
464
- 'currencyCode': '".get_woocommerce_currency()."',
465
- 'add': {
466
- 'products': [{
467
- 'id': '". esc_js( $eec_product_array[ "id" ] ) ."',
468
- 'name': '". esc_js( $eec_product_array[ "name" ] ) ."',
469
- 'price': '". esc_js( $eec_product_array[ "price" ] ) ."',
470
- 'category': '". esc_js( $eec_product_array[ "category" ] ) ."',
471
- 'quantity': jQuery( 'form.cart:first input[name=quantity]' ).val()
472
- }]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473
  }
474
- }
475
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
  });
477
  ");
478
  }
@@ -599,6 +683,10 @@ function gtm4wp_woocommerce_enhanced_ecom_product_click() {
599
 
600
  gtm4wp_woocommerce_addjs("
601
  $( '.products li:not(.product-category) a:not(.add_to_cart_button),.widget-product-item' ).click(function( event ) {
 
 
 
 
602
  var _productdata = $( this ).closest( '.product' );
603
 
604
  if ( _productdata.length > 0 ) {
@@ -620,6 +708,11 @@ function gtm4wp_woocommerce_enhanced_ecom_product_click() {
620
  return true;
621
  }
622
 
 
 
 
 
 
623
  var ctrl_key_pressed = event.ctrlKey;
624
 
625
  event.preventDefault();
@@ -659,6 +752,7 @@ function gtm4wp_woocommerce_enhanced_ecom_product_click() {
659
  function gtm4wp_woocommerce_add_prod_data( $add_to_cart_link ) {
660
  global $product, $woocommerce_loop, $wp_query, $gtm4wp_options;
661
 
 
662
  $_product_cats = get_the_terms($product->id, 'product_cat');
663
  if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) {
664
  $product_cat = array_pop( $_product_cats );
@@ -681,7 +775,7 @@ function gtm4wp_woocommerce_add_prod_data( $add_to_cart_link ) {
681
  $posts_per_page = 1;
682
  }
683
 
684
- $remarketing_id = $product->id;
685
  $product_sku = $product->get_sku();
686
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
687
  $remarketing_id = $product_sku;
@@ -692,29 +786,38 @@ function gtm4wp_woocommerce_add_prod_data( $add_to_cart_link ) {
692
  "name" => $product->post->post_title,
693
  "category" => $product_cat,
694
  "price" => $product->get_price(),
695
- "productlink" => get_permalink(),
696
  "listposition" => $woocommerce_loop[ "loop" ] + ( $posts_per_page * ($paged-1) ),
697
  "listname" => $list_name
698
  );
 
 
 
 
 
 
 
699
  $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "addtocartproductlist" );
700
 
701
- $cartlink_with_data = sprintf('data-gtm4wp_product_id="%s" data-gtm4wp_product_name="%s" data-gtm4wp_product_price="%s" data-gtm4wp_product_cat="%s" data-gtm4wp_product_url="%s" data-gtm4wp_product_listposition="%s" data-gtm4wp_productlist_name="%s" href="',
702
  esc_attr( $eec_product_array[ "id" ] ),
703
  esc_attr( $eec_product_array[ "name" ] ),
704
  esc_attr( $eec_product_array[ "price" ] ),
705
  esc_attr( $eec_product_array[ "category" ] ),
706
  esc_url( $eec_product_array[ "productlink" ] ),
707
  esc_attr( $eec_product_array[ "listposition" ] ),
708
- esc_attr( $eec_product_array[ "listname" ] )
 
709
  );
710
 
711
  return str_replace( 'href="', $cartlink_with_data, $add_to_cart_link );
712
  }
713
 
714
  $GLOBALS["gtm4wp_cart_item_proddata"] = '';
715
- function gtm4wp_woocommerce_cart_item_product_filter( $product ) {
716
  global $gtm4wp_options;
717
 
 
718
  $_product_cats = get_the_terms($product->id, 'product_cat');
719
  if ( ( is_array( $_product_cats ) ) && ( count( $_product_cats ) > 0 ) ) {
720
  $product_cat = array_pop( $_product_cats );
@@ -723,7 +826,7 @@ function gtm4wp_woocommerce_cart_item_product_filter( $product ) {
723
  $product_cat = "";
724
  }
725
 
726
- $remarketing_id = $product->id;
727
  $product_sku = $product->get_sku();
728
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
729
  $remarketing_id = $product_sku;
@@ -734,8 +837,15 @@ function gtm4wp_woocommerce_cart_item_product_filter( $product ) {
734
  "name" => $product->post->post_title,
735
  "price" => $product->get_price(),
736
  "category" => $product_cat,
737
- "productlink" => get_permalink()
738
  );
 
 
 
 
 
 
 
739
  $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "cart" );
740
  $GLOBALS["gtm4wp_cart_item_proddata"] = $eec_product_array;
741
 
@@ -751,12 +861,13 @@ function gtm4wp_woocommerce_cart_item_remove_link_filter( $remove_from_cart_link
751
  return $remove_from_cart_link;
752
  }
753
 
754
- $cartlink_with_data = sprintf('data-gtm4wp_product_id="%s" data-gtm4wp_product_name="%s" data-gtm4wp_product_price="%s" data-gtm4wp_product_cat="%s" data-gtm4wp_product_url="%s" href="',
755
  esc_attr( $GLOBALS["gtm4wp_cart_item_proddata"]["id"] ),
756
  esc_attr( $GLOBALS["gtm4wp_cart_item_proddata"]["name"] ),
757
  esc_attr( $GLOBALS["gtm4wp_cart_item_proddata"]["price"] ),
758
  esc_attr( $GLOBALS["gtm4wp_cart_item_proddata"]["category"] ),
759
- esc_url( $GLOBALS["gtm4wp_cart_item_proddata"]["productlink"] )
 
760
  );
761
  $GLOBALS["gtm4wp_cart_item_proddata"] = '';
762
 
@@ -788,6 +899,7 @@ function gtm4wp_woocommerce_after_template_part( $template_name ) {
788
  ob_end_clean();
789
 
790
  if ( "content-widget-product.php" == $template_name ) {
 
791
  $_product_cats = get_the_terms($product->id, 'product_cat');
792
  if ( ( is_array( $_product_cats ) ) && ( count( $_product_cats ) > 0 ) ) {
793
  $product_cat = array_pop( $_product_cats );
@@ -796,7 +908,7 @@ function gtm4wp_woocommerce_after_template_part( $template_name ) {
796
  $product_cat = "";
797
  }
798
 
799
- $remarketing_id = $product->id;
800
  $product_sku = $product->get_sku();
801
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
802
  $remarketing_id = $product_sku;
@@ -807,7 +919,7 @@ function gtm4wp_woocommerce_after_template_part( $template_name ) {
807
  "name" => $product->post->post_title,
808
  "price" => $product->get_price(),
809
  "category" => $product_cat,
810
- "productlink" => get_permalink(),
811
  "listname" => $gtm4wp_last_widget_title,
812
  "listposition" => $gtm4wp_product_counter
813
  );
@@ -879,6 +991,11 @@ function gtm4wp_before_related_products_loop() {
879
  function gtm4wp_woocommerce_before_shop_loop_item() {
880
  global $product, $woocommerce_loop, $wp_query, $gtm4wp_options;
881
 
 
 
 
 
 
882
  $product_cat = "";
883
  if ( is_product_category() ) {
884
  global $wp_query;
@@ -906,7 +1023,7 @@ function gtm4wp_woocommerce_before_shop_loop_item() {
906
  $posts_per_page = 1;
907
  }
908
 
909
- $remarketing_id = $product->id;
910
  $product_sku = $product->get_sku();
911
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
912
  $remarketing_id = $product_sku;
@@ -917,7 +1034,7 @@ function gtm4wp_woocommerce_before_shop_loop_item() {
917
  "name" => $product->post->post_title,
918
  "price" => $product->get_price(),
919
  "category" => $product_cat,
920
- "productlink" => get_permalink(),
921
  "listname" => $list_name,
922
  "listposition" => $woocommerce_loop[ "loop" ] + ( $posts_per_page * ($paged-1) )
923
  );
29
  }
30
  } else if ( is_product() ) {
31
  if ( ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) || ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) ) {
32
+ $postid = get_the_ID();
33
+ $product = wc_get_product( $postid );
34
+ $product_id = $product->get_id();
35
 
 
 
36
  $_product_cats = get_the_terms( $product->id, 'product_cat' );
37
  if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) {
38
  $product_cat = array_pop( $_product_cats );
41
  $product_cat = "";
42
  }
43
 
44
+ if ( "variable" != $product->product_type ) {
45
+ $product_price = $product->get_price();
46
+ if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] ) {
47
+ $product_sku = $product->get_sku();
48
+ if ( "" != $product_sku ) {
49
+ $product_id = $product_sku;
50
+ }
51
  }
 
52
 
53
+ $_temp_productdata = array(
54
+ "name" => gtm4wp_woocommerce_html_entity_decode( get_the_title() ),
55
+ "id" => $product_id,
56
+ "price" => $product_price,
57
+ "category" => $product_cat,
58
+ );
59
+ $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "productdetail" );
60
 
61
+ if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCREMARKETING ] ) {
62
+ $remarketing_id = (string)$product_id;
63
 
64
+ $dataLayer["ecomm_prodid"] = $remarketing_id;
65
+ $dataLayer["ecomm_pagetype"] = "product";
66
+ $dataLayer["ecomm_totalvalue"] = (float)$eec_product_array[ "price" ];
67
+ }
68
+
69
+ if ( true === $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCTRACKENHANCEDEC ] ) {
70
+ $dataLayer["ecommerce"] = array(
71
+ "detail" => array(
72
+ "products" => array($eec_product_array)
73
+ )
74
+ );
75
+ }
76
+ } else {
77
+ $dataLayer["ecomm_prodid"] = array();
78
  $dataLayer["ecomm_pagetype"] = "product";
79
+ $dataLayer["ecomm_totalvalue"] = 0;
80
+
81
+ gtm4wp_woocommerce_addjs("
82
+ var gtm4wp_product_detail_data = {
83
+ name: '" . gtm4wp_woocommerce_html_entity_decode( get_the_title() ) . "',
84
+ id: 0,
85
+ price: 0,
86
+ category: '" . $product_cat . "',
87
+ variant: ''
88
+ };
89
+
90
+ jQuery(document).on( 'found_variation', function( event, product_variation ) {
91
+ var current_product_detail_data = gtm4wp_product_detail_data;
92
+ current_product_detail_data.id = product_variation.variation_id;
93
+ current_product_detail_data.price = product_variation.display_price;
94
+
95
+ var _tmp = [];
96
+ for( var attrib_key in product_variation.attributes ) {
97
+ _tmp.push( product_variation.attributes[ attrib_key ] );
98
+ }
99
+ current_product_detail_data.variant = _tmp.join(',');
100
+
101
+ ". $gtm4wp_datalayer_name .".push({
102
+ 'event': 'gtm4wp.changeDetailViewEEC',
103
+ 'ecommerce': {
104
+ 'currencyCode': '".get_woocommerce_currency()."',
105
+ 'detail': current_product_detail_data,
106
+ 'ecomm_prodid': current_product_detail_data.id,
107
+ 'ecomm_pagetype': 'product',
108
+ 'ecomm_totalvalue': 0
109
  }
110
+ });
111
+ });
112
 
113
+ jQuery( '.variations select' ).trigger( 'change' );
114
+ ");
 
 
 
 
115
  }
116
  }
117
  } else if ( is_cart() ) {
140
  'id': productdata.data( 'gtm4wp_product_id' ),
141
  'price': productdata.data( 'gtm4wp_product_price' ),
142
  'category': productdata.data( 'gtm4wp_product_cat' ),
143
+ 'variant': productdata.data( 'gtm4wp_product_variant' ),
144
  'quantity': _current_value - _original_value
145
  }]
146
  }
157
  'id': productdata.data( 'gtm4wp_product_id' ),
158
  'price': productdata.data( 'gtm4wp_product_price' ),
159
  'category': productdata.data( 'gtm4wp_product_cat' ),
160
+ 'variant': productdata.data( 'gtm4wp_product_variant' ),
161
  'quantity': _original_value - _current_value
162
  }]
163
  }
174
  $products = $woocommerce->cart->get_cart();
175
  $product_ids = array();
176
  foreach( $products as $oneproduct ) {
177
+ $remarketing_id = ( isset( $oneproduct[ "variation_id" ] ) && ($oneproduct[ "variation_id" ]>0) ) ? $oneproduct[ "variation_id" ] : $oneproduct[ "product_id" ];
178
+ $product_sku = $oneproduct["data"]->get_sku();
179
+ if ( ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] ) && ( "" != $product_sku ) ) {
180
+ $remarketing_id = $product_sku;
 
 
181
  }
182
 
183
  $product_ids[] = $remarketing_id;
248
 
249
  if ( $order->get_items() ) {
250
  foreach ( $order->get_items() as $item ) {
251
+ $product = $item->get_product();
 
 
 
 
 
 
 
 
 
252
 
253
+ $product_id = $product->get_id();
254
+ $_product_cats = get_the_terms($product->id, 'product_cat');
255
+ if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) {
256
+ $product_cat = array_pop( $_product_cats );
257
+ $product_cat = $product_cat->name;
258
  } else {
259
+ $product_cat = "";
 
 
 
 
 
 
 
 
260
  }
261
 
262
+ $remarketing_id = $product_id;
263
+ $product_sku = $product->get_sku();
264
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
265
  $remarketing_id = $product_sku;
266
  }
267
 
268
+ $product_price = $order->get_item_total( $item );
269
  $_temp_productdata = array(
270
  "id" => $remarketing_id,
271
  "name" => $item['name'],
272
+ "sku" => $product_sku ? __( 'SKU:', 'duracelltomi-google-tag-manager' ) . ' ' . $product_sku : $product_id,
273
+ "category" => $product_cat,
274
+ "price" => $product_price,
275
  "currency" => get_woocommerce_currency(),
276
  "quantity" => $item['qty']
277
  );
278
+
279
+ if ( "variation" == $product->get_type() ) {
280
+ $_temp_productdata[ "variant" ] = implode(",", $product->get_variation_attributes());
281
+ }
282
+
283
  $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "purchase" );
284
  $_products[] = $eec_product_array;
285
 
286
+ $_sumprice += $product_price * $eec_product_array[ "quantity" ];
287
  $_product_ids[] = $remarketing_id;
288
  }
289
  }
315
  foreach( $woocommerce->cart->get_cart() as $cart_item_id => $cart_item_data) {
316
  $product = apply_filters( 'woocommerce_cart_item_product', $cart_item_data["data"], $cart_item_data, $cart_item_id );
317
 
318
+ $product_id = $product->get_id();
319
+
320
  $_product_cats = get_the_terms($product->id, 'product_cat');
321
  if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) {
322
  $product_cat = array_pop( $_product_cats );
325
  $product_cat = "";
326
  }
327
 
328
+ $remarketing_id = $product_id;
329
  $product_sku = $product->get_sku();
330
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
331
  $remarketing_id = $product_sku;
338
  "category" => $product_cat,
339
  "quantity" => $cart_item_data["quantity"]
340
  );
341
+
342
+ if ( "variation" == $product->get_type() ) {
343
+ $_temp_productdata[ "variant" ] = implode(",", $product->get_variation_attributes());
344
+ }
345
+
346
  $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "checkout" );
347
  $gtm4wp_checkout_products[] = $eec_product_array;
348
 
435
  'id': productdata.data( 'gtm4wp_product_id' ),
436
  'price': productdata.data( 'gtm4wp_product_price' ),
437
  'category': productdata.data( 'gtm4wp_product_cat' ),
438
+ 'variant': productdata.data( 'gtm4wp_product_variant' ),
439
  'quantity': qty
440
  }]
441
  }
449
  }
450
 
451
  function gtm4wp_woocommerce_single_add_to_cart_tracking() {
452
+ global $product, $woocommerce, $gtm4wp_datalayer_name, $gtm4wp_options;
453
+
454
  if ( ! is_single() ) {
455
  return;
456
  }
460
  return;
461
  }
462
 
 
 
463
  $_product_cats = get_the_terms($product->id, 'product_cat');
464
  if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) {
465
  $product_cat = array_pop( $_product_cats );
468
  $product_cat = "";
469
  }
470
 
471
+ $product_id = $product->get_id();
472
+ $remarketing_id = $product_id;
473
  $product_sku = $product->get_sku();
474
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
475
  $remarketing_id = $product_sku;
478
  $_temp_productdata = array(
479
  "id" => $remarketing_id,
480
  "name" => $product->post->post_title,
481
+ "sku" => $product_sku ? __( 'SKU:', 'duracelltomi-google-tag-manager' ) . ' ' . $product_sku : $product_id,
482
  "category" => $product_cat,
483
  "price" => $product->get_price(),
484
  "currency" => get_woocommerce_currency()
502
 
503
  gtm4wp_woocommerce_addjs("
504
  $( '.single_add_to_cart_button' ).click(function() {
505
+ var _product_form = jQuery( this ).closest( 'form.cart' );
506
+ var _product_var_id = jQuery( '[name=variation_id]', _product_form );
507
+
508
+ if ( _product_var_id.length > 0 ) {
509
+ _product_var_id_val = _product_var_id.val();
510
+ _product_form_variations = _product_form.data( 'product_variations' );
511
+
512
+ _product_form_variations.forEach( function( product_var ) {
513
+ if ( product_var.variation_id == _product_var_id_val ) {
514
+ _product_var_sku = product_var.sku;
515
+ if ( ! _product_var_sku ) {
516
+ _product_var_sku = _product_var_id_val;
517
+ }
518
+
519
+ var _tmp = [];
520
+ for( var attrib_key in product_var.attributes ) {
521
+ _tmp.push( product_var.attributes[ attrib_key ] );
522
+ }
523
+
524
+ ". $gtm4wp_datalayer_name .".push({
525
+ 'event': 'gtm4wp.addProductToCartEEC',
526
+ 'ecommerce': {
527
+ 'currencyCode': '".get_woocommerce_currency()."',
528
+ 'add': {
529
+ 'products': [{
530
+ 'id': " . ($gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] ? "_product_var_sku" : "_product_var_id_val") . ",
531
+ 'name': '". esc_js( $eec_product_array[ "name" ] ) ."',
532
+ 'price': product_var.display_price,
533
+ 'category': '". esc_js( $eec_product_array[ "category" ] ) ."',
534
+ 'variant': _tmp.join(','),
535
+ 'quantity': jQuery( 'form.cart:first input[name=quantity]' ).val()
536
+ }]
537
+ }
538
+ }
539
+ });
540
+
541
  }
542
+ });
543
+ } else {
544
+ ". $gtm4wp_datalayer_name .".push({
545
+ 'event': 'gtm4wp.addProductToCartEEC',
546
+ 'ecommerce': {
547
+ 'currencyCode': '".get_woocommerce_currency()."',
548
+ 'add': {
549
+ 'products': [{
550
+ 'id': '". esc_js( $eec_product_array[ "id" ] ) ."',
551
+ 'name': '". esc_js( $eec_product_array[ "name" ] ) ."',
552
+ 'price': '". esc_js( $eec_product_array[ "price" ] ) ."',
553
+ 'category': '". esc_js( $eec_product_array[ "category" ] ) ."',
554
+ 'quantity': jQuery( 'form.cart:first input[name=quantity]' ).val()
555
+ }]
556
+ }
557
+ }
558
+ });
559
+ }
560
  });
561
  ");
562
  }
683
 
684
  gtm4wp_woocommerce_addjs("
685
  $( '.products li:not(.product-category) a:not(.add_to_cart_button),.widget-product-item' ).click(function( event ) {
686
+ if ( 'undefined' == typeof google_tag_manager ) {
687
+ return true;
688
+ }
689
+
690
  var _productdata = $( this ).closest( '.product' );
691
 
692
  if ( _productdata.length > 0 ) {
708
  return true;
709
  }
710
 
711
+ // only act on links pointing to the product detail page
712
+ if ( productdata.data( 'gtm4wp_product_url' ) != $( this ).attr( 'href' ) ) {
713
+ return true;
714
+ }
715
+
716
  var ctrl_key_pressed = event.ctrlKey;
717
 
718
  event.preventDefault();
752
  function gtm4wp_woocommerce_add_prod_data( $add_to_cart_link ) {
753
  global $product, $woocommerce_loop, $wp_query, $gtm4wp_options;
754
 
755
+ $product_id = $product->get_id();
756
  $_product_cats = get_the_terms($product->id, 'product_cat');
757
  if ( ( is_array($_product_cats) ) && ( count( $_product_cats ) > 0 ) ) {
758
  $product_cat = array_pop( $_product_cats );
775
  $posts_per_page = 1;
776
  }
777
 
778
+ $remarketing_id = $product_id;
779
  $product_sku = $product->get_sku();
780
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
781
  $remarketing_id = $product_sku;
786
  "name" => $product->post->post_title,
787
  "category" => $product_cat,
788
  "price" => $product->get_price(),
789
+ "productlink" => apply_filters( 'the_permalink', get_permalink(), 0),
790
  "listposition" => $woocommerce_loop[ "loop" ] + ( $posts_per_page * ($paged-1) ),
791
  "listname" => $list_name
792
  );
793
+
794
+ if ( "variation" == $product->product_type ) {
795
+ $_temp_productdata[ "variant" ] = implode(",", $product->get_variation_attributes());
796
+ } else {
797
+ $_temp_productdata[ "variant" ] = "";
798
+ }
799
+
800
  $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "addtocartproductlist" );
801
 
802
+ $cartlink_with_data = sprintf('data-gtm4wp_product_id="%s" data-gtm4wp_product_name="%s" data-gtm4wp_product_price="%s" data-gtm4wp_product_cat="%s" data-gtm4wp_product_url="%s" data-gtm4wp_product_listposition="%s" data-gtm4wp_productlist_name="%s" data-gtm4wp_product_variant="%s" href="',
803
  esc_attr( $eec_product_array[ "id" ] ),
804
  esc_attr( $eec_product_array[ "name" ] ),
805
  esc_attr( $eec_product_array[ "price" ] ),
806
  esc_attr( $eec_product_array[ "category" ] ),
807
  esc_url( $eec_product_array[ "productlink" ] ),
808
  esc_attr( $eec_product_array[ "listposition" ] ),
809
+ esc_attr( $eec_product_array[ "listname" ] ),
810
+ esc_attr( $eec_product_array[ "variant" ] )
811
  );
812
 
813
  return str_replace( 'href="', $cartlink_with_data, $add_to_cart_link );
814
  }
815
 
816
  $GLOBALS["gtm4wp_cart_item_proddata"] = '';
817
+ function gtm4wp_woocommerce_cart_item_product_filter( $product, $cart_item, $cart_id ) {
818
  global $gtm4wp_options;
819
 
820
+ $product_id = $product->get_id();
821
  $_product_cats = get_the_terms($product->id, 'product_cat');
822
  if ( ( is_array( $_product_cats ) ) && ( count( $_product_cats ) > 0 ) ) {
823
  $product_cat = array_pop( $_product_cats );
826
  $product_cat = "";
827
  }
828
 
829
+ $remarketing_id = $product_id;
830
  $product_sku = $product->get_sku();
831
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
832
  $remarketing_id = $product_sku;
837
  "name" => $product->post->post_title,
838
  "price" => $product->get_price(),
839
  "category" => $product_cat,
840
+ "productlink" => apply_filters( 'the_permalink', get_permalink(), 0)
841
  );
842
+
843
+ if ( "variation" == $product->product_type ) {
844
+ $_temp_productdata[ "variant" ] = implode(",", $product->get_variation_attributes());
845
+ } else {
846
+ $_temp_productdata[ "variant" ] = "";
847
+ }
848
+
849
  $eec_product_array = apply_filters( GTM4WP_WPFILTER_EEC_PRODUCT_ARRAY, $_temp_productdata, "cart" );
850
  $GLOBALS["gtm4wp_cart_item_proddata"] = $eec_product_array;
851
 
861
  return $remove_from_cart_link;
862
  }
863
 
864
+ $cartlink_with_data = sprintf('data-gtm4wp_product_id="%s" data-gtm4wp_product_name="%s" data-gtm4wp_product_price="%s" data-gtm4wp_product_cat="%s" data-gtm4wp_product_url="%s" data-gtm4wp_product_variant="%s" href="',
865
  esc_attr( $GLOBALS["gtm4wp_cart_item_proddata"]["id"] ),
866
  esc_attr( $GLOBALS["gtm4wp_cart_item_proddata"]["name"] ),
867
  esc_attr( $GLOBALS["gtm4wp_cart_item_proddata"]["price"] ),
868
  esc_attr( $GLOBALS["gtm4wp_cart_item_proddata"]["category"] ),
869
+ esc_url( $GLOBALS["gtm4wp_cart_item_proddata"]["productlink"] ),
870
+ esc_attr( $GLOBALS["gtm4wp_cart_item_proddata"]["variant"] )
871
  );
872
  $GLOBALS["gtm4wp_cart_item_proddata"] = '';
873
 
899
  ob_end_clean();
900
 
901
  if ( "content-widget-product.php" == $template_name ) {
902
+ $product_id = $product->get_id();
903
  $_product_cats = get_the_terms($product->id, 'product_cat');
904
  if ( ( is_array( $_product_cats ) ) && ( count( $_product_cats ) > 0 ) ) {
905
  $product_cat = array_pop( $_product_cats );
908
  $product_cat = "";
909
  }
910
 
911
+ $remarketing_id = $product_id;
912
  $product_sku = $product->get_sku();
913
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
914
  $remarketing_id = $product_sku;
919
  "name" => $product->post->post_title,
920
  "price" => $product->get_price(),
921
  "category" => $product_cat,
922
+ "productlink" => apply_filters( 'the_permalink', get_permalink(), 0),
923
  "listname" => $gtm4wp_last_widget_title,
924
  "listposition" => $gtm4wp_product_counter
925
  );
991
  function gtm4wp_woocommerce_before_shop_loop_item() {
992
  global $product, $woocommerce_loop, $wp_query, $gtm4wp_options;
993
 
994
+ if ( !isset( $product ) ) {
995
+ return;
996
+ }
997
+
998
+ $product_id = $product->get_id();
999
  $product_cat = "";
1000
  if ( is_product_category() ) {
1001
  global $wp_query;
1023
  $posts_per_page = 1;
1024
  }
1025
 
1026
+ $remarketing_id = $product_id;
1027
  $product_sku = $product->get_sku();
1028
  if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCUSESKU ] && ( "" != $product_sku ) ) {
1029
  $remarketing_id = $product_sku;
1034
  "name" => $product->post->post_title,
1035
  "price" => $product->get_price(),
1036
  "category" => $product_cat,
1037
+ "productlink" => apply_filters( 'the_permalink', get_permalink(), 0),
1038
  "listname" => $list_name,
1039
  "listposition" => $woocommerce_loop[ "loop" ] + ( $posts_per_page * ($paged-1) )
1040
  );
public/frontend.php CHANGED
@@ -537,6 +537,16 @@ function gtm4wp_wp_body_open() {
537
  }
538
  }
539
 
 
 
 
 
 
 
 
 
 
 
540
  function gtm4wp_wp_header_begin() {
541
  global $gtm4wp_datalayer_name, $gtm4wp_options;
542
 
@@ -544,7 +554,7 @@ function gtm4wp_wp_header_begin() {
544
  <!-- Google Tag Manager for WordPress by DuracellTomi - http://duracelltomi.com -->
545
  <script data-cfasync="false" type="text/javascript">
546
  var gtm4wp_datalayer_name = "' . $gtm4wp_datalayer_name . '";
547
- var ' . $gtm4wp_datalayer_name . ' = ' . $gtm4wp_datalayer_name . ' || []';
548
 
549
  if ( $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_ENABLED ] ) {
550
  $_gtm_header_content .= '
@@ -564,6 +574,7 @@ function gtm4wp_wp_header_begin() {
564
  // add adwords remarketing tags as suggested here:
565
  // https://support.google.com/tagmanager/answer/3002580?hl=en
566
 
 
567
  $gtm4wp_remarketing_tags = (array) apply_filters( GTM4WP_WPFILTER_COMPILE_REMARKTING, $gtm4wp_datalayer_data );
568
 
569
  $_gtm_header_content .= '
537
  }
538
  }
539
 
540
+ function gtm4wp_filter_visitor_keys( $dataLayer ) {
541
+ foreach( $dataLayer as $dl_key => $dl_value ) {
542
+ if ( strpos( $dl_key, "visitor" ) !== false ) {
543
+ unset( $dataLayer[ $dl_key ] );
544
+ }
545
+ }
546
+
547
+ return $dataLayer;
548
+ }
549
+
550
  function gtm4wp_wp_header_begin() {
551
  global $gtm4wp_datalayer_name, $gtm4wp_options;
552
 
554
  <!-- Google Tag Manager for WordPress by DuracellTomi - http://duracelltomi.com -->
555
  <script data-cfasync="false" type="text/javascript">
556
  var gtm4wp_datalayer_name = "' . $gtm4wp_datalayer_name . '";
557
+ var ' . $gtm4wp_datalayer_name . ' = ' . $gtm4wp_datalayer_name . ' || [];';
558
 
559
  if ( $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_ENABLED ] ) {
560
  $_gtm_header_content .= '
574
  // add adwords remarketing tags as suggested here:
575
  // https://support.google.com/tagmanager/answer/3002580?hl=en
576
 
577
+ add_filter( GTM4WP_WPFILTER_COMPILE_REMARKTING, "gtm4wp_filter_visitor_keys" );
578
  $gtm4wp_remarketing_tags = (array) apply_filters( GTM4WP_WPFILTER_COMPILE_REMARKTING, $gtm4wp_datalayer_data );
579
 
580
  $_gtm_header_content .= '
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: duracelltomi
3
  Donate link: https://duracelltomi.com/
4
  Tags: google tag manager, tag manager, gtm, google, adwords, google adwords, adwords remarketing, remarketing, google analytics, analytics, facebook ads, facebook remarketing, facebook pixel
5
  Requires at least: 3.4.0
6
- Tested up to: 4.6.1
7
- Stable tag: 1.5.1
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl.html
10
 
@@ -271,6 +271,14 @@ If you or your social plugin inserts the Facebook buttons using IFRAMEs (like So
271
 
272
  == Changelog ==
273
 
 
 
 
 
 
 
 
 
274
  = 1.5.1 =
275
 
276
  Fixed: clicks on products in product list pages redirected to undefined URLs with some themes.
@@ -470,6 +478,10 @@ Please report all bugs found in my plugin using the [contact form on my website]
470
 
471
  == Upgrade Notice ==
472
 
 
 
 
 
473
  = 1.5.1 =
474
 
475
  Fixed: clicks on products in product list pages redirected to undefined URLs with some themes.
3
  Donate link: https://duracelltomi.com/
4
  Tags: google tag manager, tag manager, gtm, google, adwords, google adwords, adwords remarketing, remarketing, google analytics, analytics, facebook ads, facebook remarketing, facebook pixel
5
  Requires at least: 3.4.0
6
+ Tested up to: 4.7.3
7
+ Stable tag: 1.6
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl.html
10
 
271
 
272
  == Changelog ==
273
 
274
+ = 1.6 =
275
+
276
+ * Fixed: do not block product list item clicks if ad blocker is enabled
277
+ * Fixed: only track product clicks in product lists if link points to the product detail page URL
278
+ * Fixed: PHP warning in backlogs 'Undefined variable: gtm4wp_options'
279
+ * Added: product variation support in WooCommerce integration (enhanced ecommerce implementations should add the GTM event gtm4wp.changeDetailViewEEC to the ecommerce event trigger)
280
+ * Updated: better WooCommerce 3.0 compatibility
281
+
282
  = 1.5.1 =
283
 
284
  Fixed: clicks on products in product list pages redirected to undefined URLs with some themes.
478
 
479
  == Upgrade Notice ==
480
 
481
+ = 1.6 =
482
+
483
+ If you are using WooCommerce and enhanced ecommerce, please add gtm4wp.changeDetailViewEEC to the ecommerce helper trigger
484
+
485
  = 1.5.1 =
486
 
487
  Fixed: clicks on products in product list pages redirected to undefined URLs with some themes.