WooCommerce Google Analytics Integration - Version 1.0

Version Description

  • 22/11/2013 =
    • Initial release
Download this release

Release Info

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

Version 1.0

README.md ADDED
@@ -0,0 +1,5 @@
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 ADDED
@@ -0,0 +1,326 @@
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_ecommerce_tracking_enabled = $this->get_option( 'ga_ecommerce_tracking_enabled' );
33
+ $this->ga_event_tracking_enabled = $this->get_option( 'ga_event_tracking_enabled' );
34
+
35
+ // Actions
36
+ add_action( 'woocommerce_update_options_integration_google_analytics', array( $this, 'process_admin_options') );
37
+
38
+ // Tracking code
39
+ add_action( 'wp_footer', array( $this, 'google_tracking_code' ) );
40
+ add_action( 'woocommerce_thankyou', array( $this, 'ecommerce_tracking_code' ) );
41
+
42
+ // Event tracking code
43
+ add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) );
44
+ add_action( 'wp_footer', array( $this, 'loop_add_to_cart' ) );
45
+ }
46
+
47
+
48
+ /**
49
+ * Initialise Settings Form Fields
50
+ *
51
+ * @access public
52
+ * @return void
53
+ */
54
+ function init_form_fields() {
55
+
56
+ $this->form_fields = array(
57
+ 'ga_id' => array(
58
+ 'title' => __( 'Google Analytics ID', 'woocommerce' ),
59
+ 'description' => __( 'Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce' ),
60
+ 'type' => 'text',
61
+ 'default' => get_option('woocommerce_ga_id') // Backwards compat
62
+ ),
63
+ 'ga_set_domain_name' => array(
64
+ 'title' => __( 'Set Domain Name', 'woocommerce' ),
65
+ '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' ),
66
+ 'type' => 'text',
67
+ 'default' => ''
68
+ ),
69
+ 'ga_standard_tracking_enabled' => array(
70
+ 'title' => __( 'Tracking code', 'woocommerce' ),
71
+ 'label' => __( 'Add tracking code to your site. You don\'t need to enable this if using a 3rd party analytics plugin.', 'woocommerce' ),
72
+ 'type' => 'checkbox',
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',
79
+ 'checkboxgroup' => '',
80
+ 'default' => get_option('woocommerce_ga_ecommerce_tracking_enabled') ? get_option('woocommerce_ga_ecommerce_tracking_enabled') : 'no' // Backwards compat
81
+ ),
82
+ 'ga_event_tracking_enabled' => array(
83
+ 'label' => __( 'Add event tracking code for add to cart actions', 'woocommerce' ),
84
+ 'type' => 'checkbox',
85
+ 'checkboxgroup' => 'end',
86
+ 'default' => 'no'
87
+ )
88
+ );
89
+
90
+ } // End init_form_fields()
91
+
92
+
93
+ /**
94
+ * Google Analytics standard tracking
95
+ *
96
+ * @access public
97
+ * @return void
98
+ */
99
+ function google_tracking_code() {
100
+ global $woocommerce;
101
+
102
+ if ( is_admin() || current_user_can('manage_options') || $this->ga_standard_tracking_enabled == "no" ) return;
103
+
104
+ $tracking_id = $this->ga_id;
105
+
106
+ if ( ! $tracking_id ) return;
107
+
108
+ $loggedin = ( is_user_logged_in() ) ? 'yes' : 'no';
109
+ if ( is_user_logged_in() ) {
110
+ $user_id = get_current_user_id();
111
+ $current_user = get_user_by('id', $user_id);
112
+ $username = $current_user->user_login;
113
+ } else {
114
+ $user_id = '';
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
+
142
+ /**
143
+ * Google Analytics eCommerce tracking
144
+ *
145
+ * @access public
146
+ * @param mixed $order_id
147
+ * @return void
148
+ */
149
+ function ecommerce_tracking_code( $order_id ) {
150
+ global $woocommerce;
151
+
152
+ if ( $this->ga_ecommerce_tracking_enabled == "no" || current_user_can('manage_options') || get_post_meta( $order_id, '_ga_tracked', true ) == 1 )
153
+ return;
154
+
155
+ $tracking_id = $this->ga_id;
156
+
157
+ if ( ! $tracking_id ) return;
158
+
159
+ // Doing eCommerce tracking so unhook standard tracking from the footer
160
+ remove_action( 'wp_footer', array( $this, 'google_tracking_code' ) );
161
+
162
+ // Get the order and output tracking code
163
+ $order = new WC_Order( $order_id );
164
+
165
+ $loggedin = is_user_logged_in() ? 'yes' : 'no';
166
+
167
+ if ( is_user_logged_in() ) {
168
+ $user_id = get_current_user_id();
169
+ $current_user = get_user_by('id', $user_id);
170
+ $username = $current_user->user_login;
171
+ } else {
172
+ $user_id = '';
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
+
245
+ update_post_meta( $order_id, '_ga_tracked', 1 );
246
+ }
247
+
248
+
249
+ /**
250
+ * Google Analytics event tracking for single product add to cart
251
+ *
252
+ * @access public
253
+ * @return void
254
+ */
255
+ function add_to_cart() {
256
+
257
+ if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) return;
258
+ if ( ! is_single() ) return;
259
+
260
+ global $product;
261
+
262
+ $parameters = array();
263
+ // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
264
+ $parameters['category'] = "'" . __( 'Products', 'woocommerce' ) . "'";
265
+ $parameters['action'] = "'" . __( 'Add to cart', 'woocommerce' ) . "'";
266
+ $parameters['label'] = "'" . esc_js( $product->get_sku() ? __('SKU:', 'woocommerce') . ' ' . $product->get_sku() : "#" . $product->id ) . "'";
267
+
268
+ $this->event_tracking_code( $parameters, '.single_add_to_cart_button' );
269
+ }
270
+
271
+
272
+ /**
273
+ * Google Analytics event tracking for loop add to cart
274
+ *
275
+ * @access public
276
+ * @return void
277
+ */
278
+ function loop_add_to_cart() {
279
+
280
+ if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) return;
281
+
282
+ $parameters = array();
283
+ // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
284
+ $parameters['category'] = "'" . __( 'Products', 'woocommerce' ) . "'";
285
+ $parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce' ) . "'";
286
+ $parameters['label'] = "($(this).data('product_sku')) ? ('SKU: ' + $(this).data('product_sku')) : ('#' + $(this).data('product_id'))"; // Product SKU or ID
287
+
288
+ $this->event_tracking_code( $parameters, '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' );
289
+ }
290
+
291
+
292
+ /**
293
+ * Google Analytics event tracking for loop add to cart
294
+ *
295
+ * @access private
296
+ * @param mixed $parameters associative array of _trackEvent parameters
297
+ * @param mixed $selector jQuery selector for binding click event
298
+ * @return void
299
+ */
300
+ private function event_tracking_code( $parameters, $selector ) {
301
+ global $woocommerce;
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
+ }
311
+
312
+
313
+ /**
314
+ * Check if tracking is disabled
315
+ *
316
+ * @access private
317
+ * @param mixed $type
318
+ * @return bool
319
+ */
320
+ private function disable_tracking( $type ) {
321
+
322
+ if ( is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' == $type ) return true;
323
+
324
+ }
325
+
326
+ }
readme.txt ADDED
@@ -0,0 +1,45 @@
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.0
7
+ License: GPLv3
8
+ License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
+
10
+ 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
+
18
+ Contributions are welcome via the [GitHub repository](https://github.com/woothemes/woocommerce-google-analytics-integration).
19
+
20
+ == Installation ==
21
+
22
+ 1. Download the plugin file to your computer and unzip it
23
+ 2. Using an FTP program, or your hosting control panel, upload the unzipped plugin folder to your WordPress installation’s wp-content/plugins/ directory.
24
+ 3. Activate the plugin from the Plugins menu within the WordPress admin.
25
+
26
+ Or use the automatic installation wizard through your admin panel, just search for this plugins name.
27
+
28
+ == Frequently Asked Questions ==
29
+
30
+ = Where can I find the setting for this plugin? =
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.
37
+
38
+ Until you've updated to WooCommerce 2.1, this plugin puts itself in some sort of hibernate mode.
39
+
40
+ 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.
41
+
42
+ == Changelog ==
43
+
44
+ = 1.0 - 22/11/2013 =
45
+ * Initial release
woocommerce-google-analytics-integration.php ADDED
@@ -0,0 +1,23 @@
1
+ <?php
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
+ }
19
+
20
+ return $integrations;
21
+ }
22
+
23
+ add_filter( 'woocommerce_integrations', 'wc_google_analytics_add_integration', 10 );