Enhanced Ecommerce Google Analytics Plugin for WooCommerce - Version 1.0.10

Version Description

  • 09/26/2014 =
    • Allows user to set local currency
    • Captures Impressions, Product Clicks and Add to Cart on Featured Product section and Recent Product section on Homepage
    • Captures Impressions, Product Clicks and Add to Cart on Related Product section on Product Page
Download this release

Release Info

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

Code changes from version 1.0.9.1 to 1.0.10

includes/class-wc-enhanced-ecommerce-google-analytics-integration.php CHANGED
@@ -9,7 +9,6 @@
9
  * @extends WC_Integration
10
  * @author Sudhir Mishra <sudhir@tatvic.com>
11
  */
12
-
13
  class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
14
 
15
  /**
@@ -19,6 +18,13 @@ 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/' target='_blank'>Know more about Enhanced Ecommerce.</a>", "woocommerce");
@@ -27,17 +33,32 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
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
@@ -46,35 +67,92 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
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
- //add version details in footer
64
- add_action("wp_footer",array($this,"add_plugin_details"));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  }
66
-
67
- /**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  * display details of plugin
69
  *
70
  * @access public
71
  * @return void
72
  */
73
-
74
- function add_plugin_details(){
75
- echo '<!-- Plugin Version: 1.0.9.1-->';
76
  }
77
-
78
  /**
79
  * Initialise Settings Form Fields
80
  *
@@ -82,31 +160,39 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
82
  * @return void
83
  */
84
  function init_form_fields() {
85
-
86
  $this->form_fields = array(
87
  "ga_email" => array(
88
  "title" => __("Email Address", "woocommerce"),
89
- "description" => __("Provide your work email address to receive plugin enhancement updates.", "woocommerce"),
90
  "type" => "email",
91
  "required" => "required",
92
  "default" => get_option("woocommerce_ga_email") // Backwards compat
93
  ),
94
  "ga_id" => array(
95
  "title" => __("Google Analytics ID", "woocommerce"),
96
- "description" => __("Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>", "woocommerce"),
97
  "type" => "text",
98
  "default" => get_option("woocommerce_ga_id") // Backwards compat
99
  ),
100
  "ga_set_domain_name" => array(
101
  "title" => __("Set Domain Name", "woocommerce"),
102
- "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"),
103
  "type" => "text",
104
  "default" => ""
105
  ),
 
 
 
 
 
 
 
 
106
  "ga_standard_tracking_enabled" => array(
107
  "title" => __("Tracking code", "woocommerce"),
108
  "label" => __("Add Universal Analytics Tracking Code (Optional)", "woocommerce"),
109
- "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")),
110
  "type" => "checkbox",
111
  "checkboxgroup" => "start",
112
  "default" => get_option("woocommerce_ga_standard_tracking_enabled") ? get_option("woocommerce_ga_standard_tracking_enabled") : "no" // Backwards compat
@@ -115,13 +201,14 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
115
  "label" => __("Add Display Advertising Feature Code (Optional)", "woocommerce"),
116
  "type" => "checkbox",
117
  "checkboxgroup" => "",
118
- "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")),
119
- "default" => get_option("ga_display_feature_plugin") ? get_option("ga_display_feature_plugin") : "no" // Backwards compat
120
  ),
121
  "ga_enhanced_ecommerce_tracking_enabled" => array(
122
  "label" => __("Add Enhanced Ecommerce Tracking Code", "woocommerce"),
123
  "type" => "checkbox",
124
  "checkboxgroup" => "",
 
125
  "default" => get_option("woocommerce_ga_ecommerce_tracking_enabled") ? get_option("woocommerce_ga_ecommerce_tracking_enabled") : "no" // Backwards compat
126
  ),
127
  "track_login_step_for_guest_user" => array(
@@ -133,7 +220,7 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
133
  ),
134
  "ga_enhanced_ecommerce_category_page_impression_threshold" => array(
135
  "title" => __("Impression Threshold", "woocommerce"),
136
- "description" => sprintf(__("Impression threshold for category page. Send hit after these many number of products impressions", "woocommerce")),
137
  "type" => "input",
138
  "default" => "6"
139
  )
@@ -150,14 +237,13 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
150
  if ($save_for_the_plugin && $update_made_for_email) {
151
  if ($_REQUEST["woocommerce_enhanced_ecommerce_google_analytics_ga_email"] != "") {
152
  $email = $_REQUEST["woocommerce_enhanced_ecommerce_google_analytics_ga_email"];
153
- $this->send_email_to_tatvic($email);
 
154
  }
155
  }
156
  }
157
  }
158
 
159
- // End init_form_fields()
160
-
161
  /**
162
  * Google Analytics standard tracking
163
  *
@@ -166,6 +252,8 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
166
  */
167
  function google_tracking_code() {
168
  global $woocommerce;
 
 
169
  if (is_admin() || current_user_can("manage_options") || $this->ga_standard_tracking_enabled == "no") {
170
  return;
171
  }
@@ -176,6 +264,7 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
176
  return;
177
  }
178
 
 
179
 
180
  if (!empty($this->ga_set_domain_name)) {
181
  $set_domain_name = esc_js($this->ga_set_domain_name);
@@ -183,29 +272,28 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
183
  $set_domain_name = "auto";
184
  }
185
 
186
- //add display features
187
  if ($this->ga_display_feature_plugin) {
188
  $ga_display_feature_code = 'ga("require", "displayfeatures");';
189
  } else {
190
  $ga_display_feature_code = "";
191
  }
192
-
193
- $code='(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
 
194
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
195
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
196
  })(window,document,"script","//www.google-analytics.com/analytics.js","ga");
197
-
198
- ga("create", "' . esc_js($tracking_id) . '", "' . $set_domain_name . '");
199
- '.$ga_display_feature_code.'
200
- ga("require", "ec", "ec.js");';
201
-
202
- if (version_compare($woocommerce->version, "2.1", ">=")) {
203
- wc_enqueue_js($code);
204
- } else {
205
- $woocommerce->add_inline_js($code);
206
- }
207
-
208
- }
209
 
210
  /**
211
  * Google Analytics eCommerce tracking
@@ -230,39 +318,61 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
230
 
231
  // Get the order and output tracking code
232
  $order = new WC_Order($order_id);
 
 
 
 
 
 
 
 
 
 
 
 
233
 
 
234
  if (!empty($this->ga_set_domain_name)) {
235
  $set_domain_name = esc_js($this->ga_set_domain_name);
236
  } else {
237
  $set_domain_name = "auto";
238
  }
239
-
240
  //add display features
241
  if ($this->ga_display_feature_plugin) {
242
  $ga_display_feature_code = 'ga("require", "displayfeatures");';
243
  } else {
244
  $ga_display_feature_code = "";
245
  }
246
-
 
 
 
 
 
 
 
247
  $code = '(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
248
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
249
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
250
  })(window,document,"script","//www.google-analytics.com/analytics.js","ga");
251
 
252
- ga("create", "'. esc_js($tracking_id) . '", "' . $set_domain_name . '");
253
- '.$ga_display_feature_code.'
254
  ga("require", "ec", "ec.js");
255
- ';
256
-
257
-
258
  // Order items
259
  if ($order->get_items()) {
260
  foreach ($order->get_items() as $item) {
261
  $_product = $order->get_product_from_item($item);
262
 
 
 
263
  $code .= 'ga("ec:addProduct", {';
 
264
  $code .= '"name": "' . esc_js($item["name"]) . '",';
265
- $code .= '"id": "'. esc_js($_product->get_sku()) . '",';
266
  if (isset($_product->variation_data)) {
267
 
268
  $code .= '"category": "' . esc_js(woocommerce_get_formatted_variation($_product->variation_data, true)) . '",';
@@ -274,7 +384,7 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
274
  $out[] = $category->name;
275
  }
276
  }
277
- $code .= '"category": "' . esc_js(join("/", $out)) . '",';
278
  }
279
 
280
  $code .= '"price": "' . esc_js($order->get_item_total($item)) . '",';
@@ -287,16 +397,16 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
287
  "id": "' . esc_js($order->get_order_number()) . '", // Transaction ID. Required
288
  "affiliation": "' . esc_js(get_bloginfo("name")) . '", // Affiliation or store name
289
  "revenue": "' . esc_js($order->get_total()) . '", // Grand Total
 
290
  "shipping": "' . esc_js($order->get_shipping()) . '", // Shipping
291
- "tax": "' . esc_js($order->get_total_tax()) . '" // Tax
292
- });';
293
-
294
- if (version_compare($woocommerce->version, "2.1", ">=")) {
295
- wc_enqueue_js($code);
296
- } else {
297
- $woocommerce->add_inline_js($code);
298
- }
299
 
 
 
300
  update_post_meta($order_id, "_ga_tracked", 1);
301
  }
302
 
@@ -307,100 +417,66 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
307
  * @return void
308
  */
309
  public function cate_page_prod_impression() {
310
-
311
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
312
  return;
313
  }
314
- $t_category="";
315
- if(is_search()){
316
- $t_category="Search Results";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
  }
318
- global $product, $woocommerce;
319
  $impression_threshold = $this->ga_enhanced_ecommerce_category_page_impression_threshold;
