WooCommerce Google Analytics Integration - Version 1.3.0

Version Description

  • 12/11/2014 =

  • Feature - Added the transaction currency in the tracking code

  • Feature - Add data privacy option that are mandatory in some countries

  • Tweak - Moved the tracking code to the head of the page

  • Tweak - Remove the "SKU" prefix to the sku for addItem

  • Refactor - Integration class reformulated

Download this release

Release Info

Developer claudiosanches
Plugin Icon 128x128 WooCommerce Google Analytics Integration
Version 1.3.0
Comparing to
See all releases

Code changes from version 1.2.1 to 1.3.0

README.md DELETED
@@ -1,5 +0,0 @@
1
- # WooCommerce Google Analytics Integration
2
-
3
- WordPress plugin: Provides the integration between WooCommerce and Google Analytics.
4
-
5
- Will be required for WooCommerce shops using the integration from WooCommerce 2.1 and up.
 
 
 
 
 
includes/class-wc-google-analytics-integration.php DELETED
@@ -1,494 +0,0 @@
1
- <?php
2
-
3
- /**
4
- * Google Analytics Integration
5
- *
6
- * Allows tracking code to be inserted into store pages.
7
- *
8
- * @class WC_Google_Analytics
9
- * @extends WC_Integration
10
- */
11
- class WC_Google_Analytics extends WC_Integration {
12
-
13
- /**
14
- * Init and hook in the integration.
15
- *
16
- * @access public
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();
26
- $this->init_settings();
27
-
28
- // Define user set variables
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
-
37
- // Actions
38
- add_action( 'woocommerce_update_options_integration_google_analytics', array( $this, 'process_admin_options') );
39
-
40
- // Tracking code
41
- add_action( 'wp_footer', array( $this, 'google_tracking_code' ) );
42
- add_action( 'woocommerce_thankyou', array( $this, 'ecommerce_tracking_code' ) );
43
-
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
- // utm_nooverride parameter for Google AdWords
49
- add_filter( 'woocommerce_get_return_url', array( $this, 'utm_nooverride' ) );
50
- }
51
-
52
-
53
- /**
54
- * Initialise Settings Form Fields
55
- *
56
- * @access public
57
- * @return void
58
- */
59
- function init_form_fields() {
60
-
61
- $this->form_fields = array(
62
- 'ga_id' => array(
63
- 'title' => __( 'Google Analytics ID', 'woocommerce' ),
64
- 'description' => __( 'Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce' ),
65
- 'type' => 'text',
66
- 'default' => get_option('woocommerce_ga_id') // Backwards compat
67
- ),
68
- 'ga_set_domain_name' => array(
69
- 'title' => __( 'Set Domain Name', 'woocommerce' ),
70
- 'description' => sprintf( __( '(Optional) Sets the <code>_setDomainName</code> variable. <a href="%s">See here for more information</a>.', 'woocommerce' ), 'https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#multipleDomains' ),
71
- 'type' => 'text',
72
- 'default' => ''
73
- ),
74
- 'ga_standard_tracking_enabled' => array(
75
- 'title' => __( 'Tracking code', 'woocommerce' ),
76
- 'label' => __( 'Add tracking code to your site. You don\'t need to enable this if using a 3rd party analytics plugin.', 'woocommerce' ),
77
- 'type' => 'checkbox',
78
- 'checkboxgroup' => 'start',
79
- 'default' => get_option('woocommerce_ga_standard_tracking_enabled') ? get_option('woocommerce_ga_standard_tracking_enabled') : 'no' // Backwards compat
80
- ),
81
- 'ga_support_display_advertising' => array(
82
- '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' ),
83
- 'type' => 'checkbox',
84
- 'checkboxgroup' => '',
85
- 'default' => get_option('woocommerce_ga_support_display_advertising') ? get_option('woocommerce_ga_support_display_advertising') : 'no' // Backwards compat
86
- ),
87
- 'ga_use_universal_analytics' => array(
88
- 'label' => __( 'Use Universal Analytics instead of Classic Google Analytics', 'woocommerce' ),
89
- 'type' => 'checkbox',
90
- 'checkboxgroup' => '',
91
- 'default' => get_option('woocommerce_ga_use_universal_analytics') ? get_option('woocommerce_ga_use_universal_analytics') : 'no' // Backwards compat
92
- ),
93
- 'ga_ecommerce_tracking_enabled' => array(
94
- 'label' => __( 'Add eCommerce tracking code to the thankyou page', 'woocommerce' ),
95
- 'type' => 'checkbox',
96
- 'checkboxgroup' => '',
97
- 'default' => get_option('woocommerce_ga_ecommerce_tracking_enabled') ? get_option('woocommerce_ga_ecommerce_tracking_enabled') : 'no' // Backwards compat
98
- ),
99
- 'ga_event_tracking_enabled' => array(
100
- 'label' => __( 'Add event tracking code for add to cart actions', 'woocommerce' ),
101
- 'type' => 'checkbox',
102
- 'checkboxgroup' => 'end',
103
- 'default' => 'no'
104
- )
105
- );
106
-
107
- } // End init_form_fields()
108
-
109
-
110
- /**
111
- * Google Analytics standard tracking
112
- *
113
- * @access public
114
- * @return void
115
- */
116
- function google_tracking_code() {
117
- global $woocommerce;
118
-
119
- if ( is_admin() || current_user_can('manage_options') || $this->ga_standard_tracking_enabled == "no" ) {
120
- return;
121
- }
122
-
123
- $tracking_id = $this->ga_id;
124
-
125
- if ( ! $tracking_id ) return;
126
-
127
- $loggedin = ( is_user_logged_in() ) ? 'yes' : 'no';
128
- if ( is_user_logged_in() ) {
129
- $user_id = get_current_user_id();
130
- $current_user = get_user_by('id', $user_id);
131
- $username = $current_user->user_login;
132
- } else {
133
- $user_id = '';
134
- $username = __( 'Guest', 'woocommerce' );
135
- }
136
-
137
- if ( $this->ga_use_universal_analytics == 'yes' ) {
138
- if ( ! empty( $this->ga_set_domain_name ) ) {
139
- $set_domain_name = esc_js( $this->ga_set_domain_name );
140
- } else {
141
- $set_domain_name = 'auto';
142
- }
143
-
144
- $support_display_advertising = '';
145
- if ( 'yes' == $this->ga_support_display_advertising ) {
146
- $support_display_advertising = "ga('require', 'displayfeatures');";
147
- }
148
-
149
- echo "<script>
150
- (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
151
- (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
152
- m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
153
- })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
154
-
155
- ga('create', '" . esc_js( $tracking_id ) . "', '" . $set_domain_name . "');
156
- " . $support_display_advertising . "
157
- ga('set', 'dimension1', '" . $loggedin . "');
158
- ga('send', 'pageview');
159
-
160
- </script>";
161
-
162
- }
163
- else {
164
- if ( 'yes' == $this->ga_support_display_advertising ) {
165
- $ga_url = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'";
166
- } else {
167
- $ga_url = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'";
168
- }
169
-
170
-
171
- if ( ! empty( $this->ga_set_domain_name ) ) {
172
- $set_domain_name = "['_setDomainName', '" . esc_js( $this->ga_set_domain_name ) . "'],\n";
173
- } else {
174
- $set_domain_name = '';
175
- }
176
-
177
- echo "<script type='text/javascript'>
178
-
179
- var _gaq = _gaq || [];
180
- _gaq.push(
181
- ['_setAccount', '" . esc_js( $tracking_id ) . "'], " . $set_domain_name . "
182
- ['_setCustomVar', 1, 'logged-in', '" . $loggedin . "', 1],
183
- ['_trackPageview']
184
- );
185
-
186
- (function() {
187
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
188
- ga.src = ".$ga_url.";
189
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
190
- })();
191
-
192
- </script>";
193
- }
194
- }
195
-
196
-
197
- /**
198
- * Google Analytics eCommerce tracking
199
- *
200
- * @access public
201
- * @param mixed $order_id
202
- * @return void
203
- */
204
- function ecommerce_tracking_code( $order_id ) {
205
- global $woocommerce;
206
-
207
- if ( $this->ga_ecommerce_tracking_enabled == "no" || current_user_can('manage_options') || get_post_meta( $order_id, '_ga_tracked', true ) == 1 ) {
208
- return;
209
- }
210
-
211
- $tracking_id = $this->ga_id;
212
-
213
- if ( ! $tracking_id ) {
214
- return;
215
- }
216
-
217
- // Doing eCommerce tracking so unhook standard tracking from the footer
218
- remove_action( 'wp_footer', array( $this, 'google_tracking_code' ) );
219
-
220
- // Get the order and output tracking code
221
- $order = new WC_Order( $order_id );
222
-
223
- $loggedin = is_user_logged_in() ? 'yes' : 'no';
224
-
225
- if ( is_user_logged_in() ) {
226
- $user_id = get_current_user_id();
227
- $current_user = get_user_by('id', $user_id);
228
- $username = $current_user->user_login;
229
- } else {
230
- $user_id = '';
231
- $username = __( 'Guest', 'woocommerce' );
232
- }
233
-
234
- if ( $this->ga_use_universal_analytics == 'yes' ) {
235
- if ( ! empty( $this->ga_set_domain_name ) ) {
236
- $set_domain_name = esc_js( $this->ga_set_domain_name );
237
- } else {
238
- $set_domain_name = 'auto';
239
- }
240
-
241
- $support_display_advertising = '';
242
- if ( 'yes' == $this->ga_support_display_advertising ) {
243
- $support_display_advertising = "ga('require', 'displayfeatures');";
244
- }
245
-
246
- $code = "
247
- (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
248
- (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
249
- m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
250
- })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
251
-
252
- ga('create', '" . esc_js( $tracking_id ) . "', '" . $set_domain_name . "');
253
- " . $support_display_advertising . "
254
- ga('set', 'dimension1', '" . $loggedin . "');
255
- ga('send', 'pageview');
256
-
257
- ga('require', 'ecommerce', 'ecommerce.js');
258
-
259
- ga('ecommerce:addTransaction', {
260
- 'id': '" . esc_js( $order->get_order_number() ) . "', // Transaction ID. Required
261
- 'affiliation': '" . esc_js( get_bloginfo( 'name' ) ) . "', // Affiliation or store name
262
- 'revenue': '" . esc_js( $order->get_total() ) . "', // Grand Total
263
- 'shipping': '" . esc_js( $order->get_total_shipping() ) . "', // Shipping
264
- 'tax': '" . esc_js( $order->get_total_tax() ) . "' // Tax
265
- });
266
- ";
267
-
268
- // Order items
269
- if ( $order->get_items() ) {
270
- foreach ( $order->get_items() as $item ) {
271
- $_product = $order->get_product_from_item( $item );
272
-
273
- $code .= "ga('ecommerce:addItem', {";
274
- $code .= "'id': '" . esc_js( $order->get_order_number() ) . "',";
275
- $code .= "'name': '" . esc_js( $item['name'] ) . "',";
276
- $code .= "'sku': '" . esc_js( $_product->get_sku() ? __( 'SKU:', 'woocommerce' ) . ' ' . $_product->get_sku() : $_product->id ) . "',";
277
-
278
- if ( isset( $_product->variation_data ) ) {
279
-
280
- $code .= "'category': '" . esc_js( woocommerce_get_formatted_variation( $_product->variation_data, true ) ) . "',";
281
-
282
- } else {
283
- $out = array();
284
- $categories = get_the_terms($_product->id, 'product_cat');
285
- if ( $categories ) {
286
- foreach ( $categories as $category ) {
287
- $out[] = $category->name;
288
- }
289
- }
290
- $code .= "'category': '" . esc_js( join( "/", $out) ) . "',";
291
- }
292
-
293
- $code .= "'price': '" . esc_js( $order->get_item_total( $item ) ) . "',";
294
- $code .= "'quantity': '" . esc_js( $item['qty'] ) . "'";
295
- $code .= "});";
296
- }
297
- }
298
-
299
- $code .= "ga('ecommerce:send'); // Send transaction and item data to Google Analytics.";
300
- }
301
- else {
302
- if ( $this->ga_support_display_advertising == 'yes' ) {
303
- $ga_url = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'";
304
- } else {
305
- $ga_url = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'";
306
- }
307
-
308
- if ( ! empty( $this->ga_set_domain_name ) ) {
309
- $set_domain_name = "['_setDomainName', '" . esc_js( $this->ga_set_domain_name ) . "'],";
310
- } else {
311
- $set_domain_name = '';
312
- }
313
-
314
- $code = "
315
- var _gaq = _gaq || [];
316
-
317
- _gaq.push(
318
- ['_setAccount', '" . esc_js( $tracking_id ) . "'], " . $set_domain_name . "
319
- ['_setCustomVar', 1, 'logged-in', '" . esc_js( $loggedin ) . "', 1],
320
- ['_trackPageview']
321
- );
322
-
323
- _gaq.push(['_addTrans',
324
- '" . esc_js( $order->get_order_number() ) . "', // order ID - required
325
- '" . esc_js( get_bloginfo( 'name' ) ) . "', // affiliation or store name
326
- '" . esc_js( $order->get_total() ) . "', // total - required
327
- '" . esc_js( $order->get_total_tax() ) . "', // tax
328
- '" . esc_js( $order->get_total_shipping() ) . "', // shipping
329
- '" . esc_js( $order->billing_city ) . "', // city
330
- '" . esc_js( $order->billing_state ) . "', // state or province
331
- '" . esc_js( $order->billing_country ) . "' // country
332
- ]);
333
- ";
334
-
335
- // Order items
336
- if ( $order->get_items() ) {
337
- foreach ( $order->get_items() as $item ) {
338
- $_product = $order->get_product_from_item( $item );
339
-
340
- $code .= "_gaq.push(['_addItem',";
341
- $code .= "'" . esc_js( $order->get_order_number() ) . "',";
342
- $code .= "'" . esc_js( $_product->get_sku() ? __( 'SKU:', 'woocommerce' ) . ' ' . $_product->get_sku() : $_product->id ) . "',";
343
- $code .= "'" . esc_js( $item['name'] ) . "',";
344
-
345
- if ( isset( $_product->variation_data ) ) {
346
-
347
- $code .= "'" . esc_js( woocommerce_get_formatted_variation( $_product->variation_data, true ) ) . "',";
348
-
349
- } else {
350
- $out = array();
351
- $categories = get_the_terms($_product->id, 'product_cat');
352
- if ( $categories ) {
353
- foreach ( $categories as $category ){
354
- $out[] = $category->name;
355
- }
356
- }
357
- $code .= "'" . esc_js( join( "/", $out) ) . "',";
358
- }
359
-
360
- $code .= "'" . esc_js( $order->get_item_total( $item ) ) . "',";
361
- $code .= "'" . esc_js( $item['qty'] ) . "'";
362
- $code .= "]);";
363
- }
364
- }
365
-
366
- $code .= "
367
- _gaq.push(['_trackTrans']); // submits transaction to the Analytics servers
368
-
369
- (function() {
370
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
371
- ga.src = ".$ga_url.";
372
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
373
- })();
374
- ";
375
- }
376
-
377
- echo '<script type="text/javascript">' . $code . '</script>';
378
-
379
- update_post_meta( $order_id, '_ga_tracked', 1 );
380
- }
381
-
382
-
383
- /**
384
- * Google Analytics event tracking for single product add to cart
385
- *
386
- * @access public
387
- * @return void
388
- */
389
- function add_to_cart() {
390
-
391
- if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
392
- return;
393
- }
394
- if ( ! is_single() ) {
395
- return;
396
- }
397
-
398
- global $product;
399
-
400
- $parameters = array();
401
- // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
402
- $parameters['category'] = "'" . __( 'Products', 'woocommerce' ) . "'";
403
- $parameters['action'] = "'" . __( 'Add to cart', 'woocommerce' ) . "'";
404
- $parameters['label'] = "'" . esc_js( $product->get_sku() ? __('SKU:', 'woocommerce') . ' ' . $product->get_sku() : "#" . $product->id ) . "'";
405
-
406
- $this->event_tracking_code( $parameters, '.single_add_to_cart_button' );
407
- }
408
-
409
-
410
- /**
411
- * Google Analytics event tracking for loop add to cart
412
- *
413
- * @access public
414
- * @return void
415
- */
416
- function loop_add_to_cart() {
417
-
418
- if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
419
- return;
420
- }
421
-
422
- $parameters = array();
423
- // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
424
- $parameters['category'] = "'" . __( 'Products', 'woocommerce' ) . "'";
425
- $parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce' ) . "'";
426
- $parameters['label'] = "($(this).data('product_sku')) ? ('SKU: ' + $(this).data('product_sku')) : ('#' + $(this).data('product_id'))"; // Product SKU or ID
427
-
428
- $this->event_tracking_code( $parameters, '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' );
429
- }
430
-
431
-
432
- /**
433
- * Google Analytics event tracking for loop add to cart
434
- *
435
- * @access private
436
- * @param mixed $parameters associative array of _trackEvent parameters
437
- * @param mixed $selector jQuery selector for binding click event
438
- * @return void
439
- */
440
- private function event_tracking_code( $parameters, $selector ) {
441
- global $woocommerce;
442
-
443
- $parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters );
444
-
445
- if ( $this->ga_use_universal_analytics == 'yes' ) {
446
- $track_event = "ga('send', 'event', %s, %s, %s);";
447
- } else {
448
- $track_event = "_gaq.push(['_trackEvent', %s, %s, %s]);";
449
- }
450
-
451
- wc_enqueue_js("
452
- $('" . $selector . "').click(function() {
453
- " . sprintf( $track_event, $parameters['category'], $parameters['action'], $parameters['label'] ) . "
454
- });
455
- ");
456
- }
457
-
458
-
459
- /**
460
- * Check if tracking is disabled
461
- *
462
- * @access private
463
- * @param mixed $type
464
- * @return bool
465
- */
466
- private function disable_tracking( $type ) {
467
-
468
- if ( is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' == $type ) {
469
- return true;
470
- }
471
-
472
- }
473
-
474
-
475
- /**
476
- * Add the utm_nooverride parameter to any return urls. This makes sure Google Adwords doesn't mistake the offsite gateway as the referrer.
477
- *
478
- * @access public
479
- * @param string $type
480
- * @since 1.2.1
481
- * @return string
482
- */
483
- public function utm_nooverride( $return_url ) {
484
-
485
- // we don't know if the URL already has the parameter so we should remove it just in case
486
- $return_url = remove_query_arg( 'utm_nooverride', $return_url );
487
-
488
- // now add the utm_nooverride query arg to the URL
489
- $return_url = add_query_arg( 'utm_nooverride', '1', $return_url );
490
-
491
- return $return_url;
492
- }
493
-
494
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/class-wc-google-analytics.php ADDED
@@ -0,0 +1,547 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ( ! defined( 'ABSPATH' ) ) {
3
+ exit; // Exit if accessed directly
4
+ }
5
+
6
+ /**
7
+ * Google Analytics Integration
8
+ *
9
+ * Allows tracking code to be inserted into store pages.
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';
23
+ $this->method_title = __( 'Google Analytics', 'woocommerce-google-analytics-integration' );
24
+ $this->method_description = __( 'Google Analytics is a free service offered by Google that generates detailed statistics about the visitors to a website.', 'woocommerce-google-analytics-integration' );
25
+
26
+ // Load the settings.
27
+ $this->init_form_fields();
28
+ $this->init_settings();
29
+
30
+ // Define user set variables
31
+ $this->ga_id = $this->get_option( 'ga_id' );
32
+ $this->ga_set_domain_name = $this->get_option( 'ga_set_domain_name' );
33
+ $this->ga_standard_tracking_enabled = $this->get_option( 'ga_standard_tracking_enabled' );
34
+ $this->ga_support_display_advertising = $this->get_option( 'ga_support_display_advertising' );
35
+ $this->ga_use_universal_analytics = $this->get_option( 'ga_use_universal_analytics' );
36
+ $this->ga_anonymize_enabled = $this->get_option( 'ga_anonymize_enabled' );
37
+ $this->ga_ecommerce_tracking_enabled = $this->get_option( 'ga_ecommerce_tracking_enabled' );
38
+ $this->ga_event_tracking_enabled = $this->get_option( 'ga_event_tracking_enabled' );
39
+
40
+ // Actions
41
+ add_action( 'woocommerce_update_options_integration_google_analytics', array( $this, 'process_admin_options') );
42
+
43
+ // Tracking code
44
+ add_action( 'wp_head', array( $this, 'tracking_code_display' ), 999999 );
45
+
46
+ // Event tracking code
47
+ add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) );
48
+ add_action( 'wp_footer', array( $this, 'loop_add_to_cart' ) );
49
+
50
+ // utm_nooverride parameter for Google AdWords
51
+ add_filter( 'woocommerce_get_return_url', array( $this, 'utm_nooverride' ) );
52
+ }
53
+
54
+ /**
55
+ * Initialise Settings Form Fields
56
+ *
57
+ * @return void
58
+ */
59
+ public function init_form_fields() {
60
+
61
+ $this->form_fields = array(
62
+ 'ga_id' => array(
63
+ 'title' => __( 'Google Analytics ID', 'woocommerce-google-analytics-integration' ),
64
+ 'description' => __( 'Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce-google-analytics-integration' ),
65
+ 'type' => 'text',
66
+ 'default' => get_option( 'woocommerce_ga_id' ) // Backwards compat
67
+ ),
68
+ 'ga_set_domain_name' => array(
69
+ 'title' => __( 'Set Domain Name', 'woocommerce-google-analytics-integration' ),
70
+ 'description' => sprintf( __( '(Optional) Sets the <code>_setDomainName</code> variable. <a href="%s">See here for more information</a>.', 'woocommerce-google-analytics-integration' ), 'https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#multipleDomains' ),
71
+ 'type' => 'text',
72
+ 'default' => ''
73
+ ),
74
+ 'ga_standard_tracking_enabled' => array(
75
+ 'title' => __( 'Tracking code', 'woocommerce-google-analytics-integration' ),
76
+ 'label' => __( 'Add tracking code to your site. You don\'t need to enable this if using a 3rd party analytics plugin.', 'woocommerce-google-analytics-integration' ),
77
+ 'type' => 'checkbox',
78
+ 'checkboxgroup' => 'start',
79
+ 'default' => get_option( 'woocommerce_ga_standard_tracking_enabled' ) ? get_option( 'woocommerce_ga_standard_tracking_enabled' ) : 'no' // Backwards compat
80
+ ),
81
+ 'ga_support_display_advertising' => array(
82
+ '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-google-analytics-integration' ),
83
+ 'type' => 'checkbox',
84
+ 'checkboxgroup' => '',
85
+ 'default' => get_option( 'woocommerce_ga_support_display_advertising' ) ? get_option( 'woocommerce_ga_support_display_advertising' ) : 'no' // Backwards compat
86
+ ),
87
+ 'ga_use_universal_analytics' => array(
88
+ 'label' => __( 'Use Universal Analytics instead of Classic Google Analytics', 'woocommerce-google-analytics-integration' ),
89
+ 'type' => 'checkbox',
90
+ 'checkboxgroup' => '',
91
+ 'default' => get_option( 'woocommerce_ga_use_universal_analytics' ) ? get_option( 'woocommerce_ga_use_universal_analytics' ) : 'no' // Backwards compat
92
+ ),
93
+ 'ga_anonymize_enabled' => array(
94
+ 'label' => __( 'Anonymize IP addresses. Setting this option is mandatory in certain countries due to national privacy laws. <a href="https://support.google.com/analytics/answer/2763052" target="_blank">Read more about IP Anonymization</a>.', 'woocommerce-google-analytics-integration' ),
95
+ 'type' => 'checkbox',
96
+ 'checkboxgroup' => '',
97
+ 'default' => 'no'
98
+ ),
99
+ 'ga_ecommerce_tracking_enabled' => array(
100
+ 'label' => __( 'Add eCommerce tracking code to the thankyou page', 'woocommerce-google-analytics-integration' ),
101
+ 'type' => 'checkbox',
102
+ 'checkboxgroup' => '',
103
+ 'default' => get_option( 'woocommerce_ga_ecommerce_tracking_enabled' ) ? get_option( 'woocommerce_ga_ecommerce_tracking_enabled' ) : 'no' // Backwards compat
104
+ ),
105
+ 'ga_event_tracking_enabled' => array(
106
+ 'label' => __( 'Add event tracking code for add to cart actions', 'woocommerce-google-analytics-integration' ),
107
+ 'type' => 'checkbox',
108
+ 'checkboxgroup' => 'end',
109
+ 'default' => 'no'
110
+ )
111
+ );
112
+ }
113
+
114
+ /**
115
+ * Display the tracking codes
116
+ *
117
+ * @return string
118
+ */
119
+ public function tracking_code_display() {
120
+ global $wp;
121
+ $display_ecommerce_tracking = false;
122
+
123
+ if ( is_admin() || current_user_can( 'manage_options' ) || ! $this->ga_id ) {
124
+ return;
125
+ }
126
+
127
+ // Check if is order received page and stop when the products and not tracked
128
+ if ( is_order_received_page() && 'yes' == $this->ga_ecommerce_tracking_enabled ) {
129
+ $order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0;
130
+
131
+ if ( 0 < $order_id && 1 != get_post_meta( $order_id, '_ga_tracked', true ) ) {
132
+ $display_ecommerce_tracking = true;
133
+
134
+ echo $this->get_ecommerce_tracking_code( $order_id );
135
+ }
136
+ }
137
+
138
+ if ( ! $display_ecommerce_tracking && 'yes' == $this->ga_standard_tracking_enabled ) {
139
+ echo $this->get_google_tracking_code();
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Get the generic Google Analytics code snippet
145
+ *
146
+ * @return string
147
+ */
148
+ protected function get_generic_ga_code() {
149
+ return "
150
+ <script>
151
+ var gaProperty = '" . esc_js( $this->ga_id ) . "';
152
+ var disableStr = 'ga-disable-' + gaProperty;
153
+ if (document.cookie.indexOf(disableStr + '=true') > -1) {
154
+ window[disableStr] = true;
155
+ }
156
+ function gaOptout() {
157
+ document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
158
+ window[disableStr] = true;
159
+ }
160
+ </script>
161
+ ";
162
+ }
163
+
164
+ /**
165
+ * Google Analytics standard tracking
166
+ *
167
+ * @return string
168
+ */
169
+ protected function get_google_tracking_code() {
170
+ $logged_in = ( is_user_logged_in() ) ? 'yes' : 'no';
171
+
172
+ if ( 'yes' === $logged_in ) {
173
+ $user_id = get_current_user_id();
174
+ $current_user = get_user_by('id', $user_id);
175
+ $username = $current_user->user_login;
176
+ } else {
177
+ $user_id = '';
178
+ $username = __( 'Guest', 'woocommerce-google-analytics-integration' );
179
+ }
180
+
181
+ if ( 'yes' == $this->ga_use_universal_analytics ) {
182
+ if ( ! empty( $this->ga_set_domain_name ) ) {
183
+ $set_domain_name = esc_js( $this->ga_set_domain_name );
184
+ } else {
185
+ $set_domain_name = 'auto';
186
+ }
187
+
188
+ $support_display_advertising = '';
189
+ if ( 'yes' == $this->ga_support_display_advertising ) {
190
+ $support_display_advertising = "ga('require', 'displayfeatures');";
191
+ }
192
+
193
+ $anonymize_enabled = '';
194
+ if ( 'yes' == $this->ga_anonymize_enabled ) {
195
+ $anonymize_enabled = "ga('set', 'anonymizeIp', true);";
196
+ }
197
+
198
+ $code = "
199
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
200
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
201
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
202
+ })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
203
+
204
+ ga('create', '" . esc_js( $this->ga_id ) . "', '" . $set_domain_name . "');" .
205
+ $support_display_advertising .
206
+ $anonymize_enabled . "
207
+ ga('set', 'dimension1', '" . $logged_in . "');
208
+ ga('send', 'pageview');
209
+ ";
210
+
211
+ } else {
212
+ if ( 'yes' == $this->ga_support_display_advertising ) {
213
+ $ga_url = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'";
214
+ } else {
215
+ $ga_url = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'";
216
+ }
217
+
218
+ $anonymize_enabled = '';
219
+ if ( 'yes' == $this->ga_anonymize_enabled ) {
220
+ $anonymize_enabled = "['_gat._anonymizeIp'],";
221
+ }
222
+
223
+ if ( ! empty( $this->ga_set_domain_name ) ) {
224
+ $set_domain_name = "['_setDomainName', '" . esc_js( $this->ga_set_domain_name ) . "'],\n";
225
+ } else {
226
+ $set_domain_name = '';
227
+ }
228
+
229
+ $code = "
230
+ var _gaq = _gaq || [];
231
+ _gaq.push(
232
+ ['_setAccount', '" . esc_js( $this->ga_id ) . "'], " . $set_domain_name .
233
+ $anonymize_enabled . "
234
+ ['_setCustomVar', 1, 'logged-in', '" . $logged_in . "', 1],
235
+ ['_trackPageview']
236
+ );
237
+
238
+ (function() {
239
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
240
+ ga.src = " . $ga_url . ";
241
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
242
+ })();
243
+ ";
244
+ }
245
+
246
+ return "
247
+ <!-- WooCommerce Google Analytics Integration -->
248
+ " . $this->get_generic_ga_code() . "
249
+ <script type='text/javascript'>$code</script>
250
+
251
+ <!-- /WooCommerce Google Analytics Integration -->
252
+
253
+ ";
254
+
255
+ }
256
+
257
+ /**
258
+ * Google Analytics eCommerce tracking
259
+ *
260
+ * @param int $order_id
261
+ *
262
+ * @return string
263
+ */
264
+ protected function get_ecommerce_tracking_code( $order_id ) {
265
+ // Get the order and output tracking code
266
+ $order = new WC_Order( $order_id );
267
+
268
+ $logged_in = is_user_logged_in() ? 'yes' : 'no';
269
+
270
+ if ( 'yes' === $logged_in ) {
271
+ $user_id = get_current_user_id();
272
+ $current_user = get_user_by( 'id', $user_id );
273
+ $username = $current_user->user_login;
274
+ } else {
275
+ $user_id = '';
276
+ $username = __( 'Guest', 'woocommerce-google-analytics-integration' );
277
+ }
278
+
279
+ if ( 'yes' == $this->ga_use_universal_analytics ) {
280
+ if ( ! empty( $this->ga_set_domain_name ) ) {
281
+ $set_domain_name = esc_js( $this->ga_set_domain_name );
282
+ } else {
283
+ $set_domain_name = 'auto';
284
+ }
285
+
286
+ $support_display_advertising = '';
287
+ if ( 'yes' == $this->ga_support_display_advertising ) {
288
+ $support_display_advertising = "ga('require', 'displayfeatures');";
289
+ }
290
+
291
+ $anonymize_enabled = '';
292
+ if ( 'yes' == $this->ga_anonymize_enabled ) {
293
+ $anonymize_enabled = "ga('set', 'anonymizeIp', true);";
294
+ }
295
+
296
+ $code = "
297
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
298
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
299
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
300
+ })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
301
+
302
+ ga('create', '" . esc_js( $this->ga_id ) . "', '" . $set_domain_name . "');" .
303
+ $support_display_advertising .
304
+ $anonymize_enabled . "
305
+ ga('set', 'dimension1', '" . $logged_in . "');
306
+ ga('send', 'pageview');
307
+
308
+ ga('require', 'ecommerce', 'ecommerce.js');
309
+
310
+ ga('ecommerce:addTransaction', {
311
+ 'id': '" . esc_js( $order->get_order_number() ) . "', // Transaction ID. Required
312
+ 'affiliation': '" . esc_js( get_bloginfo( 'name' ) ) . "', // Affiliation or store name
313
+ 'revenue': '" . esc_js( $order->get_total() ) . "', // Grand Total
314
+ 'shipping': '" . esc_js( $order->get_total_shipping() ) . "', // Shipping
315
+ 'tax': '" . esc_js( $order->get_total_tax() ) . "', // Tax
316
+ 'currency': '" . esc_js( $order->get_order_currency() ) . "' // Currency
317
+ });
318
+ ";
319
+
320
+ // Order items
321
+ if ( $order->get_items() ) {
322
+ foreach ( $order->get_items() as $item ) {
323
+ $_product = $order->get_product_from_item( $item );
324
+
325
+ $code .= "ga('ecommerce:addItem', {";
326
+ $code .= "'id': '" . esc_js( $order->get_order_number() ) . "',";
327
+ $code .= "'name': '" . esc_js( $item['name'] ) . "',";
328
+ $code .= "'sku': '" . esc_js( $_product->get_sku() ? $_product->get_sku() : $_product->id ) . "',";
329
+
330
+ if ( isset( $_product->variation_data ) ) {
331
+
332
+ $code .= "'category': '" . esc_js( woocommerce_get_formatted_variation( $_product->variation_data, true ) ) . "',";
333
+
334
+ } else {
335
+ $out = array();
336
+ $categories = get_the_terms($_product->id, 'product_cat');
337
+ if ( $categories ) {
338
+ foreach ( $categories as $category ) {
339
+ $out[] = $category->name;
340
+ }
341
+ }
342
+ $code .= "'category': '" . esc_js( join( "/", $out) ) . "',";
343
+ }
344
+
345
+ $code .= "'price': '" . esc_js( $order->get_item_total( $item ) ) . "',";
346
+ $code .= "'quantity': '" . esc_js( $item['qty'] ) . "'";
347
+ $code .= "});";
348
+ }
349
+ }
350
+
351
+ $code .= "ga('ecommerce:send'); // Send transaction and item data to Google Analytics.";
352
+
353
+ } else {
354
+ if ( $this->ga_support_display_advertising == 'yes' ) {
355
+ $ga_url = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'";
356
+ } else {
357
+ $ga_url = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'";
358
+ }
359
+
360
+ $anonymize_enabled = '';
361
+ if ( 'yes' == $this->ga_anonymize_enabled ) {
362
+ $anonymize_enabled = "['_gat._anonymizeIp'],";
363
+ }
364
+
365
+ if ( ! empty( $this->ga_set_domain_name ) ) {
366
+ $set_domain_name = "['_setDomainName', '" . esc_js( $this->ga_set_domain_name ) . "'],";
367
+ } else {
368
+ $set_domain_name = '';
369
+ }
370
+
371
+ $code = "
372
+ var _gaq = _gaq || [];
373
+
374
+ _gaq.push(
375
+ ['_setAccount', '" . esc_js( $this->ga_id ) . "'], " . $set_domain_name .
376
+ $anonymize_enabled . "
377
+ ['_setCustomVar', 1, 'logged-in', '" . esc_js( $logged_in ) . "', 1],
378
+ ['_trackPageview'],
379
+ ['_set', 'currencyCode', '" . esc_js( $order->get_order_currency() ) . "']
380
+ );
381
+
382
+ _gaq.push(['_addTrans',
383
+ '" . esc_js( $order->get_order_number() ) . "', // order ID - required
384
+ '" . esc_js( get_bloginfo( 'name' ) ) . "', // affiliation or store name
385
+ '" . esc_js( $order->get_total() ) . "', // total - required
386
+ '" . esc_js( $order->get_total_tax() ) . "', // tax
387
+ '" . esc_js( $order->get_total_shipping() ) . "', // shipping
388
+ '" . esc_js( $order->billing_city ) . "', // city
389
+ '" . esc_js( $order->billing_state ) . "', // state or province
390
+ '" . esc_js( $order->billing_country ) . "' // country
391
+ ]);
392
+ ";
393
+
394
+ // Order items
395
+ if ( $order->get_items() ) {
396
+ foreach ( $order->get_items() as $item ) {
397
+ $_product = $order->get_product_from_item( $item );
398
+
399
+ $code .= "_gaq.push(['_addItem',";
400
+ $code .= "'" . esc_js( $order->get_order_number() ) . "',";
401
+ $code .= "'" . esc_js( $_product->get_sku() ? $_product->get_sku() : $_product->id ) . "',";
402
+ $code .= "'" . esc_js( $item['name'] ) . "',";
403
+
404
+ if ( isset( $_product->variation_data ) ) {
405
+
406
+ $code .= "'" . esc_js( woocommerce_get_formatted_variation( $_product->variation_data, true ) ) . "',";
407
+
408
+ } else {
409
+ $out = array();
410
+ $categories = get_the_terms($_product->id, 'product_cat');
411
+ if ( $categories ) {
412
+ foreach ( $categories as $category ){
413
+ $out[] = $category->name;
414
+ }
415
+ }
416
+ $code .= "'" . esc_js( join( "/", $out) ) . "',";
417
+ }
418
+
419
+ $code .= "'" . esc_js( $order->get_item_total( $item ) ) . "',";
420
+ $code .= "'" . esc_js( $item['qty'] ) . "'";
421
+ $code .= "]);";
422
+ }
423
+ }
424
+
425
+ $code .= "
426
+ _gaq.push(['_trackTrans']); // submits transaction to the Analytics servers
427
+
428
+ (function() {
429
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
430
+ ga.src = " . $ga_url . ";
431
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
432
+ })();
433
+ ";
434
+ }
435
+
436
+ // Mark the order as tracked
437
+ update_post_meta( $order_id, '_ga_tracked', 1 );
438
+
439
+ return "
440
+ <!-- WooCommerce Google Analytics Integration -->
441
+ " . $this->get_generic_ga_code() . "
442
+ <script type='text/javascript'>$code</script>
443
+ <!-- /WooCommerce Google Analytics Integration -->
444
+ ";
445
+ }
446
+
447
+ /**
448
+ * Check if tracking is disabled
449
+ *
450
+ * @param string $type
451
+ *
452
+ * @return bool
453
+ */
454
+ private function disable_tracking( $type ) {
455
+ if ( is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' == $type ) {
456
+ return true;
457
+ }
458
+ }
459
+
460
+ /**
461
+ * Google Analytics event tracking for single product add to cart
462
+ *
463
+ * @return void
464
+ */
465
+ public function add_to_cart() {
466
+
467
+ if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
468
+ return;
469
+ }
470
+ if ( ! is_single() ) {
471
+ return;
472
+ }
473
+
474
+ global $product;
475
+
476
+ $parameters = array();
477
+ // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
478
+ $parameters['category'] = "'" . __( 'Products', 'woocommerce-google-analytics-integration' ) . "'";
479
+ $parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce-google-analytics-integration' ) . "'";
480
+ $parameters['label'] = "'" . esc_js( $product->get_sku() ? __( 'SKU:', 'woocommerce-google-analytics-integration' ) . ' ' . $product->get_sku() : "#" . $product->id ) . "'";
481
+
482
+ $this->event_tracking_code( $parameters, '.single_add_to_cart_button' );
483
+ }
484
+
485
+
486
+ /**
487
+ * Google Analytics event tracking for loop add to cart
488
+ *
489
+ * @return void
490
+ */
491
+ public function loop_add_to_cart() {
492
+
493
+ if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
494
+ return;
495
+ }
496
+
497
+ $parameters = array();
498
+ // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
499
+ $parameters['category'] = "'" . __( 'Products', 'woocommerce-google-analytics-integration' ) . "'";
500
+ $parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce-google-analytics-integration' ) . "'";
501
+ $parameters['label'] = "($(this).data('product_sku')) ? ('SKU: ' + $(this).data('product_sku')) : ('#' + $(this).data('product_id'))"; // Product SKU or ID
502
+
503
+ $this->event_tracking_code( $parameters, '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' );
504
+ }
505
+
506
+ /**
507
+ * Google Analytics event tracking for loop add to cart
508
+ *
509
+ * @param array $parameters associative array of _trackEvent parameters
510
+ * @param string $selector jQuery selector for binding click event
511
+ *
512
+ * @return void
513
+ */
514
+ private function event_tracking_code( $parameters, $selector ) {
515
+ $parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters );
516
+
517
+ if ( 'yes' == $this->ga_use_universal_analytics ) {
518
+ $track_event = "ga('send', 'event', %s, %s, %s);";
519
+ } else {
520
+ $track_event = "_gaq.push(['_trackEvent', %s, %s, %s]);";
521
+ }
522
+
523
+ wc_enqueue_js( "
524
+ $( '" . $selector . "' ).click( function() {
525
+ " . sprintf( $track_event, $parameters['category'], $parameters['action'], $parameters['label'] ) . "
526
+ });
527
+ " );
528
+ }
529
+
530
+ /**
531
+ * Add the utm_nooverride parameter to any return urls. This makes sure Google Adwords doesn't mistake the offsite gateway as the referrer.
532
+ *
533
+ * @param string $type
534
+ *
535
+ * @return string
536
+ */
537
+ public function utm_nooverride( $return_url ) {
538
+
539
+ // We don't know if the URL already has the parameter so we should remove it just in case
540
+ $return_url = remove_query_arg( 'utm_nooverride', $return_url );
541
+
542
+ // Now add the utm_nooverride query arg to the URL
543
+ $return_url = add_query_arg( 'utm_nooverride', '1', $return_url );
544
+
545
+ return $return_url;
546
+ }
547
+ }
languages/woocommerce-google-analytics-integration.pot ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (C) 2014 WooThemes
2
+ # This file is distributed under the GPLv2 or later.
3
+ msgid ""
4
+ msgstr ""
5
+ "Project-Id-Version: WooCommerce Google Analytics Integration 1.3.0\n"
6
+ "Report-Msgid-Bugs-To: "
7
+ "https://wordpress.org/support/plugin/woocommerce-google-analytics-"
8
+ "integration\n"
9
+ "POT-Creation-Date: 2014-11-11 21:09:47+00:00\n"
10
+ "MIME-Version: 1.0\n"
11
+ "Content-Type: text/plain; charset=utf-8\n"
12
+ "Content-Transfer-Encoding: 8bit\n"
13
+ "PO-Revision-Date: 2014-MO-DA HO:MI+ZONE\n"
14
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
15
+ "Language-Team: LANGUAGE <EMAIL@ADDRESS>\n"
16
+ "X-Generator: grunt-wp-i18n 0.4.9\n"
17
+
18
+ #: includes/class-wc-google-analytics.php:23
19
+ msgid "Google Analytics"
20
+ msgstr ""
21
+
22
+ #: includes/class-wc-google-analytics.php:24
23
+ msgid ""
24
+ "Google Analytics is a free service offered by Google that generates "
25
+ "detailed statistics about the visitors to a website."
26
+ msgstr ""
27
+
28
+ #: includes/class-wc-google-analytics.php:63
29
+ msgid "Google Analytics ID"
30
+ msgstr ""
31
+
32
+ #: includes/class-wc-google-analytics.php:64
33
+ msgid ""
34
+ "Log into your google analytics account to find your ID. e.g. "
35
+ "<code>UA-XXXXX-X</code>"
36
+ msgstr ""
37
+
38
+ #: includes/class-wc-google-analytics.php:69
39
+ msgid "Set Domain Name"
40
+ msgstr ""
41
+
42
+ #: includes/class-wc-google-analytics.php:70
43
+ msgid ""
44
+ "(Optional) Sets the <code>_setDomainName</code> variable. <a "
45
+ "href=\"%s\">See here for more information</a>."
46
+ msgstr ""
47
+
48
+ #: includes/class-wc-google-analytics.php:75
49
+ msgid "Tracking code"
50
+ msgstr ""
51
+
52
+ #: includes/class-wc-google-analytics.php:76
53
+ msgid ""
54
+ "Add tracking code to your site. You don't need to enable this if using a "
55
+ "3rd party analytics plugin."
56
+ msgstr ""
57
+
58
+ #: includes/class-wc-google-analytics.php:82
59
+ msgid ""
60
+ "Set the Google Analytics code to support Display Advertising. <a "
61
+ "href=\"https://support.google.com/analytics/answer/2700409\" "
62
+ "target=\"_blank\">Read more about Display Advertising</a>."
63
+ msgstr ""
64
+
65
+ #: includes/class-wc-google-analytics.php:88
66
+ msgid "Use Universal Analytics instead of Classic Google Analytics"
67
+ msgstr ""
68
+
69
+ #: includes/class-wc-google-analytics.php:94
70
+ msgid ""
71
+ "Anonymize IP addresses. Setting this option is mandatory in certain "
72
+ "countries due to national privacy laws. <a "
73
+ "href=\"https://support.google.com/analytics/answer/2763052\" "
74
+ "target=\"_blank\">Read more about IP Anonymization</a>."
75
+ msgstr ""
76
+
77
+ #: includes/class-wc-google-analytics.php:100
78
+ msgid "Add eCommerce tracking code to the thankyou page"
79
+ msgstr ""
80
+
81
+ #: includes/class-wc-google-analytics.php:106
82
+ msgid "Add event tracking code for add to cart actions"
83
+ msgstr ""
84
+
85
+ #: includes/class-wc-google-analytics.php:178
86
+ #: includes/class-wc-google-analytics.php:276
87
+ msgid "Guest"
88
+ msgstr ""
89
+
90
+ #: includes/class-wc-google-analytics.php:478
91
+ #: includes/class-wc-google-analytics.php:499
92
+ msgid "Products"
93
+ msgstr ""
94
+
95
+ #: includes/class-wc-google-analytics.php:479
96
+ #: includes/class-wc-google-analytics.php:500
97
+ msgid "Add to Cart"
98
+ msgstr ""
99
+
100
+ #: includes/class-wc-google-analytics.php:480
101
+ msgid "SKU:"
102
+ msgstr ""
103
+
104
+ #: woocommerce-google-analytics-integration.php:89
105
+ msgid "WooCommerce Google Analytics depends on the last version of %s to work!"
106
+ msgstr ""
107
+
108
+ #: woocommerce-google-analytics-integration.php:89
109
+ msgid "WooCommerce"
110
+ msgstr ""
111
+
112
+ #. Plugin Name of the plugin/theme
113
+ msgid "WooCommerce Google Analytics Integration"
114
+ msgstr ""
115
+
116
+ #. Plugin URI of the plugin/theme
117
+ msgid "http://wordpress.org/plugins/woocommerce-google-analytics-integration/"
118
+ msgstr ""
119
+
120
+ #. Description of the plugin/theme
121
+ msgid ""
122
+ "Allows Google Analytics tracking code to be inserted into WooCommerce store "
123
+ "pages."
124
+ msgstr ""
125
+
126
+ #. Author of the plugin/theme
127
+ msgid "WooThemes"
128
+ msgstr ""
129
+
130
+ #. Author URI of the plugin/theme
131
+ msgid "http://woothemes.com"
132
+ msgstr ""
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === WooCommerce Google Analytics Integration ===
2
- Contributors: woothemes
3
  Tags: woocommerce, google analytics
