Enhanced Ecommerce Google Analytics Plugin for WooCommerce - Version 1.0.7

Version Description

  • 28/08/2014 =
    • Added new feature - Display Advertising Feature
    • Fixed-Allow back quotes and single quotes in product name, category name etc.
Download this release

Release Info

Developer Tatvic
Plugin Icon 128x128 Enhanced Ecommerce Google Analytics Plugin for WooCommerce
Version 1.0.7
Comparing to
See all releases

Code changes from version 1.0.6.1 to 1.0.7

includes/class-wc-enhanced-ecommerce-google-analytics-integration.php CHANGED
@@ -19,42 +19,46 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
19
  * @return void
20
  */
21
  public function __construct() {
22
- $this->id = 'enhanced_ecommerce_google_analytics';
23
- $this->method_title = __('Enhanced Ecommerce Google Analytics', 'woocommerce');
24
- $this->method_description = __('Enhanced Ecommerce is a new feature of Universal Analytics that generates detailed statistics about the users journey from product page to thank you page on your e-store. <br/><a href="http://www.tatvic.com/blog/enhanced-ecommerce/">Know more about enhanced e-commerce.</a>', 'woocommerce');
25
 
26
  // Load the settings.
27
  $this->init_form_fields();
28
  $this->init_settings();
29
 
30
  // Define user set variables
31
- $this->ga_email = $this->get_option('ga_email');
32
- $this->ga_id = $this->get_option('ga_id');
33
- $this->ga_set_domain_name = $this->get_option('ga_set_domain_name');
34
- $this->ga_standard_tracking_enabled = $this->get_option('ga_standard_tracking_enabled');
35
- $this->enable_guest_checkout = get_option('woocommerce_enable_guest_checkout') == 'yes' ? true : false;
36
- $this->track_login_step_for_guest_user = $this->get_option('track_login_step_for_guest_user') == 'yes' ? true : false;
37
- $this->ga_enhanced_ecommerce_tracking_enabled = $this->get_option('ga_enhanced_ecommerce_tracking_enabled');
38
- $this->ga_enhanced_ecommerce_category_page_impression_thresold = $this->get_option('ga_enhanced_ecommerce_category_page_impression_thresold');
 
39
 
40
  // Actions
41
- add_action('woocommerce_update_options_integration_enhanced_ecommerce_google_analytics', array($this, 'process_admin_options'));
42
  // API Call to LS with e-mail
43
  // Tracking code
44
- add_action('wp_head', array($this, 'google_tracking_code'));
45
- add_action('woocommerce_thankyou', array($this, 'ecommerce_tracking_code'));
46
 
47
  // Enhanced Ecommerce product impression hook
48
- add_action('woocommerce_after_shop_loop', array($this, 'cate_page_prod_impression')); // Hook for category page
49
- add_action('woocommerce_after_shop_loop_item', array($this, 'product_impression'));
50
- add_action('woocommerce_after_single_product', array($this, 'product_detail_view'));
51
- add_action('woocommerce_after_cart', array($this, 'remove_cart_tracking'));
52
- add_action('woocommerce_after_checkout_form', array($this, 'checkout_step_one_tracking'));
53
 
54
  // Event tracking code
55
- add_action('woocommerce_after_add_to_cart_button', array($this, 'add_to_cart'));
56
- add_action('wp_footer', array($this, 'loop_add_to_cart'));
57
- add_action('wp_footer', array($this, 'default_pageview'));
 
 
 
58
  }
59
 
60
  /**
@@ -66,65 +70,72 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
66
  function init_form_fields() {
67
 
68
  $this->form_fields = array(
69
- 'ga_email' => array(
70
- 'title' => __('Email Address', 'woocommerce'),
71
- 'description' => __('Provide your work email for updates on the plugin enhancement.', 'woocommerce'),
72
- 'type' => 'email',
73
- 'required' => 'required',
74
- 'default' => get_option('woocommerce_ga_email') // Backwards compat
 
 
 
 
 
 
75
  ),
76
- 'ga_id' => array(
77
- 'title' => __('Google Analytics ID', 'woocommerce'),
78
- 'description' => __('Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce'),
79
- 'type' => 'text',
80
- 'default' => get_option('woocommerce_ga_id') // Backwards compat
81
  ),
82
- 'ga_set_domain_name' => array(
83
- 'title' => __('Set Domain Name', 'woocommerce'),
84
- 'description' => sprintf(__('(Optional) Sets the <code>_setDomainName</code> variable. <a href="%s">See here for more information</a>.', 'woocommerce'), 'https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#multipleDomains'),
85
- 'type' => 'text',
86
- 'default' => ''
 
 
87
  ),
88
- 'ga_standard_tracking_enabled' => array(
89
- 'title' => __('Tracking code', 'woocommerce'),
90
- 'label' => __('Add tracking code to your site using this plugin &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Optional).', 'woocommerce'),
91
- 'description' => sprintf(__('You don\'t need to enable this if using a 3rd party analytics plugin. If you chose to add Universal Analytics code snippet via Third party plugins,<br/> Add <code>ga(\'require\', \'ec\', \'ec.js\');</code> below <code>ga(\'create\',\'UA-XXXXX-X\')</code> in your standard code snippet. <br/> Also ensure that the Universal Analytics code is present in the &lt;head&gt; section of the website.', 'woocommerce')),
92
- 'type' => 'checkbox',
93
- 'checkboxgroup' => 'start',
94
- 'default' => get_option('woocommerce_ga_standard_tracking_enabled') ? get_option('woocommerce_ga_standard_tracking_enabled') : 'no' // Backwards compat
95
  ),
96
- 'ga_enhanced_ecommerce_tracking_enabled' => array(
97
- 'label' => __('Enable Enhanced eCommerce tracking', 'woocommerce'),
98
- 'type' => 'checkbox',
99
- 'checkboxgroup' => '',
100
- 'default' => get_option('woocommerce_ga_ecommerce_tracking_enabled') ? get_option('woocommerce_ga_ecommerce_tracking_enabled') : 'no' // Backwards compat
101
  ),
102
- 'track_login_step_for_guest_user' => array(
103
- 'label' => __('Track Login step for Guest users if Guest Checkout is enabled &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Optional).', 'woocommerce'),
104
- 'type' => 'checkbox',
105
- 'checkboxgroup' => '',
106
- 'description' => sprintf(__('For Guest users Login is not a mandatory step. Checking the box would consider the click event on place order as Login as well as Checkout.', 'woocommerce')),
107
- 'default' => get_option('track_login_step_for_guest_user') ? get_option('track_login_step_for_guest_user') : 'no' // Backwards compat
108
  ),
109
- 'ga_enhanced_ecommerce_category_page_impression_thresold' => array(
110
- 'title' => __('Impression Threasold', 'woocommerce'),
111
- 'description' => sprintf(__('Impression thresold for category page. Send hit after these many number of products impressions', 'woocommerce')),
112
- 'type' => 'input',
113
- 'default' => '6'
114
  )
115
  );
116
  /* When user updates the email, post it to the remote server */