320
- if (version_compare($woocommerce->version, "2.1", ">=")) {
321
- wc_enqueue_js('
322
- t_cnt=0;
323
- t_ttl_prod=jQuery(".ls-pro-sku").length; //jQuery(".products li").length;
324
- jQuery(".products li").each(function(index){
325
- t_cnt++;
326
  ga("ec:addImpression", {
327
  "id": jQuery(this).find(".ls-pro-sku").val(),
328
  "name": jQuery(this).find(".ls-pro-name").val(),
329
  "category": jQuery(this).find(".ls-pro-category").val(),
 
330
  "price": jQuery(this).find(".ls-pro-price").val(),
331
  "position": index+1
332
  });
333
 
334
- if(t_ttl_prod > "' .esc_js($impression_threshold).'"){
335
- if((t_cnt%"' .esc_js($impression_threshold).'")==0){
336
- t_ttl_prod=t_ttl_prod-"'.esc_js($impression_threshold).'";
337
- ga("send", "event", "ecommerce", "product_impression_cp", {"nonInteraction": 1});
338
  }
339
  }else{
340
  t_ttl_prod--;
341
  if(t_ttl_prod==0){
342
- ga("send", "event", "ecommerce", "product_impression_cp", {"nonInteraction": 1});
343
  }
344
  }
345
 
346
  jQuery(this).find("a:not(.add_to_cart_button)").on("click",function(){
347
-
348
- if("'.esc_js($t_category).'"==""){
349
- t_category=jQuery(this).parents("li").find(".ls-pro-category").val();
350
- }else{
351
- t_category="'.esc_js($t_category).'";
352
- }
353
- ga("ec:addProduct", {
354
- "id": jQuery(this).parents("li").find(".ls-pro-sku").val(),
355
- "name": jQuery(this).parents("li").find(".ls-pro-name").val(),
356
- "category": jQuery(this).parents("li").find(".ls-pro-category").val(),
357
- "price": jQuery(this).parents("li").find(".ls-pro-price").val(),
358
- "position": index+1
359
- });
360
- ga("ec:setAction", "click", {list: t_category});
361
- ga("send", "event", "ecommerce", "product_click", {"nonInteraction": 1});
362
-
363
-
364
- });
365
- });
366
-
367
- ');
368
-
369
- } else {
370
-
371
- $woocommerce->add_inline_js('
372
- t_cnt=0;
373
- t_ttl_prod=jQuery(".products li").length;
374
- jQuery(".products li").each(function(index){
375
- t_cnt++;
376
- ga("ec:addImpression", {
377
- "id": jQuery(this).find(".ls-pro-sku").val(),
378
- "name": jQuery(this).find(".ls-pro-name").val(),
379
- "category": jQuery(this).find(".ls-pro-category").val(),
380
- "price": jQuery(this).find(".ls-pro-price").val(),
381
- "position": index+1
382
- });
383
-
384
- if(t_ttl_prod > "' .esc_js($impression_threshold).'"){
385
- if((t_cnt%"' .esc_js($impression_threshold).'")==0){
386
- t_ttl_prod=t_ttl_prod-"'.esc_js($impression_threshold).'";
387
- ga("send", "event", "ecommerce", "product_impression_cp", {"nonInteraction": 1});
388
- }
389
- }else{
390
- t_ttl_prod--;
391
- if(t_ttl_prod==0){
392
- ga("send", "event", "ecommerce", "product_impression_cp", {"nonInteraction": 1});
393
- }
394
- }
395
-
396
- jQuery(this).find("a:not(.add_to_cart_button)").on("click",function(){
397
-
398
- if("'.esc_js($t_category).'"==""){
399
- t_category=jQuery(this).parents("li").find(".ls-pro-category").val();
400
- }else{
401
- t_category="'.esc_js($t_category).'";
402
- }
403
-
404
  ga("ec:addProduct", {
405
  "id": jQuery(this).parents("li").find(".ls-pro-sku").val(),
406
  "name": jQuery(this).parents("li").find(".ls-pro-name").val(),
@@ -408,14 +484,17 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
408
  "price": jQuery(this).parents("li").find(".ls-pro-price").val(),
409
  "position": index+1
410
  });
411
- ga("ec:setAction", "click", {list: t_category});
412
- ga("send", "event", "ecommerce", "product_click", {"nonInteraction": 1});
413
 
414
 
415
  });
416
  });
417
-
418
- ');
 
 
 
419
  }
420
  }
421
 
@@ -426,49 +505,37 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
426
  * @return void
427
  */
428
  function add_to_cart() {
429
-
430
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled))
431
  return;
432
-
433
  if (!is_single())
434
  return;
435
-
436
- global $product, $woocommerce;
437
-
438
- $parameters = array();
439
-
440
- // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
441
- $parameters["label"] = '"' . esc_js($product->get_sku() ? __("SKU:", "woocommerce") . " " . $product->get_sku() : "#" . $product->id ) . '"';
 
 
 
442
 
443
- if (version_compare($woocommerce->version, "2.1", ">=")) {
444
- wc_enqueue_js('
445
- $(".single_add_to_cart_button").click(function() {
446
 
447
  // Enhanced E-commerce Add to cart clicks
448
  ga("ec:addProduct", {
449
- "id": "' . esc_js($product->get_sku()) . '",
450
- "name": "' . esc_js($product->get_title()) . '",
 
451
  "price": "' . esc_js($product->get_price()) . '",
 
452
  });
453
  ga("ec:setAction", "add");
454
- ga("send", "event", "Enhanced-Ecommerce", "add-to-cart-click", ' . $parameters["label"] . ',{"nonInteraction": 1});
455
  });
456
- ');
457
- } else {
458
- $woocommerce->add_inline_js('
459
- $(".single_add_to_cart_button").click(function() {
460
-
461
- // Enhanced E-commerce Add to cart clicks
462
- ga("ec:addProduct", {
463
- "id": "' . esc_js($product->get_sku()) . '",
464
- "name": "'. esc_js($product->get_title()) . '",
465
- "price": "' . esc_js($product->get_price()) . '",
466
- });
467
- ga("ec:setAction", "add");
468
- ga("send", "event", "Enhanced-Ecommerce", "add-to-cart-click", ' . $parameters["label"] . ',{"nonInteraction": 1});
469
- });
470
- ');
471
- }
472
  }
473
 
474
  /**
@@ -482,46 +549,28 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
482
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
483
  return;
484
  }
485
- global $woocommerce;
486
 
487
- $parameters = array();
488
- // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
489
- $parameters["category"] = '"' . __("Products", "woocommerce") . '"';
490
- $parameters["action"] = '"' . __("Add to Cart", "woocommerce") . "'";
491
- $parameters["label"] ='($(this).data("product_sku")) ? ("SKU: " + $(this).data("product_sku")) : ("#" + $(this).data("product_id"))'; // Product SKU or ID
492
-
493
- if (version_compare($woocommerce->version, "2.1", ">=")) {
494
-
495
- wc_enqueue_js('
496
- $(".add_to_cart_button:not(.product_type_variable, .product_type_grouped)").click(function() {
497
-
498
- // Enhanced E-commerce Add to cart clicks
499
- ga("ec:addProduct", {
500
- "id": $(this).parents("li").find(".ls-pro-sku").val(),
501
- "name": $(this).parents("li").find(".ls-pro-name").val(),
502
- "category": $(this).parents("li").find(".ls-pro-category").val(),
503
- "price": $(this).parents("li").find(".ls-pro-price").val(),
504
- });
505
- ga("ec:setAction", "add");
506
- ga("send", "event", "Enhanced-Ecommerce", "add-to-cart-click", ' . $parameters["label"] . ',{"nonInteraction": 1});
507
- });
508
- ');
509
- } else {
510
- $woocommerce->add_inline_js('
511
- $(".add_to_cart_button:not(.product_type_variable, .product_type_grouped)").click(function() {
512
-
513
  // Enhanced E-commerce Add to cart clicks
514
  ga("ec:addProduct", {
515
  "id": $(this).parents("li").find(".ls-pro-sku").val(),
516
  "name": $(this).parents("li").find(".ls-pro-name").val(),
517
  "category": $(this).parents("li").find(".ls-pro-category").val(),
518
  "price": $(this).parents("li").find(".ls-pro-price").val(),
 
519
  });
520
  ga("ec:setAction", "add");
521
- ga("send", "event", "Enhanced-Ecommerce", "add-to-cart-click", ' . $parameters["label"] . ',{"nonInteraction": 1});
522
  });
523
- ');
524
- }
 
525
  }
526
 
527
  /**
@@ -531,41 +580,32 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
531
  * @return void
532
  */
533
  public function product_detail_view() {
534
-
535
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
536
  return;
537
  }
538
 
539
  global $product;
540
- global $woocommerce;
541
  $category = get_the_terms($product->ID, "product_cat");
542
  $categories = "";
543
- if($category){
544
- foreach ($category as $term) {
545
- $categories.=$term->name . ",";
546
- }
547
  }
548
- //remove last comma(,) if multiple categories are there
549
  $categories = rtrim($categories, ",");
550
-
551
- if (version_compare($woocommerce->version, "2.1", ">=")) {
552
 
553
- wc_enqueue_js('ga("ec:addImpression", {
554
- "id": "' . $product->get_sku() . '", // Product details are provided in an impressionFieldObject.
555
- "name": "' . $product->get_title() . '",
556
- "category": "' . $categories . '",
557
- });
558
- ga("ec:setAction", "detail");
559
- ');
560
- } else {
561
- $woocommerce->add_inline_js('ga("ec:addImpression", {
562
- "id": "' . $product->get_sku() . '", // Product details are provided in an impressionFieldObject.
563
  "name": "' . $product->get_title() . '",
564
  "category": "' . $categories . '",
565
  });
