WooCommerce Google Analytics Integration - Version 1.1

Version Description

  • 29/05/2014 =
    • Added option to enable Display Advertising
    • Added compatibility support for WooCommerce 2.1 beta releases
Download this release

Release Info

Developer CoenJacobs
Plugin Icon 128x128 WooCommerce Google Analytics Integration
Version 1.1
Comparing to
See all releases

Code changes from version 1.0 to 1.1

includes/class-wc-google-analytics-integration.php CHANGED
@@ -29,6 +29,8 @@ class WC_Google_Analytics extends WC_Integration {
29
  $this->ga_id = $this->get_option( 'ga_id' );
30
  $this->ga_set_domain_name = $this->get_option( 'ga_set_domain_name' );
31
  $this->ga_standard_tracking_enabled = $this->get_option( 'ga_standard_tracking_enabled' );
 
 
32
  $this->ga_ecommerce_tracking_enabled = $this->get_option( 'ga_ecommerce_tracking_enabled' );
33
  $this->ga_event_tracking_enabled = $this->get_option( 'ga_event_tracking_enabled' );
34
 
@@ -73,6 +75,19 @@ class WC_Google_Analytics extends WC_Integration {
73
  'checkboxgroup' => 'start',
74
  'default' => get_option('woocommerce_ga_standard_tracking_enabled') ? get_option('woocommerce_ga_standard_tracking_enabled') : 'no' // Backwards compat
75
  ),
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  'ga_ecommerce_tracking_enabled' => array(
77
  'label' => __( 'Add eCommerce tracking code to the thankyou page', 'woocommerce' ),
78
  'type' => 'checkbox',
@@ -115,27 +130,55 @@ class WC_Google_Analytics extends WC_Integration {
115
  $username = __( 'Guest', 'woocommerce' );
116
  }
117
 
118
- if ( ! empty( $this->ga_set_domain_name ) )
119
- $set_domain_name = "['_setDomainName', '" . esc_js( $this->ga_set_domain_name ) . "'],\n";
120
- else
121
- $set_domain_name = '';
 
 
122
 
123
- echo "<script type='text/javascript'>
 
 
 
 
124
 
125
- var _gaq = _gaq || [];
126
- _gaq.push(
127
- ['_setAccount', '" . esc_js( $tracking_id ) . "'], " . $set_domain_name . "
128
- ['_setCustomVar', 1, 'logged-in', '" . $loggedin . "', 1],
129
- ['_trackPageview']
130
- );
131
 
132
- (function() {
133
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
134
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
135
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
136
- })();
137
 
138
- </script>";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  }
140
 
141
 
@@ -173,72 +216,139 @@ class WC_Google_Analytics extends WC_Integration {
173
  $username = __( 'Guest', 'woocommerce' );
174
  }
175
 
176
- if ( ! empty( $this->ga_set_domain_name ) )
177
- $set_domain_name = "['_setDomainName', '" . esc_js( $this->ga_set_domain_name ) . "'],";
178
- else
179
- $set_domain_name = '';
180
-
181
- $code = "
182
- var _gaq = _gaq || [];
183
-
184
- _gaq.push(
185
- ['_setAccount', '" . esc_js( $tracking_id ) . "'], " . $set_domain_name . "
186
- ['_setCustomVar', 1, 'logged-in', '" . esc_js( $loggedin ) . "', 1],
187
- ['_trackPageview']
188
- );
189
-
190
- _gaq.push(['_addTrans',
191
- '" . esc_js( $order->get_order_number() ) . "', // order ID - required
192
- '" . esc_js( get_bloginfo( 'name' ) ) . "', // affiliation or store name
193
- '" . esc_js( $order->get_total() ) . "', // total - required
194
- '" . esc_js( $order->get_total_tax() ) . "', // tax
195
- '" . esc_js( $order->get_shipping() ) . "', // shipping
196
- '" . esc_js( $order->billing_city ) . "', // city
197
- '" . esc_js( $order->billing_state ) . "', // state or province
198
- '" . esc_js( $order->billing_country ) . "' // country
199
- ]);
200
- ";
201
-
202
- // Order items
203
- if ( $order->get_items() ) {
204
- foreach ( $order->get_items() as $item ) {
205
- $_product = $order->get_product_from_item( $item );
206
-
207
- $code .= "_gaq.push(['_addItem',";
208
- $code .= "'" . esc_js( $order->get_order_number() ) . "',";
209
- $code .= "'" . esc_js( $_product->get_sku() ? __( 'SKU:', 'woocommerce' ) . ' ' . $_product->get_sku() : $_product->id ) . "',";
210
- $code .= "'" . esc_js( $item['name'] ) . "',";
211
-
212
- if ( isset( $_product->variation_data ) ) {
213
-
214
- $code .= "'" . esc_js( woocommerce_get_formatted_variation( $_product->variation_data, true ) ) . "',";
215
-
216
- } else {
217
- $out = array();
218
- $categories = get_the_terms($_product->id, 'product_cat');
219
- if ( $categories ) {
220
- foreach ( $categories as $category ){
221
- $out[] = $category->name;
 
 
222
  }
 
223
  }
224
- $code .= "'" . esc_js( join( "/", $out) ) . "',";
225
- }
226
 
227
- $code .= "'" . esc_js( $order->get_item_total( $item ) ) . "',";
228
- $code .= "'" . esc_js( $item['qty'] ) . "'";
229
- $code .= "]);";
 
230
  }
 
 
231
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
 
233
- $code .= "
234
- _gaq.push(['_trackTrans']); // submits transaction to the Analytics servers
 
 
 
 
 
 
235
 
236
- (function() {
237
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
238
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
239
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
240
- })();
241
- ";
 
242
 
243
  echo '<script type="text/javascript">' . $code . '</script>';
244
 
@@ -302,9 +412,16 @@ class WC_Google_Analytics extends WC_Integration {
302
 
303
  $parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters );
304
 
 
 
 
 
 
 
 
305
  $woocommerce->add_inline_js("
306
  $('" . $selector . "').click(function() {
307
- " . sprintf( "_gaq.push(['_trackEvent', %s, %s, %s]);", $parameters['category'], $parameters['action'], $parameters['label'] ) . "
308
  });
309
  ");
310
  }
29
  $this->ga_id = $this->get_option( 'ga_id' );
30
  $this->ga_set_domain_name = $this->get_option( 'ga_set_domain_name' );
31
  $this->ga_standard_tracking_enabled = $this->get_option( 'ga_standard_tracking_enabled' );
32
+ $this->ga_support_display_advertising = $this->get_option( 'ga_support_display_advertising' );
33
+ $this->ga_use_universal_analytics = $this->get_option( 'ga_use_universal_analytics' );
34
  $this->ga_ecommerce_tracking_enabled = $this->get_option( 'ga_ecommerce_tracking_enabled' );
35
  $this->ga_event_tracking_enabled = $this->get_option( 'ga_event_tracking_enabled' );
36
 
75
  'checkboxgroup' => 'start',
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
90
+ ),
91
  'ga_ecommerce_tracking_enabled' => array(
92
  'label' => __( 'Add eCommerce tracking code to the thankyou page', 'woocommerce' ),
93
  'type' => 'checkbox',
130
  $username = __( 'Guest', 'woocommerce' );
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(){
142
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
143
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
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
 
150
+ </script>";
 
 
 
 
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
+
167
+ var _gaq = _gaq || [];
168
+ _gaq.push(
169
+ ['_setAccount', '" . esc_js( $tracking_id ) . "'], " . $set_domain_name . "
170
+ ['_setCustomVar', 1, 'logged-in', '" . $loggedin . "', 1],
171
+ ['_trackPageview']
172
+ );
173
+
174
+ (function() {
175
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
176
+ ga.src = ".$ga_url.";
177
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
178
+ })();
179
+
180
+ </script>";
181
+ }
182
  }
183
 
184
 
216
  $username = __( 'Guest', 'woocommerce' );
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(){
227
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
228
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
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
+
235
+ ga('require', 'ecommerce', 'ecommerce.js');
236
+
237
+ ga('ecommerce:addTransaction', {
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
+ ";
245
+
246
+ // Order items
247
+ if ( $order->get_items() ) {
248
+ foreach ( $order->get_items() as $item ) {
249
+ $_product = $order->get_product_from_item( $item );
250
+
251
+ $code .= "ga('ecommerce:addItem', {";
252
+ $code .= "'id': '" . esc_js( $order->get_order_number() ) . "',";
253
+ $code .= "'name': '" . esc_js( $item['name'] ) . "',";
254
+ $code .= "'sku': '" . esc_js( $_product->get_sku() ? __( 'SKU:', 'woocommerce' ) . ' ' . $_product->get_sku() : $_product->id ) . "',";
255
+
256
+ if ( isset( $_product->variation_data ) ) {
257
+
258
+ $code .= "'category': '" . esc_js( woocommerce_get_formatted_variation( $_product->variation_data, true ) ) . "',";
259
+
260
+ } else {
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
  }
268
+ $code .= "'category': '" . esc_js( join( "/", $out) ) . "',";
269
  }
 
 
270
 
271
+ $code .= "'price': '" . esc_js( $order->get_item_total( $item ) ) . "',";
272
+ $code .= "'quantity': '" . esc_js( $item['qty'] ) . "'";
273
+ $code .= "});";
274
+ }
275
  }
276
+
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 || [];
292
+
293
+ _gaq.push(
294
+ ['_setAccount', '" . esc_js( $tracking_id ) . "'], " . $set_domain_name . "
295
+ ['_setCustomVar', 1, 'logged-in', '" . esc_js( $loggedin ) . "', 1],
296
+ ['_trackPageview']
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
+
311
+ // Order items
312
+ if ( $order->get_items() ) {
313
+ foreach ( $order->get_items() as $item ) {
314
+ $_product = $order->get_product_from_item( $item );
315
+
316
+ $code .= "_gaq.push(['_addItem',";
317
+ $code .= "'" . esc_js( $order->get_order_number() ) . "',";
318
+ $code .= "'" . esc_js( $_product->get_sku() ? __( 'SKU:', 'woocommerce' ) . ' ' . $_product->get_sku() : $_product->id ) . "',";
319
+ $code .= "'" . esc_js( $item['name'] ) . "',";
320
+
321
+ if ( isset( $_product->variation_data ) ) {
322
+
323
+ $code .= "'" . esc_js( woocommerce_get_formatted_variation( $_product->variation_data, true ) ) . "',";
324
+
325
+ } else {
326
+ $out = array();
327
+ $categories = get_the_terms($_product->id, 'product_cat');
328
+ if ( $categories ) {
329
+ foreach ( $categories as $category ){
330
+ $out[] = $category->name;
331
+ }
332
+ }
333
+ $code .= "'" . esc_js( join( "/", $out) ) . "',";
334
+ }
335
 
336
+ $code .= "'" . esc_js( $order->get_item_total( $item ) ) . "',";
337
+ $code .= "'" . esc_js( $item['qty'] ) . "'";
338
+ $code .= "]);";
339
+ }
340
+ }
341
+
342
+ $code .= "
343
+ _gaq.push(['_trackTrans']); // submits transaction to the Analytics servers
344
 
345
+ (function() {
346
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
347
+ ga.src = ".$ga_url.";
348
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
349
+ })();
350
+ ";
351
+ }
352
 
353
  echo '<script type="text/javascript">' . $code . '</script>';
354
 
412
 
413
  $parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters );
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
  });
426
  ");
427
  }
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: woothemes, CoenJacobs
3
  Tags: woocommerce, google analytics