117
- if (isset($_GET['tab']) && isset($_REQUEST['section']) && isset($_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'])) {
118
 
119
- $current_tab = ( empty($_GET['tab']) ) ? false : sanitize_text_field(urldecode($_GET['tab']));
120
- $current_section = ( empty($_REQUEST['section']) ) ? false : sanitize_text_field(urldecode($_REQUEST['section']));
121
 
122
  $save_for_the_plugin = ($current_tab == "integration" ) && ($current_section == "enhanced_ecommerce_google_analytics");
123
- $update_made_for_email = $_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'] != $this->get_option('woocommerce_enhanced_ecommerce_google_analytics_ga_email');
124
 
125
  if ($save_for_the_plugin && $update_made_for_email) {
126
- if ($_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'] != '') {
127
- $email = $_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'];
128
  $this->send_email_to_tatvic($email);
129
  }
130
  }
@@ -140,7 +151,7 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
140
  * @return void
141
  */
142
  function google_tracking_code() {
143
- if (is_admin() || current_user_can('manage_options') || $this->ga_standard_tracking_enabled == "no") {
144
  return;
145
  }
146
 
@@ -154,18 +165,26 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
154
  if (!empty($this->ga_set_domain_name)) {
155
  $set_domain_name = esc_js($this->ga_set_domain_name);
156
  } else {
157
- $set_domain_name = 'auto';
158
  }
159
 
160
- echo "<script>
161
- (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
 
 
 
 
 
 
 
162
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
163
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
164
- })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
165
- //Plugin Version :1.0.6.1
166
- ga('create', '" . esc_js($tracking_id) . "', '" . $set_domain_name . "');
167
- ga('require', 'ec', 'ec.js');
168
- </script>";
 
169
 
170
  }
171
 
@@ -179,7 +198,7 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
179
  function ecommerce_tracking_code($order_id) {
180
  global $woocommerce;
181
 
182
- if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled) || current_user_can('manage_options') || get_post_meta($order_id, '_ga_tracked', true) == 1)
183
  return;
184
 
185
  $tracking_id = $this->ga_id;
@@ -188,7 +207,7 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
188
  return;
189
 
190
  // Doing eCommerce tracking so unhook standard tracking from the footer
191
- remove_action('wp_footer', array($this, 'google_tracking_code'));
192
 
193
  // Get the order and output tracking code
194
  $order = new WC_Order($order_id);
@@ -196,18 +215,25 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
196
  if (!empty($this->ga_set_domain_name)) {
197
  $set_domain_name = esc_js($this->ga_set_domain_name);
198
  } else {
199
- $set_domain_name = 'auto';
200
  }
201
- $code = " (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
 
 
 
 
 
 
 
 
202
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
203
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
204
- })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
205
- //Plugin Version :1.0.6.1
206
- ga('create', '" . esc_js($tracking_id) . "', '" . $set_domain_name . "');
207
- ga('require', 'ec', 'ec.js');
208
-
209
-
210
- ";
211
 
212
 
213
  // Order items
@@ -215,41 +241,40 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
215
  foreach ($order->get_items() as $item) {
216
  $_product = $order->get_product_from_item($item);
217
 
218
- $code .= "ga('ec:addProduct', {";
219
- $code .= "'name': '" . esc_js($item['name']) . "',";
220
- $code .= "'id': '" . esc_js($_product->get_sku()) . "',";
221
-
222
  if (isset($_product->variation_data)) {
223
 
224
- $code .= "'category': '" . esc_js(woocommerce_get_formatted_variation($_product->variation_data, true)) . "',";
225
  } else {
226
  $out = array();
227
- $categories = get_the_terms($_product->id, 'product_cat');
228
  if ($categories) {
229
  foreach ($categories as $category) {
230
  $out[] = $category->name;
231
  }
232
  }
233
- $code .= "'category': '" . esc_js(join("/", $out)) . "',";
234
  }
235
 
236
- $code .= "'price': '" . esc_js($order->get_item_total($item)) . "',";
237
- $code .= "'quantity': '" . esc_js($item['qty']) . "'";
238
  $code .= "});";
239
  }
240
  }
241
 
242
- $code .="ga('ec:setAction','purchase', {
243
- 'id': '" . esc_js($order->get_order_number()) . "', // Transaction ID. Required
244
- 'affiliation': '" . esc_js(get_bloginfo('name')) . "', // Affiliation or store name
245
- 'revenue': '" . esc_js($order->get_total()) . "', // Grand Total
246
- 'shipping': '" . esc_js($order->get_shipping()) . "', // Shipping
247
- 'tax': '" . esc_js($order->get_total_tax()) . "' // Tax
248
- });";
249
 
250
- echo '<script type="text/javascript">' . $code . '</script>';
251
 
252
- update_post_meta($order_id, '_ga_tracked', 1);
253
  }
254
 