566
  ga("ec:setAction", "detail");
567
- ');
568
- }
 
 
569
  }
570
 
571
  /**
@@ -574,168 +614,356 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
574
  * @access public
575
  * @return void
576
  */
577
- public function product_impression() {
578
 
579
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
580
  return;
581
  }
582
 
583
- if (is_single()) {
584
- return;
585
- }
586
-
587
  global $product;
588
-
589
  $category = get_the_terms($product->ID, "product_cat");
590
  $categories = "";
591
- if($category){
592
- foreach ($category as $term) {
593
- $categories.=$term->name . ",";
594
- }
595
  }
596
  //remove last comma(,) if multiple categories are there
597
  $categories = rtrim($categories, ",");
598
 
599
- echo '<input type="hidden" class="ls-pro-price" value="'. esc_html($product->get_price()).'" />'
600
- . '<input type="hidden" class="ls-pro-sku" value="'. $product->get_sku() . '"/>'
601
- . '<input type="hidden" class="ls-pro-name" value="' . $product->get_title() . '"/>'
602
- . '<input type="hidden" class="ls-pro-category" value="' . $categories . '"/>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603
  }
604
 
605
  /**
606
- * Enhanced E-commerce tracking for remove from cart
607
  *
608
  * @access public
609
  * @return void
610
  */
611
- public function remove_cart_tracking() {
612
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
613
  return;
614
  }
615
- global $woocommerce;
616
- if (version_compare($woocommerce->version, "2.1", ">=")) {
617
- wc_enqueue_js('$(".remove").click(function(){
618
- ga("ec:addProduct", {
619
- "name": $(this).parents("tr").find(".product-name").text(),
620
- "price": $(this).parents("tr").find(".product-price").text(),
621
- "quantity": $(this).parents("tr").find(".product-quantity .qty").val()
622
- });
623
- ga("ec:setAction", "remove");
624
- ga("send", "event", "Enhanced-Ecommerce", "click", "remove from cart",{"nonInteraction": 1});
625
- });
626
- ');
627
- } else {
628
- $woocommerce->add_inline_js('
629
- $(".remove").click(function(){
630
- ga("ec:addProduct", {
631
- "name": $(this).parents("tr").find(".product-name").text(),
632
- "price": $(this).parents("tr").find(".product-price").text(),
633
- "quantity": $(this).parents("tr").find(".product-quantity .qty").val()
634
- });
635
- ga("ec:setAction", "remove");
636
- ga("send", "event", "Enhanced-Ecommerce", "click", "remove from cart",{"nonInteraction": 1});
637
- });
638
- ');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
639
  }
640
  }
641
 
642
  /**
643
- * Enhanced E-commerce tracking checkout steps
644
  *
645
  * @access public
646
  * @return void
647
  */
648
- public function checkout_step_one_tracking() {
649
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
650
  return;
651
  }
652
-
653
  global $woocommerce;
654
-
655
- $code="";
656
- foreach ($woocommerce->cart->cart_contents as $item) {
657
- $p = get_product($item["product_id"]);
658
-
659
  $category = get_the_terms($item["product_id"], "product_cat");
660
  $categories = "";
661
- if($category){
662
- foreach ($category as $term) {
663
- $categories.=$term->name . ",";
664
- }
665
  }
666
- //remove last comma(,) if multiple categories are there
667
  $categories = rtrim($categories, ",");
 
 
 
 
 
 
 
 
 
668
 
669
- $code .= 'ga("ec:addProduct", {"id": "' . esc_js($p->get_sku()) . '",';
670
- $code .= '"name": "' . esc_js($p->get_title()) . '",';
671
- $code .= '"category": "' . esc_js($categories) . '",';
672
- $code .= '"price": "' . esc_js($p->get_price()) . '",';
673
- $code .= '"quantity": "' . esc_js($item["quantity"]) . '"});';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
674
  }
 
675
 
676
  $code_step_1 = $code . 'ga("ec:setAction","checkout",{"step": 1});';
677
- $code_step_1 .= 'ga("send", "event", "Enhanced-Ecommerce","checkout_step_1",{"nonInteraction": 1});';
678
- if (version_compare($woocommerce->version, "2.1", ">=")) {
679
- wc_enqueue_js($code_step_1);
680
- } else {
681
- $woocommerce->add_inline_js($code_step_1);
682
- }
683
 
 
 
 
 
 
 
 
 
 
 
 
684
 
685
  $code_step_2 = $code . 'ga("ec:setAction","checkout",{"step": 2});';
686
- $code_step_2 .= 'ga("send", "event", "Enhanced-Ecommerce","checkout_step_2",{"nonInteraction": 1});';
687
 
 
688
  if (is_user_logged_in()) {
689
- if (version_compare($woocommerce->version, "2.1", ">=")) {
690
- wc_enqueue_js($code_step_2);
691
- } else {
692
- $woocommerce->add_inline_js($code_step_2);
693
- }
694
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
695
  $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);
696
 
697
  $code_step_3 = $code . 'ga("ec:setAction","checkout",{"step": 3});';
698
- $code_step_3 .= 'ga("send", "event", "Enhanced-Ecommerce", "checkout_step_3",{"nonInteraction": 1});';
699
 
700
- $inline_js = 'jQuery(document).on("click","#place_order",function(e){';
701
  if ($step_2_on_proceed_to_pay) {
702
- $inline_js .= $code_step_2;
 
703
  }
704
  $inline_js .= $code_step_3;
705
- $inline_js .= "});";
706
- if (version_compare($woocommerce->version, "2.1", ">=")) {
707
- wc_enqueue_js($inline_js);
708
- } else {
709
- $woocommerce->add_inline_js($inline_js);
710
- }
711
  }
712
 
713
  /**
714
- * Sending hits with event
715
  *
716
  * @access public
717
  * @return void
718
  */
719
- public function default_pageview() {
720
-
721
  global $woocommerce;
722
- if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
723
- return;
724
- }
 
725
 
726
- if (!$this->disable_tracking($this->ga_standard_tracking_enabled)) {
727
- $inline_js = 'ga("send", "event", "Enhanced-Ecommerce", "pageview", "footer",{"nonInteraction": 1})';
728
- } else {
729
- $inline_js = 'ga("send", "event", "Enhanced-Ecommerce", "pageview", "footer",{"nonInteraction": 1});';
730
- }
 
 
 
 
731
 
732
- if (version_compare($woocommerce->version, "2.1", ">=")) {
733
- wc_enqueue_js($inline_js);
734
- } else {
735
- $woocommerce->add_inline_js($inline_js);
 
736
  }
 
737
  }
738
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
739
  /**
740
  * Check if tracking is disabled
741
  *
@@ -749,12 +977,27 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
749
  }
750
  }
751
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
752
  /**
753
  * check UA is enabled or not
754
  *
755
  * @access public
756
  */
757
- function check_UA_enabled(){
758
  echo '<script>
759
  jQuery("#woocommerce_enhanced_ecommerce_google_analytics_ga_standard_tracking_enabled").change(function(){
760
  t_ga_chk=jQuery(this).is(":checked");
@@ -770,19 +1013,69 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
770
  });
771
  </script>';
772
  }
773
-
774
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
775
  /**
776
  * Sending email to remote server
777
  *
778
  * @access public
779
  * @return void
780
  */
