Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WooCommerce Analytics: Queue add_to_cart Events #9052

Merged
merged 3 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ 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 );

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

// add to cart on single product page
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) );

// add to carts from list views (search, store etc.)
add_action( 'wp_footer', array( $this, 'loop_add_to_cart' ) );
// 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' ) );
Expand Down Expand Up @@ -73,61 +72,37 @@ public function wp_head_bottom() {
echo "$async_code\r\n";
}

/**
* On a product page, add a click event listener to "Add to Cart" button click
*/
public function add_to_cart() {

if ( ! is_single() ) {
return;
}

$blogid = Jetpack::get_option( 'id' );
global $product;

wc_enqueue_js(
"jQuery( '" . esc_js( '.single_add_to_cart_button' ) . "' ).click( function() {
_wca.push( {
'_en': 'woocommerceanalytics_add_to_cart',
'blog_id': " . esc_js( $blogid ) . ",
'pi': '" . esc_js( $product->get_id() ) . "',
'pn' : '" . esc_js( $product->get_title() ) . "',
'pq': jQuery( 'input.qty' ).val() ? jQuery( 'input.qty' ).val() : '1',
'ui': '" . esc_js( $this->get_user_id() ) . "',
} );
} );"
);
}

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

wc_enqueue_js(
"jQuery( '" . esc_js( $selector ) . "' ).click( function() {
var productID = jQuery( this ).data( 'product_id' );
var productDetails = {
'id': productID,
'quantity': jQuery( this ).data( 'quantity' ),
};
_wca.push( {
'_en': 'woocommerceanalytics_product_view',
'blog_id': '" . esc_js( $blogid ) . "',
'pi': productDetails.id,
'ui': '" . esc_js( $this->get_user_id() ) . "',
} );
_wca.push( {
'_en': 'woocommerceanalytics_add_to_cart',
'blog_id': " . esc_js( $blogid ) . ",
'pi': productDetails.id,
'pq': productDetails.quantity,
'ui': '" . esc_js( $this->get_user_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', '' );
}
}

/**
Expand Down Expand Up @@ -189,33 +164,32 @@ public function remove_from_cart_attributes( $url, $key ) {
* @param array $product product
* @return array
*/
public function get_item_details( $product ) {
public function get_product_details( $product ) {
return array(
'id' => $product->get_id(),
'name' => $product->get_title(),
'category' => Jetpack_WooCommerce_Analytics_Utils::get_product_categories_concatenated( $product ),
'category' => $this->get_product_categories_concatenated( $product ),
'price' => $product->get_price(),
);
}

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

global $product;
$blogid = Jetpack::get_option( 'id' );

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

wc_enqueue_js(
"_wca.push( {
'_en': 'woocommerceanalytics_product_view',
'blog_id': '" . esc_js( $blogid ) . "',
'pi': '" . esc_js( $item_details['id'] ) . "',
'pn': '" . esc_js( $item_details['name'] ) . "',
'pc': '" . esc_js( $item_details['category'] ) . "',
'pp': '" . esc_js( $item_details['price'] ) . "',
'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() ) . "',
} );"
);
Expand All @@ -236,15 +210,19 @@ public function checkout_process() {
*/
$product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

$item_details = $this->get_item_details( $product );
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( $item_details['id'] ) . "',
'pn': '" . esc_js( $item_details['name'] ) . "',
'pc': '" . esc_js( $item_details['category'] ) . "',
'pp': '" . esc_js( $item_details['price'] ) . "',
'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() ) . "',
} );";
Expand All @@ -267,15 +245,15 @@ public function order_process( $order_id ) {
foreach ( $order->get_items() as $order_item_id => $order_item ) {
$product = $order->get_product_from_item( $order_item );

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

$universal_commands[] = "_wca.push( {
'_en': 'woocommerceanalytics_product_purchase',
'blog_id': '" . esc_js( $blogid ) . "',
'pi': '" . esc_js( $item_details['id'] ) . "',
'pn': '" . esc_js( $item_details['name'] ) . "',
'pc': '" . esc_js( $item_details['category'] ) . "',
'pp': '" . esc_js( $item_details['price'] ) . "',
'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() ) . "',
Expand Down Expand Up @@ -325,4 +303,84 @@ public function get_user_id() {
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;
}

}

This file was deleted.

1 change: 0 additions & 1 deletion modules/woocommerce-analytics/wp-woocommerce-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
exit;
}

require_once plugin_basename( 'classes/wp-woocommerce-analytics-utils.php' );
require_once plugin_basename( 'classes/wp-woocommerce-analytics-universal.php' );

/**
Expand Down