255
  /**
@@ -268,109 +293,106 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
268
  $t_category="Search Results";
269
  }
270
  global $product, $woocommerce;
271
- $impression_thresold = $this->ga_enhanced_ecommerce_category_page_impression_thresold;
272
-
273
- //$parameters = array();
274
- //$parameters['label'] = "'" . esc_js($product->get_sku() ? __('SKU:', 'woocommerce') . ' ' . $product->get_sku() : "#" . $product->id ) . "'";
275
- if (version_compare($woocommerce->version, '2.1', '>=')) {
276
- wc_enqueue_js("
277
  t_cnt=0;
278
- t_ttl_prod=jQuery('.products li').length;
279
- jQuery('.products li').each(function(index){
280
  t_cnt++;
281
- ga('ec:addImpression', {
282
- 'id': jQuery(this).find('.ls-pro-sku').val(),
283
- 'name': jQuery(this).find('.ls-pro-name').val(),
284
- 'category': jQuery(this).find('.ls-pro-category').val(),
285
- 'price': jQuery(this).find('.ls-pro-price').val(),
286
- 'position': index+1
287
  });
288
 
289
- if(t_ttl_prod > " .esc_js($impression_thresold)."){
290
- if((t_cnt%" .esc_js($impression_thresold).")==0){
291
- t_ttl_prod=t_ttl_prod-".esc_js($impression_thresold).";
292
- ga('send', 'event', 'ecommerce', 'product_impression_cp', {'nonInteraction': 1});
293
  }
294
  }else{
295
  t_ttl_prod--;
296
  if(t_ttl_prod==0){
297
- ga('send', 'event', 'ecommerce', 'product_impression_cp', {'nonInteraction': 1});
298
  }
299
  }
300
 
301
- jQuery(this).find('a:not(.add_to_cart_button)').on('click',function(){
302
 
303
- if('".esc_js($t_category)."'==''){
304
- t_category=jQuery(this).parents('li').find('.ls-pro-category').val();
305
  }else{
306
- t_category='".esc_js($t_category)."';
307
  }
308
- ga('ec:addProduct', {
309
- 'id': jQuery(this).parents('li').find('.ls-pro-sku').val(),
310
- 'name': jQuery(this).parents('li').find('.ls-pro-name').val(),
311
- 'category': jQuery(this).parents('li').find('.ls-pro-category').val(),
312
- 'price': jQuery(this).parents('li').find('.ls-pro-price').val(),
313
- 'position': index+1
314
  });
315
- ga('ec:setAction', 'click', {list: t_category});
316
- ga('send', 'event', 'ecommerce', 'product_click', {'nonInteraction': 1});
317
 
318
 
319
  });
320
  });
321
 
322
- ");
323
 
324
  } else {
325
 
326
- $woocommerce->add_inline_js("
327
  t_cnt=0;
328
- t_ttl_prod=jQuery('.products li').length;
329
- jQuery('.products li').each(function(index){
330
  t_cnt++;
331
- ga('ec:addImpression', {
332
- 'id': jQuery(this).find('.ls-pro-sku').val(),
333
- 'name': jQuery(this).find('.ls-pro-name').val(),
334
- 'category': jQuery(this).find('.ls-pro-category').val(),
335
- 'price': jQuery(this).find('.ls-pro-price').val(),
336
- 'position': index+1
337
  });
338
 
339
- if(t_ttl_prod > " .esc_js($impression_thresold)."){
340
- if((t_cnt%" .esc_js($impression_thresold).")==0){
341
- t_ttl_prod=t_ttl_prod-".esc_js($impression_thresold).";
342
- ga('send', 'event', 'ecommerce', 'product_impression_cp', {'nonInteraction': 1});
343
  }
344
  }else{
345
  t_ttl_prod--;
346
  if(t_ttl_prod==0){
347
- ga('send', 'event', 'ecommerce', 'product_impression_cp', {'nonInteraction': 1});
348
  }
349
  }
350
 
351
- jQuery(this).find('a:not(.add_to_cart_button)').on('click',function(){
352
 
353
- if('".esc_js($t_category)."'==''){
354
- t_category=jQuery(this).parents('li').find('.ls-pro-category').val();
355
  }else{
356
- t_category='".esc_js($t_category)."';
357
  }
358
 
359
- ga('ec:addProduct', {
360
- 'id': jQuery(this).parents('li').find('.ls-pro-sku').val(),
361
- 'name': jQuery(this).parents('li').find('.ls-pro-name').val(),
362
- 'category': jQuery(this).parents('li').find('.ls-pro-category').val(),
363
- 'price': jQuery(this).parents('li').find('.ls-pro-price').val(),
364
- 'position': index+1
365
  });
366
- ga('ec:setAction', 'click', {list: t_category});
367
- ga('send', 'event', 'ecommerce', 'product_click', {'nonInteraction': 1});
368
 
369
 
370
  });
371
  });
372
 
373
- ");
374
  }
375
  }
376
 
@@ -393,36 +415,36 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
393
  $parameters = array();
394
 
395
  // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
396
- $parameters['label'] = "'" . esc_js($product->get_sku() ? __('SKU:', 'woocommerce') . ' ' . $product->get_sku() : "#" . $product->id ) . "'";
397
 
398
- if (version_compare($woocommerce->version, '2.1', '>=')) {
399
- wc_enqueue_js("
400
- $('.single_add_to_cart_button').click(function() {
401
 
402
  // Enhanced E-commerce Add to cart clicks
403
- ga('ec:addProduct', {
404
- 'id': '" . esc_js($product->get_sku()) . "',
405
- 'name': '" . esc_js($product->get_title()) . "',
406
- 'price': '" . esc_js($product->get_price()) . "',
407
  });
408
- ga('ec:setAction', 'add');
409
- ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
410
  });
411
- ");
412
  } else {
413
- $woocommerce->add_inline_js("
414
- $('.single_add_to_cart_button').click(function() {
415
 
416
  // Enhanced E-commerce Add to cart clicks
417
- ga('ec:addProduct', {
418
- 'id': '" . esc_js($product->get_sku()) . "',
419
- 'name': '" . esc_js($product->get_title()) . "',
420
- 'price': '" . esc_js($product->get_price()) . "',
421
  });
422
- ga('ec:setAction', 'add');
423
- ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
424
  });
425
- ");
426
  }
427
  }
428
 
@@ -441,41 +463,41 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
441
 
442
  $parameters = array();
443
  // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
444
- $parameters['category'] = "'" . __('Products', 'woocommerce') . "'";
445
- $parameters['action'] = "'" . __('Add to Cart', 'woocommerce') . "'";
446
- $parameters['label'] = "($(this).data('product_sku')) ? ('SKU: ' + $(this).data('product_sku')) : ('#' + $(this).data('product_id'))"; // Product SKU or ID
447
 
448
- if (version_compare($woocommerce->version, '2.1', '>=')) {
449
 
450
- wc_enqueue_js("
451
- $('.add_to_cart_button:not(.product_type_variable, .product_type_grouped)').click(function() {
452
 
453
  // Enhanced E-commerce Add to cart clicks
454
- ga('ec:addProduct', {
455
- 'id': $(this).parents('li').find('.ls-pro-sku').val(),
456
- 'name': $(this).parents('li').find('.ls-pro-name').val(),
457
- 'category': $(this).parents('li').find('.ls-pro-category').val(),
458
- 'price': $(this).parents('li').find('.ls-pro-price').val(),
459
  });
460
- ga('ec:setAction', 'add');
461
- ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
462
  });
463
- ");
464
  } else {
465
- $woocommerce->add_inline_js("
466
- $('.add_to_cart_button:not(.product_type_variable, .product_type_grouped)').click(function() {
467
 
468
  // Enhanced E-commerce Add to cart clicks
469
- ga('ec:addProduct', {
470
- 'id': $(this).parents('li').find('.ls-pro-sku').val(),
471
- 'name': $(this).parents('li').find('.ls-pro-name').val(),
472
- 'category': $(this).parents('li').find('.ls-pro-category').val(),
473
- 'price': $(this).parents('li').find('.ls-pro-price').val(),
474
  });
475
- ga('ec:setAction', 'add');
476
- ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
477
  });
478
- ");
479
  }
480
  }
481
 
@@ -493,28 +515,31 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
493
 
494
  global $product;
495
  global $woocommerce;
496
- $category = get_the_terms($product->ID, 'product_cat');
497
- $categories = '';
498
  foreach ($category as $term) {
499
- $categories.=$term->name . ',';
500
  }
501
- if (version_compare($woocommerce->version, '2.1', '>=')) {
 
 
 
502
 
503
- wc_enqueue_js("ga('ec:addImpression', {
504
- 'id': '" . $product->get_sku() . "', // Product details are provided in an impressionFieldObject.
505
- 'name': '" . $product->get_title() . "',
506
- 'category': '" . $categories . "',
507
  });
508
- ga('ec:setAction', 'detail');
509
- ");
510
  } else {
511
- $woocommerce->add_inline_js("ga('ec:addImpression', {
512
- 'id': '" . $product->get_sku() . "', // Product details are provided in an impressionFieldObject.
513
- 'name': '" . $product->get_title() . "',
514
- 'category': '" . $categories . "',
515
  });
516
- ga('ec:setAction', 'detail');
517
- ");
518
  }
519
  }
520
 
@@ -536,18 +561,18 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
536
 
537
  global $product;
538
 
539
- $category = get_the_terms($product->ID, 'product_cat');
540
- $categories = '';
541
  foreach ($category as $term) {
542
- $categories.=$term->name . ',';
543
  }
544
  //remove last comma(,) if multiple categories are there
545
  $categories = rtrim($categories, ",");
546
 
547
- echo "<input type='hidden' class='ls-pro-price' value='" . esc_html($product->get_price()) . "'/>"
548
- . "<input type='hidden' class='ls-pro-sku' value='" . $product->get_sku() . "'/>"
549
- . "<input type='hidden' class='ls-pro-name' value='" . $product->get_title() . "'/>"
550
- . "<input type='hidden' class='ls-pro-category' value='" . $categories . "'/>";
551
  }
552
 
553
  /**
@@ -561,28 +586,29 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
561
  return;
562
  }
563
  global $woocommerce;
564
- if (version_compare($woocommerce->version, '2.1', '>=')) {
565
- wc_enqueue_js("$('.remove').click(function(){
566
- ga('ec:addProduct', {
567
- 'name': $(this).parents('tr').find('.product-name').text(),
568
- 'price': $(this).parents('tr').find('.product-price').text(),
569
- 'quantity': $(this).parents('tr').find('.product-quantity .qty').val()
570
  });
571
- ga('ec:setAction', 'remove');
572
- ga('send', 'event', 'Enhanced-Ecommerce', 'click', 'remove from cart',{'nonInteraction': 1});
573
- });"
574
- );
575
  } else {
576
- $woocommerce->add_inline_js("$('.remove').click(function(){
577
- ga('ec:addProduct', {
578
- 'name': $(this).parents('tr').find('.product-name').text(),
579
- 'price': $(this).parents('tr').find('.product-price').text(),
580
- 'quantity': $(this).parents('tr').find('.product-quantity .qty').val()
 
 
 
 
581
  });
582
- ga('ec:setAction', 'remove');
583
- ga('send', 'event', 'Enhanced-Ecommerce', 'click', 'remove from cart',{'nonInteraction': 1});
584
- });"
585
- );
586
  }
587
  }
588
 
@@ -600,35 +626,37 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
600
  global $woocommerce;
601
 
602
  foreach ($woocommerce->cart->cart_contents as $item) {
603
- $p = get_product($item['product_id']);
604
 
605
- $category = get_the_terms($item['product_id'], 'product_cat');
606
- $categories = '';
607
  foreach ($category as $term) {
608
- $categories.=$term->name . ',';
609
  }
610
-
611
- $code = "ga('ec:addProduct', {" . "'id': '" . esc_js($p->get_sku()) . "',";
612
- $code .= "'name': '" . esc_js($p->get_title()) . "',";
613
- $code .= "'category': '" . esc_js($categories) . "',";
614
- $code .= "'price': '" . esc_js($p->get_price()) . "',";
615
- $code .= "'quantity': '" . esc_js($item['quantity']) . "'" . "});";
 
 
616
  }
617
 
618
- $code_step_1 = $code . "ga('ec:setAction','checkout',{'step': 1});";
619
- $code_step_1 .= "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1});";
620
- if (version_compare($woocommerce->version, '2.1', '>=')) {
621
  wc_enqueue_js($code_step_1);
622
  } else {
623
  $woocommerce->add_inline_js($code_step_1);
624
  }
625
 
626
 
627
- $code_step_2 = $code . "ga('ec:setAction','checkout',{'step': 2});";
628
- $code_step_2 .= "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1});";
629
 
630
  if (is_user_logged_in()) {
631
- if (version_compare($woocommerce->version, '2.1', '>=')) {
632
  wc_enqueue_js($code_step_2);
633
  } else {
634
  $woocommerce->add_inline_js($code_step_2);
@@ -636,16 +664,16 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
636
  }
637
  $step_2_on_proceed_to_pay = (!is_user_logged_in() && !$this->enable_guest_checkout ) || (!is_user_logged_in() && $this->enable_guest_checkout && $this->track_login_step_for_guest_user);
638
 
639
- $code_step_3 = $code . "ga('ec:setAction','checkout',{'step': 3});";
640
- $code_step_3 .= "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1});";
641
 
642
- $inline_js = "jQuery(document).on('click','#place_order',function(e){";
643
  if ($step_2_on_proceed_to_pay) {
644
  $inline_js .= $code_step_2;
645
  }
646
  $inline_js .= $code_step_3;
647
  $inline_js .= "});";
648
- if (version_compare($woocommerce->version, '2.1', '>=')) {
649
  wc_enqueue_js($inline_js);
650
  } else {
651
  $woocommerce->add_inline_js($inline_js);
@@ -666,12 +694,12 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
666
  }
667
 
668
  if (!$this->disable_tracking($this->ga_standard_tracking_enabled)) {
669
- $inline_js = "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1})";
670
  } else {
671
- $inline_js = "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1});";
672
  }
673
 
674
- if (version_compare($woocommerce->version, '2.1', '>=')) {
675
  wc_enqueue_js($inline_js);
676
  } else {
677
  $woocommerce->add_inline_js($inline_js);
@@ -686,11 +714,34 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
686
  * @return bool
687
  */