781
- public function send_email_to_tatvic($email) {
782
  //set POST variables
783
  $url = "http://dev.tatvic.com/leadgen/woocommerce-plugin/store_email/";
784
  $fields = array(
785
  "email" => urlencode($email),
 
786
  );
787
  wp_remote_post($url, array(
788
  "method" => "POST",
@@ -796,4 +1089,5 @@ class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
796
  }
797
 
798
  }
 
799
  ?>
9
  * @extends WC_Integration
10
  * @author Sudhir Mishra <sudhir@tatvic.com>
11
  */
 
12
  class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
13
 
14
  /**
18
  * @return void
19
  */
20
  public function __construct() {
21
+ global $homepage_json_fp, $homepage_json_rp;
22
+ //incase of admin setting not saved
23
+ /* $path = plugins_url();
24
+ $admin_setting_handle = file($path . "/enhanced-e-commerce-for-woocommerce-store/includes/settings.txt");
25
+ $flag_for_admin = $this->explode_admin_setting($admin_setting_handle[0]); //settings handle either to file or screen
26
+ */
27
+
28
  $this->id = "enhanced_ecommerce_google_analytics";
29
  $this->method_title = __("Enhanced Ecommerce Google Analytics", "woocommerce");
30
  $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");
33
  $this->init_form_fields();
34
  $this->init_settings();
35
 
36
+ // Define user set variables -- check for where to read settings
37
+ // if ($flag_for_admin == "yes") {
38
+ $this->ga_email = $this->get_option("ga_email");
39
+ $this->ga_id = $this->get_option("ga_id");
40
+ $this->ga_set_domain_name = $this->get_option("ga_set_domain_name");
41
+ $this->ga_local_curr = $this->get_option("ga_local_curr");
42
+ $this->ga_standard_tracking_enabled = $this->get_option("ga_standard_tracking_enabled");
43
+ $this->enable_guest_checkout = get_option("woocommerce_enable_guest_checkout") == "yes" ? true : false; //guest checkout
44
+ $this->track_login_step_for_guest_user = $this->get_option("track_login_step_for_guest_user") == "yes" ? true : false; //guest checkout
45
+ $this->ga_enhanced_ecommerce_tracking_enabled = $this->get_option("ga_enhanced_ecommerce_tracking_enabled");
46
+ $this->ga_display_feature_plugin = $this->get_option("ga_display_feature_plugin") == "yes" ? true : false;
47
+ $this->ga_enhanced_ecommerce_category_page_impression_threshold = $this->get_option("ga_enhanced_ecommerce_category_page_impression_threshold");
48
+ /* } else {
49
+ //read from settings.txt
50
+ $this->ga_email = $this->explode_admin_setting($admin_setting_handle[1]);
51
+ $this->ga_id = $this->explode_admin_setting($admin_setting_handle[2]);
52
+ $this->ga_set_domain_name = $this->explode_admin_setting($admin_setting_handle[3]);
53
+ $this->ga_local_curr = $this->explode_admin_setting($admin_setting_handle[4]);
54
+ $this->ga_standard_tracking_enabled = $this->explode_admin_setting($admin_setting_handle[5]);
55
+ $this->enable_guest_checkout = get_option("woocommerce_enable_guest_checkout") == "yes" ? true : false; //guest checkout
56
+ $this->track_login_step_for_guest_user = $this->explode_admin_setting($admin_setting_handle[6]) === "yes" ? true : false;
57
+ $this->ga_enhanced_ecommerce_tracking_enabled = $this->explode_admin_setting($admin_setting_handle[7]);
58
+ $this->ga_display_feature_plugin = $this->explode_admin_setting($admin_setting_handle[8]) == "yes" ? true : false;
59
+ $this->ga_enhanced_ecommerce_category_page_impression_threshold = $this->explode_admin_setting($admin_setting_handle[9]);
60
+ }*/
61
+
62
  // Actions
63
  add_action("woocommerce_update_options_integration_enhanced_ecommerce_google_analytics", array($this, "process_admin_options"));
64
  // API Call to LS with e-mail
67
  add_action("woocommerce_thankyou", array($this, "ecommerce_tracking_code"));
68
 
69
  // Enhanced Ecommerce product impression hook
70
+ add_action("wp_footer", array($this, "homepage_impression"));
71
+ add_action("wp_footer", array($this, "cate_page_prod_impression")); // Hook for category page
72
+ add_action("woocommerce_after_shop_loop_item", array($this, "bind_product_metadata"));
73
  add_action("woocommerce_after_single_product", array($this, "product_detail_view"));
74
  add_action("woocommerce_after_cart", array($this, "remove_cart_tracking"));
75
+ add_action("woocommerce_before_checkout_billing_form", array($this, "checkout_step_1_tracking"));
76
+ add_action("woocommerce_after_checkout_billing_form", array($this, "checkout_step_2_tracking"));
77
+ add_action("woocommerce_after_checkout_billing_form", array($this, "checkout_step_3_tracking"));
78
 
79
  // Event tracking code
80
  add_action("woocommerce_after_add_to_cart_button", array($this, "add_to_cart"));
81
+ add_action("woocommerce_before_shop_loop_item", array($this, "add_divwrap_before_product"));
82
+ add_action("woocommerce_after_shop_loop_item", array($this, "add_divwrap_after_product"));
83
  add_action("wp_footer", array($this, "loop_add_to_cart"));
84
+ //add_action("wp_footer", array($this, "default_pageview"));
 
85
  //Enable display feature code checkbox
86
+ add_action("admin_footer", array($this, "admin_check_UA_enabled"));
87
+
88
+ //add version details in footer
89
+ add_action("wp_footer", array($this, "add_plugin_details"));
90
+
91
+ //Add Nearest JQuery
92
+ add_action("wp_head", array($this, "add_nearest_jQuery"));
93
+
94
+ //Add Dev ID
95
+ add_action("wp_head", array($this, "add_dev_id"),1);
96
+ }
97
+
98
+ /**
99
+ * explode patch settings from settings.txt file
100
+ *
101
+ * @access public
102
+ * @return void
103
+ */
104
+ function explode_admin_setting($admin_setting_handle) {
105
+ $ex_Arr = explode("=", $admin_setting_handle);
106
+ return preg_replace('/\s+/', '', $ex_Arr[1]);
107
+ }
108
+
109
+ /**
110
+ * add dev id
111
+ *
112
+ * @access public
113
+ * @return void
114
+ */
115
+ function add_dev_id() {
116
+ echo "<script>(window.gaDevIds=window.gaDevIds||[]).push('5CDcaG');</script>";
117
  }
118
+
119
+ /**
120
+ * add custom div before product shop loop
121
+ *
122
+ * @access public
123
+ * @return void
124
+ */
125
+ function add_divwrap_before_product() {
126
+ //restrict home page
127
+ if (!is_home()) {
128
+ echo "<div class=t_singleproduct_cont>";
129
+ }
130
+ }
131
+
132
+ /**
133
+ * add custom div after product shop loop
134
+ *
135
+ * @access public
136
+ * @return void
137
+ */
138
+ function add_divwrap_after_product() {
139
+ //restrict home page
140
+ if (!is_home()) {
141
+ echo "</div>";
142
+ }
143
+ }
144
+
145
+ /**
146
  * display details of plugin
147
  *
148
  * @access public
149
  * @return void
150
  */
151
+ function add_plugin_details() {
152
+ echo '<!--Enhanced Ecommerce Google Analytics Plugin for Woocommerce by Tatvic.'
153
+ . 'Plugin Version: 1.0.10-->';
154
  }
155
+
156
  /**
157
  * Initialise Settings Form Fields
158
  *
160
  * @return void
161
  */
162
  function init_form_fields() {
163
+ $ga_currency_code = array("USD" => "USD", "AED" => "AED", "ARS" => "ARS", "AUD" => "AUD", "BGN" => "BGN", "BOB" => "BOB", "BRL" => "BRL", "CAD" => "CAD", "CHF" => "CHF", "CLP" => "CLP", "CNY" => "CNY", "COP" => "COP", "CZK" => "CZK", "DKK" => "DKK", "EGP" => "EGP", "EUR" => "EUR", "FRF" => "FRF", "GBP" => "GBP", "HKD" => "HKD", "HRK" => "HRK", "HUF" => "HUF", "IDR" => "IDR", "ILS" => "ILS", "INR" => "INR", "JPY" => "JPY", "KRW" => "KRW", "LTL" => "LTL", "MAD" => "MAD", "MXN" => "MXN", "MYR" => "MYR", "NOK" => "NOK", "NZD" => "NZD", "PEN" => "PEN", "PHP" => "PHP", "PKR" => "PKR", "PLN" => "PLN", "RON" => "RON", "RSD" => "RSD", "RUB" => "RUB", "SAR" => "SAR", "SEK" => "SEK", "SGD" => "SGD", "THB" => "THB", "TRY" => "TRY", "TWD" => "TWD", "UAH" => "UAH", "VEF" => "VEF", "VND" => "VND", "ZAR" => "ZAR");
164
  $this->form_fields = array(
165
  "ga_email" => array(
166
  "title" => __("Email Address", "woocommerce"),
167
+ "description" => __("Provide your work email address to receive plugin enhancement updates", "woocommerce"),
168
  "type" => "email",
169
  "required" => "required",
170
  "default" => get_option("woocommerce_ga_email") // Backwards compat
171
  ),
172
  "ga_id" => array(
173
  "title" => __("Google Analytics ID", "woocommerce"),
174
+ "description" => __("Enter your Google Analytics ID here. You can login into your Google Analytics account to find your ID. e.g.<code>UA-XXXXX-X</code>", "woocommerce"),
175
  "type" => "text",
176
  "default" => get_option("woocommerce_ga_id") // Backwards compat
177
  ),
178
  "ga_set_domain_name" => array(
179
  "title" => __("Set Domain Name", "woocommerce"),
180
+ "description" => sprintf(__("Enter your domain name here (Optional)")),
181
  "type" => "text",
182
  "default" => ""
183
  ),
184
+ "ga_local_curr" => array(
185
+ "title" => __("Set Currency", "woocommerce"),
186
+ "description" => __("Find your Local Currency Code by visiting this <a href='https://developers.google.com/analytics/devguides/platform/currencies#supported-currencies' target='_blank'>link</a>", "woocommerce"),
187
+ "type" => "select",
188
+ "required" => "required",
189
+ "default" => "USD", // Backwards compat
190
+ "options" => $ga_currency_code
191
+ ),
192
  "ga_standard_tracking_enabled" => array(
193
  "title" => __("Tracking code", "woocommerce"),
194
  "label" => __("Add Universal Analytics Tracking Code (Optional)", "woocommerce"),
195
+ "description" => sprintf(__("This feature adds Universal Analytics Tracking Code to your Store. 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, add <code>ga(\"require\", \"ec\", \"ec.js\");</code> below <code>ga(\"create\",\"UA-XXXXX-X\")</code> in your standard code snippet. Also ensure that the Universal Analytics code is present in the &lt;head&gt; section of the website.", "woocommerce")),
196
  "type" => "checkbox",
197
  "checkboxgroup" => "start",
198
  "default" => get_option("woocommerce_ga_standard_tracking_enabled") ? get_option("woocommerce_ga_standard_tracking_enabled") : "no" // Backwards compat
201
  "label" => __("Add Display Advertising Feature Code (Optional)", "woocommerce"),
202
  "type" => "checkbox",
203
  "checkboxgroup" => "",
204
+ "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><br/>This feature can only be enabled if you have enabled UA Tracking from our Plugin. If not, you can still manually add the display advertising code by following the instructions from this <a href='https://developers.google.com/analytics/devguides/collection/analyticsjs/display-features' target='_blank'>link</a>", "woocommerce")),
205
+ "default" => get_option("woocommerce_ga_display_feature_plugin") ? get_option("woocommerce_ga_display_feature_plugin") : "no" // Backwards compat
206
  ),
207
  "ga_enhanced_ecommerce_tracking_enabled" => array(
208
  "label" => __("Add Enhanced Ecommerce Tracking Code", "woocommerce"),
209
  "type" => "checkbox",
210
  "checkboxgroup" => "",
211
+ "description" => sprintf(__("This feature adds Enhanced Ecommerce Tracking Code to your Store", "woocommerce")),
212
  "default" => get_option("woocommerce_ga_ecommerce_tracking_enabled") ? get_option("woocommerce_ga_ecommerce_tracking_enabled") : "no" // Backwards compat
213
  ),
214
  "track_login_step_for_guest_user" => array(
220
  ),
221
  "ga_enhanced_ecommerce_category_page_impression_threshold" => array(
222
  "title" => __("Impression Threshold", "woocommerce"),
223
+ "description" => sprintf(__("This feature sets Impression threshold for category page. It sends hit after these many numbers of products impressions", "woocommerce")),
224
  "type" => "input",
225
  "default" => "6"
226
  )
237
  if ($save_for_the_plugin && $update_made_for_email) {
238
  if ($_REQUEST["woocommerce_enhanced_ecommerce_google_analytics_ga_email"] != "") {
239
  $email = $_REQUEST["woocommerce_enhanced_ecommerce_google_analytics_ga_email"];
240
+ $domain_name = $_REQUEST["woocommerce_enhanced_ecommerce_google_analytics_ga_set_domain_name"];
241
+ $this->send_email_to_tatvic($email, $domain_name);
242
  }
243
  }
244
  }
