WooCommerce Google Analytics Integration - Version 1.2.0

Version Description

  • 28/07/2014 =
    • Feature - Adding display advertising parameter to Universal Analytics
    • Fix - Using get_total_shipping() instead of get_shipping
    • Fix - Using wc_enqueue_js() instead of $woocommerce->add_inline_js(
    • Tweak - Updating plugin FAQ
    • Tweak - Adding parenthesis for clarity
Download this release

Release Info

Developer BFTrick
Plugin Icon 128x128 WooCommerce Google Analytics Integration
Version 1.2.0
Comparing to
See all releases

Code changes from version 1.1 to 1.2.0

includes/class-wc-google-analytics-integration.php CHANGED
@@ -17,9 +17,9 @@ class WC_Google_Analytics extends WC_Integration {
17
* @return void
18
*/
19
public function __construct() {
20
- $this->id = 'google_analytics';
21
- $this->method_title = __( 'Google Analytics', 'woocommerce' );
22
- $this->method_description = __( 'Google Analytics is a free service offered by Google that generates detailed statistics about the visitors to a website.', 'woocommerce' );
23
24
// Load the settings.
25
$this->init_form_fields();
@@ -44,18 +44,18 @@ class WC_Google_Analytics extends WC_Integration {
44
// Event tracking code
45
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) );
46
add_action( 'wp_footer', array( $this, 'loop_add_to_cart' ) );
47
- }
48
49
50
- /**
51
- * Initialise Settings Form Fields
52
- *
53
- * @access public
54
- * @return void
55
- */
56
- function init_form_fields() {
57
58
- $this->form_fields = array(
59
'ga_id' => array(
60
'title' => __( 'Google Analytics ID', 'woocommerce' ),
61
'description' => __( 'Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce' ),
@@ -76,14 +76,13 @@ class WC_Google_Analytics extends WC_Integration {
76
'default' => get_option('woocommerce_ga_standard_tracking_enabled') ? get_option('woocommerce_ga_standard_tracking_enabled') : 'no' // Backwards compat
77
),
78
'ga_support_display_advertising' => array(
79
- 'label' => __( 'Set the Google analytics code to support Display Advertising. <a href="https://support.google.com/analytics/answer/2700409" target="_blank">Read More About Display Advertising</a>', 'woocommerce' ),
80
'type' => 'checkbox',
81
'checkboxgroup' => '',
82
'default' => get_option('woocommerce_ga_support_display_advertising') ? get_option('woocommerce_ga_support_display_advertising') : 'no' // Backwards compat
83
),
84
'ga_use_universal_analytics' => array(
85
- 'label' => __( 'Use Universal Analytics instead of Standard Google Analytics', 'woocommerce' ),
86
- 'description' => __( 'Using this disables Display Advertising', 'woocommerce' ),
87
'type' => 'checkbox',
88
'checkboxgroup' => '',
89
'default' => get_option('woocommerce_ga_use_universal_analytics') ? get_option('woocommerce_ga_use_universal_analytics') : 'no' // Backwards compat
@@ -114,7 +113,9 @@ class WC_Google_Analytics extends WC_Integration {
114
function google_tracking_code() {
115
global $woocommerce;
116
117
- if ( is_admin() || current_user_can('manage_options') || $this->ga_standard_tracking_enabled == "no" ) return;
118
119
$tracking_id = $this->ga_id;
120
@@ -131,11 +132,16 @@ class WC_Google_Analytics extends WC_Integration {
131
}
132
133
if ( $this->ga_use_universal_analytics == 'yes' ) {
134
- if ( ! empty( $this->ga_set_domain_name ) )
135
$set_domain_name = esc_js( $this->ga_set_domain_name );
136
- else
137
$set_domain_name = 'auto';
138
139
140
echo "<script>
141
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
@@ -144,6 +150,7 @@ class WC_Google_Analytics extends WC_Integration {
144
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
145
146
ga('create', '" . esc_js( $tracking_id ) . "', '" . $set_domain_name . "');
147
ga('set', 'dimension1', '" . $loggedin . "');
148
ga('send', 'pageview');
149
@@ -151,16 +158,18 @@ class WC_Google_Analytics extends WC_Integration {
151
152
}
153
else {
154
- if ( $this->ga_support_display_advertising == 'yes' )
155
$ga_url = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'";
156
- else
157
$ga_url = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'";
158
159
160
- if ( ! empty( $this->ga_set_domain_name ) )
161
$set_domain_name = "['_setDomainName', '" . esc_js( $this->ga_set_domain_name ) . "'],\n";
162
- else
163
$set_domain_name = '';
164
165
echo "<script type='text/javascript'>
166
@@ -192,12 +201,15 @@ class WC_Google_Analytics extends WC_Integration {
192
function ecommerce_tracking_code( $order_id ) {
193
global $woocommerce;
194
195
- if ( $this->ga_ecommerce_tracking_enabled == "no" || current_user_can('manage_options') || get_post_meta( $order_id, '_ga_tracked', true ) == 1 )
196
return;
197
198
$tracking_id = $this->ga_id;
199
200
- if ( ! $tracking_id ) return;
201
202
// Doing eCommerce tracking so unhook standard tracking from the footer
203
remove_action( 'wp_footer', array( $this, 'google_tracking_code' ) );
@@ -217,10 +229,16 @@ class WC_Google_Analytics extends WC_Integration {
217
}
218
219
if ( $this->ga_use_universal_analytics == 'yes' ) {
220
- if ( ! empty( $this->ga_set_domain_name ) )
221
$set_domain_name = esc_js( $this->ga_set_domain_name );
222
- else
223
$set_domain_name = 'auto';
224
225
$code = "
226
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
@@ -229,6 +247,7 @@ class WC_Google_Analytics extends WC_Integration {
229
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
230
231
ga('create', '" . esc_js( $tracking_id ) . "', '" . $set_domain_name . "');
232
ga('set', 'dimension1', '" . $loggedin . "');
233
ga('send', 'pageview');
234
@@ -238,7 +257,7 @@ class WC_Google_Analytics extends WC_Integration {
238
'id': '" . esc_js( $order->get_order_number() ) . "', // Transaction ID. Required
239
'affiliation': '" . esc_js( get_bloginfo( 'name' ) ) . "', // Affiliation or store name
240
'revenue': '" . esc_js( $order->get_total() ) . "', // Grand Total
241
- 'shipping': '" . esc_js( $order->get_shipping() ) . "', // Shipping
242
'tax': '" . esc_js( $order->get_total_tax() ) . "' // Tax
243
});
244
";
@@ -261,7 +280,7 @@ class WC_Google_Analytics extends WC_Integration {
261
$out = array();
262
$categories = get_the_terms($_product->id, 'product_cat');
263
if ( $categories ) {
264
- foreach ( $categories as $category ){
265
$out[] = $category->name;
266
}
267
}
@@ -277,15 +296,17 @@ class WC_Google_Analytics extends WC_Integration {
277
$code .= "ga('ecommerce:send'); // Send transaction and item data to Google Analytics.";
278
}
279
else {
280
- if ( $this->ga_support_display_advertising == 'yes' )
281
$ga_url = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'";
282
- else
283
$ga_url = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'";
284
285
- if ( ! empty( $this->ga_set_domain_name ) )
286
$set_domain_name = "['_setDomainName', '" . esc_js( $this->ga_set_domain_name ) . "'],";
287
- else
288
$set_domain_name = '';
289
290
$code = "
291
var _gaq = _gaq || [];
@@ -297,14 +318,14 @@ class WC_Google_Analytics extends WC_Integration {
297
);
298
299
_gaq.push(['_addTrans',
300
- '" . esc_js( $order->get_order_number() ) . "', // order ID - required
301
- '" . esc_js( get_bloginfo( 'name' ) ) . "', // affiliation or store name
302
- '" . esc_js( $order->get_total() ) . "', // total - required
303
- '" . esc_js( $order->get_total_tax() ) . "', // tax
304
- '" . esc_js( $order->get_shipping() ) . "', // shipping
305
- '" . esc_js( $order->billing_city ) . "', // city
306
- '" . esc_js( $order->billing_state ) . "', // state or province
307
- '" . esc_js( $order->billing_country ) . "' // country
308
]);
309
";
310
@@ -364,8 +385,12 @@ class WC_Google_Analytics extends WC_Integration {
364
*/
365
function add_to_cart() {
366
367
- if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) return;
368
- if ( ! is_single() ) return;
369
370
global $product;
371
@@ -387,7 +412,9 @@ class WC_Google_Analytics extends WC_Integration {
387
*/
388
function loop_add_to_cart() {
389
390
- if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) return;
391
392
$parameters = array();
393
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters
@@ -414,12 +441,11 @@ class WC_Google_Analytics extends WC_Integration {
414
415
if ( $this->ga_use_universal_analytics == 'yes' ) {
416
$track_event = "ga('send', 'event', %s, %s, %s);";
417
- }
418
- else {
419
$track_event = "_gaq.push(['_trackEvent', %s, %s, %s]);";
420
}
421
422
- $woocommerce->add_inline_js("
423
$('" . $selector . "').click(function() {
424
" . sprintf( $track_event, $parameters['category'], $parameters['action'], $parameters['label'] ) . "
425
});
@@ -436,7 +462,9 @@ class WC_Google_Analytics extends WC_Integration {
436
*/
437
private function disable_tracking( $type ) {
438
439
- if ( is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' == $type ) return true;
440
441
}
442
17
* @return void
18
*/
19
public function __construct() {
20
+ $this->id = 'google_analytics';
21
+ $this->method_title = __( 'Google Analytics', 'woocommerce' );
22
+ $this->method_description = __( 'Google Analytics is a free service offered by Google that generates detailed statistics about the visitors to a website.', 'woocommerce' );
23
24
// Load the settings.
25
$this->init_form_fields();
44
// Event tracking code
45
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) );
46
add_action( 'wp_footer', array( $this, 'loop_add_to_cart' ) );
47
+ }
48
49
50
+ /**
51
+ * Initialise Settings Form Fields
52
+ *
53
+ * @access public
54
+ * @return void
55
+ */
56
+ function init_form_fields() {
57
58
+ $this->form_fields = array(
59
'ga_id' => array(
60
'title' => __( 'Google Analytics ID', 'woocommerce' ),
61
'description' => __( 'Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce' ),
76
'default' => get_option('woocommerce_ga_standard_tracking_enabled') ? get_option('woocommerce_ga_standard_tracking_enabled') : 'no' // Backwards compat
77
),
78
'ga_support_display_advertising' => array(
79
+ 'label' => __( 'Set the Google Analytics code to support Display Advertising. <a href="https://support.google.com/analytics/answer/2700409" target="_blank">Read More About Display Advertising</a>', 'woocommerce' ),
80
'type' => 'checkbox',
81
'checkboxgroup' => '',
82
'default' => get_option('woocommerce_ga_support_display_advertising') ? get_option('woocommerce_ga_support_display_advertising') : 'no' // Backwards compat
83
),
84
'ga_use_universal_analytics' => array(
85
+ 'label' => __( 'Use Universal Analytics instead of Classic Google Analytics', 'woocommerce' ),
86
'type' => 'checkbox',
87
'checkboxgroup' => '',
88
'default' => get_option('woocommerce_ga_use_universal_analytics') ? get_option('woocommerce_ga_use_universal_analytics') : 'no' // Backwards compat
113
function google_tracking_code() {
114
global $woocommerce;
115
116
+ if ( is_admin() || current_user_can('manage_options') || $this->ga_standard_tracking_enabled == "no" ) {
117
+ return;
118
+ }
119
120
$tracking_id = $this->ga_id;
121
132
}
133
134
if ( $this->ga_use_universal_analytics == 'yes' ) {
135
+ if ( ! empty( $this->ga_set_domain_name ) ) {
136
$set_domain_name = esc_js( $this->ga_set_domain_name );
137
+ } else {
138
$set_domain_name = 'auto';
139
+ }
140
141
+ $support_display_advertising = '';
142
+ if ( 'yes' == $this->ga_support_display_advertising ) {
143
+ $support_display_advertising = "ga('require', 'displayfeatures');";
144
+ }
145
146
echo "<script>
147
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
150
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
151
152
ga('create', '" . esc_js( $tracking_id ) . "', '" . $set_domain_name . "');
153
+ " . $support_display_advertising . "
154
ga('set', 'dimension1', '" . $loggedin . "');
155
ga('send', 'pageview');
156
158
159
}
160
else {
161
+ if ( 'yes' == $this->ga_support_display_advertising ) {
162
$ga_url = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'";
163
+ } else {
164
$ga_url = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'";
165
+ }
166
167
168
+ if ( ! empty( $this->ga_set_domain_name ) ) {
169
$set_domain_name = "['_setDomainName', '" . esc_js( $this->ga_set_domain_name ) . "'],\n";
170
+ } else {
171
$set_domain_name = '';
172
+ }
173
174
echo "<script type='text/javascript'>
175
201
function ecommerce_tracking_code( $order_id ) {
202
global $woocommerce;
203
204
+ if ( $this->ga_ecommerce_tracking_enabled == "no" || current_user_can('manage_options') || get_post_meta( $order_id, '_ga_tracked', true ) == 1 ) {
205
return;
206
+ }
207
208
$tracking_id = $this->ga_id;
209
210
+ if ( ! $tracking_id ) {
211
+ return;
212
+ }
213
214
// Doing eCommerce tracking so unhook standard tracking from the footer
215
remove_action( 'wp_footer', array( $this, 'google_tracking_code' ) );
229
}
230
231
if ( $this->ga_use_universal_analytics == 'yes' ) {
232
+ if ( ! empty( $this->ga_set_domain_name ) ) {
233
$set_domain_name = esc_js( $this->ga_set_domain_name );
234
+ } else {
235
$set_domain_name = 'auto';
236
+ }
237
+
238
+ $support_display_advertising = '';
239
+ if ( 'yes' == $this->ga_support_display_advertising ) {
240
+ $support_display_advertising = "ga('require', 'displayfeatures');";
241
+ }
242
243
$code = "
244
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
247
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
248
249
ga('create', '" . esc_js( $tracking_id ) . "', '" . $set_domain_name . "');
250
+ " . $support_display_advertising . "
251
ga('set', 'dimension1', '" . $loggedin . "');
252
ga('send', 'pageview');
253
257
'id': '" . esc_js( $order->get_order_number() ) . "', // Transaction ID. Required
258
'affiliation': '" . esc_js( get_bloginfo( 'name' ) ) . "', // Affiliation or store name
259
'revenue': '" . esc_js( $order->get_total() ) . "', // Grand Total
260
+ 'shipping': '" . esc_js( $order->get_total_shipping() ) . "', // Shipping
261
'tax': '" . esc_js( $order->get_total_tax() ) . "' // Tax
262
});
263
";
280
$out = array();
281
$categories = get_the_terms($_product->id, 'product_cat');
282
if ( $categories ) {
283
+ foreach ( $categories as $category ) {
284
$out[] = $category->name;
285
}
286
}
296
$code .= "ga('ecommerce:send'); // Send transaction and item data to Google Analytics.";
297
}
298
else {
299
+ if ( $this->ga_support_display_advertising == 'yes' ) {
300
$ga_url = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'";
301
+ } else {
302
$ga_url = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'";
303
+ }
304
305
+ if ( ! empty( $this->ga_set_domain_name ) ) {
306
$set_domain_name = "['_setDomainName', '" . esc_js( $this->ga_set_domain_name ) . "'],";
307
+ } else {
308
$set_domain_name = '';
309
+ }
310
311
$code = "
312
var _gaq = _gaq || [];
318
);
319
320
_gaq.push(['_addTrans',
321
+ '" . esc_js( $order->get_order_number() ) . "', // order ID - required
322
+ '" . esc_js( get_bloginfo( 'name' ) ) . "', // affiliation or store name
323
+ '" . esc_js( $order->get_total() ) . "', // total - required
324
+ '" . esc_js( $order->get_total_tax() ) . "', // tax
325
+ '" . esc_js( $order->get_total_shipping() ) . "', // shipping
326
+ '" . esc_js( $order->billing_city ) . "', // city
327
+ '" . esc_js( $order->billing_state ) . "', // state or province
328
+ '" . esc_js( $order->billing_country ) . "' // country
329
]);
330
";
331
385
*/
386
function add_to_cart() {
387
388
+ if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
389
+ return;
390
+ }
391
+ if ( ! is_single() ) {
392
+ return;
393
+ }
394
395
global $product;
396
412
*/
413
function loop_add_to_cart() {
414
415
+ if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
416
+ return;
417
+ }
418
419
$parameters = array();
420
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters
441
442
if ( $this->ga_use_universal_analytics == 'yes' ) {
443
$track_event = "ga('send', 'event', %s, %s, %s);";
444
+ } else {
445
$track_event = "_gaq.push(['_trackEvent', %s, %s, %s]);";
446
}
447
448
+ wc_enqueue_js("
449
$('" . $selector . "').click(function() {
450
" . sprintf( $track_event, $parameters['category'], $parameters['action'], $parameters['label'] ) . "
451
});
462
*/
463
private function disable_tracking( $type ) {
464
465
+ if ( is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' == $type ) {
466
+ return true;
467
+ }
468
469
}
470
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
=== WooCommerce Google Analytics Integration ===
2
- Contributors: woothemes, CoenJacobs
3
Tags: woocommerce, google analytics
4
Requires at least: 3.8
5
Tested up to: 3.8
6
- Stable tag: 1.1
7
License: GPLv3
8
License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
@@ -22,6 +22,7 @@ Contributions are welcome via the [GitHub repository](https://github.com/woothem
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
26
Or use the automatic installation wizard through your admin panel, just search for this plugins name.
27
@@ -31,6 +32,12 @@ Or use the automatic installation wizard through your admin panel, just search f
31
32
This plugin will add the settings to the Integration tab, to be found in the WooCommerce > Settings menu.
33
34
= Can I install it already? =
35
36
Starting the WooCommerce 2.1 release, the Google Analytics integration for WooCommerce is no longer part of the WooCommerce plugin.
@@ -41,6 +48,13 @@ You can leave this plugin activated and it will seamlessly take over the integra
41
42
== Changelog ==
43
44
= 1.1 - 29/05/2014 =
45
* Added option to enable Display Advertising
46
* Added compatibility support for WooCommerce 2.1 beta releases
1
=== WooCommerce Google Analytics Integration ===
2
+ Contributors: woothemes
3
Tags: woocommerce, google analytics
4
Requires at least: 3.8
5
Tested up to: 3.8
6
+ Stable tag: 1.2.0
7
License: GPLv3
8
License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
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
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
= Can I install it already? =
42
43
Starting the WooCommerce 2.1 release, the Google Analytics integration for WooCommerce is no longer part of the WooCommerce plugin.
48
49
== Changelog ==
50
51
+ = 1.2.0 - 28/07/2014 =
52
+ * Feature - Adding display advertising parameter to Universal Analytics
53
+ * Fix - Using get_total_shipping() instead of get_shipping
54
+ * Fix - Using wc_enqueue_js() instead of $woocommerce->add_inline_js(
55
+ * Tweak - Updating plugin FAQ
56
+ * Tweak - Adding parenthesis for clarity
57
+
58
= 1.1 - 29/05/2014 =
59
* Added option to enable Display Advertising
60
* Added compatibility support for WooCommerce 2.1 beta releases
woocommerce-google-analytics-integration.php CHANGED
@@ -1,12 +1,11 @@
1
<?php
2
-
3
/*
4
Plugin Name: WooCommerce Google Analytics Integration
5
Plugin URI: http://wordpress.org/plugins/woocommerce-google-analytics-integration/
6
Description: Allows Google Analytics tracking code to be inserted into WooCommerce store pages.
7
Author: WooThemes
8
Author URI: http://www.woothemes.com
9
- Version: 1.1.0
10
*/
11
12
// Add the integration to WooCommerce
1
<?php
2
/*
3
Plugin Name: WooCommerce Google Analytics Integration
4
Plugin URI: http://wordpress.org/plugins/woocommerce-google-analytics-integration/
5
Description: Allows Google Analytics tracking code to be inserted into WooCommerce store pages.
6
Author: WooThemes
7
Author URI: http://www.woothemes.com
8
+ Version: 1.2.0
9
*/
10
11
// Add the integration to WooCommerce