Нет описания

class-jetpack-woocommerce-analytics-universal.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. /**
  3. * Jetpack_WooCommerce_Analytics_Universal
  4. *
  5. * @package automattic/jetpack
  6. * @author Automattic
  7. */
  8. use Automattic\Jetpack\Assets;
  9. /**
  10. * Bail if accessed directly
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. /**
  16. * Class Jetpack_WooCommerce_Analytics_Universal
  17. * Filters and Actions added to Store pages to perform analytics
  18. */
  19. class Jetpack_WooCommerce_Analytics_Universal {
  20. /**
  21. * Jetpack_WooCommerce_Analytics_Universal constructor.
  22. */
  23. public function __construct() {
  24. // loading _wca.
  25. add_action( 'wp_head', array( $this, 'wp_head_top' ), 1 );
  26. // add to carts from non-product pages or lists -- search, store etc.
  27. add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 );
  28. // loading s.js.
  29. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_tracking_script' ) );
  30. // Capture cart events.
  31. add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 );
  32. // single product page view.
  33. add_action( 'woocommerce_after_single_product', array( $this, 'capture_product_view' ) );
  34. add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) );
  35. add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) );
  36. add_action( 'wcct_before_cart_widget', array( $this, 'remove_from_cart' ) );
  37. add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 );
  38. // Checkout.
  39. // Send events after checkout template (shortcode).
  40. add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) );
  41. // Send events after checkout block.
  42. add_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after', array( $this, 'checkout_process' ) );
  43. // order confirmed.
  44. add_action( 'woocommerce_thankyou', array( $this, 'order_process' ), 10, 1 );
  45. add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart_via_quantity' ), 10, 1 );
  46. add_filter( 'woocommerce_checkout_posted_data', array( $this, 'save_checkout_post_data' ), 10, 1 );
  47. }
  48. /**
  49. * Make _wca available to queue events
  50. */
  51. public function wp_head_top() {
  52. if ( is_cart() || is_checkout() || is_checkout_pay_page() || is_order_received_page() || is_add_payment_method_page() ) {
  53. echo '<script>window._wca_prevent_referrer = true;</script>' . "\r\n";
  54. }
  55. echo '<script>window._wca = window._wca || [];</script>' . "\r\n";
  56. }
  57. /**
  58. * Place script to call s.js, Store Analytics.
  59. */
  60. public function enqueue_tracking_script() {
  61. $filename = sprintf(
  62. 'https://stats.wp.com/s-%d.js',
  63. gmdate( 'YW' )
  64. );
  65. Assets::enqueue_async_script( 'woocommerce-analytics', esc_url( $filename ), esc_url( $filename ), array(), null, false );
  66. }
  67. /**
  68. * Default event properties which should be included with all events.
  69. *
  70. * @return array Array of standard event props.
  71. */
  72. public function get_common_properties() {
  73. $site_info = array(
  74. 'blog_id' => Jetpack::get_option( 'id' ),
  75. 'ui' => $this->get_user_id(),
  76. 'url' => home_url(),
  77. 'woo_version' => WC()->version,
  78. );
  79. $cart_checkout_info = self::get_cart_checkout_info();
  80. return array_merge( $site_info, $cart_checkout_info );
  81. }
  82. /**
  83. * Render tracks event properties as string of JavaScript object props.
  84. *
  85. * @param array $properties Array of key/value pairs.
  86. * @return string String of the form "key1: value1, key2: value2, " (etc).
  87. */
  88. private function render_properties_as_js( $properties ) {
  89. $js_args_string = '';
  90. foreach ( $properties as $key => $value ) {
  91. if ( is_array( $value ) ) {
  92. $js_args_string = $js_args_string . "'$key': " . wp_json_encode( $value ) . ',';
  93. } else {
  94. $js_args_string = $js_args_string . "'$key': '" . esc_js( $value ) . "', ";
  95. }
  96. }
  97. return $js_args_string;
  98. }
  99. /**
  100. * Record an event with optional custom properties.
  101. *
  102. * @param string $event_name The name of the event to record.
  103. * @param integer $product_id The id of the product relating to the event.
  104. * @param array $properties Optional array of (key => value) event properties.
  105. *
  106. * @return string|void
  107. */
  108. public function record_event( $event_name, $product_id, $properties = array() ) {
  109. $js = $this->process_event_properties( $event_name, $product_id, $properties );
  110. wc_enqueue_js( "_wca.push({$js});" );
  111. }
  112. /**
  113. * Compose event properties.
  114. *
  115. * @param string $event_name The name of the event to record.
  116. * @param integer $product_id The id of the product relating to the event.
  117. * @param array $properties Optional array of (key => value) event properties.
  118. *
  119. * @return string|void
  120. */
  121. public function process_event_properties( $event_name, $product_id, $properties = array() ) {
  122. $product = wc_get_product( $product_id );
  123. if ( ! $product instanceof WC_Product ) {
  124. return;
  125. }
  126. $product_details = $this->get_product_details( $product );
  127. $all_props = array_merge(
  128. $properties,
  129. $this->get_common_properties()
  130. );
  131. $js = "{
  132. '_en': '" . esc_js( $event_name ) . "',
  133. 'pi': '" . esc_js( $product_id ) . "',
  134. 'pn': '" . esc_js( $product_details['name'] ) . "',
  135. 'pc': '" . esc_js( $product_details['category'] ) . "',
  136. 'pp': '" . esc_js( $product_details['price'] ) . "',
  137. 'pt': '" . esc_js( $product_details['type'] ) . "'," .
  138. $this->render_properties_as_js( $all_props ) . '
  139. }';
  140. return $js;
  141. }
  142. /**
  143. * On product lists or other non-product pages, add an event listener to "Add to Cart" button click
  144. */
  145. public function loop_session_events() {
  146. // Check for previous events queued in session data.
  147. if ( is_object( WC()->session ) ) {
  148. $data = WC()->session->get( 'wca_session_data' );
  149. if ( ! empty( $data ) ) {
  150. foreach ( $data as $data_instance ) {
  151. $this->record_event(
  152. $data_instance['event'],
  153. $data_instance['product_id'],
  154. array(
  155. 'pq' => $data_instance['quantity'],
  156. )
  157. );
  158. }
  159. // Clear data, now that these events have been recorded.
  160. WC()->session->set( 'wca_session_data', '' );
  161. }
  162. }
  163. }
  164. /**
  165. * On the cart page, add an event listener for removal of product click
  166. */
  167. public function remove_from_cart() {
  168. $common_props = $this->render_properties_as_js(
  169. $this->get_common_properties()
  170. );
  171. // We listen at div.woocommerce because the cart 'form' contents get forcibly
  172. // updated and subsequent removals from cart would then not have this click
  173. // handler attached.
  174. wc_enqueue_js(
  175. "jQuery( 'div.woocommerce' ).on( 'click', 'a.remove', function() {
  176. var productID = jQuery( this ).data( 'product_id' );
  177. var quantity = jQuery( this ).parent().parent().find( '.qty' ).val()
  178. var productDetails = {
  179. 'id': productID,
  180. 'quantity': quantity ? quantity : '1',
  181. };
  182. _wca.push( {
  183. '_en': 'woocommerceanalytics_remove_from_cart',
  184. 'pi': productDetails.id,
  185. 'pq': productDetails.quantity, " .
  186. $common_props . '
  187. } );
  188. } );'
  189. );
  190. }
  191. /**
  192. * Adds the product ID to the remove product link (for use by remove_from_cart above) if not present
  193. *
  194. * @param string $url Full HTML a tag of the link to remove an item from the cart.
  195. * @param string $key Unique Key ID for a cart item.
  196. *
  197. * @return mixed.
  198. */
  199. public function remove_from_cart_attributes( $url, $key ) {
  200. if ( false !== strpos( $url, 'data-product_id' ) ) {
  201. return $url;
  202. }
  203. $item = WC()->cart->get_cart_item( $key );
  204. $product = $item['data'];
  205. $new_attributes = sprintf(
  206. '" data-product_id="%s">',
  207. esc_attr( $product->get_id() )
  208. );
  209. $url = str_replace( '">', $new_attributes, $url );
  210. return $url;
  211. }
  212. /**
  213. * Gather relevant product information
  214. *
  215. * @param array $product product.
  216. * @return array
  217. */
  218. public function get_product_details( $product ) {
  219. return array(
  220. 'id' => $product->get_id(),
  221. 'name' => $product->get_title(),
  222. 'category' => $this->get_product_categories_concatenated( $product ),
  223. 'price' => $product->get_price(),
  224. 'type' => $product->get_type(),
  225. );
  226. }
  227. /**
  228. * Track a product page view
  229. */
  230. public function capture_product_view() {
  231. global $product;
  232. $this->record_event(
  233. 'woocommerceanalytics_product_view',
  234. $product->get_id()
  235. );
  236. }
  237. /**
  238. * On the Checkout page, trigger an event for each product in the cart
  239. */
  240. public function checkout_process() {
  241. $cart = WC()->cart->get_cart();
  242. $guest_checkout = ucfirst( get_option( 'woocommerce_enable_guest_checkout', 'No' ) );
  243. $create_account = ucfirst( get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'No' ) );
  244. $enabled_payment_options = array_filter(
  245. WC()->payment_gateways->get_available_payment_gateways(),
  246. function ( $payment_gateway ) {
  247. if ( ! $payment_gateway instanceof WC_Payment_Gateway ) {
  248. return false;
  249. }
  250. return $payment_gateway->is_available();
  251. }
  252. );
  253. $enabled_payment_options = array_keys( $enabled_payment_options );
  254. $include_express_payment = false;
  255. $wcpay_version = get_option( 'woocommerce_woocommerce_payments_version' );
  256. $has_required_wcpay_version = version_compare( $wcpay_version, '2.9.0', '>=' );
  257. // Check express payment availablity only if WC Pay is enabled and express checkout (payment request) is enabled.
  258. if ( in_array( 'woocommerce_payments', $enabled_payment_options, true ) && $has_required_wcpay_version ) {
  259. $wcpay_settings = get_option( 'woocommerce_woocommerce_payments_settings', array() );
  260. if ( array_key_exists( 'payment_request', $wcpay_settings ) && 'yes' === $wcpay_settings['payment_request'] ) {
  261. $include_express_payment = true;
  262. }
  263. }
  264. foreach ( $cart as $cart_item_key => $cart_item ) {
  265. /**
  266. * This filter is already documented in woocommerce/templates/cart/cart.php
  267. */
  268. $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
  269. if ( ! $product ) {
  270. continue;
  271. }
  272. if ( true === $include_express_payment ) {
  273. $properties = $this->process_event_properties(
  274. 'woocommerceanalytics_product_checkout',
  275. $product->get_id(),
  276. array(
  277. 'pq' => $cart_item['quantity'],
  278. 'payment_options' => $enabled_payment_options,
  279. 'device' => wp_is_mobile() ? 'mobile' : 'desktop',
  280. 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No',
  281. 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No',
  282. 'express_checkout' => 'null',
  283. )
  284. );
  285. wc_enqueue_js(
  286. "
  287. // wcpay.payment-request.availability event gets fired twice.
  288. // make sure we push only one event.
  289. var cartItem_{$cart_item_key}_logged = false;
  290. wp.hooks.addAction('wcpay.payment-request.availability', 'wcpay', function(args) {
  291. if ( true === cartItem_{$cart_item_key}_logged ) {
  292. return;
  293. }
  294. var properties = {$properties};
  295. properties.express_checkout = args.paymentRequestType;
  296. _wca.push(properties);
  297. cartItem_{$cart_item_key}_logged = true;
  298. });
  299. "
  300. );
  301. } else {
  302. $this->record_event(
  303. 'woocommerceanalytics_product_checkout',
  304. $product->get_id(),
  305. array(
  306. 'pq' => $cart_item['quantity'],
  307. 'payment_options' => $enabled_payment_options,
  308. 'device' => wp_is_mobile() ? 'mobile' : 'desktop',
  309. 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No',
  310. 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No',
  311. 'express_checkout' => 'null',
  312. )
  313. );
  314. }
  315. }
  316. }
  317. /**
  318. * After the checkout process, fire an event for each item in the order
  319. *
  320. * @param string $order_id Order Id.
  321. */
  322. public function order_process( $order_id ) {
  323. $order = wc_get_order( $order_id );
  324. $payment_option = $order->get_payment_method();
  325. if ( is_object( WC()->session ) ) {
  326. $create_account = true === WC()->session->get( 'wc_checkout_createaccount_used' ) ? 'Y' : 'N';
  327. } else {
  328. $create_account = 'N';
  329. }
  330. $guest_checkout = $order->get_user() ? 'N' : 'Y';
  331. $express_checkout = 'null';
  332. // When the payment option is woocommerce_payment
  333. // See if Google Pay or Apple Pay was used.
  334. if ( 'woocommerce_payments' === $payment_option ) {
  335. $payment_option_title = $order->get_payment_method_title();
  336. if ( 'Google Pay (WooCommerce Payments)' === $payment_option_title ) {
  337. $express_checkout = array( 'google_pay' );
  338. } elseif ( 'Apple Pay (WooCommerce Payments)' === $payment_option_title ) {
  339. $express_checkout = array( 'apple_pay' );
  340. }
  341. }
  342. // loop through products in the order and queue a purchase event.
  343. foreach ( $order->get_items() as $order_item ) {
  344. $product_id = is_callable( array( $order_item, 'get_product_id' ) ) ? $order_item->get_product_id() : -1;
  345. $this->record_event(
  346. 'woocommerceanalytics_product_purchase',
  347. $product_id,
  348. array(
  349. 'oi' => $order->get_order_number(),
  350. 'pq' => $order_item->get_quantity(),
  351. 'device' => wp_is_mobile() ? 'mobile' : 'desktop',
  352. 'payment_option' => $payment_option,
  353. 'create_account' => $create_account,
  354. 'guest_checkout' => $guest_checkout,
  355. 'express_checkout' => $express_checkout,
  356. )
  357. );
  358. }
  359. }
  360. /**
  361. * Listen for clicks on the "Update Cart" button to know if an item has been removed by
  362. * updating its quantity to zero
  363. */
  364. public function remove_from_cart_via_quantity() {
  365. $common_props = $this->render_properties_as_js(
  366. $this->get_common_properties()
  367. );
  368. wc_enqueue_js(
  369. "
  370. jQuery( 'button[name=update_cart]' ).on( 'click', function() {
  371. var cartItems = jQuery( '.cart_item' );
  372. cartItems.each( function( item ) {
  373. var qty = jQuery( this ).find( 'input.qty' );
  374. if ( qty && qty.val() === '0' ) {
  375. var productID = jQuery( this ).find( '.product-remove a' ).data( 'product_id' );
  376. _wca.push( {
  377. '_en': 'woocommerceanalytics_remove_from_cart',
  378. 'pi': productID, " .
  379. $common_props . '
  380. } );
  381. }
  382. } );
  383. } );'
  384. );
  385. }
  386. /**
  387. * Get the current user id
  388. *
  389. * @return int
  390. */
  391. public function get_user_id() {
  392. if ( is_user_logged_in() ) {
  393. $blogid = Jetpack::get_option( 'id' );
  394. $userid = get_current_user_id();
  395. return $blogid . ':' . $userid;
  396. }
  397. return 'null';
  398. }
  399. /**
  400. * Track adding items to the cart.
  401. *
  402. * @param string $cart_item_key Cart item key.
  403. * @param int $product_id Product added to cart.
  404. * @param int $quantity Quantity added to cart.
  405. * @param int $variation_id Product variation.
  406. * @param array $variation Variation attributes..
  407. * @param array $cart_item_data Other cart data.
  408. */
  409. public function capture_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  410. $referer_postid = isset( $_SERVER['HTTP_REFERER'] ) ? url_to_postid( $_SERVER['HTTP_REFERER'] ) : 0;
  411. // if the referring post is not a product OR the product being added is not the same as post.
  412. // (eg. related product list on single product page) then include a product view event.
  413. $product_by_referer_postid = wc_get_product( $referer_postid );
  414. if ( ! $product_by_referer_postid instanceof WC_Product || (int) $product_id !== $referer_postid ) {
  415. $this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_product_view' );
  416. }
  417. // add cart event to the session data.
  418. $this->capture_event_in_session_data( $product_id, $quantity, 'woocommerceanalytics_add_to_cart' );
  419. }
  420. /**
  421. * Track in-session data.
  422. *
  423. * @param int $product_id Product ID.
  424. * @param int $quantity Quantity.
  425. * @param string $event Fired event.
  426. */
  427. public function capture_event_in_session_data( $product_id, $quantity, $event ) {
  428. $product = wc_get_product( $product_id );
  429. if ( ! $product instanceof WC_Product ) {
  430. return;
  431. }
  432. $quantity = ( 0 === $quantity ) ? 1 : $quantity;
  433. // check for existing data.
  434. if ( is_object( WC()->session ) ) {
  435. $data = WC()->session->get( 'wca_session_data' );
  436. if ( empty( $data ) || ! is_array( $data ) ) {
  437. $data = array();
  438. }
  439. } else {
  440. $data = array();
  441. }
  442. // extract new event data.
  443. $new_data = array(
  444. 'event' => $event,
  445. 'product_id' => (string) $product_id,
  446. 'quantity' => (string) $quantity,
  447. );
  448. // append new data.
  449. $data[] = $new_data;
  450. WC()->session->set( 'wca_session_data', $data );
  451. }
  452. /**
  453. * Gets product categories or varation attributes as a formatted concatenated string
  454. *
  455. * @param object $product WC_Product.
  456. * @return string
  457. */
  458. public function get_product_categories_concatenated( $product ) {
  459. if ( ! $product instanceof WC_Product ) {
  460. return '';
  461. }
  462. $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : '';
  463. if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
  464. $line = wc_get_formatted_variation( $variation_data, true );
  465. } else {
  466. $out = array();
  467. $categories = get_the_terms( $product->get_id(), 'product_cat' );
  468. if ( $categories ) {
  469. foreach ( $categories as $category ) {
  470. $out[] = $category->name;
  471. }
  472. }
  473. $line = join( '/', $out );
  474. }
  475. return $line;
  476. }
  477. /**
  478. * Search a specific post for text content.
  479. *
  480. * Note: similar code is in a WooCommerce core PR:
  481. * https://github.com/woocommerce/woocommerce/pull/25932
  482. *
  483. * @param integer $post_id The id of the post to search.
  484. * @param string $text The text to search for.
  485. * @return integer 1 if post contains $text (otherwise 0).
  486. */
  487. public static function post_contains_text( $post_id, $text ) {
  488. global $wpdb;
  489. // Search for the text anywhere in the post.
  490. $wildcarded = "%{$text}%";
  491. $result = $wpdb->get_var(
  492. $wpdb->prepare(
  493. "
  494. SELECT COUNT( * ) FROM {$wpdb->prefix}posts
  495. WHERE ID=%d
  496. AND {$wpdb->prefix}posts.post_content LIKE %s
  497. ",
  498. array( $post_id, $wildcarded )
  499. )
  500. );
  501. return ( '0' !== $result ) ? 1 : 0;
  502. }
  503. /**
  504. * Get info about the cart & checkout pages, in particular
  505. * whether the store is using shortcodes or Gutenberg blocks.
  506. * This info is cached in a transient.
  507. *
  508. * Note: similar code is in a WooCommerce core PR:
  509. * https://github.com/woocommerce/woocommerce/pull/25932
  510. *
  511. * @return array
  512. */
  513. public static function get_cart_checkout_info() {
  514. $transient_name = 'jetpack_woocommerce_analytics_cart_checkout_info_cache';
  515. $info = get_transient( $transient_name );
  516. if ( false === $info ) {
  517. $cart_page_id = wc_get_page_id( 'cart' );
  518. $checkout_page_id = wc_get_page_id( 'checkout' );
  519. $info = array(
  520. 'cart_page_contains_cart_block' => self::post_contains_text(
  521. $cart_page_id,
  522. '<!-- wp:woocommerce/cart'
  523. ),
  524. 'cart_page_contains_cart_shortcode' => self::post_contains_text(
  525. $cart_page_id,
  526. '[woocommerce_cart]'
  527. ),
  528. 'checkout_page_contains_checkout_block' => self::post_contains_text(
  529. $checkout_page_id,
  530. '<!-- wp:woocommerce/checkout'
  531. ),
  532. 'checkout_page_contains_checkout_shortcode' => self::post_contains_text(
  533. $checkout_page_id,
  534. '[woocommerce_checkout]'
  535. ),
  536. );
  537. set_transient( $transient_name, $info, DAY_IN_SECONDS );
  538. }
  539. return $info;
  540. }
  541. /**
  542. * Save createaccount post data to be used in $this->order_process.
  543. *
  544. * @param array $data post data from the checkout page.
  545. *
  546. * @return array
  547. */
  548. public function save_checkout_post_data( array $data ) {
  549. $session = WC()->session;
  550. if ( is_object( $session ) ) {
  551. if ( isset( $data['createaccount'] ) && ! empty( $data['createaccount'] ) ) {
  552. $session->set( 'wc_checkout_createaccount_used', true );
  553. $session->save_data();
  554. }
  555. }
  556. return $data;
  557. }
  558. }