Skip to content

Commit

Permalink
Move tracking to class-wc-google-gtag-js.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Martyn Jones committed Oct 27, 2023
1 parent 1c54142 commit 2ee2fad
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
8 changes: 2 additions & 6 deletions includes/class-wc-abstract-google-analytics-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,13 @@ abstract public function remove_from_cart();

/**
* Enqueue JavaScript to track a product detail view
*
* @param WC_Product $product
*/
abstract public function product_detail( $product );
abstract public function product_detail();

/**
* Enqueue JS to track when the checkout process is started
*
* @param array $cart items/contents of the cart
*/
abstract public function checkout_process( $cart );
abstract public function checkout_process();

/**
* Enqueue JavaScript for Add to cart tracking
Expand Down
89 changes: 78 additions & 11 deletions includes/class-wc-google-gtag-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public function __construct( $options = array() ) {
// Setup frontend scripts
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'woocommerce_before_single_product', array( $this, 'setup_frontend_scripts' ) );

// Event tracking code
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) );
add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) );
add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) );
add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 );
add_filter( 'woocommerce_loop_add_to_cart_link', array( $this, 'track_product' ), 10, 2 );
add_action( 'woocommerce_after_single_product', array( $this, 'product_detail' ) );
add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) );
}

/**
Expand Down Expand Up @@ -234,10 +243,14 @@ public static function listing_click( $product ) {

/**
* Output Javascript to track add_to_cart event on single product page
*
* @param WC_Product $product The product currently being viewed
*/
public static function add_to_cart( WC_Product $product ) {
public static function add_to_cart() {
if ( 'yes' !== self::get( 'ga_event_tracking_enabled' ) || ! is_single() ) {
return;
}

global $product;

$items = array(
'id' => self::get_product_identifier( $product ),
'name' => $product->get_title(),
Expand All @@ -263,7 +276,7 @@ public static function add_to_cart( WC_Product $product ) {
}

$event_code .= self::get_event_code(
'add_to_cart',
'add_to_cart_x',
'{"items": [item_data]}',
false
);
Expand Down Expand Up @@ -382,6 +395,10 @@ public function add_item( $order, $item ) {
* Output JavaScript to track an enhanced ecommerce remove from cart action
*/
public function remove_from_cart() {
if ( 'yes' !== self::get( 'ga_enhanced_remove_from_cart_enabled' ) ) {
return;
}

$event_code = self::get_event_code(
'remove_from_cart',
'{"items": [{
Expand All @@ -401,16 +418,62 @@ public function remove_from_cart() {
);
}


/**
* Enqueue JavaScript to track a product detail view
* Adds the product ID and SKU to the remove product link if not present
*
* @param WC_Product $product
* @param string $url
* @param string $key
* @return string
*/
public function product_detail( $product ) {
if ( empty( $product ) ) {
public function remove_from_cart_attributes( $url, $key ) {
if ( strpos( $url, 'data-product_id' ) !== false ) {
return $url;
}

if ( ! is_object( WC()->cart ) ) {
return $url;
}

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

if ( ! is_object( $product ) ) {
return $url;
}

$url = str_replace( 'href=', 'data-product_id="' . esc_attr( $product->get_id() ) . '" data-product_sku="' . esc_attr( $product->get_sku() ) . '" href=', $url );
return $url;
}

/**
* Measure a product click and impression from a Product list
*
* @param string $link The Add To Cart Link
* @param WC_Product $product The Product
*/
public function track_product( $link, $product ) {
if ( 'yes' === self::get( 'ga_enhanced_product_impression_enabled' ) ) {
self::listing_impression( $product );
}

if ( 'yes' === self::get( 'ga_enhanced_product_click_enabled' ) ) {
self::listing_click( $product );
}

return $link;
}

/**
* Enqueue JavaScript to track a product detail view
*/
public function product_detail() {
if ( 'yes' !== self::get( 'ga_enhanced_product_detail_view_enabled' ) ) {
return;
}

global $product;

$event_code = self::get_event_code(
'view_item',
array(
Expand All @@ -430,10 +493,14 @@ public function product_detail( $product ) {

/**
* Enqueue JS to track when the checkout process is started
*
* @param array $cart items/contents of the cart
*/
public function checkout_process( $cart ) {
public function checkout_process() {
if ( 'yes' !== self::get( 'ga_enhanced_checkout_process_enabled' ) ) {
return;
}

$cart = WC()->cart->get_cart();

$items = array();
foreach ( $cart as $cart_item_key => $cart_item ) {
$product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
Expand Down

0 comments on commit 2ee2fad

Please sign in to comment.