245
  }
246
 
 
 
247
  /**
248
  * Google Analytics standard tracking
249
  *
252
  */
253
  function google_tracking_code() {
254
  global $woocommerce;
255
+
256
+ //common validation----start
257
  if (is_admin() || current_user_can("manage_options") || $this->ga_standard_tracking_enabled == "no") {
258
  return;
259
  }
264
  return;
265
  }
266
 
267
+ //common validation----end
268
 
269
  if (!empty($this->ga_set_domain_name)) {
270
  $set_domain_name = esc_js($this->ga_set_domain_name);
272
  $set_domain_name = "auto";
273
  }
274
 
275
+ //add display features
276
  if ($this->ga_display_feature_plugin) {
277
  $ga_display_feature_code = 'ga("require", "displayfeatures");';
278
  } else {
279
  $ga_display_feature_code = "";
280
  }
281
+
282
+ $code = '
283
+ (function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
284
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
285
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
286
  })(window,document,"script","//www.google-analytics.com/analytics.js","ga");
287
+ ga("create", "' . esc_js($tracking_id) . '", "' . $set_domain_name . '");
288
+ ' . $ga_display_feature_code . '
289
+ ga("require", "ec", "ec.js");
290
+ ga("send", "pageview");';
291
+
292
+ //include this on all pages except order confirmation page.
293
+ if (!is_order_received_page()) {
294
+ echo "<script>" . $code . "</script>";
295
+ }
296
+ }
 
 
297
 
298
  /**
299
  * Google Analytics eCommerce tracking
318
 
319
  // Get the order and output tracking code
320
  $order = new WC_Order($order_id);
321
+ //Get Applied Coupon Codes
322
+ if ($order->get_used_coupons()) {
323
+ $coupons_count = count($order->get_used_coupons());
324
+ $i = 1;
325
+ $coupons_list = '';
326
+ foreach ($order->get_used_coupons() as $coupon) {
327
+ $coupons_list .= $coupon;
328
+ if ($i < $coupons_count)
329
+ $coupons_list .= ', ';
330
+ $i++;
331
+ }
332
+ }
333
 
334
+ //get domain name if value is set
335
  if (!empty($this->ga_set_domain_name)) {
336
  $set_domain_name = esc_js($this->ga_set_domain_name);
337
  } else {
338
  $set_domain_name = "auto";
339
  }
340
+
341
  //add display features
342
  if ($this->ga_display_feature_plugin) {
343
  $ga_display_feature_code = 'ga("require", "displayfeatures");';
344
  } else {
345
  $ga_display_feature_code = "";
346
  }
347
+
348
+ //add Pageview on order page if user checked Add Standard UA code
349
+ if ($this->ga_standard_tracking_enabled) {
350
+ $ga_pageview = 'ga("send", "pageview");';
351
+ } else {
352
+ $ga_pageview = "";
353
+ }
354
+
355
  $code = '(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
356
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
357
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
358
  })(window,document,"script","//www.google-analytics.com/analytics.js","ga");
359
 
360
+ ga("create", "' . esc_js($tracking_id) . '", "' . $set_domain_name . '");
361
+ ' . $ga_display_feature_code . '
362
  ga("require", "ec", "ec.js");
363
+ ' . $ga_pageview . '
364
+ ';
 
365
  // Order items
366
  if ($order->get_items()) {
367
  foreach ($order->get_items() as $item) {
368
  $_product = $order->get_product_from_item($item);
369
 
370
+ //set local currencies
371
+ $code .= 'ga("set", "&cu", "' . $this->ga_local_curr . '");';
372
  $code .= 'ga("ec:addProduct", {';
373
+ $code .= '"id": "' . esc_js($_product->get_sku() ? $_product->get_sku() : $_product->id) . '",';
374
  $code .= '"name": "' . esc_js($item["name"]) . '",';
375
+
376
  if (isset($_product->variation_data)) {
377
 
378
  $code .= '"category": "' . esc_js(woocommerce_get_formatted_variation($_product->variation_data, true)) . '",';
384
  $out[] = $category->name;
385
  }
386
  }
387
+ $code .= '"category": "' . esc_js(join(",", $out)) . '",';
388
  }
389
 
390
  $code .= '"price": "' . esc_js($order->get_item_total($item)) . '",';
397
  "id": "' . esc_js($order->get_order_number()) . '", // Transaction ID. Required
398
  "affiliation": "' . esc_js(get_bloginfo("name")) . '", // Affiliation or store name
399
  "revenue": "' . esc_js($order->get_total()) . '", // Grand Total
400
+ "tax": "' . esc_js($order->get_total_tax()) . '", // Tax
401
  "shipping": "' . esc_js($order->get_shipping()) . '", // Shipping
402
+ "coupon":"' . $coupons_list . '"
403
+ });
404
+
405
+ ga("send", "event", "Enhanced-Ecommerce","load", "order_confirmation", {"nonInteraction": 1});
406
+ ';
 
 
 
407
 
408
+ //check woocommerce version
409
+ $this->wc_version_compare($code);
410
  update_post_meta($order_id, "_ga_tracked", 1);
411
  }
412
 
417
  * @return void
418
  */
419
  public function cate_page_prod_impression() {
420
+ global $product;
421
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
422
  return;
423
  }
424
+
425
+ //Identify List of product listing page wise :
426
+ //Included Pages: Home page ,Category Page , Search page
427
+
428
+ $t_list = "";
429
+ $t_list_clk = "";
430
+ $t_load_action = "";
431
+ $t_click_action = "";
432
+
433
+ if (is_search()) {
434
+ $t_list_clk = '"list":"Search Results"';
435
+ $t_list = $t_list_clk . ',';
436
+ $t_load_action = "product_impression_srch";
437
+ $t_click_action = "product_click_srch";
438
+ } else if (is_product_category()) {
439
+ $t_list_clk = '"list":"Category Page"';
440
+ $t_list = $t_list_clk . ',';
441
+ $t_load_action = "product_impression_cp";
442
+ $t_click_action = "product_click_cp";
443
+ } else if (is_product()) {
444
+ //Considered only Related Products
445
+ $t_list_clk = '"list":"Related Products"';
446
+ $t_list = $t_list_clk . ',';
447
+ $t_load_action = "product_impression_rdp";
448
+ $t_click_action = "product_click_rdp";
449
  }
450
+
451
  $impression_threshold = $this->ga_enhanced_ecommerce_category_page_impression_threshold;
452
+
453
+ $code = 't_cnt=0;
454
+ t_ttl_prod=jQuery(".ls-pro-sku").length;
455
+ jQuery(".t_singleproduct_cont").each(function(index){
456
+ t_cnt++;
 
457
  ga("ec:addImpression", {
458
  "id": jQuery(this).find(".ls-pro-sku").val(),
459
  "name": jQuery(this).find(".ls-pro-name").val(),
460
  "category": jQuery(this).find(".ls-pro-category").val(),
461
+ ' . $t_list . '
462
  "price": jQuery(this).find(".ls-pro-price").val(),
463
  "position": index+1
464
  });
465
 
466
+ if(t_ttl_prod > ' . esc_js($impression_threshold) . '){
467
+ if((t_cnt%' . esc_js($impression_threshold) . ')==0){
468
+ t_ttl_prod=t_ttl_prod-' . esc_js($impression_threshold) . ';
469
+ ga("send", "event", "Enhanced-Ecommerce","load","' . $t_load_action . '", {"nonInteraction": 1});
470
  }
471
  }else{
472
  t_ttl_prod--;
473
  if(t_ttl_prod==0){
474
+ ga("send", "event", "Enhanced-Ecommerce","load", "' . $t_load_action . '", {"nonInteraction": 1});
475
  }
476
  }
477
 
478
  jQuery(this).find("a:not(.add_to_cart_button)").on("click",function(){
479
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480
  ga("ec:addProduct", {
481
  "id": jQuery(this).parents("li").find(".ls-pro-sku").val(),
482
  "name": jQuery(this).parents("li").find(".ls-pro-name").val(),
484
  "price": jQuery(this).parents("li").find(".ls-pro-price").val(),
485
  "position": index+1
486
  });
487
+ ga("ec:setAction", "click", {' . $t_list_clk . '});
488
+ ga("send", "event", "Enhanced-Ecommerce","click","' . $t_click_action . '", {"nonInteraction": 1});
489
 
490
 
491
  });
492
  });