688
  private function disable_tracking($type) {
689
- if (is_admin() || current_user_can('manage_options') || (!$this->ga_id ) || 'no' == $type) {
690
  return true;
691
  }
692
  }
693
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
694
  /**
695
  * Sending email to remote server
696
  *
@@ -699,17 +750,17 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
699
  */
700
  public function send_email_to_tatvic($email) {
701
  //set POST variables
702
- $url = 'http://dev.tatvic.com/leadgen/woocommerce-plugin/store_email/';
703
  $fields = array(
704
- 'email' => urlencode($email),
705
  );
706
  wp_remote_post($url, array(
707
- 'method' => 'POST',
708
- 'timeout' => 1,
709
- 'httpversion' => '1.0',
710
- 'blocking' => false,
711
- 'headers' => array(),
712
- 'body' => $fields
713
  )
714
  );
715
  }
19
  * @return void
20
  */
21
  public function __construct() {
22
+ $this->id = "enhanced_ecommerce_google_analytics";
23
+ $this->method_title = __("Enhanced Ecommerce Google Analytics", "woocommerce");
24
+ $this->method_description = __("Enhanced Ecommerce is a new feature of Universal Analytics that generates detailed statistics about the users journey from product page to thank you page on your e-store. <br/><a href='http://www.tatvic.com/blog/enhanced-ecommerce/' target='_blank'>Know more about Enhanced Ecommerce.</a>", "woocommerce");
25
 
26
  // Load the settings.
27
  $this->init_form_fields();
28
  $this->init_settings();
29
 
30
  // Define user set variables
31
+ $this->ga_email = $this->get_option("ga_email");
32
+ $this->ga_id = $this->get_option("ga_id");
33
+ $this->ga_set_domain_name = $this->get_option("ga_set_domain_name");
34
+ $this->ga_standard_tracking_enabled = $this->get_option("ga_standard_tracking_enabled");
35
+ $this->enable_guest_checkout = get_option("woocommerce_enable_guest_checkout") == "yes" ? true : false;
36
+ $this->track_login_step_for_guest_user = $this->get_option("track_login_step_for_guest_user") == "yes" ? true : false;
37
+ $this->ga_enhanced_ecommerce_tracking_enabled = $this->get_option("ga_enhanced_ecommerce_tracking_enabled");
38
+ $this->ga_display_feature_plugin=$this->get_option("ga_display_feature_plugin") == "yes" ? true : false;
39
+ $this->ga_enhanced_ecommerce_category_page_impression_threshold = $this->get_option("ga_enhanced_ecommerce_category_page_impression_threshold");
40
 
41
  // Actions
42
+ add_action("woocommerce_update_options_integration_enhanced_ecommerce_google_analytics", array($this, "process_admin_options"));
43
  // API Call to LS with e-mail
44
  // Tracking code
45
+ add_action("wp_head", array($this, "google_tracking_code"));
46
+ add_action("woocommerce_thankyou", array($this, "ecommerce_tracking_code"));
47
 
48
  // Enhanced Ecommerce product impression hook
49
+ add_action("woocommerce_after_shop_loop", array($this, "cate_page_prod_impression")); // Hook for category page
50
+ add_action("woocommerce_after_shop_loop_item", array($this, "product_impression"));
51
+ add_action("woocommerce_after_single_product", array($this, "product_detail_view"));
52
+ add_action("woocommerce_after_cart", array($this, "remove_cart_tracking"));
53
+ add_action("woocommerce_after_checkout_form", array($this, "checkout_step_one_tracking"));
54
 
55
  // Event tracking code
56
+ add_action("woocommerce_after_add_to_cart_button", array($this, "add_to_cart"));
57
+ add_action("wp_footer", array($this, "loop_add_to_cart"));
58
+ add_action("wp_footer", array($this, "default_pageview"));
59
+
60
+ //Enable display feature code checkbox
61
+ add_action("admin_footer", array($this, "check_UA_enabled"));
62
  }
63
 
