Version Description
- 22/06/2014 =
- Initial release
Download this release
Release Info
Developer | Tatvic |
Plugin | Enhanced Ecommerce Google Analytics Plugin for WooCommerce |
Version | 1.0 |
Comparing to | |
See all releases |
Version 1.0
- includes/class-wc-enhanced-ecommerce-google-analytics-integration.php +593 -0
- readme.txt +73 -0
- trunk/includes/class-wc-enhanced-ecommerce-google-analytics-integration.php +593 -0
- trunk/readme.txt +73 -0
- trunk/woocommerce-enhanced-ecommerce-google-analytics-integration.php +39 -0
- woocommerce-enhanced-ecommerce-google-analytics-integration.php +39 -0
includes/class-wc-enhanced-ecommerce-google-analytics-integration.php
ADDED
@@ -0,0 +1,593 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
/**
|
4 |
+
* Enhanced Ecommerce for Woo-commerce stores
|
5 |
+
*
|
6 |
+
* Allows tracking code to be inserted into store pages.
|
7 |
+
*
|
8 |
+
* @class WC_Enhanced_Ecommerce_Google_Analytics
|
9 |
+
* @extends WC_Integration
|
10 |
+
* @author Sudhir Mishra <sudhirxps@gmail.com>
|
11 |
+
*/
|
12 |
+
class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
|
13 |
+
|
14 |
+
/**
|
15 |
+
* Init and hook in the integration.
|
16 |
+
*
|
17 |
+
* @access public
|
18 |
+
* @return void
|
19 |
+
*/
|
20 |
+
public function __construct() {
|
21 |
+
$this->id = 'enhanced_ecommerce_google_analytics';
|
22 |
+
$this->method_title = __('Enhanced Ecommerce Google Analytics', 'woocommerce');
|
23 |
+
$this->method_description = __('Enhanced Ecommerce is a new feature of Universal Analytics that generates detailed statistics about the users journey from product page to thank you page on your e-store. <br/><a href="https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce">Know more about enhanced e-commerce.</a>', 'woocommerce');
|
24 |
+
|
25 |
+
// Load the settings.
|
26 |
+
$this->init_form_fields();
|
27 |
+
$this->init_settings();
|
28 |
+
|
29 |
+
// Define user set variables
|
30 |
+
$this->ga_email = $this->get_option('ga_email');
|
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->enable_guest_checkout = get_option('woocommerce_enable_guest_checkout') == 'yes' ? true : false;
|
35 |
+
$this->track_login_step_for_guest_user = $this->get_option('track_login_step_for_guest_user') == 'yes' ? true : false;
|
36 |
+
$this->ga_enhanced_ecommerce_tracking_enabled = $this->get_option('ga_enhanced_ecommerce_tracking_enabled');
|
37 |
+
|
38 |
+
// Actions
|
39 |
+
add_action('woocommerce_update_options_integration_enhanced_ecommerce_google_analytics', array($this, 'process_admin_options'));
|
40 |
+
// API Call to LS with e-mail
|
41 |
+
// Tracking code
|
42 |
+
add_action('wp_head', array($this, 'google_tracking_code'));
|
43 |
+
add_action('woocommerce_thankyou', array($this, 'ecommerce_tracking_code'));
|
44 |
+
|
45 |
+
// Enhanced Ecommerce product impression hook
|
46 |
+
add_action('woocommerce_after_shop_loop_item', array($this, 'product_impression'));
|
47 |
+
add_action('woocommerce_after_single_product', array($this, 'product_detail_view'));
|
48 |
+
add_action('woocommerce_after_cart', array($this, 'remove_cart_tracking'));
|
49 |
+
add_action('woocommerce_after_checkout_form', array($this, 'checkout_step_one_tracking'));
|
50 |
+
|
51 |
+
// Event tracking code
|
52 |
+
add_action('woocommerce_after_add_to_cart_button', array($this, 'add_to_cart'));
|
53 |
+
add_action('wp_footer', array($this, 'loop_add_to_cart'));
|
54 |
+
add_action('wp_footer', array($this, 'default_pageview'));
|
55 |
+
}
|
56 |
+
|
57 |
+
/**
|
58 |
+
* Initialise Settings Form Fields
|
59 |
+
*
|
60 |
+
* @access public
|
61 |
+
* @return void
|
62 |
+
*/
|
63 |
+
function init_form_fields() {
|
64 |
+
|
65 |
+
$this->form_fields = array(
|
66 |
+
'ga_email' => array(
|
67 |
+
'title' => __('Email Address', 'woocommerce'),
|
68 |
+
'description' => __('Provide your work email for updates on the plugin enhancement.', 'woocommerce'),
|
69 |
+
'type' => 'email',
|
70 |
+
'required' => 'required',
|
71 |
+
'default' => get_option('woocommerce_ga_email') // Backwards compat
|
72 |
+
),
|
73 |
+
'ga_id' => array(
|
74 |
+
'title' => __('Google Analytics ID', 'woocommerce'),
|
75 |
+
'description' => __('Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce'),
|
76 |
+
'type' => 'text',
|
77 |
+
'default' => get_option('woocommerce_ga_id') // Backwards compat
|
78 |
+
),
|
79 |
+
'ga_set_domain_name' => array(
|
80 |
+
'title' => __('Set Domain Name', 'woocommerce'),
|
81 |
+
'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'),
|
82 |
+
'type' => 'text',
|
83 |
+
'default' => ''
|
84 |
+
),
|
85 |
+
'ga_standard_tracking_enabled' => array(
|
86 |
+
'title' => __('Tracking code', 'woocommerce'),
|
87 |
+
'label' => __('Add tracking code to your site using this plugin (Optional).', 'woocommerce'),
|
88 |
+
'description' => sprintf(__('You don\'t need to enable this if using a 3rd party analytics plugin. If you chose to add Universal Analytics code snippet via Third party plugins,<br/> Add <code>ga(\'require\', \'ec\', \'ec.js\');</code> below <code>ga(\'create\',\'UA-XXXXX-X\')</code> in your standard code snippet. <br/> Also ensure that the Universal Analytics code is present in the <head> section of the website.', 'woocommerce')),
|
89 |
+
'type' => 'checkbox',
|
90 |
+
'checkboxgroup' => 'start',
|
91 |
+
'default' => get_option('woocommerce_ga_standard_tracking_enabled') ? get_option('woocommerce_ga_standard_tracking_enabled') : 'no' // Backwards compat
|
92 |
+
),
|
93 |
+
'ga_enhanced_ecommerce_tracking_enabled' => array(
|
94 |
+
'label' => __('Enable Enhanced eCommerce tracking', '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 |
+
'track_login_step_for_guest_user' => array(
|
100 |
+
'label' => __('Track Login step for Guest users if Guest Checkout is enabled (Optional).', 'woocommerce'),
|
101 |
+
'type' => 'checkbox',
|
102 |
+
'checkboxgroup' => '',
|
103 |
+
'description' => sprintf(__('For Guest users Login is not a mandatory step. Checking the box would consider the click event on place order as Login as well as Checkout.', 'woocommerce')),
|
104 |
+
'default' => get_option('track_login_step_for_guest_user') ? get_option('track_login_step_for_guest_user') : 'no' // Backwards compat
|
105 |
+
)
|
106 |
+
);
|
107 |
+
/* When user updates the email, post it to the remote server */
|
108 |
+
if (isset($_GET['tab']) && isset($_REQUEST['section']) && isset($_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'])) {
|
109 |
+
|
110 |
+
$current_tab = ( empty($_GET['tab']) ) ? false : sanitize_text_field(urldecode($_GET['tab']));
|
111 |
+
$current_section = ( empty($_REQUEST['section']) ) ? false : sanitize_text_field(urldecode($_REQUEST['section']));
|
112 |
+
|
113 |
+
$save_for_the_plugin = ($current_tab == "integration" ) && ($current_section == "enhanced_ecommerce_google_analytics");
|
114 |
+
$update_made_for_email = $_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'] != $this->get_option('woocommerce_enhanced_ecommerce_google_analytics_ga_email');
|
115 |
+
|
116 |
+
if ($save_for_the_plugin && $update_made_for_email) {
|
117 |
+
if ($_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'] != '') {
|
118 |
+
$email = $_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'];
|
119 |
+
$this->send_email_to_tatvic($email);
|
120 |
+
}
|
121 |
+
}
|
122 |
+
}
|
123 |
+
}
|
124 |
+
|
125 |
+
// End init_form_fields()
|
126 |
+
|
127 |
+
/**
|
128 |
+
* Google Analytics standard tracking
|
129 |
+
*
|
130 |
+
* @access public
|
131 |
+
* @return void
|
132 |
+
*/
|
133 |
+
function google_tracking_code() {
|
134 |
+
if (is_admin() || current_user_can('manage_options') || $this->ga_standard_tracking_enabled == "no") {
|
135 |
+
return;
|
136 |
+
}
|
137 |
+
|
138 |
+
$tracking_id = $this->ga_id;
|
139 |
+
|
140 |
+
if (!$tracking_id) {
|
141 |
+
return;
|
142 |
+
}
|
143 |
+
|
144 |
+
|
145 |
+
if (!empty($this->ga_set_domain_name)) {
|
146 |
+
$set_domain_name = esc_js($this->ga_set_domain_name);
|
147 |
+
} else {
|
148 |
+
$set_domain_name = 'auto';
|
149 |
+
}
|
150 |
+
|
151 |
+
echo "<script>
|
152 |
+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
153 |
+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
154 |
+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
155 |
+
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
156 |
+
|
157 |
+
ga('create', '" . esc_js($tracking_id) . "', '" . $set_domain_name . "');
|
158 |
+
ga('require', 'ec', 'ec.js');
|
159 |
+
</script>";
|
160 |
+
}
|
161 |
+
|
162 |
+
/**
|
163 |
+
* Google Analytics eCommerce tracking
|
164 |
+
*
|
165 |
+
* @access public
|
166 |
+
* @param mixed $order_id
|
167 |
+
* @return void
|
168 |
+
*/
|
169 |
+
function ecommerce_tracking_code($order_id) {
|
170 |
+
global $woocommerce;
|
171 |
+
|
172 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled) || current_user_can('manage_options') || get_post_meta($order_id, '_ga_tracked', true) == 1)
|
173 |
+
return;
|
174 |
+
|
175 |
+
$tracking_id = $this->ga_id;
|
176 |
+
|
177 |
+
if (!$tracking_id)
|
178 |
+
return;
|
179 |
+
|
180 |
+
// Doing eCommerce tracking so unhook standard tracking from the footer
|
181 |
+
remove_action('wp_footer', array($this, 'google_tracking_code'));
|
182 |
+
|
183 |
+
// Get the order and output tracking code
|
184 |
+
$order = new WC_Order($order_id);
|
185 |
+
|
186 |
+
if (!empty($this->ga_set_domain_name)) {
|
187 |
+
$set_domain_name = esc_js($this->ga_set_domain_name);
|
188 |
+
} else {
|
189 |
+
$set_domain_name = 'auto';
|
190 |
+
}
|
191 |
+
$code = " (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
192 |
+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
193 |
+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
194 |
+
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
195 |
+
|
196 |
+
ga('create', '" . esc_js($tracking_id) . "', '" . $set_domain_name . "');
|
197 |
+
ga('require', 'ec', 'ec.js');
|
198 |
+
|
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 .= "ga('ec:addProduct', {";
|
208 |
+
$code .= "'id': '" . esc_js($order->get_order_number()) . "',";
|
209 |
+
$code .= "'name': '" . esc_js($item['name']) . "',";
|
210 |
+
$code .= "'sku': '" . esc_js($_product->get_sku() ? __('SKU:', 'woocommerce') . ' ' . $_product->get_sku() : $_product->id ) . "',";
|
211 |
+
|
212 |
+
if (isset($_product->variation_data)) {
|
213 |
+
|
214 |
+
$code .= "'category': '" . esc_js(woocommerce_get_formatted_variation($_product->variation_data, true)) . "',";
|
215 |
+
} else {
|
216 |
+
$out = array();
|
217 |
+
$categories = get_the_terms($_product->id, 'product_cat');
|
218 |
+
if ($categories) {
|
219 |
+
foreach ($categories as $category) {
|
220 |
+
$out[] = $category->name;
|
221 |
+
}
|
222 |
+
}
|
223 |
+
$code .= "'category': '" . esc_js(join("/", $out)) . "',";
|
224 |
+
}
|
225 |
+
|
226 |
+
$code .= "'price': '" . esc_js($order->get_item_total($item)) . "',";
|
227 |
+
$code .= "'quantity': '" . esc_js($item['qty']) . "'";
|
228 |
+
$code .= "});";
|
229 |
+
}
|
230 |
+
}
|
231 |
+
|
232 |
+
$code .="ga('ec:setAction','purchase', {
|
233 |
+
'id': '" . esc_js($order->get_order_number()) . "', // Transaction ID. Required
|
234 |
+
'affiliation': '" . esc_js(get_bloginfo('name')) . "', // Affiliation or store name
|
235 |
+
'revenue': '" . esc_js($order->get_total()) . "', // Grand Total
|
236 |
+
'shipping': '" . esc_js($order->get_shipping()) . "', // Shipping
|
237 |
+
'tax': '" . esc_js($order->get_total_tax()) . "' // Tax
|
238 |
+
});";
|
239 |
+
|
240 |
+
echo '<script type="text/javascript">' . $code . '</script>';
|
241 |
+
|
242 |
+
update_post_meta($order_id, '_ga_tracked', 1);
|
243 |
+
}
|
244 |
+
|
245 |
+
/**
|
246 |
+
* Enhanced E-commerce tracking for single product add to cart
|
247 |
+
*
|
248 |
+
* @access public
|
249 |
+
* @return void
|
250 |
+
*/
|
251 |
+
function add_to_cart() {
|
252 |
+
|
253 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled))
|
254 |
+
return;
|
255 |
+
|
256 |
+
if (!is_single())
|
257 |
+
return;
|
258 |
+
|
259 |
+
global $product, $woocommerce;
|
260 |
+
|
261 |
+
$parameters = array();
|
262 |
+
|
263 |
+
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters
|
264 |
+
$parameters['label'] = "'" . esc_js($product->get_sku() ? __('SKU:', 'woocommerce') . ' ' . $product->get_sku() : "#" . $product->id ) . "'";
|
265 |
+
|
266 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
267 |
+
wc_enqueue_js("
|
268 |
+
$('.single_add_to_cart_button').click(function() {
|
269 |
+
|
270 |
+
// Enhanced E-commerce Add to cart clicks
|
271 |
+
ga('ec:addProduct', {
|
272 |
+
'id': '" . esc_js($product->get_sku()) . "',
|
273 |
+
'name': '" . esc_js($product->get_title()) . "',
|
274 |
+
'price': '" . esc_js($product->get_price()) . "',
|
275 |
+
});
|
276 |
+
ga('ec:setAction', 'add');
|
277 |
+
ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
|
278 |
+
});
|
279 |
+
");
|
280 |
+
} else {
|
281 |
+
$woocommerce->add_inline_js("
|
282 |
+
$('.single_add_to_cart_button').click(function() {
|
283 |
+
|
284 |
+
// Enhanced E-commerce Add to cart clicks
|
285 |
+
ga('ec:addProduct', {
|
286 |
+
'id': '" . esc_js($product->get_sku()) . "',
|
287 |
+
'name': '" . esc_js($product->get_title()) . "',
|
288 |
+
'price': '" . esc_js($product->get_price()) . "',
|
289 |
+
});
|
290 |
+
ga('ec:setAction', 'add');
|
291 |
+
ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
|
292 |
+
});
|
293 |
+
");
|
294 |
+
}
|
295 |
+
}
|
296 |
+
|
297 |
+
/**
|
298 |
+
* Enhanced E-commerce tracking for loop add to cart
|
299 |
+
*
|
300 |
+
* @access public
|
301 |
+
* @return void
|
302 |
+
*/
|
303 |
+
function loop_add_to_cart() {
|
304 |
+
|
305 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
306 |
+
return;
|
307 |
+
}
|
308 |
+
global $woocommerce;
|
309 |
+
|
310 |
+
$parameters = array();
|
311 |
+
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters
|
312 |
+
$parameters['category'] = "'" . __('Products', 'woocommerce') . "'";
|
313 |
+
$parameters['action'] = "'" . __('Add to Cart', 'woocommerce') . "'";
|
314 |
+
$parameters['label'] = "($(this).data('product_sku')) ? ('SKU: ' + $(this).data('product_sku')) : ('#' + $(this).data('product_id'))"; // Product SKU or ID
|
315 |
+
|
316 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
317 |
+
|
318 |
+
wc_enqueue_js("
|
319 |
+
$('.add_to_cart_button:not(.product_type_variable, .product_type_grouped)').click(function() {
|
320 |
+
|
321 |
+
ga('ec:addImpression',{
|
322 |
+
'id': $(this).parents('li').find('.ls-pro-sku').val(),
|
323 |
+
'name': $(this).parents('li').find('.ls-pro-name').val(),
|
324 |
+
'category': $(this).parents('li').find('.ls-pro-category').val(),
|
325 |
+
'price': $(this).parents('li').find('.ls-pro-price').val(),
|
326 |
+
});
|
327 |
+
// Enhanced E-commerce Add to cart clicks
|
328 |
+
ga('ec:addProduct', {
|
329 |
+
'id': $(this).parents('li').find('.ls-pro-sku').val(),
|
330 |
+
'name': $(this).parents('li').find('.ls-pro-name').val(),
|
331 |
+
'category': $(this).parents('li').find('.ls-pro-category').val(),
|
332 |
+
'price': $(this).parents('li').find('.ls-pro-price').val(),
|
333 |
+
});
|
334 |
+
ga('ec:setAction', 'add');
|
335 |
+
ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
|
336 |
+
});
|
337 |
+
");
|
338 |
+
} else {
|
339 |
+
$woocommerce->add_inline_js("
|
340 |
+
$('.add_to_cart_button:not(.product_type_variable, .product_type_grouped)').click(function() {
|
341 |
+
|
342 |
+
ga('ec:addImpression',{
|
343 |
+
'id': $(this).parents('li').find('.ls-pro-sku').val(),
|
344 |
+
'name': $(this).parents('li').find('.ls-pro-name').val(),
|
345 |
+
'category': $(this).parents('li').find('.ls-pro-category').val(),
|
346 |
+
'price': $(this).parents('li').find('.ls-pro-price').val(),
|
347 |
+
});
|
348 |
+
// Enhanced E-commerce Add to cart clicks
|
349 |
+
ga('ec:addProduct', {
|
350 |
+
'id': $(this).parents('li').find('.ls-pro-sku').val(),
|
351 |
+
'name': $(this).parents('li').find('.ls-pro-name').val(),
|
352 |
+
'category': $(this).parents('li').find('.ls-pro-category').val(),
|
353 |
+
'price': $(this).parents('li').find('.ls-pro-price').val(),
|
354 |
+
});
|
355 |
+
ga('ec:setAction', 'add');
|
356 |
+
ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
|
357 |
+
});
|
358 |
+
");
|
359 |
+
}
|
360 |
+
}
|
361 |
+
|
362 |
+
/**
|
363 |
+
* Enhanced E-commerce tracking for product detail view
|
364 |
+
*
|
365 |
+
* @access public
|
366 |
+
* @return void
|
367 |
+
*/
|
368 |
+
public function product_detail_view() {
|
369 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
370 |
+
return;
|
371 |
+
}
|
372 |
+
|
373 |
+
global $product;
|
374 |
+
global $woocommerce;
|
375 |
+
$category = get_the_terms($product->ID, 'product_cat');
|
376 |
+
$categories = '';
|
377 |
+
foreach ($category as $term) {
|
378 |
+
$categories.=$term->name . ',';
|
379 |
+
}
|
380 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
381 |
+
|
382 |
+
wc_enqueue_js("ga('ec:addImpression', {
|
383 |
+
'id': '" . $product->get_sku() . "', // Product details are provided in an impressionFieldObject.
|
384 |
+
'name': '" . $product->get_title() . "',
|
385 |
+
'category': '" . $categories . "',
|
386 |
+
});
|
387 |
+
ga('ec:setAction', 'detail');
|
388 |
+
");
|
389 |
+
} else {
|
390 |
+
$woocommerce->add_inline_js("ga('ec:addImpression', {
|
391 |
+
'id': '" . $product->get_sku() . "', // Product details are provided in an impressionFieldObject.
|
392 |
+
'name': '" . $product->get_title() . "',
|
393 |
+
'category': '" . $categories . "',
|
394 |
+
});
|
395 |
+
ga('ec:setAction', 'detail');
|
396 |
+
");
|
397 |
+
}
|
398 |
+
}
|
399 |
+
|
400 |
+
/**
|
401 |
+
* Enhanced E-commerce tracking for product impressions on category pages
|
402 |
+
*
|
403 |
+
* @access public
|
404 |
+
* @return void
|
405 |
+
*/
|
406 |
+
public function product_impression() {
|
407 |
+
|
408 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
409 |
+
return;
|
410 |
+
}
|
411 |
+
|
412 |
+
if (is_single()) {
|
413 |
+
return;
|
414 |
+
}
|
415 |
+
|
416 |
+
global $product;
|
417 |
+
|
418 |
+
$category = get_the_terms($product->ID, 'product_cat');
|
419 |
+
$categories = '';
|
420 |
+
foreach ($category as $term) {
|
421 |
+
$categories.=$term->name . ',';
|
422 |
+
}
|
423 |
+
|
424 |
+
echo "<input type='hidden' class='ls-pro-price' value='" . esc_html($product->get_price()) . "'/>"
|
425 |
+
. "<input type='hidden' class='ls-pro-sku' value='" . $product->get_sku() . "'/>"
|
426 |
+
. "<input type='hidden' class='ls-pro-name' value='" . $product->get_title() . "'/>"
|
427 |
+
. "<input type='hidden' class='ls-pro-category' value='" . $categories . "'/>";
|
428 |
+
}
|
429 |
+
|
430 |
+
/**
|
431 |
+
* Enhanced E-commerce tracking for remove from cart
|
432 |
+
*
|
433 |
+
* @access public
|
434 |
+
* @return void
|
435 |
+
*/
|
436 |
+
public function remove_cart_tracking() {
|
437 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
438 |
+
return;
|
439 |
+
}
|
440 |
+
global $woocommerce;
|
441 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
442 |
+
wc_enqueue_js("$('.remove').click(function(){
|
443 |
+
ga('ec:addProduct', {
|
444 |
+
'name': $(this).parents('tr').find('.product-name').text(),
|
445 |
+
'price': $(this).parents('tr').find('.product-price').text(),
|
446 |
+
'quantity': $(this).parents('tr').find('.product-quantity .qty').val()
|
447 |
+
});
|
448 |
+
ga('ec:setAction', 'remove');
|
449 |
+
ga('send', 'event', 'UX', 'click', 'remove from cart',{'nonInteraction': 1});
|
450 |
+
});"
|
451 |
+
);
|
452 |
+
} else {
|
453 |
+
$woocommerce->add_inline_js("$('.remove').click(function(){
|
454 |
+
ga('ec:addProduct', {
|
455 |
+
'name': $(this).parents('tr').find('.product-name').text(),
|
456 |
+
'price': $(this).parents('tr').find('.product-price').text(),
|
457 |
+
'quantity': $(this).parents('tr').find('.product-quantity .qty').val()
|
458 |
+
});
|
459 |
+
ga('ec:setAction', 'remove');
|
460 |
+
ga('send', 'event', 'UX', 'click', 'remove from cart',{'nonInteraction': 1});
|
461 |
+
});"
|
462 |
+
);
|
463 |
+
}
|
464 |
+
}
|
465 |
+
|
466 |
+
/**
|
467 |
+
* Enhanced E-commerce tracking checkout steps
|
468 |
+
*
|
469 |
+
* @access public
|
470 |
+
* @return void
|
471 |
+
*/
|
472 |
+
public function checkout_step_one_tracking() {
|
473 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
474 |
+
return;
|
475 |
+
}
|
476 |
+
|
477 |
+
global $woocommerce;
|
478 |
+
|
479 |
+
foreach ($woocommerce->cart->cart_contents as $item) {
|
480 |
+
$p = get_product($item['product_id']);
|
481 |
+
|
482 |
+
$category = get_the_terms($item['product_id'], 'product_cat');
|
483 |
+
$categories = '';
|
484 |
+
foreach ($category as $term) {
|
485 |
+
$categories.=$term->name . ',';
|
486 |
+
}
|
487 |
+
|
488 |
+
$code = "ga('ec:addProduct', {" . "'id': '" . esc_js($p->get_sku()) . "',";
|
489 |
+
$code .= "'name': '" . esc_js($p->get_title()) . "',";
|
490 |
+
$code .= "'category': '" . esc_js($categories) . "',";
|
491 |
+
$code .= "'price': '" . esc_js($p->get_price()) . "',";
|
492 |
+
$code .= "'quantity': '" . esc_js($item['quantity']) . "'" . "});";
|
493 |
+
}
|
494 |
+
|
495 |
+
$code_step_1 = $code . "ga('ec:setAction','checkout',{'step': 1});";
|
496 |
+
$code_step_1 .= "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1});";
|
497 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
498 |
+
wc_enqueue_js($code_step_1);
|
499 |
+
} else {
|
500 |
+
$woocommerce->add_inline_js($code_step_1);
|
501 |
+
}
|
502 |
+
|
503 |
+
|
504 |
+
$code_step_2 = $code . "ga('ec:setAction','checkout',{'step': 2});";
|
505 |
+
$code_step_2 .= "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1});";
|
506 |
+
|
507 |
+
if (is_user_logged_in()) {
|
508 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
509 |
+
wc_enqueue_js($code_step_2);
|
510 |
+
} else {
|
511 |
+
$woocommerce->add_inline_js($code_step_2);
|
512 |
+
}
|
513 |
+
}
|
514 |
+
$step_2_on_proceed_to_pay = (!is_user_logged_in() && !$this->enable_guest_checkout ) || (!is_user_logged_in() && $this->enable_guest_checkout && $this->track_login_step_for_guest_user);
|
515 |
+
|
516 |
+
$code_step_3 = $code . "ga('ec:setAction','checkout',{'step': 3});";
|
517 |
+
$code_step_3 .= "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1});";
|
518 |
+
|
519 |
+
$inline_js = "jQuery(document).on('click','#place_order',function(e){";
|
520 |
+
if ($step_2_on_proceed_to_pay) {
|
521 |
+
$inline_js .= $code_step_2;
|
522 |
+
}
|
523 |
+
$inline_js .= $code_step_3;
|
524 |
+
$inline_js .= "});";
|
525 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
526 |
+
wc_enqueue_js($inline_js);
|
527 |
+
} else {
|
528 |
+
$woocommerce->add_inline_js($inline_js);
|
529 |
+
}
|
530 |
+
}
|
531 |
+
|
532 |
+
/**
|
533 |
+
* Sending hits with event
|
534 |
+
*
|
535 |
+
* @access public
|
536 |
+
* @return void
|
537 |
+
*/
|
538 |
+
public function default_pageview() {
|
539 |
+
global $woocommerce;
|
540 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled) || $this->disable_tracking($this->ga_standard_tracking_enabled)) {
|
541 |
+
return;
|
542 |
+
}
|
543 |
+
|
544 |
+
if ($this->disable_tracking($this->ga_standard_tracking_enabled) && $this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
545 |
+
$inline_js = "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1})";
|
546 |
+
} else {
|
547 |
+
$inline_js = "ga('send','pageview');";
|
548 |
+
}
|
549 |
+
|
550 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
551 |
+
wc_enqueue_js($inline_js);
|
552 |
+
} else {
|
553 |
+
$woocommerce->add_inline_js($inline_js);
|
554 |
+
}
|
555 |
+
}
|
556 |
+
|
557 |
+
/**
|
558 |
+
* Check if tracking is disabled
|
559 |
+
*
|
560 |
+
* @access private
|
561 |
+
* @param mixed $type
|
562 |
+
* @return bool
|
563 |
+
*/
|
564 |
+
private function disable_tracking($type) {
|
565 |
+
if (is_admin() || current_user_can('manage_options') || (!$this->ga_id ) || 'no' == $type) {
|
566 |
+
return true;
|
567 |
+
}
|
568 |
+
}
|
569 |
+
|
570 |
+
/**
|
571 |
+
* Sending email to remote server
|
572 |
+
*
|
573 |
+
* @access public
|
574 |
+
* @return void
|
575 |
+
*/
|
576 |
+
public function send_email_to_tatvic($email) {
|
577 |
+
//set POST variables
|
578 |
+
$url = 'http://dev.tatvic.com/leadgen/woocommerce-plugin/store_email/';
|
579 |
+
$fields = array(
|
580 |
+
'email' => urlencode($email),
|
581 |
+
);
|
582 |
+
wp_remote_post($url, array(
|
583 |
+
'method' => 'POST',
|
584 |
+
'timeout' => 1,
|
585 |
+
'httpversion' => '1.0',
|
586 |
+
'blocking' => false,
|
587 |
+
'headers' => array(),
|
588 |
+
'body' => $fields
|
589 |
+
)
|
590 |
+
);
|
591 |
+
}
|
592 |
+
|
593 |
+
}
|
readme.txt
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Enhanced E-commerce Google Analytics Plugin ===
|
2 |
+
Contributors: Tatvic
|
3 |
+
Plugin Name: Enhanced E-commerce for Woocommerce store
|
4 |
+
Plugin URI:
|
5 |
+
Tags: Google Analytics, Universal Analytics, Enhanced E-commerce, E-commerce, e-commerce, woo-commerce
|
6 |
+
Author URI: http://www.tatvic.com/
|
7 |
+
Author: Tatvic
|
8 |
+
Requires at least: 3.6
|
9 |
+
Tested up to: 3.8
|
10 |
+
Stable tag: 1.0
|
11 |
+
Version: 1.0
|
12 |
+
License: GPLv3
|
13 |
+
License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
14 |
+
|
15 |
+
Provides integration between Google Analytics Enhanced Ecommerce and WooCommerce.
|
16 |
+
|
17 |
+
== Description ==
|
18 |
+
This plugin provides the integration between Enhanced Ecommerce feature of Google Analytics and the WooCommerce plugin. You can track the user beahviour across your e-commerce store starting from product views to thank you page. It only supports the new Universal Analytics.
|
19 |
+
|
20 |
+
= Things to keep in mind before enabling the Enhanced E-commerce plugin =
|
21 |
+
* Enable Enhanced E-commerce for your profile/view. This is a profile / view level setting and can be accessed under Admin > View > E-commerce Settings
|
22 |
+
* Also, add meaningful labels for your checkout steps. We recommend you to label as, Step 1 : Checkout View; Step 2 : Login; Step 3 : Proceed to payment
|
23 |
+
* Remove standard E-commerce code from thank you along with the ecommerce.js which is included by <code>ga('require', 'ecommerce', 'ecommerce.js');</code>
|
24 |
+
* You need to include ec.js. This can be done by adding a single line of code below your default Google Analytics code snippet <code>ga('require', ‘ec’, 'ec.js');</code>
|
25 |
+
* If you are using the Universal Analytics Tag of Google Tag Manager, you need to replace it with a custom HTML tag with the following code. Replace UA-XXXX-Y with your property ID
|
26 |
+
<pre><!-- Google Analytics tag -->
|
27 |
+
<script>
|
28 |
+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
29 |
+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
30 |
+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
31 |
+
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
32 |
+
|
33 |
+
ga('create', 'UA-XXXX-Y', 'auto');
|
34 |
+
ga('send', 'pageview');
|
35 |
+
ga(‘require’,’ec’,’ec.js’);
|
36 |
+
</script>
|
37 |
+
<!-- End Google Analytics -->
|
38 |
+
</pre>
|
39 |
+
* Next, you need to activate your plugin from the Settings page for the plugin accessed under, WooCommerce > Settings > Integration > Enhanced Ecommerce Google Analytics
|
40 |
+
* Check the “Enable Enhanced E-commerce tracking”
|
41 |
+
* For tracking the Guest users, you need to check the box. Unchecking the box might cause an uneven funnel reported to Google Analytics.
|
42 |
+
|
43 |
+
= Events captured the Enhanced E-commerce plugin =
|
44 |
+
* Add to cart on all sections of the website
|
45 |
+
* Product impressions are recorded on load of product page and add-to-cart on category pages
|
46 |
+
* As woocommerce supports single step checkout, we have created a standard three steps checkout process consisting of 1) Checkout Page View; 2) Login; and 3) Proceed to payment
|
47 |
+
* As the Login step can be optional for the stores having Guest checkout functionality enabled
|
48 |
+
* The standard thank you page event that records the successful transactions
|
49 |
+
|
50 |
+
|
51 |
+
== Installation ==
|
52 |
+
1. Download the plugin file to your computer and unzip it
|
53 |
+
2. Using an FTP program, or your hosting control panel, upload the unzipped plugin folder to your WordPress installation’s wp-content/plugins/ directory.
|
54 |
+
3. Activate the plugin from the Plugins menu within the WordPress admin.
|
55 |
+
|
56 |
+
== Screenshots ==
|
57 |
+
1. Enable Enhanced E-commerce for your profile/view. This is a profile / view level setting and can be accessed under Admin > View > E-commerce Settings. Also, add meaningful labels for your checkout steps. We recommend you to label as, Step 1 : Checkout View; Step 2 : Login; Step 3 : Proceed to payment;
|
58 |
+
2. Next, you need to activate your plugin from the Settings page for the plugin accessed under, WooCommerce > Settings > Integration > Enhanced Ecommerce Google Analytics
|
59 |
+
3. For tracking the Guest users, you need to check the box. Unchecking the box might cause an uneven funnel reported to Google Analytics.
|
60 |
+
|
61 |
+
|
62 |
+
== Frequently Asked Questions ==
|
63 |
+
= Where can I find the setting for this plugin? =
|
64 |
+
|
65 |
+
This plugin will add the settings to the Integration tab, to be found in the WooCommerce > Settings menu.
|
66 |
+
|
67 |
+
= Does this conflict with the WooCommerce? =
|
68 |
+
|
69 |
+
Starting the WooCommerce 2.1 release there are no conflicts. However for earlier the plugin might conflict with the default Google Analytics integration for WooCommerce.
|
70 |
+
|
71 |
+
== Changelog ==
|
72 |
+
= 1.0 - 22/06/2014 =
|
73 |
+
* Initial release
|
trunk/includes/class-wc-enhanced-ecommerce-google-analytics-integration.php
ADDED
@@ -0,0 +1,593 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
/**
|
4 |
+
* Enhanced Ecommerce for Woo-commerce stores
|
5 |
+
*
|
6 |
+
* Allows tracking code to be inserted into store pages.
|
7 |
+
*
|
8 |
+
* @class WC_Enhanced_Ecommerce_Google_Analytics
|
9 |
+
* @extends WC_Integration
|
10 |
+
* @author Sudhir Mishra <sudhirxps@gmail.com>
|
11 |
+
*/
|
12 |
+
class WC_Enhanced_Ecommerce_Google_Analytics extends WC_Integration {
|
13 |
+
|
14 |
+
/**
|
15 |
+
* Init and hook in the integration.
|
16 |
+
*
|
17 |
+
* @access public
|
18 |
+
* @return void
|
19 |
+
*/
|
20 |
+
public function __construct() {
|
21 |
+
$this->id = 'enhanced_ecommerce_google_analytics';
|
22 |
+
$this->method_title = __('Enhanced Ecommerce Google Analytics', 'woocommerce');
|
23 |
+
$this->method_description = __('Enhanced Ecommerce is a new feature of Universal Analytics that generates detailed statistics about the users journey from product page to thank you page on your e-store. <br/><a href="https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce">Know more about enhanced e-commerce.</a>', 'woocommerce');
|
24 |
+
|
25 |
+
// Load the settings.
|
26 |
+
$this->init_form_fields();
|
27 |
+
$this->init_settings();
|
28 |
+
|
29 |
+
// Define user set variables
|
30 |
+
$this->ga_email = $this->get_option('ga_email');
|
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->enable_guest_checkout = get_option('woocommerce_enable_guest_checkout') == 'yes' ? true : false;
|
35 |
+
$this->track_login_step_for_guest_user = $this->get_option('track_login_step_for_guest_user') == 'yes' ? true : false;
|
36 |
+
$this->ga_enhanced_ecommerce_tracking_enabled = $this->get_option('ga_enhanced_ecommerce_tracking_enabled');
|
37 |
+
|
38 |
+
// Actions
|
39 |
+
add_action('woocommerce_update_options_integration_enhanced_ecommerce_google_analytics', array($this, 'process_admin_options'));
|
40 |
+
// API Call to LS with e-mail
|
41 |
+
// Tracking code
|
42 |
+
add_action('wp_head', array($this, 'google_tracking_code'));
|
43 |
+
add_action('woocommerce_thankyou', array($this, 'ecommerce_tracking_code'));
|
44 |
+
|
45 |
+
// Enhanced Ecommerce product impression hook
|
46 |
+
add_action('woocommerce_after_shop_loop_item', array($this, 'product_impression'));
|
47 |
+
add_action('woocommerce_after_single_product', array($this, 'product_detail_view'));
|
48 |
+
add_action('woocommerce_after_cart', array($this, 'remove_cart_tracking'));
|
49 |
+
add_action('woocommerce_after_checkout_form', array($this, 'checkout_step_one_tracking'));
|
50 |
+
|
51 |
+
// Event tracking code
|
52 |
+
add_action('woocommerce_after_add_to_cart_button', array($this, 'add_to_cart'));
|
53 |
+
add_action('wp_footer', array($this, 'loop_add_to_cart'));
|
54 |
+
add_action('wp_footer', array($this, 'default_pageview'));
|
55 |
+
}
|
56 |
+
|
57 |
+
/**
|
58 |
+
* Initialise Settings Form Fields
|
59 |
+
*
|
60 |
+
* @access public
|
61 |
+
* @return void
|
62 |
+
*/
|
63 |
+
function init_form_fields() {
|
64 |
+
|
65 |
+
$this->form_fields = array(
|
66 |
+
'ga_email' => array(
|
67 |
+
'title' => __('Email Address', 'woocommerce'),
|
68 |
+
'description' => __('Provide your work email for updates on the plugin enhancement.', 'woocommerce'),
|
69 |
+
'type' => 'email',
|
70 |
+
'required' => 'required',
|
71 |
+
'default' => get_option('woocommerce_ga_email') // Backwards compat
|
72 |
+
),
|
73 |
+
'ga_id' => array(
|
74 |
+
'title' => __('Google Analytics ID', 'woocommerce'),
|
75 |
+
'description' => __('Log into your google analytics account to find your ID. e.g. <code>UA-XXXXX-X</code>', 'woocommerce'),
|
76 |
+
'type' => 'text',
|
77 |
+
'default' => get_option('woocommerce_ga_id') // Backwards compat
|
78 |
+
),
|
79 |
+
'ga_set_domain_name' => array(
|
80 |
+
'title' => __('Set Domain Name', 'woocommerce'),
|
81 |
+
'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'),
|
82 |
+
'type' => 'text',
|
83 |
+
'default' => ''
|
84 |
+
),
|
85 |
+
'ga_standard_tracking_enabled' => array(
|
86 |
+
'title' => __('Tracking code', 'woocommerce'),
|
87 |
+
'label' => __('Add tracking code to your site using this plugin (Optional).', 'woocommerce'),
|
88 |
+
'description' => sprintf(__('You don\'t need to enable this if using a 3rd party analytics plugin. If you chose to add Universal Analytics code snippet via Third party plugins,<br/> Add <code>ga(\'require\', \'ec\', \'ec.js\');</code> below <code>ga(\'create\',\'UA-XXXXX-X\')</code> in your standard code snippet. <br/> Also ensure that the Universal Analytics code is present in the <head> section of the website.', 'woocommerce')),
|
89 |
+
'type' => 'checkbox',
|
90 |
+
'checkboxgroup' => 'start',
|
91 |
+
'default' => get_option('woocommerce_ga_standard_tracking_enabled') ? get_option('woocommerce_ga_standard_tracking_enabled') : 'no' // Backwards compat
|
92 |
+
),
|
93 |
+
'ga_enhanced_ecommerce_tracking_enabled' => array(
|
94 |
+
'label' => __('Enable Enhanced eCommerce tracking', '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 |
+
'track_login_step_for_guest_user' => array(
|
100 |
+
'label' => __('Track Login step for Guest users if Guest Checkout is enabled (Optional).', 'woocommerce'),
|
101 |
+
'type' => 'checkbox',
|
102 |
+
'checkboxgroup' => '',
|
103 |
+
'description' => sprintf(__('For Guest users Login is not a mandatory step. Checking the box would consider the click event on place order as Login as well as Checkout.', 'woocommerce')),
|
104 |
+
'default' => get_option('track_login_step_for_guest_user') ? get_option('track_login_step_for_guest_user') : 'no' // Backwards compat
|
105 |
+
)
|
106 |
+
);
|
107 |
+
/* When user updates the email, post it to the remote server */
|
108 |
+
if (isset($_GET['tab']) && isset($_REQUEST['section']) && isset($_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'])) {
|
109 |
+
|
110 |
+
$current_tab = ( empty($_GET['tab']) ) ? false : sanitize_text_field(urldecode($_GET['tab']));
|
111 |
+
$current_section = ( empty($_REQUEST['section']) ) ? false : sanitize_text_field(urldecode($_REQUEST['section']));
|
112 |
+
|
113 |
+
$save_for_the_plugin = ($current_tab == "integration" ) && ($current_section == "enhanced_ecommerce_google_analytics");
|
114 |
+
$update_made_for_email = $_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'] != $this->get_option('woocommerce_enhanced_ecommerce_google_analytics_ga_email');
|
115 |
+
|
116 |
+
if ($save_for_the_plugin && $update_made_for_email) {
|
117 |
+
if ($_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'] != '') {
|
118 |
+
$email = $_REQUEST['woocommerce_enhanced_ecommerce_google_analytics_ga_email'];
|
119 |
+
$this->send_email_to_tatvic($email);
|
120 |
+
}
|
121 |
+
}
|
122 |
+
}
|
123 |
+
}
|
124 |
+
|
125 |
+
// End init_form_fields()
|
126 |
+
|
127 |
+
/**
|
128 |
+
* Google Analytics standard tracking
|
129 |
+
*
|
130 |
+
* @access public
|
131 |
+
* @return void
|
132 |
+
*/
|
133 |
+
function google_tracking_code() {
|
134 |
+
if (is_admin() || current_user_can('manage_options') || $this->ga_standard_tracking_enabled == "no") {
|
135 |
+
return;
|
136 |
+
}
|
137 |
+
|
138 |
+
$tracking_id = $this->ga_id;
|
139 |
+
|
140 |
+
if (!$tracking_id) {
|
141 |
+
return;
|
142 |
+
}
|
143 |
+
|
144 |
+
|
145 |
+
if (!empty($this->ga_set_domain_name)) {
|
146 |
+
$set_domain_name = esc_js($this->ga_set_domain_name);
|
147 |
+
} else {
|
148 |
+
$set_domain_name = 'auto';
|
149 |
+
}
|
150 |
+
|
151 |
+
echo "<script>
|
152 |
+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
153 |
+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
154 |
+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
155 |
+
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
156 |
+
|
157 |
+
ga('create', '" . esc_js($tracking_id) . "', '" . $set_domain_name . "');
|
158 |
+
ga('require', 'ec', 'ec.js');
|
159 |
+
</script>";
|
160 |
+
}
|
161 |
+
|
162 |
+
/**
|
163 |
+
* Google Analytics eCommerce tracking
|
164 |
+
*
|
165 |
+
* @access public
|
166 |
+
* @param mixed $order_id
|
167 |
+
* @return void
|
168 |
+
*/
|
169 |
+
function ecommerce_tracking_code($order_id) {
|
170 |
+
global $woocommerce;
|
171 |
+
|
172 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled) || current_user_can('manage_options') || get_post_meta($order_id, '_ga_tracked', true) == 1)
|
173 |
+
return;
|
174 |
+
|
175 |
+
$tracking_id = $this->ga_id;
|
176 |
+
|
177 |
+
if (!$tracking_id)
|
178 |
+
return;
|
179 |
+
|
180 |
+
// Doing eCommerce tracking so unhook standard tracking from the footer
|
181 |
+
remove_action('wp_footer', array($this, 'google_tracking_code'));
|
182 |
+
|
183 |
+
// Get the order and output tracking code
|
184 |
+
$order = new WC_Order($order_id);
|
185 |
+
|
186 |
+
if (!empty($this->ga_set_domain_name)) {
|
187 |
+
$set_domain_name = esc_js($this->ga_set_domain_name);
|
188 |
+
} else {
|
189 |
+
$set_domain_name = 'auto';
|
190 |
+
}
|
191 |
+
$code = " (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
192 |
+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
193 |
+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
194 |
+
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
195 |
+
|
196 |
+
ga('create', '" . esc_js($tracking_id) . "', '" . $set_domain_name . "');
|
197 |
+
ga('require', 'ec', 'ec.js');
|
198 |
+
|
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 .= "ga('ec:addProduct', {";
|
208 |
+
$code .= "'id': '" . esc_js($order->get_order_number()) . "',";
|
209 |
+
$code .= "'name': '" . esc_js($item['name']) . "',";
|
210 |
+
$code .= "'sku': '" . esc_js($_product->get_sku() ? __('SKU:', 'woocommerce') . ' ' . $_product->get_sku() : $_product->id ) . "',";
|
211 |
+
|
212 |
+
if (isset($_product->variation_data)) {
|
213 |
+
|
214 |
+
$code .= "'category': '" . esc_js(woocommerce_get_formatted_variation($_product->variation_data, true)) . "',";
|
215 |
+
} else {
|
216 |
+
$out = array();
|
217 |
+
$categories = get_the_terms($_product->id, 'product_cat');
|
218 |
+
if ($categories) {
|
219 |
+
foreach ($categories as $category) {
|
220 |
+
$out[] = $category->name;
|
221 |
+
}
|
222 |
+
}
|
223 |
+
$code .= "'category': '" . esc_js(join("/", $out)) . "',";
|
224 |
+
}
|
225 |
+
|
226 |
+
$code .= "'price': '" . esc_js($order->get_item_total($item)) . "',";
|
227 |
+
$code .= "'quantity': '" . esc_js($item['qty']) . "'";
|
228 |
+
$code .= "});";
|
229 |
+
}
|
230 |
+
}
|
231 |
+
|
232 |
+
$code .="ga('ec:setAction','purchase', {
|
233 |
+
'id': '" . esc_js($order->get_order_number()) . "', // Transaction ID. Required
|
234 |
+
'affiliation': '" . esc_js(get_bloginfo('name')) . "', // Affiliation or store name
|
235 |
+
'revenue': '" . esc_js($order->get_total()) . "', // Grand Total
|
236 |
+
'shipping': '" . esc_js($order->get_shipping()) . "', // Shipping
|
237 |
+
'tax': '" . esc_js($order->get_total_tax()) . "' // Tax
|
238 |
+
});";
|
239 |
+
|
240 |
+
echo '<script type="text/javascript">' . $code . '</script>';
|
241 |
+
|
242 |
+
update_post_meta($order_id, '_ga_tracked', 1);
|
243 |
+
}
|
244 |
+
|
245 |
+
/**
|
246 |
+
* Enhanced E-commerce tracking for single product add to cart
|
247 |
+
*
|
248 |
+
* @access public
|
249 |
+
* @return void
|
250 |
+
*/
|
251 |
+
function add_to_cart() {
|
252 |
+
|
253 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled))
|
254 |
+
return;
|
255 |
+
|
256 |
+
if (!is_single())
|
257 |
+
return;
|
258 |
+
|
259 |
+
global $product, $woocommerce;
|
260 |
+
|
261 |
+
$parameters = array();
|
262 |
+
|
263 |
+
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters
|
264 |
+
$parameters['label'] = "'" . esc_js($product->get_sku() ? __('SKU:', 'woocommerce') . ' ' . $product->get_sku() : "#" . $product->id ) . "'";
|
265 |
+
|
266 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
267 |
+
wc_enqueue_js("
|
268 |
+
$('.single_add_to_cart_button').click(function() {
|
269 |
+
|
270 |
+
// Enhanced E-commerce Add to cart clicks
|
271 |
+
ga('ec:addProduct', {
|
272 |
+
'id': '" . esc_js($product->get_sku()) . "',
|
273 |
+
'name': '" . esc_js($product->get_title()) . "',
|
274 |
+
'price': '" . esc_js($product->get_price()) . "',
|
275 |
+
});
|
276 |
+
ga('ec:setAction', 'add');
|
277 |
+
ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
|
278 |
+
});
|
279 |
+
");
|
280 |
+
} else {
|
281 |
+
$woocommerce->add_inline_js("
|
282 |
+
$('.single_add_to_cart_button').click(function() {
|
283 |
+
|
284 |
+
// Enhanced E-commerce Add to cart clicks
|
285 |
+
ga('ec:addProduct', {
|
286 |
+
'id': '" . esc_js($product->get_sku()) . "',
|
287 |
+
'name': '" . esc_js($product->get_title()) . "',
|
288 |
+
'price': '" . esc_js($product->get_price()) . "',
|
289 |
+
});
|
290 |
+
ga('ec:setAction', 'add');
|
291 |
+
ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
|
292 |
+
});
|
293 |
+
");
|
294 |
+
}
|
295 |
+
}
|
296 |
+
|
297 |
+
/**
|
298 |
+
* Enhanced E-commerce tracking for loop add to cart
|
299 |
+
*
|
300 |
+
* @access public
|
301 |
+
* @return void
|
302 |
+
*/
|
303 |
+
function loop_add_to_cart() {
|
304 |
+
|
305 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
306 |
+
return;
|
307 |
+
}
|
308 |
+
global $woocommerce;
|
309 |
+
|
310 |
+
$parameters = array();
|
311 |
+
// Add single quotes to allow jQuery to be substituted into _trackEvent parameters
|
312 |
+
$parameters['category'] = "'" . __('Products', 'woocommerce') . "'";
|
313 |
+
$parameters['action'] = "'" . __('Add to Cart', 'woocommerce') . "'";
|
314 |
+
$parameters['label'] = "($(this).data('product_sku')) ? ('SKU: ' + $(this).data('product_sku')) : ('#' + $(this).data('product_id'))"; // Product SKU or ID
|
315 |
+
|
316 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
317 |
+
|
318 |
+
wc_enqueue_js("
|
319 |
+
$('.add_to_cart_button:not(.product_type_variable, .product_type_grouped)').click(function() {
|
320 |
+
|
321 |
+
ga('ec:addImpression',{
|
322 |
+
'id': $(this).parents('li').find('.ls-pro-sku').val(),
|
323 |
+
'name': $(this).parents('li').find('.ls-pro-name').val(),
|
324 |
+
'category': $(this).parents('li').find('.ls-pro-category').val(),
|
325 |
+
'price': $(this).parents('li').find('.ls-pro-price').val(),
|
326 |
+
});
|
327 |
+
// Enhanced E-commerce Add to cart clicks
|
328 |
+
ga('ec:addProduct', {
|
329 |
+
'id': $(this).parents('li').find('.ls-pro-sku').val(),
|
330 |
+
'name': $(this).parents('li').find('.ls-pro-name').val(),
|
331 |
+
'category': $(this).parents('li').find('.ls-pro-category').val(),
|
332 |
+
'price': $(this).parents('li').find('.ls-pro-price').val(),
|
333 |
+
});
|
334 |
+
ga('ec:setAction', 'add');
|
335 |
+
ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
|
336 |
+
});
|
337 |
+
");
|
338 |
+
} else {
|
339 |
+
$woocommerce->add_inline_js("
|
340 |
+
$('.add_to_cart_button:not(.product_type_variable, .product_type_grouped)').click(function() {
|
341 |
+
|
342 |
+
ga('ec:addImpression',{
|
343 |
+
'id': $(this).parents('li').find('.ls-pro-sku').val(),
|
344 |
+
'name': $(this).parents('li').find('.ls-pro-name').val(),
|
345 |
+
'category': $(this).parents('li').find('.ls-pro-category').val(),
|
346 |
+
'price': $(this).parents('li').find('.ls-pro-price').val(),
|
347 |
+
});
|
348 |
+
// Enhanced E-commerce Add to cart clicks
|
349 |
+
ga('ec:addProduct', {
|
350 |
+
'id': $(this).parents('li').find('.ls-pro-sku').val(),
|
351 |
+
'name': $(this).parents('li').find('.ls-pro-name').val(),
|
352 |
+
'category': $(this).parents('li').find('.ls-pro-category').val(),
|
353 |
+
'price': $(this).parents('li').find('.ls-pro-price').val(),
|
354 |
+
});
|
355 |
+
ga('ec:setAction', 'add');
|
356 |
+
ga('send', 'event', 'Enhanced-Ecommerce', 'add-to-cart-click', " . $parameters['label'] . ",{'nonInteraction': 1});
|
357 |
+
});
|
358 |
+
");
|
359 |
+
}
|
360 |
+
}
|
361 |
+
|
362 |
+
/**
|
363 |
+
* Enhanced E-commerce tracking for product detail view
|
364 |
+
*
|
365 |
+
* @access public
|
366 |
+
* @return void
|
367 |
+
*/
|
368 |
+
public function product_detail_view() {
|
369 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
370 |
+
return;
|
371 |
+
}
|
372 |
+
|
373 |
+
global $product;
|
374 |
+
global $woocommerce;
|
375 |
+
$category = get_the_terms($product->ID, 'product_cat');
|
376 |
+
$categories = '';
|
377 |
+
foreach ($category as $term) {
|
378 |
+
$categories.=$term->name . ',';
|
379 |
+
}
|
380 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
381 |
+
|
382 |
+
wc_enqueue_js("ga('ec:addImpression', {
|
383 |
+
'id': '" . $product->get_sku() . "', // Product details are provided in an impressionFieldObject.
|
384 |
+
'name': '" . $product->get_title() . "',
|
385 |
+
'category': '" . $categories . "',
|
386 |
+
});
|
387 |
+
ga('ec:setAction', 'detail');
|
388 |
+
");
|
389 |
+
} else {
|
390 |
+
$woocommerce->add_inline_js("ga('ec:addImpression', {
|
391 |
+
'id': '" . $product->get_sku() . "', // Product details are provided in an impressionFieldObject.
|
392 |
+
'name': '" . $product->get_title() . "',
|
393 |
+
'category': '" . $categories . "',
|
394 |
+
});
|
395 |
+
ga('ec:setAction', 'detail');
|
396 |
+
");
|
397 |
+
}
|
398 |
+
}
|
399 |
+
|
400 |
+
/**
|
401 |
+
* Enhanced E-commerce tracking for product impressions on category pages
|
402 |
+
*
|
403 |
+
* @access public
|
404 |
+
* @return void
|
405 |
+
*/
|
406 |
+
public function product_impression() {
|
407 |
+
|
408 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
409 |
+
return;
|
410 |
+
}
|
411 |
+
|
412 |
+
if (is_single()) {
|
413 |
+
return;
|
414 |
+
}
|
415 |
+
|
416 |
+
global $product;
|
417 |
+
|
418 |
+
$category = get_the_terms($product->ID, 'product_cat');
|
419 |
+
$categories = '';
|
420 |
+
foreach ($category as $term) {
|
421 |
+
$categories.=$term->name . ',';
|
422 |
+
}
|
423 |
+
|
424 |
+
echo "<input type='hidden' class='ls-pro-price' value='" . esc_html($product->get_price()) . "'/>"
|
425 |
+
. "<input type='hidden' class='ls-pro-sku' value='" . $product->get_sku() . "'/>"
|
426 |
+
. "<input type='hidden' class='ls-pro-name' value='" . $product->get_title() . "'/>"
|
427 |
+
. "<input type='hidden' class='ls-pro-category' value='" . $categories . "'/>";
|
428 |
+
}
|
429 |
+
|
430 |
+
/**
|
431 |
+
* Enhanced E-commerce tracking for remove from cart
|
432 |
+
*
|
433 |
+
* @access public
|
434 |
+
* @return void
|
435 |
+
*/
|
436 |
+
public function remove_cart_tracking() {
|
437 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
438 |
+
return;
|
439 |
+
}
|
440 |
+
global $woocommerce;
|
441 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
442 |
+
wc_enqueue_js("$('.remove').click(function(){
|
443 |
+
ga('ec:addProduct', {
|
444 |
+
'name': $(this).parents('tr').find('.product-name').text(),
|
445 |
+
'price': $(this).parents('tr').find('.product-price').text(),
|
446 |
+
'quantity': $(this).parents('tr').find('.product-quantity .qty').val()
|
447 |
+
});
|
448 |
+
ga('ec:setAction', 'remove');
|
449 |
+
ga('send', 'event', 'UX', 'click', 'remove from cart',{'nonInteraction': 1});
|
450 |
+
});"
|
451 |
+
);
|
452 |
+
} else {
|
453 |
+
$woocommerce->add_inline_js("$('.remove').click(function(){
|
454 |
+
ga('ec:addProduct', {
|
455 |
+
'name': $(this).parents('tr').find('.product-name').text(),
|
456 |
+
'price': $(this).parents('tr').find('.product-price').text(),
|
457 |
+
'quantity': $(this).parents('tr').find('.product-quantity .qty').val()
|
458 |
+
});
|
459 |
+
ga('ec:setAction', 'remove');
|
460 |
+
ga('send', 'event', 'UX', 'click', 'remove from cart',{'nonInteraction': 1});
|
461 |
+
});"
|
462 |
+
);
|
463 |
+
}
|
464 |
+
}
|
465 |
+
|
466 |
+
/**
|
467 |
+
* Enhanced E-commerce tracking checkout steps
|
468 |
+
*
|
469 |
+
* @access public
|
470 |
+
* @return void
|
471 |
+
*/
|
472 |
+
public function checkout_step_one_tracking() {
|
473 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
474 |
+
return;
|
475 |
+
}
|
476 |
+
|
477 |
+
global $woocommerce;
|
478 |
+
|
479 |
+
foreach ($woocommerce->cart->cart_contents as $item) {
|
480 |
+
$p = get_product($item['product_id']);
|
481 |
+
|
482 |
+
$category = get_the_terms($item['product_id'], 'product_cat');
|
483 |
+
$categories = '';
|
484 |
+
foreach ($category as $term) {
|
485 |
+
$categories.=$term->name . ',';
|
486 |
+
}
|
487 |
+
|
488 |
+
$code = "ga('ec:addProduct', {" . "'id': '" . esc_js($p->get_sku()) . "',";
|
489 |
+
$code .= "'name': '" . esc_js($p->get_title()) . "',";
|
490 |
+
$code .= "'category': '" . esc_js($categories) . "',";
|
491 |
+
$code .= "'price': '" . esc_js($p->get_price()) . "',";
|
492 |
+
$code .= "'quantity': '" . esc_js($item['quantity']) . "'" . "});";
|
493 |
+
}
|
494 |
+
|
495 |
+
$code_step_1 = $code . "ga('ec:setAction','checkout',{'step': 1});";
|
496 |
+
$code_step_1 .= "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1});";
|
497 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
498 |
+
wc_enqueue_js($code_step_1);
|
499 |
+
} else {
|
500 |
+
$woocommerce->add_inline_js($code_step_1);
|
501 |
+
}
|
502 |
+
|
503 |
+
|
504 |
+
$code_step_2 = $code . "ga('ec:setAction','checkout',{'step': 2});";
|
505 |
+
$code_step_2 .= "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1});";
|
506 |
+
|
507 |
+
if (is_user_logged_in()) {
|
508 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
509 |
+
wc_enqueue_js($code_step_2);
|
510 |
+
} else {
|
511 |
+
$woocommerce->add_inline_js($code_step_2);
|
512 |
+
}
|
513 |
+
}
|
514 |
+
$step_2_on_proceed_to_pay = (!is_user_logged_in() && !$this->enable_guest_checkout ) || (!is_user_logged_in() && $this->enable_guest_checkout && $this->track_login_step_for_guest_user);
|
515 |
+
|
516 |
+
$code_step_3 = $code . "ga('ec:setAction','checkout',{'step': 3});";
|
517 |
+
$code_step_3 .= "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1});";
|
518 |
+
|
519 |
+
$inline_js = "jQuery(document).on('click','#place_order',function(e){";
|
520 |
+
if ($step_2_on_proceed_to_pay) {
|
521 |
+
$inline_js .= $code_step_2;
|
522 |
+
}
|
523 |
+
$inline_js .= $code_step_3;
|
524 |
+
$inline_js .= "});";
|
525 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
526 |
+
wc_enqueue_js($inline_js);
|
527 |
+
} else {
|
528 |
+
$woocommerce->add_inline_js($inline_js);
|
529 |
+
}
|
530 |
+
}
|
531 |
+
|
532 |
+
/**
|
533 |
+
* Sending hits with event
|
534 |
+
*
|
535 |
+
* @access public
|
536 |
+
* @return void
|
537 |
+
*/
|
538 |
+
public function default_pageview() {
|
539 |
+
global $woocommerce;
|
540 |
+
if ($this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled) || $this->disable_tracking($this->ga_standard_tracking_enabled)) {
|
541 |
+
return;
|
542 |
+
}
|
543 |
+
|
544 |
+
if ($this->disable_tracking($this->ga_standard_tracking_enabled) && $this->disable_tracking($this->ga_enhanced_ecommerce_tracking_enabled)) {
|
545 |
+
$inline_js = "ga('send', 'event', 'Enhanced-Ecommerce', 'pageview', 'footer',{'nonInteraction': 1})";
|
546 |
+
} else {
|
547 |
+
$inline_js = "ga('send','pageview');";
|
548 |
+
}
|
549 |
+
|
550 |
+
if (version_compare($woocommerce->version, '2.1', '>=')) {
|
551 |
+
wc_enqueue_js($inline_js);
|
552 |
+
} else {
|
553 |
+
$woocommerce->add_inline_js($inline_js);
|
554 |
+
}
|
555 |
+
}
|
556 |
+
|
557 |
+
/**
|
558 |
+
* Check if tracking is disabled
|
559 |
+
*
|
560 |
+
* @access private
|
561 |
+
* @param mixed $type
|
562 |
+
* @return bool
|
563 |
+
*/
|
564 |
+
private function disable_tracking($type) {
|
565 |
+
if (is_admin() || current_user_can('manage_options') || (!$this->ga_id ) || 'no' == $type) {
|
566 |
+
return true;
|
567 |
+
}
|
568 |
+
}
|
569 |
+
|
570 |
+
/**
|
571 |
+
* Sending email to remote server
|
572 |
+
*
|
573 |
+
* @access public
|
574 |
+
* @return void
|
575 |
+
*/
|
576 |
+
public function send_email_to_tatvic($email) {
|
577 |
+
//set POST variables
|
578 |
+
$url = 'http://dev.tatvic.com/leadgen/woocommerce-plugin/store_email/';
|
579 |
+
$fields = array(
|
580 |
+
'email' => urlencode($email),
|
581 |
+
);
|
582 |
+
wp_remote_post($url, array(
|
583 |
+
'method' => 'POST',
|
584 |
+
'timeout' => 1,
|
585 |
+
'httpversion' => '1.0',
|
586 |
+
'blocking' => false,
|
587 |
+
'headers' => array(),
|
588 |
+
'body' => $fields
|
589 |
+
)
|
590 |
+
);
|
591 |
+
}
|
592 |
+
|
593 |
+
}
|
trunk/readme.txt
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Enhance E-Commerce Google Analytics Plugin for WooCommerce ===
|
2 |
+
Contributors: Tatvic
|
3 |
+
Plugin Name: Enhanced E-commerce for Woocommerce store
|
4 |
+
Plugin URI:
|
5 |
+
Tags: Google Analytics, Universal Analytics, Enhanced E-commerce, E-commerce, e-commerce, woo-commerce
|
6 |
+
Author URI: http://www.tatvic.com/
|
7 |
+
Author: Tatvic
|
8 |
+
Requires at least: 3.6
|
9 |
+
Tested up to: 3.8
|
10 |
+
Stable tag: 1.0
|
11 |
+
Version: 1.0
|
12 |
+
License: GPLv3
|
13 |
+
License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
14 |
+
|
15 |
+
Provides integration between Google Analytics Enhanced Ecommerce and WooCommerce.
|
16 |
+
|
17 |
+
== Description ==
|
18 |
+
This plugin provides the integration between Enhanced Ecommerce feature of Google Analytics and the WooCommerce plugin. You can track the user beahviour across your e-commerce store starting from product views to thank you page. It only supports the new Universal Analytics.
|
19 |
+
|
20 |
+
= Things to keep in mind before enabling the Enhanced E-commerce plugin =
|
21 |
+
* Enable Enhanced E-commerce for your profile/view. This is a profile / view level setting and can be accessed under Admin > View > E-commerce Settings
|
22 |
+
* Also, add meaningful labels for your checkout steps. We recommend you to label as, Step 1 : Checkout View; Step 2 : Login; Step 3 : Proceed to payment
|
23 |
+
* Remove standard E-commerce code from thank you along with the ecommerce.js which is included by <code>ga('require', 'ecommerce', 'ecommerce.js');</code>
|
24 |
+
* You need to include ec.js. This can be done by adding a single line of code below your default Google Analytics code snippet <code>ga('require', ‘ec’, 'ec.js');</code>
|
25 |
+
* If you are using the Universal Analytics Tag of Google Tag Manager, you need to replace it with a custom HTML tag with the following code. Replace UA-XXXX-Y with your property ID
|
26 |
+
<pre><!-- Google Analytics tag -->
|
27 |
+
<script>
|
28 |
+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
29 |
+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
30 |
+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
31 |
+
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
32 |
+
|
33 |
+
ga('create', 'UA-XXXX-Y', 'auto');
|
34 |
+
ga('send', 'pageview');
|
35 |
+
ga(‘require’,’ec’,’ec.js’);
|
36 |
+
</script>
|
37 |
+
<!-- End Google Analytics -->
|
38 |
+
</pre>
|
39 |
+
* Next, you need to activate your plugin from the Settings page for the plugin accessed under, WooCommerce > Settings > Integration > Enhanced Ecommerce Google Analytics
|
40 |
+
* Check the “Enable Enhanced E-commerce tracking”
|
41 |
+
* For tracking the Guest users, you need to check the box. Unchecking the box might cause an uneven funnel reported to Google Analytics.
|
42 |
+
|
43 |
+
= Events captured by the Enhanced E-commerce plugin =
|
44 |
+
* Add to Cart event captured on all pages of the website
|
45 |
+
* Product Impressions event captured on the load of the product page and Add-to-Cart on category pages
|
46 |
+
* Standard three steps checkout process consisting of a) Checkout Page View; b) Login; and c) Proceed to payment
|
47 |
+
* Login step can be optional for the stores who have Guest checkout functionality enabled
|
48 |
+
* Standard thank you page event that captures transaction data
|
49 |
+
|
50 |
+
|
51 |
+
== Installation ==
|
52 |
+
1. Download the plugin file to your computer and unzip it
|
53 |
+
2. Using an FTP program, or your hosting control panel, upload the unzipped plugin folder to your WordPress installation’s wp-content/plugins/ directory.
|
54 |
+
3. Activate the plugin from the Plugins menu within the WordPress admin.
|
55 |
+
|
56 |
+
== Screenshots ==
|
57 |
+
1. Enable Enhanced E-commerce for your profile/view. This is a profile / view level setting and can be accessed under Admin > View > E-commerce Settings. Also, add meaningful labels for your checkout steps. We recommend you to label as, Step 1 : Checkout View; Step 2 : Login; Step 3 : Proceed to payment;
|
58 |
+
2. Next, you need to activate your plugin from the Settings page for the plugin accessed under, WooCommerce > Settings > Integration > Enhanced Ecommerce Google Analytics
|
59 |
+
3. For tracking the Guest users, you need to check the box. Unchecking the box might cause an uneven funnel reported to Google Analytics.
|
60 |
+
|
61 |
+
|
62 |
+
== Frequently Asked Questions ==
|
63 |
+
= Where can I find the setting for this plugin? =
|
64 |
+
|
65 |
+
This plugin will add the settings to the Integration tab, to be found in the WooCommerce > Settings menu.
|
66 |
+
|
67 |
+
= Does this conflict with the WooCommerce? =
|
68 |
+
|
69 |
+
Starting the WooCommerce 2.1 release there are no conflicts. However for earlier the plugin might conflict with the default Google Analytics integration for WooCommerce.
|
70 |
+
|
71 |
+
== Changelog ==
|
72 |
+
= 1.0 - 25/06/2014 =
|
73 |
+
* Initial release
|
trunk/woocommerce-enhanced-ecommerce-google-analytics-integration.php
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
/* Copyright 2014 Sudhir Mishra (email : sudhirxps@gmail.com)
|
4 |
+
|
5 |
+
This program is free software; you can redistribute it and/or modify
|
6 |
+
it under the terms of the GNU General Public License, version 2, as
|
7 |
+
published by the Free Software Foundation.
|
8 |
+
|
9 |
+
This program is distributed in the hope that it will be useful,
|
10 |
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
+
GNU General Public License for more details.
|
13 |
+
|
14 |
+
You should have received a copy of the GNU General Public License
|
15 |
+
along with this program; if not, write to the Free Software
|
16 |
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
17 |
+
*/
|
18 |
+
/*
|
19 |
+
Plugin Name: Enhanced E-commerce for Woocommerce store
|
20 |
+
Plugin URI: http://www.tatvic.com/enhanced-ecommerce-for-woocommerece/
|
21 |
+
Description: Allows Enhanced E-commerce Google Analytics tracking code to be inserted into WooCommerce store pages.
|
22 |
+
Author: Tatvic
|
23 |
+
Author URI: http://www.tatvic.com
|
24 |
+
Version: 1.0
|
25 |
+
*/
|
26 |
+
|
27 |
+
// Add the integration to WooCommerce
|
28 |
+
function wc_enhanced_ecommerce_google_analytics_add_integration($integrations) {
|
29 |
+
global $woocommerce;
|
30 |
+
|
31 |
+
if (is_object($woocommerce)) {
|
32 |
+
include_once( 'includes/class-wc-enhanced-ecommerce-google-analytics-integration.php' );
|
33 |
+
$integrations[] = 'WC_Enhanced_Ecommerce_Google_Analytics';
|
34 |
+
}
|
35 |
+
|
36 |
+
return $integrations;
|
37 |
+
}
|
38 |
+
|
39 |
+
add_filter('woocommerce_integrations', 'wc_enhanced_ecommerce_google_analytics_add_integration', 10);
|
woocommerce-enhanced-ecommerce-google-analytics-integration.php
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
|
3 |
+
/* Copyright 2014 Sudhir Mishra (email : sudhirxps@gmail.com)
|
4 |
+
|
5 |
+
This program is free software; you can redistribute it and/or modify
|
6 |
+
it under the terms of the GNU General Public License, version 2, as
|
7 |
+
published by the Free Software Foundation.
|
8 |
+
|
9 |
+
This program is distributed in the hope that it will be useful,
|
10 |
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
+
GNU General Public License for more details.
|
13 |
+
|
14 |
+
You should have received a copy of the GNU General Public License
|
15 |
+
along with this program; if not, write to the Free Software
|
16 |
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
17 |
+
*/
|
18 |
+
/*
|
19 |
+
Plugin Name: Enhanced E-commerce for Woocommerce store
|
20 |
+
Plugin URI: http://www.tatvic.com/enhanced-ecommerce-for-woocommerece/
|
21 |
+
Description: Allows Enhanced E-commerce Google Analytics tracking code to be inserted into WooCommerce store pages.
|
22 |
+
Author: Tatvic
|
23 |
+
Author URI: http://www.tatvic.com
|
24 |
+
Version: 1.0
|
25 |
+
*/
|
26 |
+
|
27 |
+
// Add the integration to WooCommerce
|
28 |
+
function wc_enhanced_ecommerce_google_analytics_add_integration($integrations) {
|
29 |
+
global $woocommerce;
|
30 |
+
|
31 |
+
if (is_object($woocommerce)) {
|
32 |
+
include_once( 'includes/class-wc-enhanced-ecommerce-google-analytics-integration.php' );
|
33 |
+
$integrations[] = 'WC_Enhanced_Ecommerce_Google_Analytics';
|
34 |
+
}
|
35 |
+
|
36 |
+
return $integrations;
|
37 |
+
}
|
38 |
+
|
39 |
+
add_filter('woocommerce_integrations', 'wc_enhanced_ecommerce_google_analytics_add_integration', 10);
|