493
+ ';
494
+
495
+ if (!is_home()) {
496
+ //check woocommerce version
497
+ $this->wc_version_compare($code);
498
  }
499
  }
500
 
505
  * @return void
506
  */
507
  function add_to_cart() {
 
508
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled))
509
  return;
 
510
  if (!is_single())
511
  return;
512
+ global $product;
513
+ $category = get_the_terms($product->ID, "product_cat");
514
+ $categories = "";
515
+ if ($category) {
516
+ foreach ($category as $term) {
517
+ $categories.=$term->name . ",";
518
+ }
519
+ }
520
+ //remove last comma(,) if multiple categories are there
521
+ $categories = rtrim($categories, ",");
522
 
523
+ $code = '$(".single_add_to_cart_button").click(function() {
 
 
524
 
525
  // Enhanced E-commerce Add to cart clicks
526
  ga("ec:addProduct", {
527
+ "id" : "' . esc_js($product->get_sku() ? $product->get_sku() : $product->id ) . '",
528
+ "name": "' . esc_js($product->get_title()) . '",
529
+ "category" :"' . $categories . '",
530
  "price": "' . esc_js($product->get_price()) . '",
531
+ "quantity" :jQuery(this).parent().find("input[name=quantity]").val()
532
  });
533
  ga("ec:setAction", "add");
534
+ ga("send", "event", "Enhanced-Ecommerce","click", "add_to_cart_click", {"nonInteraction": 1});
535
  });
536
+ ';
537
+ //check woocommerce version
538
+ $this->wc_version_compare($code);
 
 
 
 
 
 
 
 
 
 
 
 
 
539
  }
540
 
541
  /**
549
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
550
  return;
551
  }
 
552
 
553
+ $code = '$(".add_to_cart_button:not(.product_type_variable, .product_type_grouped)").click(function() {
554
+
555
+ t_qty=$(this).parent().find("input[name=quantity]").val();
556
+ //default quantity 1 if quantity box is not there
557
+ if(t_qty=="" || t_qty===undefined){
558
+ t_qty="1";
559
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
  // Enhanced E-commerce Add to cart clicks
561
  ga("ec:addProduct", {
562
  "id": $(this).parents("li").find(".ls-pro-sku").val(),
563
  "name": $(this).parents("li").find(".ls-pro-name").val(),
564
  "category": $(this).parents("li").find(".ls-pro-category").val(),
565
  "price": $(this).parents("li").find(".ls-pro-price").val(),
566
+ "quantity" :t_qty
567
  });
568
  ga("ec:setAction", "add");
569
+ ga("send", "event", "Enhanced-Ecommerce","click", "add_to_cart_click",{"nonInteraction": 1});
570
  });
571
+ ';
572
+ //check woocommerce version
573
+ $this->wc_version_compare($code);
574
  }
575
 
576
  /**
580
  * @return void
581
  */
582
  public function product_detail_view() {
583
+
584
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
585
  return;
586
  }
587
 
588
  global $product;
 
589
  $category = get_the_terms($product->ID, "product_cat");
590
  $categories = "";
591
+ if ($category) {
592
+ foreach ($category as $term) {
593
+ $categories.=$term->name . ",";
594
+ }
595
  }
596
+ //remove last comma(,) if multiple categories are there
597
  $categories = rtrim($categories, ",");
 
 
598
 
599
+ $code = 'ga("ec:addProduct", {
600
+ "id": "' . esc_js($product->get_sku() ? $product->get_sku() : $product->id) . '", // Product details are provided in an impressionFieldObject.
 
 
 
 
 
 
 
 
601
  "name": "' . $product->get_title() . '",
602
  "category": "' . $categories . '",
603
  });
604
  ga("ec:setAction", "detail");
605
+ ga("send", "event", "Enhanced-Ecommerce", "load","product_impression_pp", {"nonInteraction": 1});
606
+ ';
607
+ //check woocommerce version
608
+ $this->wc_version_compare($code);
609
  }
610
 
611
  /**
614
  * @access public
615
  * @return void
616
  */
617
+ public function bind_product_metadata() {
618
 
619
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
620
  return;
621
  }
622
 
 
 
 
 
623
  global $product;
 
624
  $category = get_the_terms($product->ID, "product_cat");
625
  $categories = "";
626
+ if ($category) {
627
+ foreach ($category as $term) {
628
+ $categories.=$term->name . ",";
629
+ }
630
  }
631
  //remove last comma(,) if multiple categories are there
632
  $categories = rtrim($categories, ",");
633
 
634
+ echo '<input type="hidden" class="ls-pro-price" value="' . esc_html($product->get_price()) . '" />'
635
+ . '<input type="hidden" class="ls-pro-sku" value="' . esc_html($product->get_sku() ? $product->get_sku() : $product->id) . '"/>'
636
+ . '<input type="hidden" class="ls-pro-name" value="' . esc_html($product->get_title()) . '"/>'
637
+ . '<input type="hidden" class="ls-pro-category" value="' . esc_html($categories) . '"/>'
638
+ . '<input type="hidden" class="ls-pro-isfeatured" value="' . $product->is_featured() . '"/> ';
639
+
640
+ global $homepage_json_fp, $homepage_json_rp;
641
+ if (is_home()) {
642
+ if (!is_array($homepage_json_fp) && !is_array($homepage_json_rp)) {
643
+ $homepage_json_fp = array();
644
+ $homepage_json_rp = array();
645
+ }
646
+ if ($product->is_featured()) {
647
+ $jsonArr_prod_fp = array(get_permalink($product->id) => array(
648
+ "id" => esc_html($product->id),
649
+ "sku" => esc_html($product->get_sku() ? $product->get_sku() : $product->id),
650
+ "name" => esc_html($product->get_title()),
651
+ "price" => esc_html($product->get_price()),
652
+ "category" => esc_html($categories)
653
+ ));
654
+ array_push($homepage_json_fp, $jsonArr_prod_fp);
655
+ } else {
656
+ $jsonArr_prod_rp = array(get_permalink($product->id) => array(
657
+ "id" => esc_html($product->id),
658
+ "sku" => esc_html($product->get_sku() ? $product->get_sku() : $product->id),
659
+ "name" => esc_html($product->get_title()),
660
+ "price" => esc_html($product->get_price()),
661
+ "category" => esc_html($categories)
662
+ ));
663
+ array_push($homepage_json_rp, $jsonArr_prod_rp);
664
+ }
665
+ }
666
  }
667
 
668
  /**
669
+ * Enhanced E-commerce tracking for product impressions,clicks on Home pages
670
  *
671
  * @access public
672
  * @return void
673
  */
674
+ function homepage_impression() {
675
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
676
  return;
677
  }
678
+ //get impression threshold
679
+ $impression_threshold = $this->ga_enhanced_ecommerce_category_page_impression_threshold;
680
+
681
+ //Product impression on Home Page
682
+ global $homepage_json_fp, $homepage_json_rp;
683
+ $this->wc_version_compare("homepage_json_fp=" . json_encode($homepage_json_fp) . ";");
684
+ $this->wc_version_compare("homepage_json_rp=" . json_encode($homepage_json_rp) . ";");
685
+ $hmpg_impressions_jQ = '
686
+
687
+ function hmpg_impressions(t_json_name,t_action,t_list){
688
+ t_send_threshold=0;
689
+ t_prod_pos=0;
690
+
691
+ t_json_length=t_json_name.length;
692
+
693
+ for(i = 0;i < t_json_name.length;i++) {
694
+ t_send_threshold++;
695
+ t_prod_url_key=Object.keys(t_json_name[i]);
696
+ t_prod_pos++;
697
+
698
+ ga("ec:addImpression", {
699
+ "id": t_json_name[i][t_prod_url_key]["sku"],
700
+ "name": t_json_name[i][t_prod_url_key]["name"],
701
+ "category": t_json_name[i][t_prod_url_key]["category"],
702
+ "list":t_list,
703
+ "price": t_json_name[i][t_prod_url_key]["price"],
704
+ "position": t_prod_pos
705
+ });
706
+
707
+ if(t_json_length > ' . esc_js($impression_threshold) . '){
708
+ if((t_send_threshold%' . esc_js($impression_threshold) . ')==0){
709
+ t_json_length=t_json_length-' . esc_js($impression_threshold) . ';
710
+ ga("send", "event", "Enhanced-Ecommerce","load","product_impression_"+t_action , {"nonInteraction": 1});
711
+ }
712
+ }else{
713
+
714
+ t_json_length--;
715
+ if(t_json_length==0){
716
+ ga("send", "event", "Enhanced-Ecommerce","load", "product_impression_"+t_action, {"nonInteraction": 1});
717
+ }
718
+ }
719
+ }
720
+ }
721
+ jQuery("a").on("click",function(){
722
+ t_url=jQuery(this).attr("href");
723
+ prod_exists_in_JSON(t_url,homepage_json_fp,"Featured Products","fp");
724
+ prod_exists_in_JSON(t_url,homepage_json_rp,"Recent Products","rp");
725
+ });
726
+ //function for comparing urls in json object
727
+ function prod_exists_in_JSON(t_url,t_json_name,t_list,t_action){
728
+ for(i=0;i<t_json_name.length;i++){
729
+ if(t_url==Object.keys(t_json_name[i])){
730
+ t_prod_url_key=Object.keys(t_json_name[i]);
731
+ ga("ec:addProduct", {
732
+ "id": t_json_name[i][t_prod_url_key]["sku"],
733
+ "name": t_json_name[i][t_prod_url_key]["name"],
734
+ "category": t_json_name[i][t_prod_url_key]["category"],
735
+ "price": t_json_name[i][t_prod_url_key]["price"],
736
+ "position": ++i
737
+ });
738
+ ga("ec:setAction", "click", {
739
+ "list": t_list
740
+ });
741
+ ga("send", "event", "Enhanced-Ecommerce","click", "product_click_"+t_action, {"nonInteraction": 1});
742
+ }
743
+ }
744
+ }
745
+ if(homepage_json_fp.length !== 0){
746
+ hmpg_impressions(homepage_json_fp,"fp","Featured Products");
747
+ }
748
+ if(homepage_json_rp.length !== 0){
749
+ hmpg_impressions(homepage_json_rp,"rp","Recent Products");
750
+ }
751
+ ';
752
+ if (is_home()) {
753
+ $this->wc_version_compare($hmpg_impressions_jQ);
754
  }
