WooCommerce Google Analytics Integration - Version 1.5.0

Version Description

  • 2020-12-17 =
  • Add - Option to use Global Site Tag and the gtag.js library (for Universal Analytics or Google Analytics 4).
  • Add - Several new values added to the Tracker data.
  • Add - Developer ID for gtag.js and analytics.js.
  • Tweak - Bump minimum-supported WooCommerce version to 3.2.
  • Tweak - Remove deprecated jQuery .click().
  • Fix - Settings link in plugins table row points directly to plugin settings.
  • Fix - Issue with multiple consecutive "Remove from Cart" events sent from the mini cart.
Download this release

Release Info

Developer automattic
Plugin Icon 128x128 WooCommerce Google Analytics Integration
Version 1.5.0
Comparing to
See all releases

Code changes from version 1.4.25 to 1.5.0

assets/js/admin-enhanced-settings.js DELETED
@@ -1,35 +0,0 @@
1
- jQuery(document).ready( function($) {
2
-
3
- var enhancedSettingParentRow = $( '.enhanced-setting' ).parent().parent().parent().parent();
4
- var enhancedToggle = $( '#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled' ).parent().parent().parent().parent();
5
-
6
- if ( false === $( '#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled' ).is( ':checked' ) ) {
7
- enhancedSettingParentRow.hide();
8
- }
9
-
10
- if ( false === $( '#woocommerce_google_analytics_ga_use_universal_analytics' ).is( ':checked' ) ) {
11
- enhancedSettingParentRow.hide();
12
- enhancedToggle.hide();
13
- }
14
-
15
- $( '#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled' ).on( 'click', function() {
16
- if ( false === $( '#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled' ).is( ':checked' ) ) {
17
- enhancedSettingParentRow.hide();
18
- } else {
19
- enhancedSettingParentRow.show();
20
- }
21
- } );
22
-
23
- $( '#woocommerce_google_analytics_ga_use_universal_analytics' ).on( 'click', function() {
24
- if ( false === $( '#woocommerce_google_analytics_ga_use_universal_analytics' ).is( ':checked' ) ) {
25
- enhancedSettingParentRow.hide();
26
- enhancedToggle.hide();
27
- } else {
28
- if ( true === $( '#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled' ).is( ':checked' ) ) {
29
- enhancedSettingParentRow.show();
30
- }
31
- enhancedToggle.show();
32
- }
33
- } );
34
-
35
- } );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
assets/js/admin-enhanced-settings.min.js DELETED
@@ -1 +0,0 @@
1
- jQuery(document).ready(function(e){var c=e(".enhanced-setting").parent().parent().parent().parent(),a=e("#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled").parent().parent().parent().parent();!1===e("#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled").is(":checked")&&c.hide(),!1===e("#woocommerce_google_analytics_ga_use_universal_analytics").is(":checked")&&(c.hide(),a.hide()),e("#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled").on("click",function(){!1===e("#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled").is(":checked")?c.hide():c.show()}),e("#woocommerce_google_analytics_ga_use_universal_analytics").on("click",function(){!1===e("#woocommerce_google_analytics_ga_use_universal_analytics").is(":checked")?(c.hide(),a.hide()):(!0===e("#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled").is(":checked")&&c.show(),a.show())})});
 
assets/js/admin-ga-settings.js ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(document).ready( function($) {
2
+
3
+ var ecCheckbox = $( '#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled' );
4
+ var uaCheckbox = $( '#woocommerce_google_analytics_ga_use_universal_analytics' );
5
+ var gtagCheckbox = $( '#woocommerce_google_analytics_ga_gtag_enabled' );
6
+
7
+ updateToggles();
8
+
9
+ ecCheckbox.change(updateToggles);
10
+ uaCheckbox.change(updateToggles);
11
+ gtagCheckbox.change(updateToggles);
12
+
13
+ function updateToggles() {
14
+ var isEnhancedEcommerce = ecCheckbox.is( ':checked' );
15
+ var isUniversalAnalytics = uaCheckbox.is( ':checked' );
16
+ var isGtag = gtagCheckbox.is( ':checked' );
17
+
18
+ // Legacy: gtag NO
19
+ toggleCheckboxRow( $( '.legacy-setting' ), ! isGtag );
20
+
21
+ // Enhanced settings: Enhanced YES + universal YES or gtag YES
22
+ toggleCheckboxRow( $( '.enhanced-setting' ), isEnhancedEcommerce && ( isUniversalAnalytics || isGtag ) );
23
+
24
+ // Enhanced toggle: universal YES or gtag YES
25
+ toggleCheckboxRow( ecCheckbox, isUniversalAnalytics || isGtag );
26
+
27
+ // Universal toggle: gtag NO
28
+ toggleCheckboxRow( uaCheckbox, ! isGtag );
29
+
30
+ }
31
+
32
+ function toggleCheckboxRow ( checkbox, isVisible ) {
33
+ if ( isVisible ) {
34
+ checkbox.closest('tr').show();
35
+ } else {
36
+ checkbox.closest('tr').hide();
37
+ }
38
+ }
39
+ } );
40
+
41
+
assets/js/admin-ga-settings.min.js ADDED
@@ -0,0 +1 @@
 
1
+ jQuery(document).ready(function(n){var o=n("#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled"),g=n("#woocommerce_google_analytics_ga_use_universal_analytics"),t=n("#woocommerce_google_analytics_ga_gtag_enabled");function e(){var e=o.is(":checked"),c=g.is(":checked"),a=t.is(":checked");_(n(".legacy-setting"),!a),_(n(".enhanced-setting"),e&&(c||a)),_(o,c||a),_(g,!a)}function _(e,c){c?e.closest("tr").show():e.closest("tr").hide()}e(),o.change(e),g.change(e),t.change(e)});
changelog.txt CHANGED
@@ -1,5 +1,14 @@
1
  *** Changelog ***
2
 
 
 
 
 
 
 
 
 
 
3
  = 1.4.25 - 2020-11-25 =
4
  * Tweak - WC 4.7 compatibility.
5
  * Tweak - WordPress 5.6 compatibility.
1
  *** Changelog ***
2
 
3
+ = 1.5.0 - 2020-12-17 =
4
+ * Add - Option to use Global Site Tag and the gtag.js library (for Universal Analytics or Google Analytics 4).
5
+ * Add - Several new values added to the Tracker data.
6
+ * Add - Developer ID for gtag.js and analytics.js.
7
+ * Tweak - Bump minimum-supported WooCommerce version to 3.2.
8
+ * Tweak - Remove deprecated jQuery .click().
9
+ * Fix - Settings link in plugins table row points directly to plugin settings.
10
+ * Fix - Issue with multiple consecutive "Remove from Cart" events sent from the mini cart.
11
+
12
  = 1.4.25 - 2020-11-25 =
13
  * Tweak - WC 4.7 compatibility.
14
  * Tweak - WordPress 5.6 compatibility.