64
  /**
70
  function init_form_fields() {
71
 
72
  $this->form_fields = array(
73
+ "ga_email" => array(
74
+ "title" => __("Email Address", "woocommerce"),
75
+ "description" => __("Provide your work email address to receive plugin enhancement updates.", "woocommerce"),
76
+ "type" => "email",
77
+ "required" => "required",
78
+ "default" => get_option("woocommerce_ga_email") // Backwards compat
79
+ ),
80
+ "ga_id" => array(
81
+ "title" => __("Google Analytics ID", "woocommerce"),
82
+ "description" => __("Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>", "woocommerce"),
83
+ "type" => "text",
84
+ "default" => get_option("woocommerce_ga_id") // Backwards compat
85
  ),
86
+ "ga_set_domain_name" => array(
87
+ "title" => __("Set Domain Name", "woocommerce"),
88
+ "description" => sprintf(__("Enter your domain name here. (Optional) <a href='%s' target='_blank'>See here for more information</a>.", "woocommerce"), "https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#multipleDomains"),
89
+ "type" => "text",
90
+ "default" => ""
91
  ),
92
+ "ga_standard_tracking_enabled" => array(
93
+ "title" => __("Tracking code", "woocommerce"),
94
+ "label" => __("Add Universal Analytics Tracking Code (Optional)", "woocommerce"),
95
+ "description" => sprintf(__("You don\"t need to enable this if using a 3rd party analytics plugin. If you chose to add Universal Analytics code snippet via Third party plugins,<br/> Add <code>ga(\"require\", \"ec\", \"ec.js\");</code> below <code>ga(\"create\",\"UA-XXXXX-X\")</code> in your standard code snippet. <br/> Also ensure that the Universal Analytics code is present in the &lt;head&gt; section of the website.", "woocommerce")),
96
+ "type" => "checkbox",
97
+ "checkboxgroup" => "start",
98
+ "default" => get_option("woocommerce_ga_standard_tracking_enabled") ? get_option("woocommerce_ga_standard_tracking_enabled") : "no" // Backwards compat
99
  ),
100
+ "ga_display_feature_plugin" => array(
101
+ "label" => __("Add Display Advertising Feature Code (Optional)", "woocommerce"),
102
+ "type" => "checkbox",
103
+ "checkboxgroup" => "",
104
+ "description" => sprintf(__("This Feature enables remarketing with Google Analytics & Demographic reports. Adding the code is the first step in a 3 step process. <a href='https://support.google.com/analytics/answer/2819948?hl=en' target='_blank'>Learn More</a>", "woocommerce")),
105
+ "default" => get_option("ga_display_feature_plugin") ? get_option("ga_display_feature_plugin") : "no" // Backwards compat
 
106
  ),
107
+ "ga_enhanced_ecommerce_tracking_enabled" => array(
108
+ "label" => __("Add Enhanced Ecommerce Tracking Code", "woocommerce"),
109
+ "type" => "checkbox",
110
+ "checkboxgroup" => "",
111
+ "default" => get_option("woocommerce_ga_ecommerce_tracking_enabled") ? get_option("woocommerce_ga_ecommerce_tracking_enabled") : "no" // Backwards compat
112
  ),
113
+ "track_login_step_for_guest_user" => array(
114
+ "label" => __("Add Code to Track the Login Step of Guest Users (Optional)", "woocommerce"),
115
+ "type" => "checkbox",
116
+ "checkboxgroup" => "",
117
+ "description" => sprintf(__("If you have Guest Check out enable, we recommend you to add this code", "woocommerce")),
118
+ "default" => get_option("track_login_step_for_guest_user") ? get_option("track_login_step_for_guest_user") : "no" // Backwards compat
119
  ),
120
+ "ga_enhanced_ecommerce_category_page_impression_threshold" => array(
121
+ "title" => __("Impression Threshold", "woocommerce"),
122
+ "description" => sprintf(__("Impression threshold for category page. Send hit after these many number of products impressions", "woocommerce")),
123
+ "type" => "input",
124
+ "default" => "6"
125
  )
126
  );
127
  /* When user updates the email, post it to the remote server */
128
+ if (isset($_GET["tab"]) && isset($_REQUEST["section"]) && isset($_REQUEST["woocommerce_enhanced_ecommerce_google_analytics_ga_email"])) {
129
 
130
+ $current_tab = ( empty($_GET["tab"]) ) ? false : sanitize_text_field(urldecode($_GET["tab"]));
131
+ $current_section = ( empty($_REQUEST["section"]) ) ? false : sanitize_text_field(urldecode($_REQUEST["section"]));
132
 
133
  $save_for_the_plugin = ($current_tab == "integration" ) && ($current_section == "enhanced_ecommerce_google_analytics");
134
+ $update_made_for_email = $_REQUEST["woocommerce_enhanced_ecommerce_google_analytics_ga_email"] != $this->get_option("woocommerce_enhanced_ecommerce_google_analytics_ga_email");
135
 
136
  if ($save_for_the_plugin && $update_made_for_email) {
137
+ if ($_REQUEST["woocommerce_enhanced_ecommerce_google_analytics_ga_email"] != "") {
138
+ $email = $_REQUEST["woocommerce_enhanced_ecommerce_google_analytics_ga_email"];
139
  $this->send_email_to_tatvic($email);
140
  }
141
  }
151
  * @return void
152
  */
153
  function google_tracking_code() {
154
+ if (is_admin() || current_user_can("manage_options") || $this->ga_standard_tracking_enabled == "no") {
155
  return;
156
  }
157
 
165
  if (!empty($this->ga_set_domain_name)) {
166
  $set_domain_name = esc_js($this->ga_set_domain_name);
167
  } else {
168
+ $set_domain_name = "auto";
169
  }
170
 
171
+ //add display features
172
+ if ($this->ga_display_feature_plugin) {
173
+ $ga_display_feature_code = 'ga("require", "displayfeatures");';
174
+ } else {
175
+ $ga_display_feature_code = "";
176
+ }
177
+
178
+ echo '<script>
179
+ (function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
180
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
181
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
182
+ })(window,document,"script","//www.google-analytics.com/analytics.js","ga");
183
+ //Plugin Version :1.0.7
184
+ ga("create", "' . esc_js($tracking_id) . '", "' . $set_domain_name . '");
185
+ '.$ga_display_feature_code.'
186
+ ga("require", "ec", "ec.js");
187
+ </script>';
188
 
189
  }
190
 
198
  function ecommerce_tracking_code($order_id) {
199
  global $woocommerce;
200
 
201
+ if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled) || current_user_can("manage_options") || get_post_meta($order_id, "_ga_tracked", true) == 1)
202
  return;
203
 
204
  $tracking_id = $this->ga_id;
207
  return;
208
 
209
  // Doing eCommerce tracking so unhook standard tracking from the footer
210
+ remove_action("wp_footer", array($this, "google_tracking_code"));
211
 
212
  // Get the order and output tracking code
213
  $order = new WC_Order($order_id);
215
  if (!empty($this->ga_set_domain_name)) {
216
  $set_domain_name = esc_js($this->ga_set_domain_name);
217
  } else {
218
+ $set_domain_name = "auto";
219
  }
220
+
221
+ //add display features
222
+ if ($this->ga_display_feature_plugin) {
223
+ $ga_display_feature_code = 'ga("require", "displayfeatures");';
224
+ } else {
225
+ $ga_display_feature_code = "";
226
+ }
227
+
228
+ $code = '(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
229
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
230
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
231
+ })(window,document,"script","//www.google-analytics.com/analytics.js","ga");
232
+ //Plugin Version :1.0.7
233
+ ga("create", "'. esc_js($tracking_id) . '", "' . $set_domain_name . '");
234
+ '.$ga_display_feature_code.'
235
+ ga("require", "ec", "ec.js");
236
+ ';
 
237
 
238
 
239
  // Order items