755
  }
756
 
757
  /**
758
+ * Enhanced E-commerce tracking for remove from cart
759
  *
760
  * @access public
761
  * @return void
762
  */
763
+ public function remove_cart_tracking() {
764
  if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
765
  return;
766
  }
 
767
  global $woocommerce;
768
+ $cartpage_prod_array_main = array();
769
+ foreach ($woocommerce->cart->cart_contents as $key => $item) {
770
+ $prod_meta = get_product($item["product_id"]);
 
 
771
  $category = get_the_terms($item["product_id"], "product_cat");
772
  $categories = "";
773
+ if ($category) {
774
+ foreach ($category as $term) {
775
+ $categories.=$term->name . ",";
776
+ }
777
  }
778
+ //remove last comma(,) if multiple categories are there
779
  $categories = rtrim($categories, ",");
780
+ $cartpage_prod_array = array($key => array(
781
+ "id" => esc_html($prod_meta->id),
782
+ "sku" => esc_html($prod_meta->get_sku() ? $prod_meta->get_sku() : $prod_meta->id),
783
+ "name" => esc_html($prod_meta->get_title()),
784
+ "price" => esc_html($prod_meta->get_price()),
785
+ "category" => esc_html($categories)
786
+ ));
787
+ array_push($cartpage_prod_array_main, $cartpage_prod_array);
788
+ }
789
 
790
+ //Cart Page item Array to Json
791
+ $this->wc_version_compare("cartpage_prod_json=" . json_encode($cartpage_prod_array_main) . ";");
792
+
793
+ $code = '
794
+ $.urlParam = function(name,t_url){
795
+ var results = new RegExp("[\?&]" + name + "=([^&#]*)").exec(t_url);
796
+ if (results==null){
797
+ return null;
798
+ }
799
+ else{
800
+ return results[1] || 0;
801
+ }
802
+ }
803
+
804
+ $(".remove").click(function(){
805
+ t_get_session_id=jQuery(this).attr("href");
806
+ t_get_session_id=$.urlParam("remove_item",t_get_session_id);
807
+ for(i=0;i<cartpage_prod_json.length;i++){
808
+ if(t_get_session_id==Object.keys(cartpage_prod_json[i])){
809
+ if(cartpage_prod_json[i][t_get_session_id]["sku"]!==""){
810
+ t_prod_id=cartpage_prod_json[i][t_get_session_id]["sku"];
811
+ }else{
812
+ t_prod_id=cartpage_prod_json[i][t_get_session_id]["id"];
813
+ }
814
+ t_prod_name=cartpage_prod_json[i][t_get_session_id]["name"];
815
+ t_prod_price=cartpage_prod_json[i][t_get_session_id]["price"];
816
+ t_prod_cat=cartpage_prod_json[i][t_get_session_id]["category"];
817
+
818
+ }
819
+ }
820
+ ga("ec:addProduct", {
821
+ "id":t_prod_id,
822
+ "name": t_prod_name,
823
+ "category":t_prod_cat,
824
+ "price": t_prod_price,
825
+ "quantity": $(this).parents("tr").find(".product-quantity .qty").val()
826
+ });
827
+ ga("ec:setAction", "remove");
828
+ ga("send", "event", "Enhanced-Ecommerce", "click", "remove_from_cart_click",{"nonInteraction": 1});
829
+ });
830
+ ';
831
+ //check woocommerce version
832
+ $this->wc_version_compare($code);
833
+ }
834
+
835
+ /**
836
+ * Enhanced E-commerce tracking checkout step 1
837
+ *
838
+ * @access public
839
+ * @return void
840
+ */
841
+ public function checkout_step_1_tracking() {
842
+ if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
843
+ return;
844
  }
845
+ $code = $this->get_ordered_items();
846
 
847
  $code_step_1 = $code . 'ga("ec:setAction","checkout",{"step": 1});';
848
+ $code_step_1 .= 'ga("send", "event", "Enhanced-Ecommerce","load","checkout_step_1",{"nonInteraction": 1});';
849
+
850
+ //check woocommerce version and add code
851
+ $this->wc_version_compare($code_step_1);
852
+ }
 
853
 
854
+ /**
855
+ * Enhanced E-commerce tracking checkout step 2
856
+ *
857
+ * @access public
858
+ * @return void
859
+ */
860
+ public function checkout_step_2_tracking() {
861
+ if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
862
+ return;
863
+ }
864
+ $code = $this->get_ordered_items();
865
 
866
  $code_step_2 = $code . 'ga("ec:setAction","checkout",{"step": 2});';
867
+ $code_step_2 .= 'ga("send", "event", "Enhanced-Ecommerce","load","checkout_step_2",{"nonInteraction": 1});';
868
 
869
+ //if logged in and first name is filled - Guest Check out
870
  if (is_user_logged_in()) {
871
+ $step2_onFocus = 't_tracked_focus=0; if(t_tracked_focus===0){' . $code_step_2 . ' t_tracked_focus++;}';
872
+ } else {
873
+ //first name on focus call fire
874
+ $step2_onFocus = 't_tracked_focus=0; jQuery("input[name=billing_first_name]").on("focus",function(){ if(t_tracked_focus===0){' . $code_step_2 . ' t_tracked_focus++;}});';
 
875
  }
876
+
877
+ //check woocommerce version and add code
878
+ $this->wc_version_compare($step2_onFocus);
879
+ }
880
+
881
+ /**
882
+ * Enhanced E-commerce tracking checkout step 3
883
+ *
884
+ * @access public
885
+ * @return void
886
+ */
887
+ public function checkout_step_3_tracking() {
888
+ if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
889
+ return;
890
+ }
891
+ $code = $this->get_ordered_items();
892
+
893
+ //check if guest check out is enabled or not
894
  $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);
895
 
896
  $code_step_3 = $code . 'ga("ec:setAction","checkout",{"step": 3});';
897
+ $code_step_3 .= 'ga("send", "event", "Enhanced-Ecommerce","load", "checkout_step_3",{"nonInteraction": 1});';
898
 
899
+ $inline_js = 't_track_clk=0; jQuery(document).on("click","#place_order",function(e){ if(t_track_clk===0){';
900
  if ($step_2_on_proceed_to_pay) {
901
+ if (isset($code_step_2))
902
+ $inline_js .= $code_step_2;
903
  }
904
  $inline_js .= $code_step_3;
905
+ $inline_js .= "t_track_clk++; }});";
906
+
907
+ //check woocommerce version and add code
908
+ $this->wc_version_compare($inline_js);
 
 
909
  }
910
 
911
  /**
912
+ * Get oredered Items for check out page.
913
  *
914
  * @access public
915
  * @return void
916
  */
917
+ public function get_ordered_items() {
 
918
  global $woocommerce;
919
+ $code = "";
920
+ //get all items added into the cart
921
+ foreach ($woocommerce->cart->cart_contents as $item) {
922
+ $p = get_product($item["product_id"]);
923
 
924
+ $category = get_the_terms($item["product_id"], "product_cat");
925
+ $categories = "";
926
+ if ($category) {
927
+ foreach ($category as $term) {
928
+ $categories.=$term->name . ",";
929
+ }
930
+ }
931
+ //remove last comma(,) if multiple categories are there
932
+ $categories = rtrim($categories, ",");
933
 
934
+ $code .= 'ga("ec:addProduct", {"id": "' . esc_js($p->get_sku() ? $p->get_sku() : $p->id) . '",';
935
+ $code .= '"name": "' . esc_js($p->get_title()) . '",';
936
+ $code .= '"category": "' . esc_js($categories) . '",';
937
+ $code .= '"price": "' . esc_js($p->get_price()) . '",';
938
+ $code .= '"quantity": "' . esc_js($item["quantity"]) . '"});';
939
  }
940
+ return $code;
941
  }
942
 
