summaryrefslogtreecommitdiff
blob: 8482bb29566f75b3ef990c72b2795640786688ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
 * Jetpack_WooCommerce_Analytics_Universal
 *
 * @package Jetpack
 * @author Automattic
 */

/**
 * Bail if accessed directly
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Jetpack_WooCommerce_Analytics_Universal
 * Filters and Actions added to Store pages to perform analytics
 */
class Jetpack_WooCommerce_Analytics_Universal {
	/**
	 * Jetpack_WooCommerce_Analytics_Universal constructor.
	 */
	public function __construct() {
		// loading _wca
		add_action( 'wp_head', array( $this, 'wp_head_top' ), 1 );

		// add to carts from non-product pages or lists (search, store etc.)
		add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 );

		// loading s.js
		add_action( 'wp_head', array( $this, 'wp_head_bottom' ), 999999 );

		// Capture cart events
		add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 );

		// single product page view
		add_action( 'woocommerce_after_single_product', array( $this, 'capture_product_view' ) );

		add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) );
		add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) );
		add_action( 'wcct_before_cart_widget', array( $this, 'remove_from_cart' ) );
		add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 );

		// cart checkout
		add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) );

		// order confirmed
		add_action( 'woocommerce_thankyou', array( $this, 'order_process' ), 10, 1 );
		add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart_via_quantity' ), 10, 1 );
	}

	/**
	 * Make _wca available to queue events
	 */
	public function wp_head_top() {
		if ( is_cart() || is_checkout() || is_checkout_pay_page() || is_order_received_page() || is_add_payment_method_page() ) {
			$prevent_referrer_code = "<script>window._wca_prevent_referrer = true;</script>";
			echo "$prevent_referrer_code\r\n";
		}
		$wca_code = "<script>window._wca = window._wca || [];</script>";
		echo "$wca_code\r\n";
	}


	/**
	 * Place script to call s.js, Store Analytics
	 */
	public function wp_head_bottom() {
		$filename = 's-' . gmdate( 'YW' ) . '.js';
		$async_code = "<script async src='https://stats.wp.com/" . $filename . "'></script>";
		echo "$async_code\r\n";
	}

	/**
	 * On product lists or other non-product pages, add an event listener to "Add to Cart" button click
	 */
	public function loop_session_events() {
		$blogid   = Jetpack::get_option( 'id' );

		// check for previous add-to-cart cart events
		$data = WC()->session->get( 'wca_session_data' );
		if ( ! empty( $data ) ) {
			foreach ( $data as $data_instance ) {
				$product = wc_get_product( $data_instance['product_id'] );
				if ( ! $product ) {
					continue;
				}
				$product_details = $this->get_product_details( $product );
				wc_enqueue_js(
					"_wca.push( {
							'_en': '" . esc_js( $data_instance['event'] ) . "',
							'blog_id': '" . esc_js( $blogid ) . "',
							'pi': '" . esc_js( $data_instance['product_id'] ) . "',
							'pn': '" . esc_js( $product_details['name'] ) . "',
							'pc': '" . esc_js( $product_details['category'] ) . "',
							'pp': '" . esc_js( $product_details['price'] ) . "',
							'pq': '" . esc_js( $data_instance['quantity'] ) . "',
							'ui': '" . esc_js( $this->get_user_id() ) . "',
						} );"
				);
			}
			// clear data
			WC()->session->set( 'wca_session_data', '' );
		}
	}

	/**
	 * On the cart page, add an event listener for removal of product click
	 */
	public function remove_from_cart() {

		// We listen at div.woocommerce because the cart 'form' contents get forcibly
		// updated and subsequent removals from cart would then not have this click
		// handler attached.
		$blogid = Jetpack::get_option( 'id' );
		wc_enqueue_js(
			"jQuery( 'div.woocommerce' ).on( 'click', 'a.remove', function() {
				var productID = jQuery( this ).data( 'product_id' );
				var quantity = jQuery( this ).parent().parent().find( '.qty' ).val()
				var productDetails = {
					'id': productID,
					'quantity': quantity ? quantity : '1',
				};
				_wca.push( {
					'_en': 'woocommerceanalytics_remove_from_cart',
					'blog_id': '" . esc_js( $blogid ) . "',
					'pi': productDetails.id,
					'pq': productDetails.quantity,
					'ui': '" . esc_js( $this->get_user_id() ) . "',
				} );
			} );"
		);
	}

	/**
	 * Adds the product ID to the remove product link (for use by remove_from_cart above) if not present
	 *
	 * @param string $url Full HTML a tag of the link to remove an item from the cart.
	 * @param string $key Unique Key ID for a cart item.
	 *
	 * @return mixed.
	 */
	public function remove_from_cart_attributes( $url, $key ) {
		if ( false !== strpos( $url, 'data-product_id' ) ) {
			return $url;
		}

		$item    = WC()->cart->get_cart_item( $key );
		$product = $item['data'];

		$new_attributes = sprintf(
			'" data-product_id="%s">',
			esc_attr( $product->get_id() )
		);

		$url = str_replace( '">', $new_attributes, $url );
		return $url;
	}

	/**
	 * Gather relevant product information
	 *
	 * @param array $product product
	 * @return array
	 */
	public function get_product_details( $product ) {
		return array(
			'id'       => $product->get_id(),
			'name'     => $product->get_title(),
			'category' => $this->get_product_categories_concatenated( $product ),
			'price'    => $product->get_price(),
		);
	}

	/**
	 * Track a product page view
	 */
	public function capture_product_view() {

		global $product;
		$blogid = Jetpack::get_option( 'id' );
		$product_details = $this->get_product_details( $product );

		wc_enqueue_js(
			"_wca.push( {
				'_en': 'woocommerceanalytics_product_view',
				'blog_id': '" . esc_js( $blogid ) . "',
				'pi': '" . esc_js( $product_details['id'] ) . "',
				'pn': '" . esc_js( $product_details['name'] ) . "',
				'pc': '" . esc_js( $product_details['category'] ) . "',
				'pp': '" . esc_js( $product_details['price'] ) . "',
				'ui': '" . esc_js( $this->get_user_id() ) . "',
			} );"
		);
	}

	/**
	 * On the Checkout page, trigger an event for each product in the cart
	 */
	public function checkout_process() {

		$universal_commands = array();
		$cart               = WC()->cart->get_cart();
		$blogid             = Jetpack::get_option( 'id' );

		foreach ( $cart as $cart_item_key => $cart_item ) {
			/**
			* This filter is already documented in woocommerce/templates/cart/cart.php
			*/
			$product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

			if ( ! $product ) {
				continue;
			}

			$product_details = $this->get_product_details( $product );

			$universal_commands[] = "_wca.push( {
				'_en': 'woocommerceanalytics_product_checkout',
				'blog_id': '" . esc_js( $blogid ) . "',
				'pi': '" . esc_js( $product_details['id'] ) . "',
				'pn': '" . esc_js( $product_details['name'] ) . "',
				'pc': '" . esc_js( $product_details['category'] ) . "',
				'pp': '" . esc_js( $product_details['price'] ) . "',
				'pq': '" . esc_js( $cart_item['quantity'] ) . "',
				'ui': '" . esc_js( $this->get_user_id() ) . "',
			} );";
		}

		wc_enqueue_js( implode( "\r\n", $universal_commands ) );
	}

	/**
	 * After the checkout process, fire an event for each item in the order
	 *
	 * @param string $order_id Order Id.
	 */
	public function order_process( $order_id ) {
		$order              = wc_get_order( $order_id );
		$universal_commands = array();
		$blogid             = Jetpack::get_option( 'id' );

		// loop through products in the order and queue a purchase event.
		foreach ( $order->get_items() as $order_item_id => $order_item ) {
			$product = $order->get_product_from_item( $order_item );

			$product_details = $this->get_product_details( $product );

			$universal_commands[] = "_wca.push( {
				'_en': 'woocommerceanalytics_product_purchase',
				'blog_id': '" . esc_js( $blogid ) . "',
				'pi': '" . esc_js( $product_details['id'] ) . "',
				'pn': '" . esc_js( $product_details['name'] ) . "',
				'pc': '" . esc_js( $product_details['category'] ) . "',
				'pp': '" . esc_js( $product_details['price'] ) . "',
				'pq': '" . esc_js( $order_item->get_quantity() ) . "',
				'oi': '" . esc_js( $order->get_order_number() ) . "',
				'ui': '" . esc_js( $this->get_user_id() ) . "',
			} );";
		}

		wc_enqueue_js( implode( "\r\n", $universal_commands ) );
	}

	/**
	 * Listen for clicks on the "Update Cart" button to know if an item has been removed by
	 * updating its quantity to zero
	 */
	public function remove_from_cart_via_quantity() {
		$blogid = Jetpack::get_option( 'id' );

		wc_enqueue_js( "
			jQuery( 'button[name=update_cart]' ).on( 'click', function() {
				var cartItems = jQuery( '.cart_item' );
				cartItems.each( function( item ) {
					var qty = jQuery( this ).find( 'input.qty' );
					if ( qty && qty.val() === '0' ) {
						var productID = jQuery( this ).find( '.product-remove a' ).data( 'product_id' );
						_wca.push( {
							'_en': 'woocommerceanalytics_remove_from_cart',
							'blog_id': '" . esc_js( $blogid ) . "',
							'pi': productID,
							'ui': '" . esc_js( $this->get_user_id() ) . "',
						} );
					}
				} );
			} );
		" );
	}

	/**
	 * Get the current user id
	 *
	 * @return int
	 */
	public function get_user_id() {
		if ( is_user_logged_in() ) {
			$blogid = Jetpack::get_option( 'id' );
			$userid = get_current_user_id();
			return $blogid . ":" . $userid;
		}
		return 'null';
	}

	/**
		* @param $cart_item_key
		* @param $product_id
		* @param $quantity
		* @param $variation_id
		* @param $variation
		* @param $cart_item_data
		*/
	public function capture_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
		$referer_postid = isset( $_SERVER['HTTP_REFERER'] ) ? url_to_postid( $_SERVER['HTTP_REFERER'] ) : 0;
		// if the referring post is not a product OR the product being added is not the same as post
		// (eg. related product list on single product page) then include a product view event
		if ( ! wc_get_product( $referer_postid ) || $product_id != $referer_postid ) {
			$this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_product_view' );
		}
		// add cart event to the session data
		$this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_add_to_cart' );
	}

	/**
	 * @param $product_id
	 * @param $quantity
	 * @param $event
	 */
	public function capture_event_in_session_data( $product_id, $quantity, $event ) {

		$product = wc_get_product( $product_id );
		if ( ! $product ) {
			return;
		}

		$quantity = ( $quantity == 0 ) ? 1 : $quantity;

		// check for existing data
		$data = WC()->session->get( 'wca_session_data' );
		if ( empty( $data ) || ! is_array( $data ) ) {
			$data = array();
		}

		// extract new event data
		$new_data = array(
			'event' => $event,
			'product_id' => (string) $product_id,
			'quantity' => (string) $quantity,
		);

		// append new data
		$data[] = $new_data;

		WC()->session->set( 'wca_session_data', $data );
	}

	/**
	 * Gets product categories or varation attributes as a formatted concatenated string
	 *
	 * @param object $product WC_Product.
	 * @return string
	 */
	public function get_product_categories_concatenated( $product ) {

		if ( ! $product ) {
			return '';
		}

		$variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : '';
		if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
			$line = wc_get_formatted_variation( $variation_data, true );
		} else {
			$out = array();
			$categories = get_the_terms( $product->get_id(), 'product_cat' );
			if ( $categories ) {
				foreach ( $categories as $category ) {
					$out[] = $category->name;
				}
			}
			$line = join( '/', $out );
		}
		return $line;
	}

}