From 5a4e588d6dfc30d4388df65dc43380476b057db5 Mon Sep 17 00:00:00 2001 From: Rua Haszard Date: Thu, 26 Mar 2020 11:34:37 +1300 Subject: [PATCH 1/4] add woo analytics event props for block/shortcode use in cart/checkout --- .../wp-woocommerce-analytics-universal.php | 75 ++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/modules/woocommerce-analytics/classes/wp-woocommerce-analytics-universal.php b/modules/woocommerce-analytics/classes/wp-woocommerce-analytics-universal.php index c01f634b5c399..4937a1351adca 100644 --- a/modules/woocommerce-analytics/classes/wp-woocommerce-analytics-universal.php +++ b/modules/woocommerce-analytics/classes/wp-woocommerce-analytics-universal.php @@ -82,12 +82,14 @@ public function enqueue_tracking_script() { * @return array Array of standard event props. */ public function get_common_properties() { - return array( + $site_info = array( 'blog_id' => Jetpack::get_option( 'id' ), 'ui' => $this->get_user_id(), 'url' => home_url(), 'woo_version' => WC()->version, ); + $cart_checkout_info = self::get_cart_checkout_info(); + return array_merge( $site_info, $cart_checkout_info ); } /** @@ -416,4 +418,75 @@ public function get_product_categories_concatenated( $product ) { return $line; } + /** + * Search a specific post for text content. + * + * Note: similar code is in a WooCommerce core PR: + * https://github.com/woocommerce/woocommerce/pull/25932 + * + * @param integer $post_id The id of the post to search. + * @param string $text The text to search for. + * @return string 'Yes' if post contains $text (otherwise 'No'). + */ + public static function post_contains_text( $post_id, $text ) { + global $wpdb; + + // Search for the text anywhere in the post. + $wildcarded = "%{$text}%"; + + $result = $wpdb->get_var( + $wpdb->prepare( + " + SELECT COUNT( * ) FROM {$wpdb->prefix}posts + WHERE ID=%d + AND {$wpdb->prefix}posts.post_content LIKE %s + ", + array( $post_id, $wildcarded ) + ) + ); + + return ( '0' !== $result ) ? 'Yes' : 'No'; + } + + /** + * Get info about the cart & checkout pages, in particular + * whether the store is using shortcodes or Gutenberg blocks. + * This info is cached in a transient. + * + * Note: similar code is in a WooCommerce core PR: + * https://github.com/woocommerce/woocommerce/pull/25932 + * + * @return array + */ + public static function get_cart_checkout_info() { + $info = get_transient( 'jetpack-woocommerce-analytics-cart-checkout-info-cache' ); + + if ( false === $info ) { + $cart_page_id = wc_get_page_id( 'cart' ); + $checkout_page_id = wc_get_page_id( 'checkout' ); + + $info = array( + 'cart_page_contains_cart_block' => self::post_contains_text( + $cart_page_id, + '