943
+ /**
944
+ * Sending hits with event
945
+ *
946
+ * @access public
947
+ * @return void
948
+ */
949
+ /* public function default_pageview() {
950
+
951
+ global $woocommerce;
952
+ if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
953
+ return;
954
+ }
955
+ if (!is_order_received_page()) {
956
+ if (!$this->disable_tracking($this->ga_standard_tracking_enabled)) {
957
+ $inline_js = 'ga("send", "event", "Enhanced-Ecommerce", "pageview", "footer",{"nonInteraction": 1})';
958
+ } else {
959
+ $inline_js = 'ga("send", "event", "Enhanced-Ecommerce", "pageview", "footer",{"nonInteraction": 1});';
960
+ }
961
+ }
962
+
963
+ //check woocommerce version and add code
964
+ $this->wc_version_compare($inline_js);
965
+ } */
966
+
967
  /**
968
  * Check if tracking is disabled
969
  *
977
  }
978
  }
979
 
980
+ /**
981
+ * woocommerce version compare
982
+ *
983
+ * @access public
984
+ * @return void
985
+ */
986
+ function wc_version_compare($codeSnippet) {
987
+ global $woocommerce;
988
+ if (version_compare($woocommerce->version, "2.1", ">=")) {
989
+ wc_enqueue_js($codeSnippet);
990
+ } else {
991
+ $woocommerce->add_inline_js($codeSnippet);
992
+ }
993
+ }
994
+
995
  /**
996
  * check UA is enabled or not
997
  *
998
  * @access public
999
  */
1000
+ function admin_check_UA_enabled() {
1001
  echo '<script>
1002
  jQuery("#woocommerce_enhanced_ecommerce_google_analytics_ga_standard_tracking_enabled").change(function(){
1003
  t_ga_chk=jQuery(this).is(":checked");
1013
  });
1014
  </script>';
1015
  }
1016
+
1017
+ /**
1018
+ * Adding Nearest Javascript Ref. code
1019
+ *
1020
+ * @access public
1021
+ * @return void
1022
+ */
1023
+ function add_nearest_jQuery() {
1024
+ $nearest_jQ = "(function($, d) {
1025
+ $.fn.nearest = function(selector) {
1026
+ var self, nearest, el, s, p,
1027
+ hasQsa = d.querySelectorAll;
1028
+
1029
+ function update(el) {
1030
+ nearest = nearest ? nearest.add(el) : $(el);
1031
+ }
1032
+
1033
+ this.each(function() {
1034
+ self = this;
1035
+
1036
+ $.each(selector.split(', '), function() {
1037
+ s = $.trim(this);
1038
+
1039
+ if (!s.indexOf('#')) {
1040
+ // selector starts with an ID
1041
+ update((hasQsa ? d.querySelectorAll(s) : $(s)));
1042
+ } else {
1043
+ // is a class or tag selector
1044
+ // so need to traverse
1045
+ p = self . parentNode;
1046
+ while (p) {
1047
+ el = hasQsa ? p.querySelectorAll(s) : $(p) . find(s);
1048
+ if (el . length) {
1049
+ update(el);
1050
+ break;
1051
+ }
1052
+ p = p . parentNode;
1053
+ }
1054
+ }
1055
+ });
1056
+
1057
+ });
1058
+
1059
+ return nearest || $();
1060
+ };
1061
+ }(jQuery, document));
1062
+ ";
1063
+
1064
+ $this->wc_version_compare($nearest_jQ);
1065
+ }
1066
+
1067
  /**
1068
  * Sending email to remote server
1069
  *
1070
  * @access public
1071
  * @return void
1072
  */
1073
+ public function send_email_to_tatvic($email, $domain_name) {
1074
  //set POST variables
1075
  $url = "http://dev.tatvic.com/leadgen/woocommerce-plugin/store_email/";
1076
  $fields = array(
1077
  "email" => urlencode($email),
1078
+ "domain_name" => urlencode($domain_name)
1079
  );
1080
  wp_remote_post($url, array(
1081
  "method" => "POST",
1089
  }
1090
 
1091
  }
1092
+
1093
  ?>
includes/settings.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ screen=yes
2
+ email=test@test.com
3
+ ga_id=UA-XXXXXX-1
4
+ domain=auto
5
+ setcurrency=INR
6
+ UA_code=yes
7
+ display_feature=yes
8
+ EE_code=yes
9
+ enabled for Guest Users=yes
10
+ impression_threshold=6
11
+
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.9.1
11
- Version: 1.0.9.1
12
  License: GPLv3
13
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
14
 
@@ -25,9 +25,13 @@ Provides integration between Google Analytics Enhanced Ecommerce and WooCommerce
25
  * Product Performance Report
26
  * Sales Performance Report
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
@@ -59,6 +63,10 @@ ga('require','ec','ec.js');
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
64
  2. Using an FTP program, or your hosting control panel, upload the unzipped plugin folder to your WordPress installation’s wp-content/plugins/ directory
@@ -67,8 +75,8 @@ ga('require','ec','ec.js');
67
 
68
  == Screenshots ==
69
  1. 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. Also, add meaningful labels for your checkout steps. We recommend you to label as, Step 1 : Checkout View; Step 2 : Login; Step 3 : Proceed to payment;
70
- 2. Next, you need to activate your plugin from the Settings page for the plugin accessed under, WooCommerce > Settings > Integration > Enhanced Ecommerce Google Analytics
71
- 3. For tracking the Guest users, you need to check the box. Unchecking the box might cause an uneven funnel reported to Google Analytics.
72
 
73
 
74
  == Frequently Asked Questions ==
@@ -98,3 +106,8 @@ Starting the WooCommerce 2.1 release there are no conflicts. However for earlier
98
 
99
  = 1.0.9.1 - 09/11/2014 =
100
  * Fixed- Minor bug on order page
 
 
 
 
 
7
  Author: Tatvic
8
  Requires at least: 3.6
9
  Tested up to: 3.9.2
10
+ Stable tag: 1.0.10
11
+ Version: 1.0.10
12
  License: GPLv3
13
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
14
 
25
  * Product Performance Report
26
  * Sales Performance Report
27
  3. Supports Guest checkout functionality
28
+ 4. Supports Display Advertising Feature
29
+ 5. Captures Product Impressions, Add to Cart & Product Clicks events on category page
30
+ 6. Captures Product Impressions, Add to Cart & Product Clicks events on product page
31
+ 7. Captures Product Impressions, Add to Cart & Product Clicks events on featured Product Section on Homepage
32
+ 8. Captures Product Impressions, Add to Cart & Product Clicks events on Recent Product Section on Homepage
33
+ 9. Captures Product Impressions, Add to Cart & Product Clicks events on Related Product Section on Productpage
34
+ 10. Set your local currency
35
 
36
  = Things to keep in mind before enabling the Enhanced E-commerce plugin =
37
  * 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
63
 
64
  * 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.
65
 
66
+ *All the product sections on homepage other than feature product will be fired as Recent Product and will be available in product list performance report.
67
+
68
+ *All the product sections on product page will be fired as Related Product and will be available in product list performance report.
69
+
70
  == Installation ==
71
  1. Download the plugin file to your computer and unzip it
72
  2. Using an FTP program, or your hosting control panel, upload the unzipped plugin folder to your WordPress installation’s wp-content/plugins/ directory
75
 
76
  == Screenshots ==
77
  1. 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. Also, add meaningful labels for your checkout steps. We recommend you to label as, Step 1 : Checkout View; Step 2 : Login; Step 3 : Proceed to payment;
78
+ 2. Next, you need to activate your plugin from the Settings page by clicking the checkbox “Add Enhanced Ecommerce Tracking Code". You can access the same from: WooCommerce > Settings > Integration > Enhanced Ecommerce Google Analytics.
79
+ 3. To Track Guest Users, Check the box – Add Code to Track the Login Steps of Guest Users. If you have a Guest Check out & if it’s Unchecked, then it might cause an uneven funnel reporting in Google Analytics.
80
 
81
 
82
  == Frequently Asked Questions ==
106
 
107
  = 1.0.9.1 - 09/11/2014 =
108
  * Fixed- Minor bug on order page
109
+
110
+ = 1.0.10 - 09/26/2014 =
111
+ * Allows user to set local currency
112
+ * Captures Impressions, Product Clicks and Add to Cart on Featured Product section and Recent Product section on Homepage
113
+ * Captures Impressions, Product Clicks and Add to Cart on Related Product section on Product Page
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.9.1
25
  */
26
 
27
  // Add the integration to WooCommerce
@@ -31,9 +31,9 @@ function wc_enhanced_ecommerce_google_analytics_add_integration($integrations) {
31
  if (is_object($woocommerce)) {
32
  include_once( 'includes/class-wc-enhanced-ecommerce-google-analytics-integration.php' );
33
  $integrations[] = 'WC_Enhanced_Ecommerce_Google_Analytics';
34
- }
35
 
36
  return $integrations;
37
- }
38
 
39
  add_filter('woocommerce_integrations', 'wc_enhanced_ecommerce_google_analytics_add_integration', 10);
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.10
25
  */
26
 
27
  // Add the integration to WooCommerce
31
  if (is_object($woocommerce)) {
32
  include_once( 'includes/class-wc-enhanced-ecommerce-google-analytics-integration.php' );
33
  $integrations[] = 'WC_Enhanced_Ecommerce_Google_Analytics';
34
+ }
35
 
36
  return $integrations;
37
+ }
38
 
39
  add_filter('woocommerce_integrations', 'wc_enhanced_ecommerce_google_analytics_add_integration', 10);