wp-woocommerce-analytics-universal.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * Jetpack_WooCommerce_Analytics_Universal
  4. *
  5. * @package Jetpack
  6. * @author Automattic
  7. */
  8. /**
  9. * Bail if accessed directly
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. /**
  15. * Class Jetpack_WooCommerce_Analytics_Universal
  16. * Filters and Actions added to Store pages to perform analytics
  17. */
  18. class Jetpack_WooCommerce_Analytics_Universal {
  19. /**
  20. * Jetpack_WooCommerce_Analytics_Universal constructor.
  21. */
  22. public function __construct() {
  23. // loading _wca
  24. add_action( 'wp_head', array( $this, 'wp_head_top' ), 1 );
  25. // add to carts from non-product pages or lists (search, store etc.)
  26. add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 );
  27. // loading s.js
  28. add_action( 'wp_head', array( $this, 'wp_head_bottom' ), 999999 );
  29. // Capture cart events
  30. add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 );
  31. // single product page view
  32. add_action( 'woocommerce_after_single_product', array( $this, 'capture_product_view' ) );
  33. add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) );
  34. add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) );
  35. add_action( 'wcct_before_cart_widget', array( $this, 'remove_from_cart' ) );
  36. add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 );
  37. // cart checkout
  38. add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) );
  39. // order confirmed
  40. add_action( 'woocommerce_thankyou', array( $this, 'order_process' ), 10, 1 );
  41. add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart_via_quantity' ), 10, 1 );
  42. }
  43. /**
  44. * Make _wca available to queue events
  45. */
  46. public function wp_head_top() {
  47. if ( is_cart() || is_checkout() || is_checkout_pay_page() || is_order_received_page() || is_add_payment_method_page() ) {
  48. $prevent_referrer_code = "<script>window._wca_prevent_referrer = true;</script>";
  49. echo "$prevent_referrer_code\r\n";
  50. }
  51. $wca_code = "<script>window._wca = window._wca || [];</script>";
  52. echo "$wca_code\r\n";
  53. }
  54. /**
  55. * Place script to call s.js, Store Analytics
  56. */
  57. public function wp_head_bottom() {
  58. $filename = 's-' . gmdate( 'YW' ) . '.js';
  59. $async_code = "<script async src='https://stats.wp.com/" . $filename . "'></script>";
  60. echo "$async_code\r\n";
  61. }
  62. /**
  63. * On product lists or other non-product pages, add an event listener to "Add to Cart" button click
  64. */
  65. public function loop_session_events() {
  66. $blogid = Jetpack::get_option( 'id' );
  67. // check for previous add-to-cart cart events
  68. $data = WC()->session->get( 'wca_session_data' );
  69. if ( ! empty( $data ) ) {
  70. foreach ( $data as $data_instance ) {
  71. $product = wc_get_product( $data_instance['product_id'] );
  72. if ( ! $product ) {
  73. continue;
  74. }
  75. $product_details = $this->get_product_details( $product );
  76. wc_enqueue_js(
  77. "_wca.push( {
  78. '_en': '" . esc_js( $data_instance['event'] ) . "',
  79. 'blog_id': '" . esc_js( $blogid ) . "',
  80. 'pi': '" . esc_js( $data_instance['product_id'] ) . "',
  81. 'pn': '" . esc_js( $product_details['name'] ) . "',
  82. 'pc': '" . esc_js( $product_details['category'] ) . "',
  83. 'pp': '" . esc_js( $product_details['price'] ) . "',
  84. 'pq': '" . esc_js( $data_instance['quantity'] ) . "',
  85. 'ui': '" . esc_js( $this->get_user_id() ) . "',
  86. } );"
  87. );
  88. }
  89. // clear data
  90. WC()->session->set( 'wca_session_data', '' );
  91. }
  92. }
  93. /**
  94. * On the cart page, add an event listener for removal of product click
  95. */
  96. public function remove_from_cart() {
  97. // We listen at div.woocommerce because the cart 'form' contents get forcibly
  98. // updated and subsequent removals from cart would then not have this click
  99. // handler attached.
  100. $blogid = Jetpack::get_option( 'id' );
  101. wc_enqueue_js(
  102. "jQuery( 'div.woocommerce' ).on( 'click', 'a.remove', function() {
  103. var productID = jQuery( this ).data( 'product_id' );
  104. var quantity = jQuery( this ).parent().parent().find( '.qty' ).val()
  105. var productDetails = {
  106. 'id': productID,
  107. 'quantity': quantity ? quantity : '1',
  108. };
  109. _wca.push( {
  110. '_en': 'woocommerceanalytics_remove_from_cart',
  111. 'blog_id': '" . esc_js( $blogid ) . "',
  112. 'pi': productDetails.id,
  113. 'pq': productDetails.quantity,
  114. 'ui': '" . esc_js( $this->get_user_id() ) . "',
  115. } );
  116. } );"
  117. );
  118. }
  119. /**
  120. * Adds the product ID to the remove product link (for use by remove_from_cart above) if not present
  121. *
  122. * @param string $url Full HTML a tag of the link to remove an item from the cart.
  123. * @param string $key Unique Key ID for a cart item.
  124. *
  125. * @return mixed.
  126. */
  127. public function remove_from_cart_attributes( $url, $key ) {
  128. if ( false !== strpos( $url, 'data-product_id' ) ) {
  129. return $url;
  130. }
  131. $item = WC()->cart->get_cart_item( $key );
  132. $product = $item['data'];
  133. $new_attributes = sprintf(
  134. '" data-product_id="%s">',
  135. esc_attr( $product->get_id() )
  136. );
  137. $url = str_replace( '">', $new_attributes, $url );
  138. return $url;
  139. }
  140. /**
  141. * Gather relevant product information
  142. *
  143. * @param array $product product
  144. * @return array
  145. */
  146. public function get_product_details( $product ) {
  147. return array(
  148. 'id' => $product->get_id(),
  149. 'name' => $product->get_title(),
  150. 'category' => $this->get_product_categories_concatenated( $product ),
  151. 'price' => $product->get_price(),
  152. );
  153. }
  154. /**
  155. * Track a product page view
  156. */
  157. public function capture_product_view() {
  158. global $product;
  159. $blogid = Jetpack::get_option( 'id' );
  160. $product_details = $this->get_product_details( $product );
  161. wc_enqueue_js(
  162. "_wca.push( {
  163. '_en': 'woocommerceanalytics_product_view',
  164. 'blog_id': '" . esc_js( $blogid ) . "',
  165. 'pi': '" . esc_js( $product_details['id'] ) . "',
  166. 'pn': '" . esc_js( $product_details['name'] ) . "',
  167. 'pc': '" . esc_js( $product_details['category'] ) . "',
  168. 'pp': '" . esc_js( $product_details['price'] ) . "',
  169. 'ui': '" . esc_js( $this->get_user_id() ) . "',
  170. } );"
  171. );
  172. }
  173. /**
  174. * On the Checkout page, trigger an event for each product in the cart
  175. */
  176. public function checkout_process() {
  177. $universal_commands = array();
  178. $cart = WC()->cart->get_cart();
  179. $blogid = Jetpack::get_option( 'id' );
  180. foreach ( $cart as $cart_item_key => $cart_item ) {
  181. /**
  182. * This filter is already documented in woocommerce/templates/cart/cart.php
  183. */
  184. $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
  185. if ( ! $product ) {
  186. continue;
  187. }
  188. $product_details = $this->get_product_details( $product );
  189. $universal_commands[] = "_wca.push( {
  190. '_en': 'woocommerceanalytics_product_checkout',
  191. 'blog_id': '" . esc_js( $blogid ) . "',
  192. 'pi': '" . esc_js( $product_details['id'] ) . "',
  193. 'pn': '" . esc_js( $product_details['name'] ) . "',
  194. 'pc': '" . esc_js( $product_details['category'] ) . "',
  195. 'pp': '" . esc_js( $product_details['price'] ) . "',
  196. 'pq': '" . esc_js( $cart_item['quantity'] ) . "',
  197. 'ui': '" . esc_js( $this->get_user_id() ) . "',
  198. } );";
  199. }
  200. wc_enqueue_js( implode( "\r\n", $universal_commands ) );
  201. }
  202. /**
  203. * After the checkout process, fire an event for each item in the order
  204. *
  205. * @param string $order_id Order Id.
  206. */
  207. public function order_process( $order_id ) {
  208. $order = wc_get_order( $order_id );
  209. $universal_commands = array();
  210. $blogid = Jetpack::get_option( 'id' );
  211. // loop through products in the order and queue a purchase event.
  212. foreach ( $order->get_items() as $order_item_id => $order_item ) {
  213. $product = $order->get_product_from_item( $order_item );
  214. $product_details = $this->get_product_details( $product );
  215. $universal_commands[] = "_wca.push( {
  216. '_en': 'woocommerceanalytics_product_purchase',
  217. 'blog_id': '" . esc_js( $blogid ) . "',
  218. 'pi': '" . esc_js( $product_details['id'] ) . "',
  219. 'pn': '" . esc_js( $product_details['name'] ) . "',
  220. 'pc': '" . esc_js( $product_details['category'] ) . "',
  221. 'pp': '" . esc_js( $product_details['price'] ) . "',
  222. 'pq': '" . esc_js( $order_item->get_quantity() ) . "',
  223. 'oi': '" . esc_js( $order->get_order_number() ) . "',
  224. 'ui': '" . esc_js( $this->get_user_id() ) . "',
  225. } );";
  226. }
  227. wc_enqueue_js( implode( "\r\n", $universal_commands ) );
  228. }
  229. /**
  230. * Listen for clicks on the "Update Cart" button to know if an item has been removed by
  231. * updating its quantity to zero
  232. */
  233. public function remove_from_cart_via_quantity() {
  234. $blogid = Jetpack::get_option( 'id' );
  235. wc_enqueue_js( "
  236. jQuery( 'button[name=update_cart]' ).on( 'click', function() {
  237. var cartItems = jQuery( '.cart_item' );
  238. cartItems.each( function( item ) {
  239. var qty = jQuery( this ).find( 'input.qty' );
  240. if ( qty && qty.val() === '0' ) {
  241. var productID = jQuery( this ).find( '.product-remove a' ).data( 'product_id' );
  242. _wca.push( {
  243. '_en': 'woocommerceanalytics_remove_from_cart',
  244. 'blog_id': '" . esc_js( $blogid ) . "',
  245. 'pi': productID,
  246. 'ui': '" . esc_js( $this->get_user_id() ) . "',
  247. } );
  248. }
  249. } );
  250. } );
  251. " );
  252. }
  253. /**
  254. * Get the current user id
  255. *
  256. * @return int
  257. */
  258. public function get_user_id() {
  259. if ( is_user_logged_in() ) {
  260. $blogid = Jetpack::get_option( 'id' );
  261. $userid = get_current_user_id();
  262. return $blogid . ":" . $userid;
  263. }
  264. return 'null';
  265. }
  266. /**
  267. * @param $cart_item_key
  268. * @param $product_id
  269. * @param $quantity
  270. * @param $variation_id
  271. * @param $variation
  272. * @param $cart_item_data
  273. */
  274. public function capture_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
  275. $referer_postid = isset( $_SERVER['HTTP_REFERER'] ) ? url_to_postid( $_SERVER['HTTP_REFERER'] ) : 0;
  276. // if the referring post is not a product OR the product being added is not the same as post
  277. // (eg. related product list on single product page) then include a product view event
  278. if ( ! wc_get_product( $referer_postid ) || $product_id != $referer_postid ) {
  279. $this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_product_view' );
  280. }
  281. // add cart event to the session data
  282. $this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_add_to_cart' );
  283. }
  284. /**
  285. * @param $product_id
  286. * @param $quantity
  287. * @param $event
  288. */
  289. public function capture_event_in_session_data( $product_id, $quantity, $event ) {
  290. $product = wc_get_product( $product_id );
  291. if ( ! $product ) {
  292. return;
  293. }
  294. $quantity = ( $quantity == 0 ) ? 1 : $quantity;
  295. // check for existing data
  296. $data = WC()->session->get( 'wca_session_data' );
  297. if ( empty( $data ) || ! is_array( $data ) ) {
  298. $data = array();
  299. }
  300. // extract new event data
  301. $new_data = array(
  302. 'event' => $event,
  303. 'product_id' => (string) $product_id,
  304. 'quantity' => (string) $quantity,
  305. );
  306. // append new data
  307. $data[] = $new_data;
  308. WC()->session->set( 'wca_session_data', $data );
  309. }
  310. /**
  311. * Gets product categories or varation attributes as a formatted concatenated string
  312. *
  313. * @param object $product WC_Product.
  314. * @return string
  315. */
  316. public function get_product_categories_concatenated( $product ) {
  317. if ( ! $product ) {
  318. return '';
  319. }
  320. $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : '';
  321. if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
  322. $line = wc_get_formatted_variation( $variation_data, true );
  323. } else {
  324. $out = array();
  325. $categories = get_the_terms( $product->get_id(), 'product_cat' );
  326. if ( $categories ) {
  327. foreach ( $categories as $category ) {
  328. $out[] = $category->name;
  329. }
  330. }
  331. $line = join( '/', $out );
  332. }
  333. return $line;
  334. }
  335. }