241
  foreach ($order->get_items() as $item) {
242
  $_product = $order->get_product_from_item($item);
243
 
244
+ $code .= 'ga("ec:addProduct", {';
245
+ $code .= '"name": "' . esc_js($item["name"]) . '",';
246
+ $code .= '"id": "'. esc_js($_product->get_sku()) . '",';
 
247
  if (isset($_product->variation_data)) {
248
 
249
+ $code .= '"category": "' . esc_js(woocommerce_get_formatted_variation($_product->variation_data, true)) . '",';
250
  } else {
251
  $out = array();
252
+ $categories = get_the_terms($_product->id, "product_cat");
253
  if ($categories) {
254
  foreach ($categories as $category) {
255
  $out[] = $category->name;
256
  }
257
  }
258
+ $code .= '"category": "' . esc_js(join("/", $out)) . '",';
259
  }
260
 
261
+ $code .= '"price": "' . esc_js($order->get_item_total($item)) . '",';
262
+ $code .= '"quantity": "' . esc_js($item["qty"]) . '"';
263
  $code .= "});";
264
  }
265
  }
266
 
267
+ $code .='ga("ec:setAction","purchase", {
268
+ "id": "' . esc_js($order->get_order_number()) . '", // Transaction ID. Required
269
+ "affiliation": "' . esc_js(get_bloginfo("name")) . '", // Affiliation or store name
270
+ "revenue": "' . esc_js($order->get_total()) . '", // Grand Total
271
+ "shipping": "' . esc_js($order->get_shipping()) . '", // Shipping
272
+ "tax": "' . esc_js($order->get_total_tax()) . '" // Tax
273
+ });';
274
 
275
+ echo "<script type='text/javascript'>" . $code . "</script>";
276
 
277
+ update_post_meta($order_id, "_ga_tracked", 1);
278
  }
279
 
