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

Fixed: Woo Add To Cart event wasn't sent on product pages. #219

Merged
merged 2 commits into from
Jul 18, 2024
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
42 changes: 32 additions & 10 deletions src/Integrations/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ private function init( $init ) {
/**
* Trigger tracking events.
*/
add_filter( 'woocommerce_after_add_to_cart_form', [ $this, 'track_add_to_cart_on_product_page' ] );
add_action( 'woocommerce_before_add_to_cart_quantity', [ $this, 'add_cart_form_hidden_input' ] );
add_action( 'woocommerce_after_add_to_cart_form', [ $this, 'track_add_to_cart_on_product_page' ] );
add_filter( 'woocommerce_store_api_validate_add_to_cart', [ $this, 'track_add_to_cart' ], 10, 2 );
add_filter( 'woocommerce_ajax_added_to_cart', [ $this, 'track_ajax_add_to_cart' ] );
add_action( 'woocommerce_remove_cart_item', [ $this, 'track_remove_cart_item' ], 10, 2 );
Expand Down Expand Up @@ -129,8 +130,29 @@ public function add_http_referer( $add_to_cart_data, $request ) {
}

/**
* A hacky approach (with lack of a proper solution) to make sure Add To Cart events are tracked on simple product pages. Unfortunately, cart
* information isn't available this way.
* Adds a hidden input with the same name and value as the add-to-cart button.
*
* TODO: This hack can be removed when the JS library uses sendBeacon to send the event.
*
* @return void
*
* @codeCoverageIgnore Because we can't test JS here.
*/
public function add_cart_form_hidden_input() {
$product = wc_get_product();

if ( ! $product ) {
return;
}
?>
<input type="hidden" name="add-to-cart" value="<?php echo $product->get_id(); ?>"/>
<?php
}

/**
* A hacky approach (with lack of a proper solution) to make sure Add To Cart events are tracked on simple product pages.
*
* TODO: Once our JS library uses sendBeacon we might be able to refactor this into a less hacky approach.
*
* @return void
*
Expand All @@ -144,18 +166,18 @@ public function track_add_to_cart_on_product_page() {
}
?>
<script>
let addToCartButton = document.querySelector('button.single_add_to_cart_button');
let addToCartForm = document.querySelector('form.cart');
let quantity = document.querySelector('input[name="quantity"]');

addToCartButton.classList.add('plausible-event-name=<?php echo str_replace( ' ', '+', $this->event_goals[ 'add-to-cart' ] ); ?>');
addToCartButton.classList.add('plausible-event-quantity=' + quantity.value);
addToCartButton.classList.add('plausible-event-product_id=<?php echo $product->get_id(); ?>');
addToCartButton.classList.add('plausible-event-product_name=<?php echo str_replace( ' ', '+', $product->get_name( null ) ); ?>');
addToCartButton.classList.add('plausible-event-price=<?php echo $product->get_price( null ); ?>');
addToCartForm.classList.add('plausible-event-name=<?php echo str_replace( ' ', '+', $this->event_goals[ 'add-to-cart' ] ); ?>');
addToCartForm.classList.add('plausible-event-quantity=' + quantity.value);
addToCartForm.classList.add('plausible-event-product_id=<?php echo $product->get_id(); ?>');
addToCartForm.classList.add('plausible-event-product_name=<?php echo str_replace( ' ', '+', $product->get_name( null ) ); ?>');
addToCartForm.classList.add('plausible-event-price=<?php echo $product->get_price( null ); ?>');

quantity.addEventListener('change', function (e) {
let target = e.target;
addToCartButton.className = addToCartButton.className.replace(/(plausible-event-quantity=).+?/, "\$1" + target.value);
addToCartForm.className = addToCartForm.className.replace(/(plausible-event-quantity=).+?/, "\$1" + target.value);
});
</script>
<?php
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public function testGetFilename() {

add_filter( 'plausible_analytics_settings', [ $this, 'enableSearch' ] );

global $wp_query;

$wp_query = new \WP_Query();
$wp_query->query( 's=test' );

$filename = Helpers::get_filename();

$this->assertEquals( 'plausible.pageview-props.manual', $filename );
Expand Down
Loading