includes/class-wc-abstract-google-analytics-js.php ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'ABSPATH' ) ) {
3
+ exit;
4
+ }
5
+
6
+ /**
7
+ * WC_Abstract_Google_Analytics_JS class
8
+ *
9
+ * Abstract JS for recording Google Analytics/Gtag info
10
+ */
11
+ abstract class WC_Abstract_Google_Analytics_JS {
12
+
13
+ /** @var WC_Abstract_Google_Analytics_JS $instance Class Instance */
14
+ protected static $instance;
15
+
16
+ /** @var array $options Inherited Analytics options */
17
+ protected static $options;
18
+
19
+ /** @var string Developer ID */
20
+ const DEVELOPER_ID = 'dOGY3NW';
21
+
22
+ /**
23
+ * Get the class instance
24
+ *
25
+ * @param array $options Options
26
+ * @return WC_Abstract_Google_Analytics_JS
27
+ */
28
+ abstract public static function get_instance( $options = array() );
29
+
30
+ /**
31
+ * Return one of our options
32
+ *
33
+ * @param string $option Key/name for the option
34
+ * @return string Value of the option
35
+ */
36
+ protected static function get( $option ) {
37
+ return self::$options[$option];
38
+ }
39
+
40
+ /**
41
+ * Returns the tracker variable this integration should use
42
+ *
43
+ * @return string
44
+ */
45
+ abstract public static function tracker_var();
46
+
47
+ /**
48
+ * Generic GA / header snippet for opt out
49
+ */
50
+ public static function header() {
51
+ return "<script type='text/javascript'>
52
+ var gaProperty = '" . esc_js( self::get( 'ga_id' ) ) . "';
53
+ var disableStr = 'ga-disable-' + gaProperty;
54
+ if ( document.cookie.indexOf( disableStr + '=true' ) > -1 ) {
55
+ window[disableStr] = true;
56
+ }
57
+ function gaOptout() {
58
+ document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
59
+ window[disableStr] = true;
60
+ }
61
+ </script>";
62
+ }
63
+
64
+ /**
65
+ * Enqueues JavaScript to build the addImpression object
66
+ *
67
+ * @param WC_Product $product
68
+ * @param int $position
69
+ */
70
+ abstract public static function listing_impression( $product, $position );
71
+
72
+ /**
73
+ * Enqueues JavaScript to build an addProduct and click object
74
+ *
75
+ * @param WC_Product $product
76
+ * @param int $position
77
+ */
78
+ abstract public static function listing_click( $product, $position ) ;
79
+
80
+ /**
81
+ * Loads the correct Google Gtag code (classic or universal)
82
+ *
83
+ * @param boolean|WC_Order $order Classic analytics needs order data to set the currency correctly
84
+ * @return string Analytics loading code
85
+ */
86
+ abstract public static function load_analytics( $order = false );
87
+
88
+ /**
89
+ * Generate code used to pass transaction data to Google Analytics.
90
+ *
91
+ * @param WC_Order $order WC_Order Object
92
+ * @return string Add Transaction code
93
+ */
94
+ public function add_transaction( $order ) {
95
+ if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) || 'yes' === self::get( 'ga_gtag_enabled' ) ) {
96
+ return static::add_transaction_enhanced( $order );
97
+ } else {
98
+ return self::add_transaction_universal( $order );
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Generate Enhanced eCommerce transaction tracking code
104
+ *
105
+ * @param WC_Order $order WC_Order object
106
+ * @return string Add Transaction Code
107
+ */
108
+ abstract protected function add_transaction_enhanced( $order );
109
+
110
+ /**
111
+ * Generate Universal Analytics add item tracking code
112
+ *
113
+ * @param WC_Order $order WC_Order Object
114
+ * @param WC_Order_Item $item The item to add to a transaction/order
115
+ * @return string
116
+ */
117
+ protected function add_item_universal( $order, $item ) {
118
+ $_product = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_product_from_item( $item ) : $item->get_product();
119
+
120
+ $code = "ga('ecommerce:addItem', {";
121
+ $code .= "'id': '" . esc_js( $order->get_order_number() ) . "',";
122
+ $code .= "'name': '" . esc_js( $item['name'] ) . "',";
123
+ $code .= "'sku': '" . esc_js( $_product->get_sku() ? $_product->get_sku() : $_product->get_id() ) . "',";
124
+ $code .= "'category': " . self::product_get_category_line( $_product );
125
+ $code .= "'price': '" . esc_js( $order->get_item_total( $item ) ) . "',";
126
+ $code .= "'quantity': '" . esc_js( $item['qty'] ) . "'";
127
+ $code .= "});";
128
+
129
+ return $code;
130
+ }
131
+
132
+ /**
133
+ * Generate Universal Analytics transaction tracking code
134
+ *
135
+ * @param WC_Order $order WC_Order object
136
+ * @return string Add Transaction tracking code
137
+ */
138
+ protected function add_transaction_universal( $order ) {
139
+ $code = "ga('ecommerce:addTransaction', {
140
+ 'id': '" . esc_js( $order->get_order_number() ) . "', // Transaction ID. Required
141
+ 'affiliation': '" . esc_js( get_bloginfo( 'name' ) ) . "', // Affiliation or store name
142
+ 'revenue': '" . esc_js( $order->get_total() ) . "', // Grand Total
143
+ 'shipping': '" . esc_js( $order->get_total_shipping() ) . "', // Shipping
144
+ 'tax': '" . esc_js( $order->get_total_tax() ) . "', // Tax
145
+ 'currency': '" . esc_js( version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_order_currency() : $order->get_currency() ) . "' // Currency
146
+ });";
147
+
148
+ // Order items
149
+ if ( $order->get_items() ) {
150
+ foreach ( $order->get_items() as $item ) {
151
+ $code .= self::add_item_universal( $order, $item );
152
+ }
153
+ }
154
+
155
+ $code .= "ga('ecommerce:send');";
156
+ return $code;
157
+ }
158
+
159
+ /**
160
+ * Returns a 'category' JSON line based on $product
161
+ *
162
+ * @param WC_Product $_product Product to pull info for
163
+ * @return string Line of JSON
164
+ */
165
+ protected static function product_get_category_line( $_product ) {
166
+ $out = array();
167
+ $variation_data = version_compare( WC_VERSION, '3.0', '<' ) ? $_product->variation_data : ( $_product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $_product->get_id() ) : '' );
168
+ $categories = get_the_terms( $_product->get_id(), 'product_cat' );
169
+
170
+ if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
171
+ $parent_product = wc_get_product( version_compare( WC_VERSION, '3.0', '<' ) ? $_product->parent->id : $_product->get_parent_id() );
172
+ $categories = get_the_terms( $parent_product->get_id(), 'product_cat' );
173
+ }
174
+
175
+ if ( $categories ) {
176
+ foreach ( $categories as $category ) {
177
+ $out[] = $category->name;
178
+ }
179
+ }
180
+
181
+ return "'" . esc_js( join( "/", $out ) ) . "',";
182
+ }
183
+
184
+ /**
185
+ * Returns a 'variant' JSON line based on $product
186
+ *
187
+ * @param WC_Product $_product Product to pull info for
188
+ * @return string Line of JSON
189
+ */
190
+ protected static function product_get_variant_line( $_product ) {
191
+ $out = '';
192
+ $variation_data = version_compare( WC_VERSION, '3.0', '<' ) ? $_product->variation_data : ( $_product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $_product->get_id() ) : '' );
193
+
194
+ if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
195
+ $out = "'" . esc_js( wc_get_formatted_variation( $variation_data, true ) ) . "',";
196
+ }
197
+
198
+ return $out;
199
+ }
200
+
201
+ /**
202
+ * Echo JavaScript to track an enhanced ecommerce remove from cart action
203
+ */
204
+ abstract public function remove_from_cart();
205
+
206
+ /**
207
+ * Enqueue JavaScript to track a product detail view
208
+ *
209
+ * @param WC_Product $product
210
+ */
211
+ abstract public function product_detail( $product );
212
+
213
+ /**
214
+ * Enqueue JS to track when the checkout process is started
215
+ *
216
+ * @param array $cart items/contents of the cart
217
+ */
218
+ abstract public function checkout_process( $cart );
219
+
220
+ /**
221
+ * Enqueue JavaScript for Add to cart tracking
222
+ *
223
+ * @param array $parameters associative array of _trackEvent parameters
224
+ * @param string $selector jQuery selector for binding click event
225
+ */
226
+ abstract public function event_tracking_code( $parameters, $selector );
227
+ }
includes/class-wc-google-analytics-info-banner.php CHANGED
@@ -10,7 +10,7 @@ if ( ! defined( 'ABSPATH' ) ) {
10
  */
11
  class WC_Google_Analytics_Info_Banner {
12
 
13
- /** @var object Class Instance */
14
  private static $instance;
15
 
16
  /** @var boolean If the banner has been dismissed */
@@ -18,6 +18,10 @@ class WC_Google_Analytics_Info_Banner {
18
 
19
  /**
20
  * Get the class instance
 
 
 
 
21
  */
22
  public static function get_instance( $dismissed = false, $ga_id = '' ) {
23
  return null === self::$instance ? ( self::$instance = new self( $dismissed, $ga_id ) ) : self::$instance;
@@ -25,6 +29,9 @@ class WC_Google_Analytics_Info_Banner {
25
 
26
  /**
27
  * Constructor
 
 
 
28
  */
29
  public function __construct( $dismissed = false, $ga_id = '' ) {
30
  $this->is_dismissed = (bool) $dismissed;
@@ -42,7 +49,7 @@ class WC_Google_Analytics_Info_Banner {
42
  }
43
 
44
  /**
45
- * Displays a info banner on WooCommerce settings pages
46
  */
47
  public function banner() {
48
  $screen = get_current_screen();
10
  */
11
  class WC_Google_Analytics_Info_Banner {
12
 
13
+ /** @var WC_Google_Analytics_Info_Banner Class Instance */
14
  private static $instance;
15
 
16
  /** @var boolean If the banner has been dismissed */
18
 
19
  /**
20
  * Get the class instance
21
+ *
22
+ * @param boolean $dismissed
23
+ * @param string $ga_id
24
+ * @return WC_Google_Analytics_Info_Banner
25
  */
26
  public static function get_instance( $dismissed = false, $ga_id = '' ) {
27
  return null === self::$instance ? ( self::$instance = new self( $dismissed, $ga_id ) ) : self::$instance;
29
 
30
  /**
31
  * Constructor
32
+ *
33
+ * @param boolean $dismissed
34
+ * @param string $ga_id
35
  */
36
  public function __construct( $dismissed = false, $ga_id = '' ) {
37
  $this->is_dismissed = (bool) $dismissed;
49
  }
50
 
51
  /**
52
+ * Displays an info banner on WooCommerce settings pages
53
  */
54
  public function banner() {
55
  $screen = get_current_screen();
includes/class-wc-google-analytics-js.php CHANGED
@@ -8,19 +8,13 @@ if ( ! defined( 'ABSPATH' ) ) {
8
  *
9
  * JS for recording Google Analytics info
10
  */
11
- class WC_Google_Analytics_JS {
12
-
13
- /** @var object Class Instance */
14
- private static $instance;
15
-
16
- /** @var array Inherited Analytics options */
17
- private static $options;
18
 
19
  /**
20
  * Get the class instance
21
- * @param array $options
22
  *
23
- * @return WC_Google_Analytics_JS
 
24
  */
25
  public static function get_instance( $options = array() ) {
26
  return null === self::$instance ? ( self::$instance = new self( $options ) ) : self::$instance;
@@ -30,49 +24,26 @@ class WC_Google_Analytics_JS {
30
  * Constructor
31
  * Takes our options from the parent class so we can later use them in the JS snippets
32
  *
33
- * @param array $options
34
  */
35
  public function __construct( $options = array() ) {
36
  self::$options = $options;
37
  }
38
 
39
- /**
40
- * Return one of our options
41
- * @param string $option Key/name for the option
42
- * @return string Value of the option
43
- */
44
- public static function get( $option ) {
45
- return self::$options[$option];
46
- }
47
-
48
  /**
49
  * Returns the tracker variable this integration should use
 
 
50
  */
51
  public static function tracker_var() {
52
  return apply_filters( 'woocommerce_ga_tracker_variable', 'ga' );
53
  }
54
 
55
- /**
56
- * Generic GA / header snippet for opt out
57
- */
58
- public static function header() {
59
- return "<script type='text/javascript'>
60
- var gaProperty = '" . esc_js( self::get( 'ga_id' ) ) . "';
61
- var disableStr = 'ga-disable-' + gaProperty;
62
- if ( document.cookie.indexOf( disableStr + '=true' ) > -1 ) {
63
- window[disableStr] = true;
64
- }
65
- function gaOptout() {
66
- document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
67
- window[disableStr] = true;
68
- }
69
- </script>";
70
- }
71
-
72
  /**
73
  * Loads the correct Google Analytics code (classic or universal)
74
- * @param boolean $order Classic analytics needs order data to set the currency correctly
75
- * @return string Analytics loading code
 
76
  */
77
  public static function load_analytics( $order = false ) {
78
  $logged_in = is_user_logged_in() ? 'yes' : 'no';
@@ -88,11 +59,13 @@ class WC_Google_Analytics_JS {
88
 
89
  /**
90
  * Loads ga.js analytics tracking code
91
- * @param string $logged_in 'yes' if the user is logged in, no if not (this is a string so we can pass it to GA)
92
- * @param boolean|object $order We don't always need to load order data for currency, so we omit that if false is set, otherwise this is an order object
93
- * @return string Classic Analytics loading code
 
 
94
  */
95
- public static function load_analytics_classic( $logged_in, $order = false ) {
96
  $anonymize_enabled = '';
97
  if ( 'yes' === self::get( 'ga_anonymize_enabled' ) ) {
98
  $anonymize_enabled = "['_gat._anonymizeIp'],";
@@ -127,11 +100,16 @@ class WC_Google_Analytics_JS {
127
 
128
  $code .= ");";
129
 
130
- return $code;
 
 
131
  }
132
 
133
  /**
134
- * Builds the addImpression object
 
 
 
135
  */
136
  public static function listing_impression( $product, $position ) {
137
  if ( isset( $_GET['s'] ) ) {
@@ -152,7 +130,10 @@ class WC_Google_Analytics_JS {
152
  }
153
 
154
  /**
155
- * Builds an addProduct and click object
 
 
 
156
  */
157
  public static function listing_click( $product, $position ) {
158
  if ( isset( $_GET['s'] ) ) {
@@ -162,7 +143,7 @@ class WC_Google_Analytics_JS {
162
  }
163
 
164
  wc_enqueue_js( "
165
- $( '.products .post-" . esc_js( $product->get_id() ) . " a' ).click( function() {
166
  if ( true === $(this).hasClass( 'add_to_cart_button' ) ) {
167
  return;
168
  }
@@ -181,7 +162,6 @@ class WC_Google_Analytics_JS {
181
  }
182
 
183
  /**
184
- * Asynchronously loads the classic Google Analytics code, and does so after all of our properties are loaded
185
  * Loads in the footer
186
  * @see wp_footer
187
  */
@@ -200,7 +180,7 @@ class WC_Google_Analytics_JS {
200
  }
201
 
202
  /**
203
- * Sends the pageview last thing (needed for things like addImpression)
204
  */
205
  public static function universal_analytics_footer() {
206
  if ( apply_filters( 'wc_google_analytics_send_pageview', true ) ) {
@@ -211,6 +191,8 @@ class WC_Google_Analytics_JS {
211
  /**
212
  * This was created to fix public facing api typo in a filter name
213
  * and inform about the deprecation.
 
 
214
  */
215
  public static function universal_analytics_footer_filter( $send_pageview ) {
216
  return apply_filters_deprecated( 'wc_goole_analytics_send_pageview', array( $send_pageview ), '1.4.20', 'wc_google_analytics_send_pageview' );
@@ -218,10 +200,11 @@ class WC_Google_Analytics_JS {
218
 
219
  /**
220
  * Loads the universal analytics code
 
221
  * @param string $logged_in 'yes' if the user is logged in, no if not (this is a string so we can pass it to GA)
222
- * @return string Universal Analytics Code
223
  */
224
- public static function load_analytics_universal( $logged_in ) {
225
 
226
  $domainname = self::get( 'ga_set_domain_name' );
227
 
@@ -260,13 +243,18 @@ class WC_Google_Analytics_JS {
260
  })(window,document,'script', '{$src}','" . self::tracker_var() . "');";
261
 
262
  $ga_id = self::get( 'ga_id' );
263
- $ga_snippet_create = self::tracker_var() . "( 'create', '" . esc_js( $ga_id ) . "', '" . $set_domain_name . "' );";
264
 
265
- $ga_snippet_require =
266
- $support_display_advertising .
267
- $support_enhanced_link_attribution .
268
- $anonymize_enabled .
269
- $track_404_enabled . "
 
 
 
 
 
270
  " . self::tracker_var() . "( 'set', 'dimension1', '" . $logged_in . "' );\n";
271
 
272
  if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) ) {
@@ -275,21 +263,22 @@ class WC_Google_Analytics_JS {
275
  $ga_snippet_require .= "" . self::tracker_var() . "( 'require', 'ecommerce', 'ecommerce.js');";
276
  }
277
 
278
- $ga_snippet_head = apply_filters( 'woocommerce_ga_snippet_head' , $ga_snippet_head );
279
- $ga_snippet_create = apply_filters( 'woocommerce_ga_snippet_create' , $ga_snippet_create, $ga_id );
280
- $ga_snippet_require = apply_filters( 'woocommerce_ga_snippet_require' , $ga_snippet_require );
 
281
 
282
- $code = $ga_snippet_head . $ga_snippet_create . $ga_snippet_require;
283
  $code = apply_filters( 'woocommerce_ga_snippet_output', $code );
284
 
285
  return $code;
286
  }
287
 
288
  /**
289
- * Used to pass transaction data to Google Analytics.
290
  *
291
- * @param object $order WC_Order Object.
292
- * @return string Add Transaction code.
293
  */
294
  public function add_transaction( $order ) {
295
  if ( 'yes' === self::get( 'ga_use_universal_analytics' ) ) {
@@ -322,10 +311,10 @@ class WC_Google_Analytics_JS {
322
 
323
  /**
324
  * ga.js (classic) transaction tracking
325
- * @param object $order WC_Order Object
326
- * @return string Add Transaction Code
327
  */
328
- function add_transaction_classic( $order ) {
329
  $code = "_gaq.push(['_addTrans',
330
  '" . esc_js( $order->get_order_number() ) . "', // order ID - required
331
  '" . esc_js( get_bloginfo( 'name' ) ) . "', // affiliation or store name
@@ -349,35 +338,12 @@ class WC_Google_Analytics_JS {
349
  }
350
 
351
  /**
352
- * Universal Analytics transaction tracking
353
- * @param object $order WC_Order object
354
- * @return string Add Transaction Code
355
- */
356
- function add_transaction_universal( $order ) {
357
- $code = "" . self::tracker_var() . "('ecommerce:addTransaction', {
358
- 'id': '" . esc_js( $order->get_order_number() ) . "', // Transaction ID. Required
359
- 'affiliation': '" . esc_js( get_bloginfo( 'name' ) ) . "', // Affiliation or store name
360
- 'revenue': '" . esc_js( $order->get_total() ) . "', // Grand Total
361
- 'shipping': '" . esc_js( $order->get_total_shipping() ) . "', // Shipping
362
- 'tax': '" . esc_js( $order->get_total_tax() ) . "', // Tax
363
- 'currency': '" . esc_js( version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_order_currency() : $order->get_currency() ) . "' // Currency
364
- });";
365
-
366
- // Order items
367
- if ( $order->get_items() ) {
368
- foreach ( $order->get_items() as $item ) {
369
- $code .= self::add_item_universal( $order, $item );
370
- }
371
- }
372
-
373
- $code .= "" . self::tracker_var() . "('ecommerce:send');";
374
- return $code;
375
- }
376
-
377
- /**
378
- * Enhanced Ecommerce Universal Analytics transaction tracking
379
  */
380
- function add_transaction_enhanced( $order ) {
381
  $code = "" . self::tracker_var() . "( 'set', '&cu', '" . esc_js( version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_order_currency() : $order->get_currency() ) . "' );";
382
 
383
  // Order items
@@ -400,10 +366,12 @@ class WC_Google_Analytics_JS {
400
 
401
  /**
402
  * Add Item (Classic)
403
- * @param object $order WC_Order Object
404
- * @param array $item The item to add to a transaction/order
 
 
405
  */
406
- function add_item_classic( $order, $item ) {
407
  $_product = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_product_from_item( $item ) : $item->get_product();
408
 
409
  $code = "_gaq.push(['_addItem',";
@@ -418,32 +386,14 @@ class WC_Google_Analytics_JS {
418
  return $code;
419
  }
420
 
421
- /**
422
- * Add Item (Universal)
423
- * @param object $order WC_Order Object
424
- * @param array $item The item to add to a transaction/order
425
- */
426
- function add_item_universal( $order, $item ) {
427
- $_product = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_product_from_item( $item ) : $item->get_product();
428
-
429
- $code = "" . self::tracker_var() . "('ecommerce:addItem', {";
430
- $code .= "'id': '" . esc_js( $order->get_order_number() ) . "',";
431
- $code .= "'name': '" . esc_js( $item['name'] ) . "',";
432
- $code .= "'sku': '" . esc_js( $_product->get_sku() ? $_product->get_sku() : $_product->get_id() ) . "',";
433
- $code .= "'category': " . self::product_get_category_line( $_product );
434
- $code .= "'price': '" . esc_js( $order->get_item_total( $item ) ) . "',";
435
- $code .= "'quantity': '" . esc_js( $item['qty'] ) . "'";
436
- $code .= "});";
437
-
438
- return $code;
439
- }
440
-
441
  /**
442
  * Add Item (Enhanced, Universal)
443
- * @param object $order WC_Order Object
444
- * @param array $item The item to add to a transaction/order
 
 
445
  */
446
- function add_item_enhanced( $order, $item ) {
447
  $_product = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_product_from_item( $item ) : $item->get_product();
448
  $variant = self::product_get_variant_line( $_product );
449
 
@@ -464,55 +414,13 @@ class WC_Google_Analytics_JS {
464
  }
465
 
466
  /**
467
- * Returns a 'category' JSON line based on $product
468
- * @param object $product Product to pull info for
469
- * @return string Line of JSON
470
  */
471
- private static function product_get_category_line( $_product ) {
472
-
473
- $out = array();
474
- $variation_data = version_compare( WC_VERSION, '3.0', '<' ) ? $_product->variation_data : ( $_product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $_product->get_id() ) : '' );
475
- $categories = get_the_terms( $_product->get_id(), 'product_cat' );
476
-
477
- if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
478
- $parent_product = wc_get_product( version_compare( WC_VERSION, '3.0', '<' ) ? $_product->parent->id : $_product->get_parent_id() );
479
- $categories = get_the_terms( $parent_product->get_id(), 'product_cat' );
480
- }
481
-
482
- if ( $categories ) {
483
- foreach ( $categories as $category ) {
484
- $out[] = $category->name;
485
- }
486
- }
487
-
488
- return "'" . esc_js( join( "/", $out ) ) . "',";
489
- }
490
-
491
- /**
492
- * Returns a 'variant' JSON line based on $product
493
- * @param object $product Product to pull info for
494
- * @return string Line of JSON
495
- */
496
- private static function product_get_variant_line( $_product ) {
497
-
498
- $out = '';
499
- $variation_data = version_compare( WC_VERSION, '3.0', '<' ) ? $_product->variation_data : ( $_product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $_product->get_id() ) : '' );
500
-
501
- if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
502
- $out = "'" . esc_js( wc_get_formatted_variation( $variation_data, true ) ) . "',";
503
- }
504
-
505
- return $out;
506
- }
507
-
508
- /**
509
- * Tracks an enhanced ecommerce remove from cart action
510
- */
511
- function remove_from_cart() {
512
  echo( "
513
  <script>
514
  (function($) {
515
- $( document.body ).on( 'click', '.remove', function() {
516
  " . self::tracker_var() . "( 'ec:addProduct', {
517
  'id': ($(this).data('product_sku')) ? ($(this).data('product_sku')) : ('#' + $(this).data('product_id')),
518
  'quantity': $(this).parent().parent().find( '.qty' ).val() ? $(this).parent().parent().find( '.qty' ).val() : '1',
@@ -526,9 +434,11 @@ class WC_Google_Analytics_JS {
526
  }
527
 
528
  /**
529
- * Tracks a product detail view
 
 
530
  */
531
- function product_detail( $product ) {
532
  if ( empty( $product ) ) {
533
  return;
534
  }
@@ -545,9 +455,11 @@ class WC_Google_Analytics_JS {
545
  }
546
 
547
  /**
548
- * Tracks when the checkout process is started
 
 
549
  */
550
- function checkout_process( $cart ) {
551
  $code = "";
552
 
553
  foreach ( $cart as $cart_item_key => $cart_item ) {
@@ -572,12 +484,10 @@ class WC_Google_Analytics_JS {
572
  }
573
 
574
  /**
575
- * Add to cart
576
  *
577
  * @param array $parameters associative array of _trackEvent parameters
578
  * @param string $selector jQuery selector for binding click event
579
- *
580
- * @return void
581
  */
582
  public function event_tracking_code( $parameters, $selector ) {
583
  $parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters );
@@ -585,7 +495,7 @@ class WC_Google_Analytics_JS {
585
  if ( 'yes' === self::get( 'ga_use_universal_analytics' ) ) {
586
  if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) ) {
587
  wc_enqueue_js( "
588
- $( '" . $selector . "' ).click( function() {
589
  " . $parameters['enhanced'] . "
590
  " . self::tracker_var() . "( 'ec:setAction', 'add' );
591
  " . self::tracker_var() . "( 'send', 'event', 'UX', 'click', 'add to cart' );
@@ -600,7 +510,7 @@ class WC_Google_Analytics_JS {
600
  }
601
 
602
  wc_enqueue_js( "
603
- $( '" . $selector . "' ).click( function() {
604
  " . sprintf( $track_event, $parameters['category'], $parameters['action'], $parameters['label'] ) . "
605
  });
606
  " );
8
  *
9
  * JS for recording Google Analytics info
10
  */
11
+ class WC_Google_Analytics_JS extends WC_Abstract_Google_Analytics_JS {
 
 
 
 
 
 
12
 
13
  /**
14
  * Get the class instance
 
15
  *
16
+ * @param array $options Options
17
+ * @return WC_Abstract_Google_Analytics_JS
18
  */
19
  public static function get_instance( $options = array() ) {
20
  return null === self::$instance ? ( self::$instance = new self( $options ) ) : self::$instance;
24
  * Constructor
25
  * Takes our options from the parent class so we can later use them in the JS snippets
26
  *
27
+ * @param array $options Options
28
  */
29
  public function __construct( $options = array() ) {
30
  self::$options = $options;
31
  }
32
 
 
 
 
 
 
 
 
 
 
33
  /**
34
  * Returns the tracker variable this integration should use
35
+ *
36
+ * @return string
37
  */
38
  public static function tracker_var() {
39
  return apply_filters( 'woocommerce_ga_tracker_variable', 'ga' );
40
  }
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  /**
43
  * Loads the correct Google Analytics code (classic or universal)
44
+ *
45
+ * @param boolean|WC_Order $order Classic analytics needs order data to set the currency correctly
46
+ * @return string Analytics loading code
47
  */
48
  public static function load_analytics( $order = false ) {
49
  $logged_in = is_user_logged_in() ? 'yes' : 'no';
59
 
60
  /**
61
  * Loads ga.js analytics tracking code
62
+ *
63
+ * @param string $logged_in 'yes' if the user is logged in, no if not (this is a string so we can pass it to GA)
64
+ * @param boolean|WC_Order $order We don't always need to load order data for currency, so we omit
65
+ * that if false is set, otherwise this is an order object
66
+ * @return string Classic Analytics loading code
67
  */
68
+ protected static function load_analytics_classic( $logged_in, $order = false ) {
69
  $anonymize_enabled = '';
70
  if ( 'yes' === self::get( 'ga_anonymize_enabled' ) ) {
71
  $anonymize_enabled = "['_gat._anonymizeIp'],";
100
 
101
  $code .= ");";
102
 
103
+
104
+ $code = "<script type='text/javascript'>" . $code . "</script>";
105
+ return apply_filters( 'woocommerce_ga_classic_snippet_output', $code );
106
  }
107
 
108
  /**
109
+ * Enqueues JavaScript to build the addImpression object
110
+ *
111
+ * @param WC_Product $product
112
+ * @param int $position
113
  */
114
  public static function listing_impression( $product, $position ) {
115
  if ( isset( $_GET['s'] ) ) {
130
  }
131
 
132
  /**
133
+ * Enqueues JavaScript to build an addProduct and click object
134
+ *
135
+ * @param WC_Product $product
136
+ * @param int $position
137
  */
138
  public static function listing_click( $product, $position ) {
139
  if ( isset( $_GET['s'] ) ) {
143
  }
144
 
145
  wc_enqueue_js( "
146
+ $( '.products .post-" . esc_js( $product->get_id() ) . " a' ).on( 'click', function() {
147
  if ( true === $(this).hasClass( 'add_to_cart_button' ) ) {
148
  return;
149
  }
162
  }
163
 
164
  /**
 
165
  * Loads in the footer
166
  * @see wp_footer
167
  */
180
  }
181
 
182
  /**
183
+ * Enqueues JavaScript to send the pageview last thing (needed for things like addImpression)
184
  */
185
  public static function universal_analytics_footer() {
186
  if ( apply_filters( 'wc_google_analytics_send_pageview', true ) ) {
191
  /**
192
  * This was created to fix public facing api typo in a filter name
193
  * and inform about the deprecation.
194
+ *
195
+ * @param boolean $send_pageview
196
  */
197
  public static function universal_analytics_footer_filter( $send_pageview ) {
198
  return apply_filters_deprecated( 'wc_goole_analytics_send_pageview', array( $send_pageview ), '1.4.20', 'wc_google_analytics_send_pageview' );
200
 
201
  /**
202
  * Loads the universal analytics code
203
+ *
204
  * @param string $logged_in 'yes' if the user is logged in, no if not (this is a string so we can pass it to GA)
205
+ * @return string Universal Analytics Code
206
  */
207
+ protected static function load_analytics_universal( $logged_in ) {
208
 
209
  $domainname = self::get( 'ga_set_domain_name' );
210
 
243
  })(window,document,'script', '{$src}','" . self::tracker_var() . "');";
244
 
245
  $ga_id = self::get( 'ga_id' );
246
+ $ga_snippet_create = self::tracker_var() . "( 'create', '" . esc_js( $ga_id ) . "', '" . $set_domain_name . "' );";
247
 
248
+ if ( ! empty( self::DEVELOPER_ID ) ) {
249
+ $ga_snippet_developer_id = "(window.gaDevIds=window.gaDevIds||[]).push('" . self::DEVELOPER_ID . "');";
250
+ } else {
251
+ $ga_snippet_developer_id = '';
252
+ }
253
+
254
+ $ga_snippet_require = $support_display_advertising .
255
+ $support_enhanced_link_attribution .
256
+ $anonymize_enabled .
257
+ $track_404_enabled . "
258
  " . self::tracker_var() . "( 'set', 'dimension1', '" . $logged_in . "' );\n";
259
 
260
  if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) ) {
263
  $ga_snippet_require .= "" . self::tracker_var() . "( 'require', 'ecommerce', 'ecommerce.js');";
264
  }
265
 
266
+ $ga_snippet_head = apply_filters( 'woocommerce_ga_snippet_head', $ga_snippet_head );
267
+ $ga_snippet_create = apply_filters( 'woocommerce_ga_snippet_create', $ga_snippet_create, $ga_id );
268
+ $ga_snippet_developer_id = apply_filters( 'woocommerce_ga_snippet_developer_id', $ga_snippet_developer_id );
269
+ $ga_snippet_require = apply_filters( 'woocommerce_ga_snippet_require', $ga_snippet_require );
270
 
271
+ $code = "<script type='text/javascript'>" . $ga_snippet_head . $ga_snippet_create . $ga_snippet_developer_id . $ga_snippet_require . "</script>";
272
  $code = apply_filters( 'woocommerce_ga_snippet_output', $code );
273
 
274
  return $code;
275
  }
276
 
277
  /**
278
+ * Generate code used to pass transaction data to Google Analytics.
279
  *
280
+ * @param WC_Order $order WC_Order Object.
281
+ * @return string Add Transaction code.
282
  */
283
  public function add_transaction( $order ) {
284
  if ( 'yes' === self::get( 'ga_use_universal_analytics' ) ) {
311
 
312
  /**
313
  * ga.js (classic) transaction tracking
314
+ * @param WC_Order $order WC_Order Object
315
+ * @return string Add Transaction Code
316
  */
317
+ protected function add_transaction_classic( $order ) {
318
  $code = "_gaq.push(['_addTrans',
319
  '" . esc_js( $order->get_order_number() ) . "', // order ID - required
320
  '" . esc_js( get_bloginfo( 'name' ) ) . "', // affiliation or store name
338
  }
339
 
340
  /**
341
+ * Generate Universal Analytics Enhanced Ecommerce transaction tracking code
342
+ *
343
+ * @param WC_Order $order
344
+ * @return string
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
  */
346
+ protected function add_transaction_enhanced( $order ) {
347
  $code = "" . self::tracker_var() . "( 'set', '&cu', '" . esc_js( version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_order_currency() : $order->get_currency() ) . "' );";
348
 
349
  // Order items
366
 
367
  /**
368
  * Add Item (Classic)
369
+ *
370
+ * @param WC_Order $order WC_Order Object
371
+ * @param array $item The item to add to a transaction/order
372
+ * @return string
373
  */
374
+ protected function add_item_classic( $order, $item ) {
375
  $_product = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_product_from_item( $item ) : $item->get_product();
376
 
377
  $code = "_gaq.push(['_addItem',";
386
  return $code;
387
  }
388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389
  /**
390
  * Add Item (Enhanced, Universal)
391
+ *
392
+ * @param WC_Order $order WC_Order Object
393
+ * @param WC_Order_Item $item The item to add to a transaction/order
394
+ * @return string
395
  */
396
+ protected function add_item_enhanced( $order, $item ) {
397
  $_product = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_product_from_item( $item ) : $item->get_product();
398
  $variant = self::product_get_variant_line( $_product );
399
 
414
  }
415
 
416
  /**
417
+ * Output JavaScript to track an enhanced ecommerce remove from cart action
 
 
418
  */
419
+ public function remove_from_cart() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
  echo( "
421
  <script>
422
  (function($) {
423
+ $( document.body ).off('click', '.remove').on( 'click', '.remove', function() {
424
  " . self::tracker_var() . "( 'ec:addProduct', {
425
  'id': ($(this).data('product_sku')) ? ($(this).data('product_sku')) : ('#' + $(this).data('product_id')),
426
  'quantity': $(this).parent().parent().find( '.qty' ).val() ? $(this).parent().parent().find( '.qty' ).val() : '1',
434
  }
435
 
436
  /**
437
+ * Enqueue JavaScript to track a product detail view
438
+ *
439
+ * @param WC_Product $product
440
  */
441
+ public function product_detail( $product ) {
442
  if ( empty( $product ) ) {
443
  return;
444
  }
455
  }
456
 
457
  /**
458
+ * Enqueue JS to track when the checkout process is started
459
+ *
460
+ * @param array $cart items/contents of the cart
461
  */
462
+ public function checkout_process( $cart ) {
463
  $code = "";
464
 
465
  foreach ( $cart as $cart_item_key => $cart_item ) {
484
  }
485
 
486
  /**
487
+ * Enqueue JavaScript for Add to cart tracking
488
  *
489
  * @param array $parameters associative array of _trackEvent parameters
490
  * @param string $selector jQuery selector for binding click event
 
 
491
  */
492
  public function event_tracking_code( $parameters, $selector ) {
493
  $parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters );
495
  if ( 'yes' === self::get( 'ga_use_universal_analytics' ) ) {
496
  if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) ) {
497
  wc_enqueue_js( "
498
+ $( '" . $selector . "' ).on( 'click', function() {
499
  " . $parameters['enhanced'] . "
500
  " . self::tracker_var() . "( 'ec:setAction', 'add' );
501
  " . self::tracker_var() . "( 'send', 'event', 'UX', 'click', 'add to cart' );
510
  }
511
 
512
  wc_enqueue_js( "
513
+ $( '" . $selector . "' ).on( 'click', function() {
514
  " . sprintf( $track_event, $parameters['category'], $parameters['action'], $parameters['label'] ) . "
515
  });
516
  " );
includes/class-wc-google-analytics.php CHANGED
@@ -10,13 +10,44 @@ if ( ! defined( 'ABSPATH' ) ) {
10
  *
11
  * @class WC_Google_Analytics
12
  * @extends WC_Integration
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  */
14
  class WC_Google_Analytics extends WC_Integration {
15
 
16
  /**
17
- * Init and hook in the integration.
18
  *
19
- * @return void
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  */
21
  public function __construct() {
22
  $this->id = 'google_analytics';
@@ -30,8 +61,10 @@ class WC_Google_Analytics extends WC_Integration {
30
  $constructor = $this->init_options();
31
 
32
  // Contains snippets/JS tracking code
 
33
  include_once( 'class-wc-google-analytics-js.php' );
34
- WC_Google_Analytics_JS::get_instance( $constructor );
 
35
 
36
  // Display an info banner on how to configure WooCommerce
37
  if ( is_admin() ) {
@@ -64,13 +97,15 @@ class WC_Google_Analytics extends WC_Integration {
64
  }
65
 
66
  /**
67
- * Loads all of our options for this plugin
 
68
  * @return array An array of options that can be passed to other classes
69
  */
70
  public function init_options() {
71
  $options = array(
72
  'ga_id',
73
  'ga_set_domain_name',
 
74
  'ga_standard_tracking_enabled',
75
  'ga_support_display_advertising',
76
  'ga_support_enhanced_link_attribution',
@@ -84,7 +119,7 @@ class WC_Google_Analytics extends WC_Integration {
84
  'ga_enhanced_product_click_enabled',
85
  'ga_enhanced_checkout_process_enabled',
86
  'ga_enhanced_product_detail_view_enabled',
87
- 'ga_event_tracking_enabled'
88
  );
89
 
90
  $constructor = array();
@@ -99,10 +134,19 @@ class WC_Google_Analytics extends WC_Integration {
99
  * Tells WooCommerce which settings to display under the "integration" tab
100
  */
101
  public function init_form_fields() {
 
 
 
 
 
 
 
 
 
102
  $this->form_fields = array(
103
  'ga_id' => array(
104
- 'title' => __( 'Google Analytics ID', 'woocommerce-google-analytics-integration' ),
105
- 'description' => __( 'Log into your Google Analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce-google-analytics-integration' ),
106
  'type' => 'text',
107
  'placeholder' => 'UA-XXXXX-X',
108
  'default' => get_option( 'woocommerce_ga_id' ) // Backwards compat
@@ -111,10 +155,28 @@ class WC_Google_Analytics extends WC_Integration {
111
  'title' => __( 'Set Domain Name', 'woocommerce-google-analytics-integration' ),
112
  'description' => sprintf( __( '(Optional) Sets the <code>_setDomainName</code> variable. <a href="%s" target="_blank">See here for more information</a>.', 'woocommerce-google-analytics-integration' ), 'https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#multipleDomains' ),
113
  'type' => 'text',
114
- 'default' => ''
 
115
  ),
116
- 'ga_standard_tracking_enabled' => array(
 
117
  'title' => __( 'Tracking Options', 'woocommerce-google-analytics-integration' ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  'label' => __( 'Enable Standard Tracking', 'woocommerce-google-analytics-integration' ),
119
  'description' => __( 'This tracks session data such as demographics, system, etc. You don\'t need to enable this if you are using a 3rd party Google analytics plugin.', 'woocommerce-google-analytics-integration' ),
120
  'type' => 'checkbox',
@@ -126,7 +188,7 @@ class WC_Google_Analytics extends WC_Integration {
126
  'description' => sprintf( __( 'Set the Google Analytics code to support Display Advertising. %sRead more about Display Advertising%s.', 'woocommerce-google-analytics-integration' ), '<a href="https://support.google.com/analytics/answer/2700409" target="_blank">', '</a>' ),
127
  'type' => 'checkbox',
128
  'checkboxgroup' => '',
129
- 'default' => get_option( 'woocommerce_ga_support_display_advertising' ) ? get_option( 'woocommerce_ga_support_display_advertising' ) : 'no' // Backwards compat
130
  ),
131
  'ga_support_enhanced_link_attribution' => array(
132
  'label' => __( 'Use Enhanced Link Attribution', 'woocommerce-google-analytics-integration' ),
@@ -135,22 +197,15 @@ class WC_Google_Analytics extends WC_Integration {
135
  'checkboxgroup' => '',
136
  'default' => get_option( 'woocommerce_ga_support_enhanced_link_attribution' ) ? get_option( 'woocommerce_ga_support_enhanced_link_attribution' ) : 'no' // Backwards compat
137
  ),
138
- 'ga_use_universal_analytics' => array(
139
- 'label' => __( 'Enable Universal Analytics', 'woocommerce-google-analytics-integration' ),
140
- 'description' => sprintf( __( 'Uses Universal Analytics instead of Classic Google Analytics. If you have <strong>not</strong> previously used Google Analytics on this site, check this box. Otherwise, %sfollow step 1 of the Universal Analytics upgrade guide.%s Enabling this setting will take care of step 2. %sRead more about Universal Analytics%s. Universal Analytics must be enabled to enable enhanced eCommerce.', 'woocommerce-google-analytics-integration' ), '<a href="https://developers.google.com/analytics/devguides/collection/upgrade/guide" target="_blank">', '</a>', '<a href="https://support.google.com/analytics/answer/2790010?hl=en" target="_blank">', '</a>' ),
141
- 'type' => 'checkbox',
142
- 'checkboxgroup' => '',
143
- 'default' => get_option( 'woocommerce_ga_use_universal_analytics' ) ? get_option( 'woocommerce_ga_use_universal_analytics' ) : 'no' // Backwards compat
144
- ),
145
  'ga_anonymize_enabled' => array(
146
- 'label' => __( 'Anonymize IP addresses.', 'woocommerce-google-analytics-integration' ),
147
  'description' => sprintf( __( 'Enabling this option is mandatory in certain countries due to national privacy laws. %sRead more about IP Anonymization%s.', 'woocommerce-google-analytics-integration' ), '<a href="https://support.google.com/analytics/answer/2763052" target="_blank">', '</a>' ),
148
  'type' => 'checkbox',
149
  'checkboxgroup' => '',
150
  'default' => 'yes'
151
  ),
152
  'ga_404_tracking_enabled' => array(
153
- 'label' => __( 'Track 404 (Not found) Errors.', 'woocommerce-google-analytics-integration' ),
154
  'description' => sprintf( __( 'Enable this to find broken or dead links. An "Event" with category "Error" and action "404 Not Found" will be created in Google Analytics for each incoming pageview to a non-existing page. By setting up a "Custom Goal" for these events within Google Analytics you can find out where broken links originated from (the referrer). %sRead how to set up a goal%s.', 'woocommerce-google-analytics-integration' ), '<a href="https://support.google.com/analytics/answer/1032415" target="_blank">', '</a>' ),
155
  'type' => 'checkbox',
156
  'checkboxgroup' => '',
@@ -161,25 +216,26 @@ class WC_Google_Analytics extends WC_Integration {
161
  'description' => __( 'This requires a payment gateway that redirects to the thank you/order received page after payment. Orders paid with gateways which do not do this will not be tracked.', 'woocommerce-google-analytics-integration' ),
162
  'type' => 'checkbox',
163
  'checkboxgroup' => 'start',
164
- 'default' => get_option( 'woocommerce_ga_ecommerce_tracking_enabled' ) ? get_option( 'woocommerce_ga_ecommerce_tracking_enabled' ) : 'yes' // Backwards compat
165
  ),
166
  'ga_event_tracking_enabled' => array(
167
  'label' => __( 'Add to Cart Events', 'woocommerce-google-analytics-integration' ),
168
  'type' => 'checkbox',
169
  'checkboxgroup' => '',
170
- 'default' => 'yes'
171
  ),
172
 
173
  'ga_enhanced_ecommerce_tracking_enabled' => array(
174
  'title' => __( 'Enhanced eCommerce', 'woocommerce-google-analytics-integration' ),
175
  'label' => __( 'Enable Enhanced eCommerce ', 'woocommerce-google-analytics-integration' ),
176
- 'description' => sprintf( __( 'Enhanced eCommerce allows you to measure more user interactions with your store, including: product impressions, product detail views, starting the checkout process, adding cart items, and removing cart items. Universal Analytics must be enabled for Enhanced eCommerce to work. Before enabling this setting, turn on Enhanced eCommerce in your Google Analytics dashboard. <a href="%s">See here for more information</a>.', 'woocommerce-google-analytics-integration' ), 'https://support.google.com/analytics/answer/6032539?hl=en' ),
177
  'type' => 'checkbox',
178
  'checkboxgroup' => '',
179
- 'default' => 'no'
 
180
  ),
181
 
182
- // Enhanced eCommerce Settings
183
 
184
  'ga_enhanced_remove_from_cart_enabled' => array(
185
  'label' => __( 'Remove from Cart Events', 'woocommerce-google-analytics-integration' ),
@@ -237,28 +293,40 @@ class WC_Google_Analytics extends WC_Integration {
237
  /**
238
  * Hooks into woocommerce_tracker_data and tracks some of the analytic settings (just enabled|disabled status)
239
  * only if you have opted into WooCommerce tracking
240
- * http://www.woothemes.com/woocommerce/usage-tracking/
241
  *
242
- * @param array $data Current WC tracker data.
243
- *
244
- * @return array Updated WC Tracker data.
245
  */
246
  function track_options( $data ) {
247
  $data['wc-google-analytics'] = array(
248
- 'standard_tracking_enabled' => $this->ga_standard_tracking_enabled,
249
- 'support_display_advertising' => $this->ga_support_display_advertising,
250
- 'support_enhanced_link_attribution' => $this->ga_support_enhanced_link_attribution,
251
- 'use_universal_analytics' => $this->ga_use_universal_analytics,
252
- 'anonymize_enabled' => $this->ga_anonymize_enabled,
253
- 'ga_404_tracking_enabled' => $this->ga_404_tracking_enabled,
254
- 'ecommerce_tracking_enabled' => $this->ga_ecommerce_tracking_enabled,
255
- 'event_tracking_enabled' => $this->ga_event_tracking_enabled
 
 
 
 
256
  );
 
 
 
 
 
 
 
 
 
257
  return $data;
258
  }
259
 
260
  /**
261
- *
262
  */
263
  function load_admin_assets() {
264
  $screen = get_current_screen();
@@ -274,7 +342,7 @@ class WC_Google_Analytics extends WC_Integration {
274
  return;
275
  }
276
 
277
- wp_enqueue_script( 'wc-google-analytics-admin-enhanced-settings', plugins_url( '/assets/js/admin-enhanced-settings.js', dirname( __FILE__ ) ), array(), WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION, true );
278
  }
279
 
280
  /**
@@ -309,17 +377,17 @@ class WC_Google_Analytics extends WC_Integration {
309
  }
310
 
311
  /**
312
- * Standard Google Analytics tracking
313
  */
314
  protected function get_standard_tracking_code() {
315
  return "<!-- WooCommerce Google Analytics Integration -->
316
- " . WC_Google_Analytics_JS::get_instance()->header() . "
317
- <script type='text/javascript'>" . WC_Google_Analytics_JS::get_instance()->load_analytics() . "</script>
318
  <!-- /WooCommerce Google Analytics Integration -->";
319
  }
320
 
321
  /**
322
- * eCommerce tracking
323
  *
324
  * @param int $order_id
325
  * @return string
@@ -333,15 +401,16 @@ class WC_Google_Analytics extends WC_Integration {
333
  return '';
334
  }
335
 
336
- $code = WC_Google_Analytics_JS::get_instance()->load_analytics( $order );
337
- $code .= WC_Google_Analytics_JS::get_instance()->add_transaction( $order );
338
 
339
  // Mark the order as tracked.
340
  update_post_meta( $order_id, '_ga_tracked', 1 );
341
 
342
  return "
343
  <!-- WooCommerce Google Analytics Integration -->
344
- " . WC_Google_Analytics_JS::get_instance()->header() . "
 
345
  <script type='text/javascript'>$code</script>
346
  <!-- /WooCommerce Google Analytics Integration -->
347
  ";
@@ -350,20 +419,15 @@ class WC_Google_Analytics extends WC_Integration {
350
  /**
351
  * Check if tracking is disabled
352
  *
353
- * @param string $type The setting to check
354
- *
355
- * @return bool True if tracking for a certain setting is disabled
356
  */
357
  private function disable_tracking( $type ) {
358
- if ( is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' === $type || apply_filters( 'woocommerce_ga_disable_tracking', false, $type ) ) {
359
- return true;
360
- }
361
  }
362
 
363
  /**
364
  * Google Analytics event tracking for single product add to cart
365
- *
366
- * @return void
367
  */
368
  public function add_to_cart() {
369
  if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
@@ -382,43 +446,38 @@ class WC_Google_Analytics extends WC_Integration {
382
  $parameters['label'] = "'" . esc_js( $product->get_sku() ? __( 'ID:', 'woocommerce-google-analytics-integration' ) . ' ' . $product->get_sku() : "#" . $product->get_id() ) . "'";
383
 
384
  if ( ! $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
385
- $code = "" . WC_Google_Analytics_JS::get_instance()->tracker_var() . "( 'ec:addProduct', {";
386
- $code .= "'id': '" . esc_js( $product->get_sku() ? $product->get_sku() : ( '#' . $product->get_id() ) ) . "',";
387
- $code .= "'name': '" . esc_js( $product->get_title() ) . "',";
388
- $code .= "'quantity': $( 'input.qty' ).val() ? $( 'input.qty' ).val() : '1'";
389
- $code .= "} );";
 
 
 
390
  $parameters['enhanced'] = $code;
391
  }
392
 
393
- WC_Google_Analytics_JS::get_instance()->event_tracking_code( $parameters, '.single_add_to_cart_button' );
394
  }
395
 
396
  /**
397
  * Enhanced Analytics event tracking for removing a product from the cart
398
  */
399
  public function remove_from_cart() {
400
- if ( $this->disable_tracking( $this->ga_use_universal_analytics ) ) {
401
- return;
402
- }
403
 
404
- if ( $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
405
  return;
406
  }
407
 
408
- if ( $this->disable_tracking( $this->ga_enhanced_remove_from_cart_enabled ) ) {
409
- return;
410
- }
411
-
412
- WC_Google_Analytics_JS::get_instance()->remove_from_cart();
413
  }
414
 
415
  /**
416
  * Adds the product ID and SKU to the remove product link if not present
417
  *
418
- * @param string $url
419
- * @param string $key
420
- *
421
- * @return string|string[]
422
  */
423
  public function remove_from_cart_attributes( $url, $key ) {
424
  if ( strpos( $url,'data-product_id' ) !== false ) {
@@ -442,8 +501,6 @@ class WC_Google_Analytics extends WC_Integration {
442
 
443
  /**
444
  * Google Analytics event tracking for loop add to cart
445
- *
446
- * @return void
447
  */
448
  public function loop_add_to_cart() {
449
  if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
@@ -457,102 +514,108 @@ class WC_Google_Analytics extends WC_Integration {
457
  $parameters['label'] = "($(this).data('product_sku')) ? ($(this).data('product_sku')) : ('#' + $(this).data('product_id'))"; // Product SKU or ID
458
 
459
  if ( ! $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
460
- $code = "" . WC_Google_Analytics_JS::get_instance()->tracker_var() . "( 'ec:addProduct', {";
461
- $code .= "'id': ($(this).data('product_sku')) ? ($(this).data('product_sku')) : ('#' + $(this).data('product_id')),";
462
- $code .= "'quantity': $(this).data('quantity')";
463
- $code .= "} );";
 
 
 
464
  $parameters['enhanced'] = $code;
465
  }
466
 
467
- WC_Google_Analytics_JS::get_instance()->event_tracking_code( $parameters, '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' );
468
  }
469
 
470
  /**
471
- * Measures a listing impression (from search results)
 
 
 
 
472
  */
473
- public function listing_impression() {
474
- if ( $this->disable_tracking( $this->ga_use_universal_analytics ) ) {
475
- return;
476
  }
477
 
 
 
 
 
 
 
478
  if ( $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
479
- return;
 
 
 
 
 
 
 
480
  }
481
 
482
- if ( $this->disable_tracking( $this->ga_enhanced_product_impression_enabled ) ) {
 
 
 
 
 
 
 
 
483
  return;
484
  }
485
 
486
  global $product, $woocommerce_loop;
487
- WC_Google_Analytics_JS::get_instance()->listing_impression( $product, $woocommerce_loop['loop'] );
488
  }
489
 
490
  /**
491
  * Measure a product click from a listing page
492
  */
493
  public function listing_click() {
494
- if ( $this->disable_tracking( $this->ga_use_universal_analytics ) ) {
495
- return;
496
- }
497
 
498
- if ( $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
499
- return;
500
- }
501
-
502
- if ( $this->disable_tracking( $this->ga_enhanced_product_click_enabled ) ) {
503
  return;
504
  }
505
 
506
  global $product, $woocommerce_loop;
507
- WC_Google_Analytics_JS::get_instance()->listing_click( $product, $woocommerce_loop['loop'] );
508
  }
509
 
510
  /**
511
  * Measure a product detail view
512
  */
513
  public function product_detail() {
514
- if ( $this->disable_tracking( $this->ga_use_universal_analytics ) ) {
515
- return;
516
- }
517
 
518
- if ( $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
519
- return;
520
- }
521
-
522
- if ( $this->disable_tracking( $this->ga_enhanced_product_detail_view_enabled ) ) {
523
  return;
524
  }
525
 
526
  global $product;
527
- WC_Google_Analytics_JS::get_instance()->product_detail( $product );
528
  }
529
 
530
  /**
531
  * Tracks when the checkout form is loaded
532
  *
533
- * @param $checkout
534
  */
535
  public function checkout_process( $checkout ) {
536
- if ( $this->disable_tracking( $this->ga_use_universal_analytics ) ) {
537
- return;
538
- }
539
 
540
- if ( $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
541
  return;
542
  }
543
 
544
- if ( $this->disable_tracking( $this->ga_enhanced_checkout_process_enabled ) ) {
545
- return;
546
- }
547
-
548
- WC_Google_Analytics_JS::get_instance()->checkout_process( WC()->cart->get_cart() );
549
  }
550
 
551
  /**
552
  * Add the utm_nooverride parameter to any return urls. This makes sure Google Adwords doesn't mistake the offsite gateway as the referrer.
553
  *
554
  * @param string $return_url WooCommerce Return URL
555
- *
556
  * @return string URL
557
  */
558
  public function utm_nooverride( $return_url ) {
10
  *
11
  * @class WC_Google_Analytics
12
  * @extends WC_Integration
13
+ *
14
+ * @property $ga_id
15
+ * @property $ga_set_domain_name
16
+ * @property $ga_gtag_enabled
17
+ * @property $ga_standard_tracking_enabled
18
+ * @property $ga_support_display_advertising
19
+ * @property $ga_support_enhanced_link_attribution
20
+ * @property $ga_use_universal_analytics
21
+ * @property $ga_anonymize_enabled
22
+ * @property $ga_404_tracking_enabled
23
+ * @property $ga_ecommerce_tracking_enabled
24
+ * @property $ga_enhanced_ecommerce_tracking_enabled
25
+ * @property $ga_enhanced_remove_from_cart_enabled
26
+ * @property $ga_enhanced_product_impression_enabled
27
+ * @property $ga_enhanced_product_click_enabled
28
+ * @property $ga_enhanced_checkout_process_enabled
29
+ * @property $ga_enhanced_product_detail_view_enabled
30
+ * @property $ga_event_tracking_enabled
31
  */
32
  class WC_Google_Analytics extends WC_Integration {
33
 
34
  /**
35
+ * Returns the proper class based on Gtag settings.
36
  *
37
+ * @param array $options Options
38
+ * @return WC_Abstract_Google_Analytics_JS
39
+ */
40
+ protected function get_tracking_instance( $options = array() ) {
41
+ if ( 'yes' === $this->ga_gtag_enabled ) {
42
+ return WC_Google_Gtag_JS::get_instance( $options );
43
+ }
44
+
45
+ return WC_Google_Analytics_JS::get_instance( $options );
46
+ }
47
+
48
+ /**
49
+ * Constructor
50
+ * Init and hook in the integration.
51
  */
52
  public function __construct() {
53
  $this->id = 'google_analytics';
61
  $constructor = $this->init_options();
62
 
63
  // Contains snippets/JS tracking code
64
+ include_once( 'class-wc-abstract-google-analytics-js.php' );
65
  include_once( 'class-wc-google-analytics-js.php' );
66
+ include_once( 'class-wc-google-gtag-js.php' );
67
+ $this->get_tracking_instance( $constructor );
68
 
69
  // Display an info banner on how to configure WooCommerce
70
  if ( is_admin() ) {
97
  }
98
 
99
  /**
100
+ * Loads all of our options for this plugin (stored as properties as well)
101
+ *
102
  * @return array An array of options that can be passed to other classes
103
  */
104
  public function init_options() {
105
  $options = array(
106
  'ga_id',
107
  'ga_set_domain_name',
108
+ 'ga_gtag_enabled',
109
  'ga_standard_tracking_enabled',
110
  'ga_support_display_advertising',
111
  'ga_support_enhanced_link_attribution',
119
  'ga_enhanced_product_click_enabled',
120
  'ga_enhanced_checkout_process_enabled',
121
  'ga_enhanced_product_detail_view_enabled',
122
+ 'ga_event_tracking_enabled',
123
  );
124
 
125
  $constructor = array();
134
  * Tells WooCommerce which settings to display under the "integration" tab
135
  */
136
  public function init_form_fields() {
137
+
138
+ // backwards_compatibility
139
+ if ( get_option( 'woocommerce_ga_use_universal_analytics' ) ) {
140
+ $ua_default_value = get_option( 'woocommerce_ga_use_universal_analytics' );
141
+ } else {
142
+ // don't enable for extension updates, only default to enabled on new installs
143
+ $ua_default_value = get_option( $this->get_option_key() ) ? 'no' : 'yes';
144
+ }
145
+
146
  $this->form_fields = array(
147
  'ga_id' => array(
148
+ 'title' => __( 'Google Analytics Tracking ID', 'woocommerce-google-analytics-integration' ),
149
+ 'description' => __( 'Log into your Google Analytics account to find your ID. e.g. <code>G-XXXXX</code> or <code>UA-XXXXX-X</code>', 'woocommerce-google-analytics-integration' ),
150
  'type' => 'text',
151
  'placeholder' => 'UA-XXXXX-X',
152
  'default' => get_option( 'woocommerce_ga_id' ) // Backwards compat
155
  'title' => __( 'Set Domain Name', 'woocommerce-google-analytics-integration' ),
156
  'description' => sprintf( __( '(Optional) Sets the <code>_setDomainName</code> variable. <a href="%s" target="_blank">See here for more information</a>.', 'woocommerce-google-analytics-integration' ), 'https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#multipleDomains' ),
157
  'type' => 'text',
158
+ 'default' => '',
159
+ 'class' => 'legacy-setting',
160
  ),
161
+
162
+ 'ga_gtag_enabled' => array(
163
  'title' => __( 'Tracking Options', 'woocommerce-google-analytics-integration' ),
164
+ 'label' => __( 'Use Global Site Tag', 'woocommerce-google-analytics-integration' ),
165
+ 'description' => sprintf( __( 'The Global Site Tag provides streamlined tagging across Google’s site measurement, conversion tracking, and remarketing products. This must be enabled to use a Google Analytics 4 Measurement ID (e.g., <code>G-XXXXX</code>). <a href="%s" target="_blank">See here for more information</a>.', 'woocommerce-google-analytics-integration' ), 'https://support.google.com/analytics/answer/7475631?hl=en' ),
166
+ 'type' => 'checkbox',
167
+ 'checkboxgroup' => '',
168
+ 'default' => get_option( $this->get_option_key() ) ? 'no' : 'yes', // don't enable on updates, only default on new installs
169
+ ),
170
+
171
+ 'ga_use_universal_analytics' => array(
172
+ 'label' => __( 'Enable Universal Analytics', 'woocommerce-google-analytics-integration' ),
173
+ 'description' => sprintf( __( 'Uses Universal Analytics instead of Classic Google Analytics. If you have <strong>not</strong> previously used Google Analytics on this site, check this box. Otherwise, %sfollow step 1 of the Universal Analytics upgrade guide.%s Enabling this setting will take care of step 2. %sRead more about Universal Analytics%s. Universal Analytics or Global Site Tag must be enabled to enable enhanced eCommerce.', 'woocommerce-google-analytics-integration' ), '<a href="https://developers.google.com/analytics/devguides/collection/upgrade/guide" target="_blank">', '</a>', '<a href="https://support.google.com/analytics/answer/2790010?hl=en" target="_blank">', '</a>' ),
174
+ 'type' => 'checkbox',
175
+ 'checkboxgroup' => '',
176
+ 'default' => $ua_default_value,
177
+ 'class' => 'legacy-setting',
178
+ ),
179
+ 'ga_standard_tracking_enabled' => array(
180
  'label' => __( 'Enable Standard Tracking', 'woocommerce-google-analytics-integration' ),
181
  'description' => __( 'This tracks session data such as demographics, system, etc. You don\'t need to enable this if you are using a 3rd party Google analytics plugin.', 'woocommerce-google-analytics-integration' ),
182
  'type' => 'checkbox',
188
  'description' => sprintf( __( 'Set the Google Analytics code to support Display Advertising. %sRead more about Display Advertising%s.', 'woocommerce-google-analytics-integration' ), '<a href="https://support.google.com/analytics/answer/2700409" target="_blank">', '</a>' ),
189
  'type' => 'checkbox',
190
  'checkboxgroup' => '',
191
+ 'default' => get_option( 'woocommerce_ga_support_display_advertising' ) ? get_option( 'woocommerce_ga_support_display_advertising' ) : 'yes', // Backwards compat
192
  ),
193
  'ga_support_enhanced_link_attribution' => array(
194
  'label' => __( 'Use Enhanced Link Attribution', 'woocommerce-google-analytics-integration' ),
197
  'checkboxgroup' => '',
198
  'default' => get_option( 'woocommerce_ga_support_enhanced_link_attribution' ) ? get_option( 'woocommerce_ga_support_enhanced_link_attribution' ) : 'no' // Backwards compat
199
  ),
 
 
 
 
 
 
 
200
  'ga_anonymize_enabled' => array(
201
+ 'label' => __( 'Anonymize IP addresses', 'woocommerce-google-analytics-integration' ),
202
  'description' => sprintf( __( 'Enabling this option is mandatory in certain countries due to national privacy laws. %sRead more about IP Anonymization%s.', 'woocommerce-google-analytics-integration' ), '<a href="https://support.google.com/analytics/answer/2763052" target="_blank">', '</a>' ),
203
  'type' => 'checkbox',
204
  'checkboxgroup' => '',
205
  'default' => 'yes'
206
  ),
207
  'ga_404_tracking_enabled' => array(
208
+ 'label' => __( 'Track 404 (Not found) Errors', 'woocommerce-google-analytics-integration' ),
209
  'description' => sprintf( __( 'Enable this to find broken or dead links. An "Event" with category "Error" and action "404 Not Found" will be created in Google Analytics for each incoming pageview to a non-existing page. By setting up a "Custom Goal" for these events within Google Analytics you can find out where broken links originated from (the referrer). %sRead how to set up a goal%s.', 'woocommerce-google-analytics-integration' ), '<a href="https://support.google.com/analytics/answer/1032415" target="_blank">', '</a>' ),
210
  'type' => 'checkbox',
211
  'checkboxgroup' => '',
216
  'description' => __( 'This requires a payment gateway that redirects to the thank you/order received page after payment. Orders paid with gateways which do not do this will not be tracked.', 'woocommerce-google-analytics-integration' ),
217
  'type' => 'checkbox',
218
  'checkboxgroup' => 'start',
219
+ 'default' => get_option( 'woocommerce_ga_ecommerce_tracking_enabled' ) ? get_option( 'woocommerce_ga_ecommerce_tracking_enabled' ) : 'yes', // Backwards compat
220
  ),
221
  'ga_event_tracking_enabled' => array(
222
  'label' => __( 'Add to Cart Events', 'woocommerce-google-analytics-integration' ),
223
  'type' => 'checkbox',
224
  'checkboxgroup' => '',
225
+ 'default' => 'yes',
226
  ),
227
 
228
  'ga_enhanced_ecommerce_tracking_enabled' => array(
229
  'title' => __( 'Enhanced eCommerce', 'woocommerce-google-analytics-integration' ),
230
  'label' => __( 'Enable Enhanced eCommerce ', 'woocommerce-google-analytics-integration' ),
231
+ 'description' => sprintf( __( 'Enhanced eCommerce allows you to measure more user interactions with your store, including: product impressions, product detail views, starting the checkout process, adding cart items, and removing cart items. Universal Analytics or Global Site Tag must be enabled for Enhanced eCommerce to work. If using Universal Analytics, turn on Enhanced eCommerce in your Google Analytics dashboard before enabling this setting. <a href="%s" target="_blank">See here for more information</a>.', 'woocommerce-google-analytics-integration' ), 'https://support.google.com/analytics/answer/6032539?hl=en' ),
232
  'type' => 'checkbox',
233
  'checkboxgroup' => '',
234
+ 'default' => 'no',
235
+ 'class' => 'legacy-setting'
236
  ),
237
 
238
+ // Enhanced eCommerce Sub-Settings
239
 
240
  'ga_enhanced_remove_from_cart_enabled' => array(
241
  'label' => __( 'Remove from Cart Events', 'woocommerce-google-analytics-integration' ),
293
  /**
294
  * Hooks into woocommerce_tracker_data and tracks some of the analytic settings (just enabled|disabled status)
295
  * only if you have opted into WooCommerce tracking
296
+ * https://woocommerce.com/usage-tracking/
297
  *
298
+ * @param array $data Current WC tracker data.
299
+ * @return array Updated WC Tracker data.
 
300
  */
301
  function track_options( $data ) {
302
  $data['wc-google-analytics'] = array(
303
+ 'standard_tracking_enabled' => $this->ga_standard_tracking_enabled,
304
+ 'support_display_advertising' => $this->ga_support_display_advertising,
305
+ 'support_enhanced_link_attribution' => $this->ga_support_enhanced_link_attribution,
306
+ 'use_universal_analytics' => $this->ga_use_universal_analytics,
307
+ 'anonymize_enabled' => $this->ga_anonymize_enabled,
308
+ 'ga_404_tracking_enabled' => $this->ga_404_tracking_enabled,
309
+ 'ecommerce_tracking_enabled' => $this->ga_ecommerce_tracking_enabled,
310
+ 'event_tracking_enabled' => $this->ga_event_tracking_enabled,
311
+ 'gtag_enabled' => $this->ga_gtag_enabled,
312
+ 'set_domain_name' => empty( $this->ga_set_domain_name ) ? 'no' : 'yes',
313
+ 'plugin_version' => WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION,
314
+ 'enhanced_ecommerce_tracking_enabled' => $this->ga_enhanced_ecommerce_tracking_enabled,
315
  );
316
+
317
+ // ID prefix, blank, or X for unknown
318
+ $prefix = substr( strtoupper( $this->ga_id ), 0, 2 );
319
+ if ( 'UA' === $prefix || 'G-' === $prefix || empty( $prefix ) ) {
320
+ $data['wc-google-analytics']['ga_id'] = str_replace( '-', '', $prefix );
321
+ } else {
322
+ $data['wc-google-analytics']['ga_id'] = 'X';
323
+ }
324
+
325
  return $data;
326
  }
327
 
328
  /**
329
+ * Enqueue the admin JavaScript
330
  */
331
  function load_admin_assets() {
332
  $screen = get_current_screen();
342
  return;
343
  }
344
 
345
+ wp_enqueue_script( 'wc-google-analytics-admin-enhanced-settings', plugins_url( '/assets/js/admin-ga-settings.js', dirname( __FILE__ ) ), array(), WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION, true );
346
  }
347
 
348
  /**
377
  }
378
 
379
  /**
380
+ * Generate Standard Google Analytics tracking
381
  */
382
  protected function get_standard_tracking_code() {
383
  return "<!-- WooCommerce Google Analytics Integration -->
384
+ " . $this->get_tracking_instance()->header() .
385
+ $this->get_tracking_instance()->load_analytics() . "
386
  <!-- /WooCommerce Google Analytics Integration -->";
387
  }
388
 
389
  /**
390
+ * Generate eCommerce tracking code
391
  *
392
  * @param int $order_id
393
  * @return string
401
  return '';
402
  }
403
 
404
+ $load = $this->get_tracking_instance()->load_analytics( $order );
405
+ $code = $this->get_tracking_instance()->add_transaction( $order );
406
 
407
  // Mark the order as tracked.
408
  update_post_meta( $order_id, '_ga_tracked', 1 );
409
 
410
  return "
411
  <!-- WooCommerce Google Analytics Integration -->
412
+ " . $this->get_tracking_instance()->header() . "
413
+ " . $load . "
414
  <script type='text/javascript'>$code</script>
415
  <!-- /WooCommerce Google Analytics Integration -->
416
  ";
419
  /**
420
  * Check if tracking is disabled
421
  *
422
+ * @param string $type The setting to check
423
+ * @return bool True if tracking for a certain setting is disabled
 
424
  */
425
  private function disable_tracking( $type ) {
426
+ return is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' === $type || apply_filters( 'woocommerce_ga_disable_tracking', false, $type );
 
 
427
  }
428
 
429
  /**
430
  * Google Analytics event tracking for single product add to cart
 
 
431
  */
432
  public function add_to_cart() {
433
  if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
446
  $parameters['label'] = "'" . esc_js( $product->get_sku() ? __( 'ID:', 'woocommerce-google-analytics-integration' ) . ' ' . $product->get_sku() : "#" . $product->get_id() ) . "'";
447
 
448
  if ( ! $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
449
+ $item = "{";
450
+ $item .= "'id': '" . esc_js( $product->get_sku() ? $product->get_sku() : ( '#' . $product->get_id() ) ) . "',";
451
+ $item .= "'name': '" . esc_js( $product->get_title() ) . "',";
452
+ $item .= "'quantity': $( 'input.qty' ).val() ? $( 'input.qty' ).val() : '1'";
453
+ $item .= "}";
454
+ $parameters['item'] = $item;
455
+
456
+ $code = "" . $this->get_tracking_instance()->tracker_var() . "( 'ec:addProduct', " . $item . " );";
457
  $parameters['enhanced'] = $code;
458
  }
459
 
460
+ $this->get_tracking_instance()->event_tracking_code( $parameters, '.single_add_to_cart_button' );
461
  }
462
 
463
  /**
464
  * Enhanced Analytics event tracking for removing a product from the cart
465
  */
466
  public function remove_from_cart() {
 
 
 
467
 
468
+ if ( ! $this->enhanced_ecommerce_enabled( $this->ga_enhanced_remove_from_cart_enabled ) ) {
469
  return;
470
  }
471
 
472
+ $this->get_tracking_instance()->remove_from_cart();
 
 
 
 
473
  }
474
 
475
  /**
476
  * Adds the product ID and SKU to the remove product link if not present
477
  *
478
+ * @param string $url
479
+ * @param string $key
480
+ * @return string
 
481
  */
482
  public function remove_from_cart_attributes( $url, $key ) {
483
  if ( strpos( $url,'data-product_id' ) !== false ) {
501
 
502
  /**
503
  * Google Analytics event tracking for loop add to cart
 
 
504
  */
505
  public function loop_add_to_cart() {
506
  if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
514
  $parameters['label'] = "($(this).data('product_sku')) ? ($(this).data('product_sku')) : ('#' + $(this).data('product_id'))"; // Product SKU or ID
515
 
516
  if ( ! $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
517
+ $item = "{";
518
+ $item .= "'id': ($(this).data('product_sku')) ? ($(this).data('product_sku')) : ('#' + $(this).data('product_id')),";
519
+ $item .= "'quantity': $(this).data('quantity')";
520
+ $item .= "}";
521
+ $parameters['item'] = $item;
522
+
523
+ $code = "" . $this->get_tracking_instance()->tracker_var() . "( 'ec:addProduct', " . $item ." );";
524
  $parameters['enhanced'] = $code;
525
  }
526
 
527
+ $this->get_tracking_instance()->event_tracking_code( $parameters, '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' );
528
  }
529
 
530
  /**
531
+ * Determine if the conditions are met for enhanced ecommerce interactions to be displayed.
532
+ * Currently checks if Global Tags OR Universal Analytics are enabled, plus Enhanced eCommerce.
533
+ *
534
+ * @param array $extra_checks Any extra option values that should be 'yes' to proceed
535
+ * @return bool Whether enhanced ecommerce transactions can be displayed.
536
  */
537
+ protected function enhanced_ecommerce_enabled( $extra_checks = [] ) {
538
+ if ( !is_array( $extra_checks ) ) {
539
+ $extra_checks = [ $extra_checks ];
540
  }
541
 
542
+ // False if gtag and UA are disabled.
543
+ if ( $this->disable_tracking( $this->ga_use_universal_analytics ) && $this->disable_tracking( $this->ga_gtag_enabled ) ) {
544
+ return false;
545
+ }
546
+
547
+ // False if gtag or UA is enabled, but enhanced ecommerce is disabled.
548
  if ( $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
549
+ return false;
550
+ }
551
+
552
+ // False if any specified interaction-level checks are disabled.
553
+ foreach ( $extra_checks as $option_value ) {
554
+ if ( $this->disable_tracking( $option_value ) ) {
555
+ return false;
556
+ }
557
  }
558
 
559
+ return true;
560
+ }
561
+
562
+ /**
563
+ * Measures a listing impression (from search results)
564
+ */
565
+ public function listing_impression() {
566
+
567
+ if ( ! $this->enhanced_ecommerce_enabled( $this->ga_enhanced_product_impression_enabled ) ) {
568
  return;
569
  }
570
 
571
  global $product, $woocommerce_loop;
572
+ $this->get_tracking_instance()->listing_impression( $product, $woocommerce_loop['loop'] );
573
  }
574
 
575
  /**
576
  * Measure a product click from a listing page
577
  */
578
  public function listing_click() {
 
 
 
579
 
580
+ if ( ! $this->enhanced_ecommerce_enabled( $this->ga_enhanced_product_click_enabled ) ) {
 
 
 
 
581
  return;
582
  }
583
 
584
  global $product, $woocommerce_loop;
585
+ $this->get_tracking_instance()->listing_click( $product, $woocommerce_loop['loop'] );
586
  }
587
 
588
  /**
589
  * Measure a product detail view
590
  */
591
  public function product_detail() {
 
 
 
592
 
593
+ if ( ! $this->enhanced_ecommerce_enabled( $this->ga_enhanced_product_detail_view_enabled ) ) {
 
 
 
 
594
  return;
595
  }
596
 
597
  global $product;
598
+ $this->get_tracking_instance()->product_detail( $product );
599
  }
600
 
601
  /**
602
  * Tracks when the checkout form is loaded
603
  *
604
+ * @param mixed $checkout (unused)
605
  */
606
  public function checkout_process( $checkout ) {
 
 
 
607
 
608
+ if ( ! $this->enhanced_ecommerce_enabled( $this->ga_enhanced_checkout_process_enabled ) ) {
609
  return;
610
  }
611
 
612
+ $this->get_tracking_instance()->checkout_process( WC()->cart->get_cart() );
 
 
 
 
613
  }
614
 
615
  /**
616
  * Add the utm_nooverride parameter to any return urls. This makes sure Google Adwords doesn't mistake the offsite gateway as the referrer.
617
  *
618
  * @param string $return_url WooCommerce Return URL
 
619
  * @return string URL
620
  */
621
  public function utm_nooverride( $return_url ) {
includes/class-wc-google-gtag-js.php ADDED
@@ -0,0 +1,313 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'ABSPATH' ) ) {
3
+ exit;
4
+ }
5
+
6
+ /**
7
+ * WC_Google_Gtag_JS class
8
+ *
9
+ * JS for recording Google Gtag info
10
+ */
11
+ class WC_Google_Gtag_JS extends WC_Abstract_Google_Analytics_JS {
12
+
13
+ /**
14
+ * Get the class instance
15
+ *
16
+ * @param array $options Options
17
+ * @return WC_Abstract_Google_Analytics_JS
18
+ */
19
+ public static function get_instance( $options = array() ) {
20
+ return null === self::$instance ? ( self::$instance = new self( $options ) ) : self::$instance;
21
+ }
22
+
23
+ /**
24
+ * Constructor
25
+ * Takes our options from the parent class so we can later use them in the JS snippets
26
+ *
27
+ * @param array $options Options
28
+ */
29
+ public function __construct( $options = array() ) {
30
+ self::$options = $options;
31
+ }
32
+
33
+ /**
34
+ * Returns the tracker variable this integration should use
35
+ *
36
+ * @return string
37
+ */
38
+ public static function tracker_var() {
39
+ return apply_filters( 'woocommerce_gtag_tracker_variable', 'gtag' );
40
+ }
41
+
42
+ /**
43
+ * Enqueues JavaScript to build the addImpression event
44
+ *
45
+ * @param WC_Product $product
46
+ * @param int $position
47
+ */
48
+ public static function listing_impression( $product, $position ) {
49
+ if ( isset( $_GET['s'] ) ) {
50
+ $list = "Search Results";
51
+ } else {
52
+ $list = "Product List";
53
+ }
54
+
55
+ wc_enqueue_js( "
56
+ " . self::tracker_var() . "( 'event', 'view_item_list', { 'items': [ {
57
+ 'id': '" . esc_js( $product->get_id() ) . "',
58
+ 'name': '" . esc_js( $product->get_title() ) . "',
59
+ 'category': " . self::product_get_category_line( $product ) . "
60
+ 'list': '" . esc_js( $list ) . "',
61
+ 'list_position': '" . esc_js( $position ) . "'
62
+ } ] } );
63
+ " );
64
+ }
65
+
66
+ /**
67
+ * Enqueues JavaScript to build an addProduct and click event
68
+ *
69
+ * @param WC_Product $product
70
+ * @param int $position
71
+ */
72
+ public static function listing_click( $product, $position ) {
73
+ if ( isset( $_GET['s'] ) ) {
74
+ $list = "Search Results";
75
+ } else {
76
+ $list = "Product List";
77
+ }
78
+
79
+ wc_enqueue_js( "
80
+ $( '.products .post-" . esc_js( $product->get_id() ) . " a' ).on('click', function() {
81
+ if ( true === $(this).hasClass( 'add_to_cart_button' ) ) {
82
+ return;
83
+ }
84
+ " . self::tracker_var() . "( 'event', 'select_content', {
85
+ 'content_type': 'product',
86
+ 'items': [ {
87
+ 'id': '" . esc_js( $product->get_id() ) . "',
88
+ 'name': '" . esc_js( $product->get_title() ) . "',
89
+ 'category': " . self::product_get_category_line( $product ) . "
90
+ 'list_position': '" . esc_js( $position ) . "'
91
+ } ],
92
+ } );
93
+ });
94
+ " );
95
+ }
96
+
97
+ /**
98
+ * Loads the standard Gtag code
99
+ *
100
+ * @param boolean|WC_Order $order Classic analytics needs order data to set the currency correctly
101
+ * @return string Gtag loading code
102
+ */
103
+ public static function load_analytics( $order = false ) {
104
+ $logged_in = is_user_logged_in() ? 'yes' : 'no';
105
+
106
+ $track_404_enabled = '';
107
+ if ( 'yes' === self::get( 'ga_404_tracking_enabled' ) && is_404() ) {
108
+ // See https://developers.google.com/analytics/devguides/collection/gtagjs/events for reference
109
+ $track_404_enabled = self::tracker_var() . "( 'event', '404_not_found', { 'event_category':'error', 'event_label':'page: ' + document.location.pathname + document.location.search + ' referrer: ' + document.referrer });";
110
+ }
111
+
112
+ $gtag_developer_id = '';
113
+ if ( ! empty( self::DEVELOPER_ID ) ) {
114
+ $gtag_developer_id = "gtag('set', 'developer_id." . self::DEVELOPER_ID . "', true);";
115
+ }
116
+
117
+ $gtag_id = self::get( 'ga_id' );
118
+ $gtag_snippet = '<script async src="https://www.googletagmanager.com/gtag/js?id=' . esc_js( $gtag_id ) . '"></script>';
119
+ $gtag_snippet .= "
120
+ <script>
121
+ window.dataLayer = window.dataLayer || [];
122
+ function gtag(){dataLayer.push(arguments);}
123
+ gtag('js', new Date());
124
+ $gtag_developer_id
125
+
126
+ gtag('config', '" . esc_js( $gtag_id ) . "', {
127
+ 'allow_google_signals': " . ( 'yes' === self::get( 'ga_support_display_advertising' ) ? 'true' : 'false' ) . ",
128
+ 'link_attribution': " . ( 'yes' === self::get( 'ga_support_enhanced_link_attribution' ) ? 'true' : 'false' ) . ",
129
+ 'anonymize_ip': " . ( 'yes' === self::get( 'ga_anonymize_enabled' ) ? 'true' : 'false' ) . ",
130
+ 'custom_map': {
131
+ 'dimension1': 'logged_in'
132
+ },
133
+ 'logged_in': '$logged_in'
134
+ } );
135
+
136
+ $track_404_enabled
137
+ </script>
138
+ ";
139
+
140
+ $gtag_snippet = apply_filters( 'woocommerce_gtag_snippet' , $gtag_snippet );
141
+
142
+ return $gtag_snippet;
143
+ }
144
+
145
+ /**
146
+ * Generate Gtag transaction tracking code
147
+ *
148
+ * @param WC_Order $order
149
+ * @return string
150
+ */
151
+ protected function add_transaction_enhanced( $order ) {
152
+ // Order items
153
+ $items = "[";
154
+ if ( $order->get_items() ) {
155
+ foreach ( $order->get_items() as $item ) {
156
+ $items .= self::add_item( $order, $item );
157
+ }
158
+ }
159
+ $items .= "]";
160
+
161
+ $code = "" . self::tracker_var() . "( 'event', 'purchase', {
162
+ 'transaction_id': '" . esc_js( $order->get_order_number() ) . "',
163
+ 'affiliation': '" . esc_js( get_bloginfo( 'name' ) ) . "',
164
+ 'value': '" . esc_js( $order->get_total() ) . "',
165
+ 'tax': '" . esc_js( $order->get_total_tax() ) . "',
166
+ 'shipping': '" . esc_js( $order->get_total_shipping() ) . "',
167
+ 'currency': '" . esc_js( version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_order_currency() : $order->get_currency() ) . "', // Currency,
168
+ 'items': " . $items . ",
169
+ } );";
170
+
171
+ return $code;
172
+ }
173
+
174
+ /**
175
+ * Add Item
176
+ *
177
+ * @param WC_Order $order WC_Order Object
178
+ * @param WC_Order_Item $item The item to add to a transaction/order
179
+ */
180
+ protected function add_item( $order, $item ) {
181
+ $_product = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_product_from_item( $item ) : $item->get_product();
182
+ $variant = self::product_get_variant_line( $_product );
183
+
184
+ $code = "{";
185
+ $code .= "'id': '" . esc_js( $_product->get_sku() ? $_product->get_sku() : $_product->get_id() ) . "',";
186
+ $code .= "'name': '" . esc_js( $item['name'] ) . "',";
187
+ $code .= "'category': " . self::product_get_category_line( $_product );
188
+
189
+ if ( '' !== $variant ) {
190
+ $code .= "'variant': " . $variant;
191
+ }
192
+
193
+ $code .= "'price': '" . esc_js( $order->get_item_total( $item ) ) . "',";
194
+ $code .= "'quantity': '" . esc_js( $item['qty'] ) . "'";
195
+ $code .= "},";
196
+
197
+ return $code;
198
+ }
199
+
200
+ /**
201
+ * Output JavaScript to track an enhanced ecommerce remove from cart action
202
+ */
203
+ public function remove_from_cart() {
204
+ echo( "
205
+ <script>
206
+ (function($) {
207
+ $( '.remove' ).off('click', '.remove').on( 'click', function() {
208
+ " . self::tracker_var() . "( 'event', 'remove_from_cart', {
209
+ 'items': [ {
210
+ 'id': ($(this).data('product_sku')) ? ($(this).data('product_sku')) : ('#' + $(this).data('product_id')),
211
+ 'quantity': $(this).parent().parent().find( '.qty' ).val() ? $(this).parent().parent().find( '.qty' ).val() : '1',
212
+ } ]
213
+ } );
214
+ });
215
+ })(jQuery);
216
+ </script>
217
+ " );
218
+ }
219
+
220
+ /**
221
+ * Enqueue JavaScript to track a product detail view
222
+ *
223
+ * @param WC_Product $product
224
+ */
225
+ public function product_detail( $product ) {
226
+ if ( empty( $product ) ) {
227
+ return;
228
+ }
229
+
230
+ wc_enqueue_js( "
231
+ " . self::tracker_var() . "( 'event', 'view_item', {
232
+ 'items': [ {
233
+ 'id': '" . esc_js( $product->get_sku() ? $product->get_sku() : ( '#' . $product->get_id() ) ) . "',
234
+ 'name': '" . esc_js( $product->get_title() ) . "',
235
+ 'category': " . self::product_get_category_line( $product ) . "
236
+ 'price': '" . esc_js( $product->get_price() ) . "',
237
+ } ]
238
+ } );" );
239
+ }
240
+
241
+ /**
242
+ * Enqueue JS to track when the checkout process is started
243
+ *
244
+ * @param array $cart items/contents of the cart
245
+ */
246
+ public function checkout_process( $cart ) {
247
+ $items = "[";
248
+
249
+ foreach ( $cart as $cart_item_key => $cart_item ) {
250
+ $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
251
+
252
+ $items .= "
253
+ {
254
+ 'id': '" . esc_js( $product->get_sku() ? $product->get_sku() : ( '#' . $product->get_id() ) ) . "',
255
+ 'name': '" . esc_js( $product->get_title() ) . "',
256
+ 'category': " . self::product_get_category_line( $product );
257
+
258
+ $variant = self::product_get_variant_line( $product );
259
+ if ( '' !== $variant ) {
260
+ $items .= "
261
+ 'variant': " . $variant;
262
+ }
263
+
264
+ $items .= "
265
+ 'price': '" . esc_js( $product->get_price() ) . "',
266
+ 'quantity': '" . esc_js( $cart_item['quantity'] ) . "'
267
+ },";
268
+ }
269
+
270
+ $items .= '
271
+ ]';
272
+
273
+ $code = "" . self::tracker_var() . "( 'event', 'begin_checkout', {
274
+ 'items': " . $items . ",
275
+ } );";
276
+
277
+ wc_enqueue_js( $code );
278
+ }
279
+
280
+ /**
281
+ * Enqueue JavaScript for Add to cart tracking
282
+ *
283
+ * @param array $parameters associative array of _trackEvent parameters
284
+ * @param string $selector jQuery selector for binding click event
285
+ */
286
+ public function event_tracking_code( $parameters, $selector ) {
287
+
288
+ // Called with invalid 'Add to Cart' action, update to sync with Default Google Analytics Event 'add_to_cart'
289
+ $parameters['action'] = '\'add_to_cart\'';
290
+ $parameters['category'] = '\'ecommerce\'';
291
+
292
+ $parameters = apply_filters( 'woocommerce_gtag_event_tracking_parameters', $parameters );
293
+
294
+ if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) ) {
295
+ $track_event = sprintf(
296
+ self::tracker_var() . "( 'event', %s, { 'event_category': %s, 'event_label': %s, 'items': [ %s ] } );",
297
+ $parameters['action'], $parameters['category'], $parameters['label'], $parameters['item']
298
+ );
299
+ } else {
300
+ $track_event = sprintf(
301
+ self::tracker_var() . "( 'event', %s, { 'event_category': %s, 'event_label': %s } );",
302
+ $parameters['action'], $parameters['category'], $parameters['label']
303
+ );
304
+ }
305
+
306
+ wc_enqueue_js( "
307
+ $( '" . $selector . "' ).on( 'click', function() {
308
+ " . $track_event . "
309
+ });
310
+ " );
311
+ }
312
+
313
+ }
languages/woocommerce-google-analytics-integration.pot CHANGED
@@ -2,11 +2,11 @@
2
  # This file is distributed under the GPLv2 or later.
3
  msgid ""
4
  msgstr ""
5
- "Project-Id-Version: WooCommerce Google Analytics Integration 1.4.25\n"
6
  "Report-Msgid-Bugs-To: "
7
  "https://wordpress.org/support/plugin/woocommerce-google-analytics-"
8
  "integration\n"
9
- "POT-Creation-Date: 2020-11-25 04:45:18+00:00\n"
10
  "MIME-Version: 1.0\n"
11
  "Content-Type: text/plain; charset=utf-8\n"
12
  "Content-Transfer-Encoding: 8bit\n"
@@ -15,117 +15,130 @@ msgstr ""
15
  "Language-Team: LANGUAGE <LL@li.org>\n"
16
  "X-Generator: node-wp-i18n 1.2.3\n"
17
 
18
- #: includes/class-wc-google-analytics-info-banner.php:57
19
  msgid "Google Analytics &amp; WooCommerce"
20
  msgstr ""
21
 
22
- #: includes/class-wc-google-analytics-info-banner.php:58
23
  msgid ""
24
  "<a href=\"%s\">Connect WooCommerce to Google Analytics</a> to finish "
25
  "setting up this integration."
26
  msgstr ""
27
 
28
- #: includes/class-wc-google-analytics-info-banner.php:62
29
  msgid "Dismiss this notice."
30
  msgstr ""
31
 
32
- #: includes/class-wc-google-analytics-info-banner.php:62
33
  msgid "(Dismiss)"
34
  msgstr ""
35
 
36
- #: includes/class-wc-google-analytics.php:23
37
  msgid "Google Analytics"
38
  msgstr ""
39
 
40
- #: includes/class-wc-google-analytics.php:24
41
  msgid ""
42
  "Google Analytics is a free service offered by Google that generates "
43
  "detailed statistics about the visitors to a website."
44
  msgstr ""
45
 
46
- #: includes/class-wc-google-analytics.php:104
47
- msgid "Google Analytics ID"
48
  msgstr ""
49
 
50
- #: includes/class-wc-google-analytics.php:105
51
  msgid ""
52
  "Log into your Google Analytics account to find your ID. e.g. "
53
- "<code>UA-XXXXX-X</code>"
54
  msgstr ""
55
 
56
- #: includes/class-wc-google-analytics.php:111
57
  msgid "Set Domain Name"
58
  msgstr ""
59
 
60
- #: includes/class-wc-google-analytics.php:112
61
  msgid ""
62
  "(Optional) Sets the <code>_setDomainName</code> variable. <a href=\"%s\" "
63
  "target=\"_blank\">See here for more information</a>."
64
  msgstr ""
65
 
66
- #: includes/class-wc-google-analytics.php:117
67
  msgid "Tracking Options"
68
  msgstr ""
69
 
70
- #: includes/class-wc-google-analytics.php:118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  msgid "Enable Standard Tracking"
72
  msgstr ""
73
 
74
- #: includes/class-wc-google-analytics.php:119
75
  msgid ""
76
  "This tracks session data such as demographics, system, etc. You don't need "
77
  "to enable this if you are using a 3rd party Google analytics plugin."
78
  msgstr ""
79
 
80
- #: includes/class-wc-google-analytics.php:125
81
  msgid "\"Display Advertising\" Support"
82
  msgstr ""
83
 
84
- #: includes/class-wc-google-analytics.php:126
85
  msgid ""
86
  "Set the Google Analytics code to support Display Advertising. %sRead more "
87
  "about Display Advertising%s."
88
  msgstr ""
89
 
90
- #: includes/class-wc-google-analytics.php:132
91
  msgid "Use Enhanced Link Attribution"
92
  msgstr ""
93
 
94
- #: includes/class-wc-google-analytics.php:133
95
  msgid ""
96
  "Set the Google Analytics code to support Enhanced Link Attribution. %sRead "
97
  "more about Enhanced Link Attribution%s."
98
  msgstr ""
99
 
100
- #: includes/class-wc-google-analytics.php:139
101
- msgid "Enable Universal Analytics"
102
- msgstr ""
103
-
104
- #: includes/class-wc-google-analytics.php:140
105
- msgid ""
106
- "Uses Universal Analytics instead of Classic Google Analytics. If you have "
107
- "<strong>not</strong> previously used Google Analytics on this site, check "
108
- "this box. Otherwise, %sfollow step 1 of the Universal Analytics upgrade "
109
- "guide.%s Enabling this setting will take care of step 2. %sRead more about "
110
- "Universal Analytics%s. Universal Analytics must be enabled to enable "
111
- "enhanced eCommerce."
112
- msgstr ""
113
-
114
- #: includes/class-wc-google-analytics.php:146
115
- msgid "Anonymize IP addresses."
116
  msgstr ""
117
 
118
- #: includes/class-wc-google-analytics.php:147
119
  msgid ""
120
  "Enabling this option is mandatory in certain countries due to national "
121
  "privacy laws. %sRead more about IP Anonymization%s."
122
  msgstr ""
123
 
124
- #: includes/class-wc-google-analytics.php:153
125
- msgid "Track 404 (Not found) Errors."
126
  msgstr ""
127
 
128
- #: includes/class-wc-google-analytics.php:154
129
  msgid ""
130
  "Enable this to find broken or dead links. An \"Event\" with category "
131
  "\"Error\" and action \"404 Not Found\" will be created in Google Analytics "
@@ -134,93 +147,94 @@ msgid ""
134
  "broken links originated from (the referrer). %sRead how to set up a goal%s."
135
  msgstr ""
136
 
137
- #: includes/class-wc-google-analytics.php:160
138
  msgid "Purchase Transactions"
139
  msgstr ""
140
 
141
- #: includes/class-wc-google-analytics.php:161
142
  msgid ""
143
  "This requires a payment gateway that redirects to the thank you/order "
144
  "received page after payment. Orders paid with gateways which do not do this "
145
  "will not be tracked."
146
  msgstr ""
147
 
148
- #: includes/class-wc-google-analytics.php:167
149
  msgid "Add to Cart Events"
150
  msgstr ""
151
 
152
- #: includes/class-wc-google-analytics.php:174
153
  msgid "Enhanced eCommerce"
154
  msgstr ""
155
 
156
- #: includes/class-wc-google-analytics.php:175
157
  msgid "Enable Enhanced eCommerce "
158
  msgstr ""
159
 
160
- #: includes/class-wc-google-analytics.php:176
161
  msgid ""
162
  "Enhanced eCommerce allows you to measure more user interactions with your "
163
  "store, including: product impressions, product detail views, starting the "
164
  "checkout process, adding cart items, and removing cart items. Universal "
165
- "Analytics must be enabled for Enhanced eCommerce to work. Before enabling "
166
- "this setting, turn on Enhanced eCommerce in your Google Analytics "
167
- "dashboard. <a href=\"%s\">See here for more information</a>."
 
168
  msgstr ""
169
 
170
- #: includes/class-wc-google-analytics.php:185
171
  msgid "Remove from Cart Events"
172
  msgstr ""
173
 
174
- #: includes/class-wc-google-analytics.php:193
175
  msgid "Product Impressions from Listing Pages"
176
  msgstr ""
177
 
178
- #: includes/class-wc-google-analytics.php:201
179
  msgid "Product Clicks from Listing Pages"
180
  msgstr ""
181
 
182
- #: includes/class-wc-google-analytics.php:209
183
  msgid "Product Detail Views"
184
  msgstr ""
185
 
186
- #: includes/class-wc-google-analytics.php:217
187
  msgid "Checkout Process Initiated"
188
  msgstr ""
189
 
190
- #: includes/class-wc-google-analytics.php:230
191
  msgid "Please allow Google Analytics 24 hours to start displaying results."
192
  msgstr ""
193
 
194
- #: includes/class-wc-google-analytics.php:233
195
  msgid ""
196
  "Please note, for transaction tracking to work properly, you will need to "
197
  "use a payment gateway that redirects the customer back to a WooCommerce "
198
  "order received/thank you page."
199
  msgstr ""
200
 
201
- #: includes/class-wc-google-analytics.php:380
202
- #: includes/class-wc-google-analytics.php:455
203
  msgid "Products"
204
  msgstr ""
205
 
206
- #: includes/class-wc-google-analytics.php:381
207
- #: includes/class-wc-google-analytics.php:456
208
  msgid "Add to Cart"
209
  msgstr ""
210
 
211
- #: includes/class-wc-google-analytics.php:382
212
  msgid "ID:"
213
  msgstr ""
214
 
215
- #: woocommerce-google-analytics-integration.php:72
216
  msgid "Settings"
217
  msgstr ""
218
 
219
- #: woocommerce-google-analytics-integration.php:73
220
  msgid "Support"
221
  msgstr ""
222
 
223
- #: woocommerce-google-analytics-integration.php:109
224
  msgid "WooCommerce Google Analytics depends on the last version of %s to work!"
225
  msgstr ""
226
 
2
  # This file is distributed under the GPLv2 or later.
3
  msgid ""
4
  msgstr ""
5
+ "Project-Id-Version: WooCommerce Google Analytics Integration 1.5.0\n"
6
  "Report-Msgid-Bugs-To: "
7
  "https://wordpress.org/support/plugin/woocommerce-google-analytics-"
8
  "integration\n"
9
+ "POT-Creation-Date: 2020-12-17 15:40:15+00:00\n"
10
  "MIME-Version: 1.0\n"
11
  "Content-Type: text/plain; charset=utf-8\n"
12
  "Content-Transfer-Encoding: 8bit\n"
15
  "Language-Team: LANGUAGE <LL@li.org>\n"
16
  "X-Generator: node-wp-i18n 1.2.3\n"
17
 
18
+ #: includes/class-wc-google-analytics-info-banner.php:64
19
  msgid "Google Analytics &amp; WooCommerce"
20
  msgstr ""
21
 
22
+ #: includes/class-wc-google-analytics-info-banner.php:65
23
  msgid ""
24
  "<a href=\"%s\">Connect WooCommerce to Google Analytics</a> to finish "
25
  "setting up this integration."
26
  msgstr ""
27
 
28
+ #: includes/class-wc-google-analytics-info-banner.php:69
29
  msgid "Dismiss this notice."
30
  msgstr ""
31
 
32
+ #: includes/class-wc-google-analytics-info-banner.php:69
33
  msgid "(Dismiss)"
34
  msgstr ""
35
 
36
+ #: includes/class-wc-google-analytics.php:54
37
  msgid "Google Analytics"
38
  msgstr ""
39
 
40
+ #: includes/class-wc-google-analytics.php:55
41
  msgid ""
42
  "Google Analytics is a free service offered by Google that generates "
43
  "detailed statistics about the visitors to a website."
44
  msgstr ""
45
 
46
+ #: includes/class-wc-google-analytics.php:148
47
+ msgid "Google Analytics Tracking ID"
48
  msgstr ""
49
 
50
+ #: includes/class-wc-google-analytics.php:149
51
  msgid ""
52
  "Log into your Google Analytics account to find your ID. e.g. "
53
+ "<code>G-XXXXX</code> or <code>UA-XXXXX-X</code>"
54
  msgstr ""
55
 
56
+ #: includes/class-wc-google-analytics.php:155
57
  msgid "Set Domain Name"
58
  msgstr ""
59
 
60
+ #: includes/class-wc-google-analytics.php:156
61
  msgid ""
62
  "(Optional) Sets the <code>_setDomainName</code> variable. <a href=\"%s\" "
63
  "target=\"_blank\">See here for more information</a>."
64
  msgstr ""
65
 
66
+ #: includes/class-wc-google-analytics.php:163
67
  msgid "Tracking Options"
68
  msgstr ""
69
 
70
+ #: includes/class-wc-google-analytics.php:164
71
+ msgid "Use Global Site Tag"
72
+ msgstr ""
73
+
74
+ #: includes/class-wc-google-analytics.php:165
75
+ msgid ""
76
+ "The Global Site Tag provides streamlined tagging across Google’s site "
77
+ "measurement, conversion tracking, and remarketing products. This must be "
78
+ "enabled to use a Google Analytics 4 Measurement ID (e.g., "
79
+ "<code>G-XXXXX</code>). <a href=\"%s\" target=\"_blank\">See here for more "
80
+ "information</a>."
81
+ msgstr ""
82
+
83
+ #: includes/class-wc-google-analytics.php:172
84
+ msgid "Enable Universal Analytics"
85
+ msgstr ""
86
+
87
+ #: includes/class-wc-google-analytics.php:173
88
+ msgid ""
89
+ "Uses Universal Analytics instead of Classic Google Analytics. If you have "
90
+ "<strong>not</strong> previously used Google Analytics on this site, check "
91
+ "this box. Otherwise, %sfollow step 1 of the Universal Analytics upgrade "
92
+ "guide.%s Enabling this setting will take care of step 2. %sRead more about "
93
+ "Universal Analytics%s. Universal Analytics or Global Site Tag must be "
94
+ "enabled to enable enhanced eCommerce."
95
+ msgstr ""
96
+
97
+ #: includes/class-wc-google-analytics.php:180
98
  msgid "Enable Standard Tracking"
99
  msgstr ""
100
 
101
+ #: includes/class-wc-google-analytics.php:181
102
  msgid ""
103
  "This tracks session data such as demographics, system, etc. You don't need "
104
  "to enable this if you are using a 3rd party Google analytics plugin."
105
  msgstr ""
106
 
107
+ #: includes/class-wc-google-analytics.php:187
108
  msgid "\"Display Advertising\" Support"
109
  msgstr ""
110
 
111
+ #: includes/class-wc-google-analytics.php:188
112
  msgid ""
113
  "Set the Google Analytics code to support Display Advertising. %sRead more "
114
  "about Display Advertising%s."
115
  msgstr ""
116
 
117
+ #: includes/class-wc-google-analytics.php:194
118
  msgid "Use Enhanced Link Attribution"
119
  msgstr ""
120
 
121
+ #: includes/class-wc-google-analytics.php:195
122
  msgid ""
123
  "Set the Google Analytics code to support Enhanced Link Attribution. %sRead "
124
  "more about Enhanced Link Attribution%s."
125
  msgstr ""
126
 
127
+ #: includes/class-wc-google-analytics.php:201
128
+ msgid "Anonymize IP addresses"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  msgstr ""
130
 
131
+ #: includes/class-wc-google-analytics.php:202
132
  msgid ""
133
  "Enabling this option is mandatory in certain countries due to national "
134
  "privacy laws. %sRead more about IP Anonymization%s."
135
  msgstr ""
136
 
137
+ #: includes/class-wc-google-analytics.php:208
138
+ msgid "Track 404 (Not found) Errors"
139
  msgstr ""
140
 
141
+ #: includes/class-wc-google-analytics.php:209
142
  msgid ""
143
  "Enable this to find broken or dead links. An \"Event\" with category "
144
  "\"Error\" and action \"404 Not Found\" will be created in Google Analytics "
147
  "broken links originated from (the referrer). %sRead how to set up a goal%s."
148
  msgstr ""
149
 
150
+ #: includes/class-wc-google-analytics.php:215
151
  msgid "Purchase Transactions"
152
  msgstr ""
153
 
154
+ #: includes/class-wc-google-analytics.php:216
155
  msgid ""
156
  "This requires a payment gateway that redirects to the thank you/order "
157
  "received page after payment. Orders paid with gateways which do not do this "
158
  "will not be tracked."
159
  msgstr ""
160
 
161
+ #: includes/class-wc-google-analytics.php:222
162
  msgid "Add to Cart Events"
163
  msgstr ""
164
 
165
+ #: includes/class-wc-google-analytics.php:229
166
  msgid "Enhanced eCommerce"
167
  msgstr ""
168
 
169
+ #: includes/class-wc-google-analytics.php:230
170
  msgid "Enable Enhanced eCommerce "
171
  msgstr ""
172
 
173
+ #: includes/class-wc-google-analytics.php:231
174
  msgid ""
175
  "Enhanced eCommerce allows you to measure more user interactions with your "
176
  "store, including: product impressions, product detail views, starting the "
177
  "checkout process, adding cart items, and removing cart items. Universal "
178
+ "Analytics or Global Site Tag must be enabled for Enhanced eCommerce to "
179
+ "work. If using Universal Analytics, turn on Enhanced eCommerce in your "
180
+ "Google Analytics dashboard before enabling this setting. <a href=\"%s\" "
181
+ "target=\"_blank\">See here for more information</a>."
182
  msgstr ""
183
 
184
+ #: includes/class-wc-google-analytics.php:241
185
  msgid "Remove from Cart Events"
186
  msgstr ""
187
 
188
+ #: includes/class-wc-google-analytics.php:249
189
  msgid "Product Impressions from Listing Pages"
190
  msgstr ""
191
 
192
+ #: includes/class-wc-google-analytics.php:257
193
  msgid "Product Clicks from Listing Pages"
194
  msgstr ""
195
 
196
+ #: includes/class-wc-google-analytics.php:265
197
  msgid "Product Detail Views"
198
  msgstr ""
199
 
200
+ #: includes/class-wc-google-analytics.php:273
201
  msgid "Checkout Process Initiated"
202
  msgstr ""
203
 
204
+ #: includes/class-wc-google-analytics.php:286
205
  msgid "Please allow Google Analytics 24 hours to start displaying results."
206
  msgstr ""
207
 
208
+ #: includes/class-wc-google-analytics.php:289
209
  msgid ""
210
  "Please note, for transaction tracking to work properly, you will need to "
211
  "use a payment gateway that redirects the customer back to a WooCommerce "
212
  "order received/thank you page."
213
  msgstr ""
214
 
215
+ #: includes/class-wc-google-analytics.php:444
216
+ #: includes/class-wc-google-analytics.php:512
217
  msgid "Products"
218
  msgstr ""
219
 
220
+ #: includes/class-wc-google-analytics.php:445
221
+ #: includes/class-wc-google-analytics.php:513
222
  msgid "Add to Cart"
223
  msgstr ""
224
 
225
+ #: includes/class-wc-google-analytics.php:446
226
  msgid "ID:"
227
  msgstr ""
228
 
229
+ #: woocommerce-google-analytics-integration.php:75
230
  msgid "Settings"
231
  msgstr ""
232
 
233
+ #: woocommerce-google-analytics-integration.php:76
234
  msgid "Support"
235
  msgstr ""
236
 
237
+ #: woocommerce-google-analytics-integration.php:110
238
  msgid "WooCommerce Google Analytics depends on the last version of %s to work!"
239
  msgstr ""
240
 
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: woocommerce, automattic, claudiosanches, bor0, royho, laurendavissmith001, c-shultz
3
  Tags: woocommerce, google analytics
4
  Requires at least: 3.9
5
- Tested up to: 5.5
6
- Stable tag: 1.4.25
7
  License: GPLv3
8
  License URI: https://www.gnu.org/licenses/gpl-3.0.html
9
 
@@ -11,44 +11,36 @@ Provides integration between Google Analytics and WooCommerce.
11
 
12
  == Description ==
13
 
14
- This plugin provides the integration between Google Analytics and the WooCommerce plugin. You can link a referral to a purchase and add transaction information to your Google Analytics data. It also supports the new Universal Analytics, eCommerce, and enhanced eCommerce event tracking.
15
 
16
- Starting WooCommerce 2.1, this integration will no longer be part of WooCommerce and will only be available by using this plugin.
17
 
18
- Contributions are welcome via the [GitHub repository](https://github.com/woothemes/woocommerce-google-analytics-integration).
19
 
20
  == Installation ==
21
 
22
  1. Download the plugin file to your computer and unzip it
23
  2. Using an FTP program, or your hosting control panel, upload the unzipped plugin folder to your WordPress installation’s wp-content/plugins/ directory.
24
  3. Activate the plugin from the Plugins menu within the WordPress admin.
25
- 4. Don't forget to enable e-commerce tracking in your Google Analytics account: https://support.google.com/analytics/answer/1009612?hl=en
26
 
27
- Or use the automatic installation wizard through your admin panel, just search for this plugins name.
28
 
29
  == Frequently Asked Questions ==
30
 
31
  = Where can I find the setting for this plugin? =
32
 
33
- This plugin will add the settings to the Integration tab, to be found in the WooCommerce > Settings menu.
34
 
35
  = I don't see the code on my site. Where is it? =
36
 
37
- We purposefully don't track admin visits to the site. Log out of the site (or open a Google Chrome Incognito window) and check if the site is there for non-admins.
38
 
39
- Also please make sure your Google Analytics ID under WooCommerce -> Settings -> Integrations.
40
 
41
  = My code is there. Why is it still not tracking sales? =
42
 
43
- Duplicate Google Analytics code causes a conflict in tracking. Remove any other Google Analytics plugin or code from your site to avoid duplication and conflicts in tracking.
44
-
45
- = Can I install it already? =
46
-
47
- Starting the WooCommerce 2.1 release, the Google Analytics integration for WooCommerce is no longer part of the WooCommerce plugin.
48
-
49
- Until you've updated to WooCommerce 2.1, this plugin puts itself in some sort of hibernate mode.
50
-
51
- You can leave this plugin activated and it will seamlessly take over the integration that once was in the WooCommerce plugin, once you update to the next version.
52
 
53
  = My settings are not saving! =
54
 
@@ -68,6 +60,15 @@ Exact wording depends on the national data privacy laws and should be adjusted.
68
 
69
  == Changelog ==
70
 
 
 
 
 
 
 
 
 
 
71
  = 1.4.25 - 2020-11-25 =
72
  * Tweak - WC 4.7 compatibility.
73
  * Tweak - WordPress 5.6 compatibility.
2
  Contributors: woocommerce, automattic, claudiosanches, bor0, royho, laurendavissmith001, c-shultz
3
  Tags: woocommerce, google analytics
4
  Requires at least: 3.9
5
+ Tested up to: 5.6
6
+ Stable tag: 1.5.0
7
  License: GPLv3
8
  License URI: https://www.gnu.org/licenses/gpl-3.0.html
9
 
11
 
12
  == Description ==
13
 
14
+ This plugin provides the integration between Google Analytics and the WooCommerce plugin. You can link a referral to a purchase and add transaction information to your Google Analytics data. It also supports Global Site Tag, Universal Analytics, eCommerce, and enhanced eCommerce event tracking.
15
 
16
+ Starting from WooCommerce 2.1, this integration is no packaged with WooCommerce and is only available by using this plugin.
17
 
18
+ Contributions are welcome via the [GitHub repository](https://github.com/woocommerce/woocommerce-google-analytics-integration).
19
 
20
  == Installation ==
21
 
22
  1. Download the plugin file to your computer and unzip it
23
  2. Using an FTP program, or your hosting control panel, upload the unzipped plugin folder to your WordPress installation’s wp-content/plugins/ directory.
24
  3. Activate the plugin from the Plugins menu within the WordPress admin.
25
+ 4. Don't forget to enable e-commerce tracking in your Google Analytics account: [https://support.google.com/analytics/answer/1009612?hl=en](https://support.google.com/analytics/answer/1009612?hl=en)
26
 
27
+ Or use the automatic installation wizard through your admin panel, just search for this plugin's name.
28
 
29
  == Frequently Asked Questions ==
30
 
31
  = Where can I find the setting for this plugin? =
32
 
33
+ This plugin will add the settings to the Integration tab, found in the WooCommerce Settings menu.
34
 
35
  = I don't see the code on my site. Where is it? =
36
 
37
+ We purposefully don't track admin visits to the site. Log out of the site (or open a Google Chrome Incognito window) and check if the code is there for non-admins.
38
 
39
+ Also please make sure to enter your Google Analytics ID under WooCommerce Settings Integrations.
40
 
41
  = My code is there. Why is it still not tracking sales? =
42
 
43
+ Duplicate Google Analytics code causes a conflict in tracking. Remove any other Google Analytics plugins or code from your site to avoid duplication and conflicts in tracking.
 
 
 
 
 
 
 
 
44
 
45
  = My settings are not saving! =
46
 
60
 
61
  == Changelog ==
62
 
63
+ = 1.5.0 - 2020-12-17 =
64
+ * Add - Option to use Global Site Tag and the gtag.js library (for Universal Analytics or Google Analytics 4).
65
+ * Add - Several new values added to the Tracker data.
66
+ * Add - Developer ID for gtag.js and analytics.js.
67
+ * Tweak - Bump minimum-supported WooCommerce version to 3.2.
68
+ * Tweak - Remove deprecated jQuery .click().
69
+ * Fix - Settings link in plugins table row points directly to plugin settings.
70
+ * Fix - Issue with multiple consecutive "Remove from Cart" events sent from the mini cart.
71
+
72
  = 1.4.25 - 2020-11-25 =
73
  * Tweak - WC 4.7 compatibility.
74
  * Tweak - WordPress 5.6 compatibility.
woocommerce-google-analytics-integration.php CHANGED
@@ -5,8 +5,8 @@
5
  * Description: Allows Google Analytics tracking code to be inserted into WooCommerce store pages.
6
  * Author: WooCommerce
7
  * Author URI: https://woocommerce.com
8
- * Version: 1.4.25
9
- * WC requires at least: 2.1
10
  * WC tested up to: 4.7
11
  * Tested up to: 5.6
12
  * License: GPLv2 or later
@@ -20,18 +20,14 @@ if ( ! defined( 'ABSPATH' ) ) {
20
 
21
  if ( ! class_exists( 'WC_Google_Analytics_Integration' ) ) {
22
 
23
- define( 'WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION', '1.4.25' ); // WRCS: DEFINED_VERSION.
24
 
25
  /**
26
  * WooCommerce Google Analytics Integration main class.
27
  */
28
  class WC_Google_Analytics_Integration {
29
 
30
- /**
31
- * Instance of this class.
32
- *
33
- * @var object
34
- */
35
  protected static $instance = null;
36
 
37
  /**
@@ -46,8 +42,8 @@ if ( ! class_exists( 'WC_Google_Analytics_Integration' ) ) {
46
  add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
47
  add_action( 'init', array( $this, 'show_ga_pro_notices' ) );
48
 
49
- // Checks with WooCommerce is installed.
50
- if ( class_exists( 'WC_Integration' ) && defined( 'WOOCOMMERCE_VERSION' ) && version_compare( WOOCOMMERCE_VERSION, '2.1-beta-1', '>=' ) ) {
51
  include_once 'includes/class-wc-google-analytics.php';
52
 
53
  // Register the integration.
@@ -59,11 +55,18 @@ if ( ! class_exists( 'WC_Google_Analytics_Integration' ) ) {
59
  add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_links' ) );
60
  }
61
 
 
 
 
 
 
 
62
  public function plugin_links( $links ) {
63
  $settings_url = add_query_arg(
64
  array(
65
- 'page' => 'wc-settings',
66
- 'tab' => 'integration',
 
67
  ),
68
  admin_url( 'admin.php' )
69
  );
@@ -79,7 +82,7 @@ if ( ! class_exists( 'WC_Google_Analytics_Integration' ) ) {
79
  /**
80
  * Return an instance of this class.
81
  *
82
- * @return object A single instance of this class.
83
  */
84
  public static function get_instance() {
85
  // If the single instance hasn't been set, set it now.
@@ -92,8 +95,6 @@ if ( ! class_exists( 'WC_Google_Analytics_Integration' ) ) {
92
 
93
  /**
94
  * Load the plugin text domain for translation.
95
- *
96
- * @return void
97
  */
98
  public function load_plugin_textdomain() {
99
  $locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-google-analytics-integration' );
@@ -106,15 +107,14 @@ if ( ! class_exists( 'WC_Google_Analytics_Integration' ) ) {
106
  * WooCommerce fallback notice.
107
  */
108
  public function woocommerce_missing_notice() {
109
- echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Google Analytics depends on the last version of %s to work!', 'woocommerce-google-analytics-integration' ), '<a href="http://www.woothemes.com/woocommerce/" target="_blank">' . __( 'WooCommerce', 'woocommerce-google-analytics-integration' ) . '</a>' ) . '</p></div>';
110
  }
111
 
112
  /**
113
  * Add a new integration to WooCommerce.
114
  *
115
  * @param array $integrations WooCommerce integrations.
116
- *
117
- * @return array Google Analytics integration.
118
  */
119
  public function add_integration( $integrations ) {
120
  $integrations[] = 'WC_Google_Analytics';
5
  * Description: Allows Google Analytics tracking code to be inserted into WooCommerce store pages.
6
  * Author: WooCommerce
7
  * Author URI: https://woocommerce.com
8
+ * Version: 1.5.0
9
+ * WC requires at least: 3.2
10
  * WC tested up to: 4.7
11
  * Tested up to: 5.6
12
  * License: GPLv2 or later
20
 
21
  if ( ! class_exists( 'WC_Google_Analytics_Integration' ) ) {
22
 
23
+ define( 'WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION', '1.5.0' ); // WRCS: DEFINED_VERSION.
24
 
25
  /**
26
  * WooCommerce Google Analytics Integration main class.
27
  */
28
  class WC_Google_Analytics_Integration {
29
 
30
+ /** @var WC_Google_Analytics_Integration $instance Instance of this class. */
 
 
 
 
31
  protected static $instance = null;
32
 
33
  /**
42
  add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
43
  add_action( 'init', array( $this, 'show_ga_pro_notices' ) );
44
 
45
+ // Checks which WooCommerce is installed.
46
+ if ( class_exists( 'WC_Integration' ) && defined( 'WOOCOMMERCE_VERSION' ) && version_compare( WOOCOMMERCE_VERSION, '3.2', '>=' ) ) {
47
  include_once 'includes/class-wc-google-analytics.php';
48
 
49
  // Register the integration.
55
  add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_links' ) );
56
  }
57
 
58
+ /**
59
+ * Add links on the plugins page (Settings & Support)
60
+ *
61
+ * @param array $links Default links
62
+ * @return array Default + added links
63
+ */
64
  public function plugin_links( $links ) {
65
  $settings_url = add_query_arg(
66
  array(
67
+ 'page' => 'wc-settings',
68
+ 'tab' => 'integration',
69
+ 'section' => 'google_analytics',
70
  ),
71
  admin_url( 'admin.php' )
72
  );
82
  /**
83
  * Return an instance of this class.
84
  *
85
+ * @return WC_Google_Analytics_Integration A single instance of this class.
86
  */
87
  public static function get_instance() {
88
  // If the single instance hasn't been set, set it now.
95
 
96
  /**
97
  * Load the plugin text domain for translation.
 
 
98
  */
99
  public function load_plugin_textdomain() {
100
  $locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-google-analytics-integration' );
107
  * WooCommerce fallback notice.
108
  */
109
  public function woocommerce_missing_notice() {
110
+ echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Google Analytics depends on the last version of %s to work!', 'woocommerce-google-analytics-integration' ), '<a href="https://woocommerce.com/" target="_blank">' . __( 'WooCommerce', 'woocommerce-google-analytics-integration' ) . '</a>' ) . '</p></div>';
111
  }
112
 
113
  /**
114
  * Add a new integration to WooCommerce.
115
  *
116
  * @param array $integrations WooCommerce integrations.
117
+ * @return array Google Analytics integration added.
 
118
  */
119
  public function add_integration( $integrations ) {
120
  $integrations[] = 'WC_Google_Analytics';