280
  /**
293
  $t_category="Search Results";
294
  }
295
  global $product, $woocommerce;
296
+ $impression_threshold = $this->ga_enhanced_ecommerce_category_page_impression_threshold;
297
+ if (version_compare($woocommerce->version, "2.1", ">=")) {
298
+ wc_enqueue_js('
 
 
 
299
  t_cnt=0;
300
+ t_ttl_prod=jQuery(".products li").length;
301
+ jQuery(".products li").each(function(index){
302
  t_cnt++;
303
+ ga("ec:addImpression", {
304
+ "id": jQuery(this).find(".ls-pro-sku").val(),
305
+ "name": jQuery(this).find(".ls-pro-name").val(),
306
+ "category": jQuery(this).find(".ls-pro-category").val(),
307
+ "price": jQuery(this).find(".ls-pro-price").val(),
308
+ "position": index+1
309
  });
310
 
311
+ if(t_ttl_prod > "' .esc_js($impression_threshold).'"){
312
+ if((t_cnt%"' .esc_js($impression_threshold).'")==0){
313
+ t_ttl_prod=t_ttl_prod-"'.esc_js($impression_threshold).'";
314
+ ga("send", "event", "ecommerce", "product_impression_cp", {"nonInteraction": 1});
315
  }
316
  }else{
317
  t_ttl_prod--;
318
  if(t_ttl_prod==0){
319
+ ga("send", "event", "ecommerce", "product_impression_cp", {"nonInteraction": 1});
320
  }
321
  }
322
 
323
+ jQuery(this).find("a:not(.add_to_cart_button)").on("click",function(){
324
 
325
+ if("'.esc_js($t_category).'"==""){
326
+ t_category=jQuery(this).parents("li").find(".ls-pro-category").val();
327
  }else{
328
+ t_category="'.esc_js($t_category).'";
329
  }
330
+ ga("ec:addProduct", {
331
+ "id": jQuery(this).parents("li").find(".ls-pro-sku").val(),
332
+ "name": jQuery(this).parents("li").find(".ls-pro-name").val(),
333
+ "category": jQuery(this).parents("li").find(".ls-pro-category").val(),
334
+ "price": jQuery(this).parents("li").find(".ls-pro-price").val(),
335
+ "position": index+1
336
  });
337
+ ga("ec:setAction", "click", {list: t_category});
338
+ ga("send", "event", "ecommerce", "product_click", {"nonInteraction": 1});
339
 
340
 
341
  });
342
  });
343
 
344
+ ');
345
 
346
  } else {
347
 
348
+ $woocommerce->add_inline_js('
349
  t_cnt=0;
350
+ t_ttl_prod=jQuery(".products li").length;
351
+ jQuery(".products li").each(function(index){
352
  t_cnt++;
353
+ ga("ec:addImpression", {
354
+ "id": jQuery(this).find(".ls-pro-sku").val(),
355
+ "name": jQuery(this).find(".ls-pro-name").val(),
356
+ "category": jQuery(this).find(".ls-pro-category").val(),
357
+ "price": jQuery(this).find(".ls-pro-price").val(),
358
+ "position": index+1
359
  });
360
 
361
+ if(t_ttl_prod > "' .esc_js($impression_threshold).'"){
362
+ if((t_cnt%"' .esc_js($impression_threshold).'")==0){
363
+ t_ttl_prod=t_ttl_prod-"'.esc_js($impression_threshold).'";
364
+ ga("send", "event", "ecommerce", "product_impression_cp", {"nonInteraction": 1});
365
  }
366
  }else{
367
  t_ttl_prod--;
368
  if(t_ttl_prod==0){
369
+ ga("send", "event", "ecommerce", "product_impression_cp", {"nonInteraction": 1});
370
  }
371
  }
372
 
373
+ jQuery(this).find("a:not(.add_to_cart_button)").on("click",function(){
374
 
375
+ if("'.esc_js($t_category).'"==""){
376
+ t_category=jQuery(this).parents("li").find(".ls-pro-category").val();
377
  }else{
378
+ t_category="'.esc_js($t_category).'";
379
  }
380
 
381
+ ga("ec:addProduct", {
382
+ "id": jQuery(this).parents("li").find(".ls-pro-sku").val(),
383
+ "name": jQuery(this).parents("li").find(".ls-pro-name").val(),
384
+ "category": jQuery(this).parents("li").find(".ls-pro-category").val(),
385
+ "price": jQuery(this).parents("li").find(".ls-pro-price").val(),
386
+ "position": index+1
387
  });
388
+ ga("ec:setAction", "click", {list: t_category});
389
+ ga("send", "event", "ecommerce", "product_click", {"nonInteraction": 1});
390
 
391
 
392
  });
393
  });
394
 
395
+ ');
396
  }
397
  }
398
 
415
  $parameters = array();
416
 
417
  // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
418
+ $parameters["label"] = '"' . esc_js($product->get_sku() ? __("SKU:", "woocommerce") . " " . $product->get_sku() : "#" . $product->id ) . '"';
419
 
420
+ if (version_compare($woocommerce->version, "2.1", ">=")) {
421
+ wc_enqueue_js('
422
+ $(".single_add_to_cart_button").click(function() {
423
 
424
  // Enhanced E-commerce Add to cart clicks
425
+ ga("ec:addProduct", {
426
+ "id": "' . esc_js($product->get_sku()) . '",
427
+ "name": "' . esc_js($product->get_title()) . '",
428
+ "price": "' . esc_js($product->get_price()) . '",
429
  });
430
+ ga("ec:setAction", "add");
431
+ ga("send", "event", "Enhanced-Ecommerce", "add-to-cart-click", ' . $parameters["label"] . ',{"nonInteraction": 1});
432
  });
433
+ ');
434
  } else {
435
+ $woocommerce->add_inline_js('
436
+ $(".single_add_to_cart_button").click(function() {
437
 
438
  // Enhanced E-commerce Add to cart clicks
439
+ ga("ec:addProduct", {
440
+ "id": "' . esc_js($product->get_sku()) . '",
441
+ "name": "'. esc_js($product->get_title()) . '",
442
+ "price": "' . esc_js($product->get_price()) . '",
443
  });
444
+ ga("ec:setAction", "add");
445
+ ga("send", "event", "Enhanced-Ecommerce", "add-to-cart-click", ' . $parameters["label"] . ',{"nonInteraction": 1});
446
  });
447
+ ');
448
  }
449
  }
450
 
463
 
464
  $parameters = array();
465
  // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
466
+ $parameters["category"] = '"' . __("Products", "woocommerce") . '"';
467
+ $parameters["action"] = '"' . __("Add to Cart", "woocommerce") . "'";
468
+ $parameters["label"] ='($(this).data("product_sku")) ? ("SKU: " + $(this).data("product_sku")) : ("#" + $(this).data("product_id"))'; // Product SKU or ID
469
 
470
+ if (version_compare($woocommerce->version, "2.1", ">=")) {
471
 
472
+ wc_enqueue_js('
473
+ $(".add_to_cart_button:not(.product_type_variable, .product_type_grouped)").click(function() {
474
 
475
  // Enhanced E-commerce Add to cart clicks
476
+ ga("ec:addProduct", {
477
+ "id": $(this).parents("li").find(".ls-pro-sku").val(),
478
+ "name": $(this).parents("li").find(".ls-pro-name").val(),
479
+ "category": $(this).parents("li").find(".ls-pro-category").val(),
480
+ "price": $(this).parents("li").find(".ls-pro-price").val(),
481
  });
482
+ ga("ec:setAction", "add");
483
+ ga("send", "event", "Enhanced-Ecommerce", "add-to-cart-click", ' . $parameters["label"] . ',{"nonInteraction": 1});
484
  });
485
+ ');
486
  } else {
487
+ $woocommerce->add_inline_js('
488
+ $(".add_to_cart_button:not(.product_type_variable, .product_type_grouped)").click(function() {
489
 
490
  // Enhanced E-commerce Add to cart clicks
491
+ ga("ec:addProduct", {
492
+ "id": $(this).parents("li").find(".ls-pro-sku").val(),
493
+ "name": $(this).parents("li").find(".ls-pro-name").val(),
494
+ "category": $(this).parents("li").find(".ls-pro-category").val(),
495
+ "price": $(this).parents("li").find(".ls-pro-price").val(),
496
  });
497
+ ga("ec:setAction", "add");
498
+ ga("send", "event", "Enhanced-Ecommerce", "add-to-cart-click", ' . $parameters["label"] . ',{"nonInteraction": 1});
499
  });
500
+ ');
501
  }
502
  }
503
 
515
 
516
  global $product;
517
  global $woocommerce;
518
+ $category = get_the_terms($product->ID, "product_cat");
519
+ $categories = "";
520
  foreach ($category as $term) {
521
+ $categories.=$term->name . ",";
522
  }
523
+ //remove last comma(,) if multiple categories are there
524
+ $categories = rtrim($categories, ",");
525
+
526
+ if (version_compare($woocommerce->version, "2.1", ">=")) {
527
 
528
+ wc_enqueue_js('ga("ec:addImpression", {
529
+ "id": "' . $product->get_sku() . '", // Product details are provided in an impressionFieldObject.
530
+ "name": "' . $product->get_title() . '",
531
+ "category": "' . $categories . '",
532
  });
533
+ ga("ec:setAction", "detail");
534
+ ');
535
  } else {
536
+ $woocommerce->add_inline_js('ga("ec:addImpression", {
537
+ "id": "' . $product->get_sku() . '", // Product details are provided in an impressionFieldObject.
538
+ "name": "' . $product->get_title() . '",
539
+ "category": "' . $categories . '",
540
  });
541
+ ga("ec:setAction", "detail");
542
+ ');
543
  }
544
  }
545
 
561
 
562
  global $product;
563
 
564
+ $category = get_the_terms($product->ID, "product_cat");
565
+ $categories = "";
566
  foreach ($category as $term) {
567
+ $categories.=$term->name . ",";
568
  }
569
  //remove last comma(,) if multiple categories are there
570
  $categories = rtrim($categories, ",");
571
 
572
+ echo '<input type="hidden" class="ls-pro-price" value="'. esc_html($product->get_price()).'" />'
573
+ . '<input type="hidden" class="ls-pro-sku" value="'. $product->get_sku() . '"/>'
574
+ . '<input type="hidden" class="ls-pro-name" value="' . $product->get_title() . '"/>'
575
+ . '<input type="hidden" class="ls-pro-category" value="' . $categories . '"/>';
576
  }
577
 
578
  /**
586
  return;
587
  }
588
  global $woocommerce;
589
+ if (version_compare($woocommerce->version, "2.1", ">=")) {
590
+ wc_enqueue_js('$(".remove").click(function(){
591
+ ga("ec:addProduct", {
592
+ "name": $(this).parents("tr").find(".product-name").text(),
593
+ "price": $(this).parents("tr").find(".product-price").text(),
594
+ "quantity": $(this).parents("tr").find(".product-quantity .qty").val()
595
  });
596
+ ga("ec:setAction", "remove");
597
+ ga("send", "event", "Enhanced-Ecommerce", "click", "remove from cart",{"nonInteraction": 1});
598
+ });
599
+ ');
600
  } else {
601
+ $woocommerce->add_inline_js('
602
+ $(".remove").click(function(){
603
+ ga("ec:addProduct", {
604
+ "name": $(this).parents("tr").find(".product-name").text(),
605
+ "price": $(this).parents("tr").find(".product-price").text(),
606
+ "quantity": $(this).parents("tr").find(".product-quantity .qty").val()
607
+ });
608
+ ga("ec:setAction", "remove");
609
+ ga("send", "event", "Enhanced-Ecommerce", "click", "remove from cart",{"nonInteraction": 1});
610
  });
611
+ ');
 
 
 
612
  }
613
  }
614
 
626
  global $woocommerce;
627
 
628
  foreach ($woocommerce->cart->cart_contents as $item) {
629
+ $p = get_product($item["product_id"]);
630
 
631
+ $category = get_the_terms($item["product_id"], "product_cat");
632
+ $categories = "";
633
  foreach ($category as $term) {
634
+ $categories.=$term->name . ",";
635
  }
636
+ //remove last comma(,) if multiple categories are there
637
+ $categories = rtrim($categories, ",");
638
+
639
+ $code = 'ga("ec:addProduct", {"id": "' . esc_js($p->get_sku()) . '",';
640
+ $code .= '"name": "' . esc_js($p->get_title()) . '",';
641
+ $code .= '"category": "' . esc_js($categories) . '",';
642
+ $code .= '"price": "' . esc_js($p->get_price()) . '",';
643
+ $code .= '"quantity": "' . esc_js($item["quantity"]) . '"});';
644
  }
645
 
646
+ $code_step_1 = $code . 'ga("ec:setAction","checkout",{"step": 1});';
647
+ $code_step_1 .= 'ga("send", "event", "Enhanced-Ecommerce", "pageview", "footer",{"nonInteraction": 1});';
648
+ if (version_compare($woocommerce->version, "2.1", ">=")) {
649
  wc_enqueue_js($code_step_1);
650
  } else {
651
  $woocommerce->add_inline_js($code_step_1);
652
  }
653
 
654
 
655
+ $code_step_2 = $code . 'ga("ec:setAction","checkout",{"step": 2});';
656
+ $code_step_2 .= 'ga("send", "event", "Enhanced-Ecommerce", "pageview", "footer",{"nonInteraction": 1});';
657
 
658
  if (is_user_logged_in()) {
659
+ if (version_compare($woocommerce->version, "2.1", ">=")) {
660
  wc_enqueue_js($code_step_2);
661
  } else {
662
  $woocommerce->add_inline_js($code_step_2);
664
  }
665
  $step_2_on_proceed_to_pay = (!is_user_logged_in() && !$this->enable_guest_checkout ) || (!is_user_logged_in() && $this->enable_guest_checkout && $this->track_login_step_for_guest_user);
666
 
667
+ $code_step_3 = $code . 'ga("ec:setAction","checkout",{"step": 3});';
668
+ $code_step_3 .= 'ga("send", "event", "Enhanced-Ecommerce", "pageview", "footer",{"nonInteraction": 1});';
669
 
670
+ $inline_js = 'jQuery(document).on("click","#place_order",function(e){';
671
  if ($step_2_on_proceed_to_pay) {
672
  $inline_js .= $code_step_2;
673
  }
674
  $inline_js .= $code_step_3;
675
  $inline_js .= "});";
676
+ if (version_compare($woocommerce->version, "2.1", ">=")) {
677
  wc_enqueue_js($inline_js);
678
  } else {
679
  $woocommerce->add_inline_js($inline_js);
694
  }
695
 
696
  if (!$this->disable_tracking($this->ga_standard_tracking_enabled)) {
697
+ $inline_js = 'ga("send", "event", "Enhanced-Ecommerce", "pageview", "footer",{"nonInteraction": 1})';
698
  } else {
699
+ $inline_js = 'ga("send", "event", "Enhanced-Ecommerce", "pageview", "footer",{"nonInteraction": 1});';
700
  }
701
 
702
+ if (version_compare($woocommerce->version, "2.1", ">=")) {
703
  wc_enqueue_js($inline_js);
704
  } else {
705
  $woocommerce->add_inline_js($inline_js);
714
  * @return bool
715
  */