4
  Requires at least: 3.8
5
  Tested up to: 4.0
6
- Stable tag: 1.2.1
7
  License: GPLv3
8
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
 
@@ -36,7 +36,11 @@ This plugin will add the settings to the Integration tab, to be found in the Woo
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
 
@@ -46,21 +50,50 @@ Until you've updated to WooCommerce 2.1, this plugin puts itself in some sort of
46
 
47
  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.
48
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  == Changelog ==
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  = 1.2.1 - 17/09/2014 =
52
- * Tweak - Adding utmnooverride to return url for Google Adwords
 
53
 
54
  = 1.2.0 - 28/07/2014 =
55
- * Feature - Adding display advertising parameter to Universal Analytics
56
- * Fix - Using get_total_shipping() instead of get_shipping
57
- * Fix - Using wc_enqueue_js() instead of $woocommerce->add_inline_js(
58
- * Tweak - Updating plugin FAQ
59
- * Tweak - Adding parenthesis for clarity
 
60
 
61
  = 1.1 - 29/05/2014 =
62
- * Added option to enable Display Advertising
63
- * Added compatibility support for WooCommerce 2.1 beta releases
 
64
 
65
  = 1.0 - 22/11/2013 =
66
- * Initial release
 
1
  === WooCommerce Google Analytics Integration ===
2
+ Contributors: woothemes, claudiosanches
3
  Tags: woocommerce, google analytics
4
  Requires at least: 3.8
5
  Tested up to: 4.0
6
+ Stable tag: 1.3.0
7
  License: GPLv3
8
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
 
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
 
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 national data privacy laws require that I offer an opt-out for users, how can I do this? =
54
+
55
+ Include the following html code snippet within the page where you want to have the opt-out, e.g. the your Imprint our Data Privacy page:
56
+
57
+ https://gist.github.com/claudiosmweb/b12d15b245be21c92ebc
58
+
59
+ Exact wording depends on the national data privacy laws and should be adjusted.
60
+
61
+ == Screenshots ==
62
+
63
+ 1. Google Analytics Integration Settings.
64
+
65
  == Changelog ==
66
 
67
+ = 1.3.0 - 12/11/2014 =
68
+
69
+ * Feature - Added the transaction currency in the tracking code
70
+ * Feature - Add data privacy option that are mandatory in some countries
71
+ * Tweak - Moved the tracking code to the head of the page
72
+ * Tweak - Remove the "SKU" prefix to the sku for addItem
73
+ * Refactor - Integration class reformulated
74
+
75
+ = 1.2.2 - 15/10/2014 =
76
+
77
+ * Feature - Adding option to anonymize IP addresses
78
+ * Feature - Adding gaOptOut function to be called from any page for OptOut
79
+
80
  = 1.2.1 - 17/09/2014 =
81
+
82
+ * Tweak - Adding utmnooverride to return url for Google Adwords
83
 
84
  = 1.2.0 - 28/07/2014 =
85
+
86
+ * Feature - Adding display advertising parameter to Universal Analytics
87
+ * Fix - Using get_total_shipping() instead of get_shipping
88
+ * Fix - Using wc_enqueue_js() instead of $woocommerce->add_inline_js(
89
+ * Tweak - Updating plugin FAQ
90
+ * Tweak - Adding parenthesis for clarity
91
 
92
  = 1.1 - 29/05/2014 =
93
+
94
+ * Added option to enable Display Advertising
95
+ * Added compatibility support for WooCommerce 2.1 beta releases
96
 
97
  = 1.0 - 22/11/2013 =
98
+
99
+ * Initial release
woocommerce-google-analytics-integration.php CHANGED
@@ -1,23 +1,108 @@
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.1
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-beta-1', '>=' ) ) {
16
- include_once( 'includes/class-wc-google-analytics-integration.php' );
17
- $integrations[] = 'WC_Google_Analytics';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  }
19
 
20
- return $integrations;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  }
22
 