4
  Requires at least: 3.8
5
  Tested up to: 3.8
6
- Stable tag: 1.0
7
  License: GPLv3
8
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
 
@@ -11,7 +11,7 @@ Provides integration between Google Analytics and WooCommerce.
11
 
12
  == Description ==
13
 
14
- This plugin provides the integration between Google Analytics and the WooCommerce plugin.
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
 
@@ -41,5 +41,9 @@ You can leave this plugin activated and it will seamlessly take over the integra
41
 
42
  == Changelog ==
43
 
 
 
 
 
44
  = 1.0 - 22/11/2013 =
45
- * Initial release
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
 
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 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
 
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
47
+
48
  = 1.0 - 22/11/2013 =
49
+ * Initial release
woocommerce-google-analytics-integration.php CHANGED
@@ -2,17 +2,18 @@
2
 
3
  /*
4
  Plugin Name: 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.0
9
  */
10
 
11
  // Add the integration to WooCommerce
12
  function wc_google_analytics_add_integration( $integrations ) {
13
  global $woocommerce;
14
 
15
- if ( is_object( $woocommerce ) && version_compare( $woocommerce->version, '2.1', '>=' ) ) {
16
  include_once( 'includes/class-wc-google-analytics-integration.php' );
17
  $integrations[] = 'WC_Google_Analytics';
18
  }
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
13
  function wc_google_analytics_add_integration( $integrations ) {
14
  global $woocommerce;
15
 
16
+ if ( is_object( $woocommerce ) && version_compare( $woocommerce->version, '2.1-beta-1', '>=' ) ) {
17
  include_once( 'includes/class-wc-google-analytics-integration.php' );
18
  $integrations[] = 'WC_Google_Analytics';
19
  }