716
  private function disable_tracking($type) {
717
+ if (is_admin() || current_user_can("manage_options") || (!$this->ga_id ) || "no" == $type) {
718
  return true;
719
  }
720
  }
721
 
722
+ /**
723
+ * check UA is enabled or not
724
+ *
725
+ * @access public
726
+ */
727
+ function check_UA_enabled(){
728
+ echo '<script>
729
+ jQuery("#woocommerce_enhanced_ecommerce_google_analytics_ga_standard_tracking_enabled").change(function(){
730
+ t_ga_chk=jQuery(this).is(":checked");
731
+
732
+ if(t_ga_chk){
733
+ jQuery("#woocommerce_enhanced_ecommerce_google_analytics_ga_display_feature_plugin").removeAttr("disabled");
734
+ }else{
735
+ jQuery("#woocommerce_enhanced_ecommerce_google_analytics_ga_display_feature_plugin").attr("disabled",true);
736
+ t_display_chk=jQuery("#woocommerce_enhanced_ecommerce_google_analytics_ga_display_feature_plugin").is(":checked");
737
+ if(t_display_chk){
738
+ jQuery("#woocommerce_enhanced_ecommerce_google_analytics_ga_display_feature_plugin").removeAttr("checked");
739
+ } }
740
+ });
741
+ </script>';
742
+ }
743
+
744
+
745
  /**
746
  * Sending email to remote server
747
  *
750
  */
751
  public function send_email_to_tatvic($email) {
752
  //set POST variables
753
+ $url = "http://dev.tatvic.com/leadgen/woocommerce-plugin/store_email/";
754
  $fields = array(
755
+ "email" => urlencode($email),
756
  );
757
  wp_remote_post($url, array(
758
+ "method" => "POST",
759
+ "timeout" => 1,
760
+ "httpversion" => "1.0",
761
+ "blocking" => false,
762
+ "headers" => array(),
763
+ "body" => $fields
764
  )
765
  );
766
  }
readme.txt CHANGED
@@ -7,8 +7,8 @@ Author URI: http://www.tatvic.com/
7
  Author: Tatvic
8
  Requires at least: 3.6
9
  Tested up to: 3.9.2
10
- Stable tag: 1.0.6.1
11
- Version: 1.0.6.1
12
  License: GPLv3
13
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
14
 
@@ -27,6 +27,7 @@ Provides integration between Google Analytics Enhanced Ecommerce and WooCommerce
27
  3. Supports Guest checkout functionality
28
  4. Captures Product Impressions events on the load of the category page and Add-to-Cart events when add-to-cart button is clicked
29
  5. Captures Product Impressions events on the load of the product page and Add-to-Cart events when add-to-cart button is clicked
 
30
 
31
  = Things to keep in mind before enabling the Enhanced E-commerce plugin =
32
  * Enable Enhanced E-commerce for your profile/view. This is a profile / view level setting and can be accessed under Admin > View > E-commerce Settings
@@ -54,9 +55,9 @@ ga('require','ec','ec.js');
54
  </pre>
55
  * After adding the above code, you will have to activate your plug-in from the Settings page. You can access the setting page from here WooCommerce -> Settings ->Integration ->Enhanced Ecommerce Google Analytics.
56
 
57
- * Find “Enable Enhanced Ecommerce Tracking” in the settings page and check the box to enable the plugin
58
 
59
- * If you have a guest checkout on your WooCommerce store, then Check the box “Track Login Steps For Guest Users”. If you have a guest login but you do not check the box, then it might cause an uneven funnel reporting in Google Analytics.
60
 
61
  == Installation ==
62
  1. Download the plugin file to your computer and unzip it
@@ -87,4 +88,8 @@ Starting the WooCommerce 2.1 release there are no conflicts. However for earlier
87
  = 1.0.6.1 - 15/08/2014 =
88
  * Added new feature - Product impressions and Product click on category page view , including the default pagination
89
  * Fixed-Allow Special Characters in javascript
 
 
 
 
90
 
7
  Author: Tatvic
8
  Requires at least: 3.6
9
  Tested up to: 3.9.2
10
+ Stable tag: 1.0.7
11
+ Version: 1.0.7
12
  License: GPLv3
13
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
14
 
27
  3. Supports Guest checkout functionality
28
  4. Captures Product Impressions events on the load of the category page and Add-to-Cart events when add-to-cart button is clicked
29
  5. Captures Product Impressions events on the load of the product page and Add-to-Cart events when add-to-cart button is clicked
30
+ 6. Supports Display Advertising Feature
31
 
32
  = Things to keep in mind before enabling the Enhanced E-commerce plugin =
33
  * Enable Enhanced E-commerce for your profile/view. This is a profile / view level setting and can be accessed under Admin > View > E-commerce Settings
55
  </pre>
56
  * After adding the above code, you will have to activate your plug-in from the Settings page. You can access the setting page from here WooCommerce -> Settings ->Integration ->Enhanced Ecommerce Google Analytics.
57
 
58
+ * Find “Add Enhanced Ecommerce Tracking Code” in the settings page and check the box to enable the plugin
59
 
60
+ * If you have a guest checkout on your WooCommerce store, then Check the box “Add Code to Track the Login Step of Guest Users”. If you have a guest login but you do not check the box, then it might cause an uneven funnel reporting in Google Analytics.
61
 
62
  == Installation ==
63
  1. Download the plugin file to your computer and unzip it
88
  = 1.0.6.1 - 15/08/2014 =
89
  * Added new feature - Product impressions and Product click on category page view , including the default pagination
90
  * Fixed-Allow Special Characters in javascript
91
+
92
+ = 1.0.7 - 28/08/2014 =
93
+ * Added new feature - Display Advertising Feature
94
+ * Fixed-Allow back quotes and single quotes in product name, category name etc.
95
 
woocommerce-enhanced-ecommerce-google-analytics-integration.php CHANGED
@@ -21,7 +21,7 @@
21
  Description: Allows Enhanced E-commerce Google Analytics tracking code to be inserted into WooCommerce store pages.
22
  Author: Tatvic
23
  Author URI: http://www.tatvic.com
24
- Version: 1.0.6.1
25
  */
26
 
27
  // Add the integration to WooCommerce
21
  Description: Allows Enhanced E-commerce Google Analytics tracking code to be inserted into WooCommerce store pages.
22
  Author: Tatvic
23
  Author URI: http://www.tatvic.com
24
+ Version: 1.0.7
25
  */
26
 
27
  // Add the integration to WooCommerce