23
- add_filter( 'woocommerce_integrations', 'wc_google_analytics_add_integration', 10 );
 
 
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://woothemes.com
8
+ * Version: 1.3.0
9
+ * License: GPLv2 or later
10
+ * Text Domain: woocommerce-google-analytics-integration
11
+ * Domain Path: languages/
12
+ */
13
+
14
+ if ( ! defined( 'ABSPATH' ) ) {
15
+ exit;
16
+ }
17
+
18
+ if ( ! class_exists( 'WC_Google_Analytics_Integration' ) ) :
19
+
20
+ /**
21
+ * WooCommerce Google Analytics Integration main class.
22
+ */
23
+ class WC_Google_Analytics_Integration {
24
+
25
+ /**
26
+ * Plugin version.
27
+ *
28
+ * @var string
29
+ */
30
+ const VERSION = '1.3.0';
31
+
32
+ /**
33
+ * Instance of this class.
34
+ *
35
+ * @var object
36
+ */
37
+ protected static $instance = null;
38
+
39
+ /**
40
+ * Initialize the plugin.
41
+ */
42
+ private function __construct() {
43
+ // Load plugin text domain
44
+ add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
45
+
46
+ // Checks with WooCommerce is installed.
47
+ if ( class_exists( 'WC_Integration' ) && defined( 'WOOCOMMERCE_VERSION' ) && version_compare( WOOCOMMERCE_VERSION, '2.1-beta-1', '>=' ) ) {
48
+ include_once 'includes/class-wc-google-analytics.php';
49
+
50
+ // Register the integration.
51
+ add_filter( 'woocommerce_integrations', array( $this, 'add_integration' ) );
52
+ } else {
53
+ add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Return an instance of this class.
59
+ *
60
+ * @return object A single instance of this class.
61
+ */
62
+ public static function get_instance() {
63
+ // If the single instance hasn't been set, set it now.
64
+ if ( null == self::$instance ) {
65
+ self::$instance = new self;
66
+ }
67
+
68
+ return self::$instance;
69
+ }
70
+
71
+ /**
72
+ * Load the plugin text domain for translation.
73
+ *
74
+ * @return void
75
+ */
76
+ public function load_plugin_textdomain() {
77
+ $locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-google-analytics-integration' );
78
+
79
+ load_textdomain( 'woocommerce-google-analytics-integration', trailingslashit( WP_LANG_DIR ) . 'woocommerce-google-analytics-integration/woocommerce-google-analytics-integration-' . $locale . '.mo' );
80
+ load_plugin_textdomain( 'woocommerce-google-analytics-integration', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
81
  }
82
 
83
+ /**
84
+ * WooCommerce fallback notice.
85
+ *
86
+ * @return string
87
+ */
88
+ public function woocommerce_missing_notice() {
89
+ 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>';
90
+ }
91
+
92
+ /**
93
+ * Add a new integration to WooCommerce.
94
+ *
95
+ * @param array $integrations WooCommerce integrations.
96
+ *
97
+ * @return array Google Analytics integration.
98
+ */
99
+ public function add_integration( $integrations ) {
100
+ $integrations[] = 'WC_Google_Analytics';
101
+
102
+ return $integrations;
103
+ }
104
  }
105
 
106
+ add_action( 'plugins_loaded', array( 'WC_Google_Analytics_Integration', 'get_instance' ), 0 );
107
+
108
+ endif;