Skip to content
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
47 changes: 34 additions & 13 deletions src/Integrations/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ private function init( $init ) {
*/
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_store_api_validate_add_to_cart', [ $this, 'track_add_to_cart' ], 10, 2 );
add_action( 'woocommerce_ajax_added_to_cart', [ $this, 'track_ajax_add_to_cart' ] );
/** @see \WC_Form_Handler::add_to_cart_action() runs on priority 20. */
add_action( 'wp_loaded', [ $this, 'track_direct_add_to_cart' ], 21 );
add_action( 'woocommerce_remove_cart_item', [ $this, 'track_remove_cart_item' ], 10, 2 );
add_action( 'wp_head', [ $this, 'track_entered_checkout' ] );
add_action( 'woocommerce_thankyou', [ $this, 'track_purchase' ] );
Expand Down Expand Up @@ -173,22 +175,22 @@ public function track_add_to_cart_on_product_page() {
}

/**
* Track (non-Interactivity API, i.e. AJAX) add to cart events.
*
* @param string|int $product_id ID of the product added to the cart.
* Track add to cart actions by direct link, e.g. ?product_type=download&add-to-cart=1&quantity=1
*
* @return void
*
* @codeCoverageIgnore Because we can't test XHR requests here.
* @codeCoverageIgnore Because we can't test XHR here.
*/
public function track_ajax_add_to_cart( $product_id ) {
$product = wc_get_product( $product_id );
$add_to_cart_data = [
'id' => $product_id,
'quantity' => $_POST[ 'quantity' ] ?? 1,
];
public function track_direct_add_to_cart() {
if ( ! isset( $_REQUEST[ 'add-to-cart' ] ) || ! is_numeric( wp_unslash( $_REQUEST[ 'add-to-cart' ] ) ) ) {
return;
}

$this->track_add_to_cart( $product, $add_to_cart_data );
$product_id = absint( wp_unslash( $_REQUEST[ 'add-to-cart' ] ) );
$product = wc_get_product( $product_id );
$quantity = isset( $_REQUEST[ 'quantity' ] ) ? absint( wp_unslash( $_REQUEST[ 'quantity' ] ) ) : 1;

$this->track_add_to_cart( $product, [ 'id' => $product_id, 'quantity' => $quantity ] );
}

/**
Expand Down Expand Up @@ -241,6 +243,25 @@ private function clean_data( $product ) {
return $product;
}

/**
* Track (non-Interactivity API, i.e. AJAX) add to cart events.
*
* @param string|int $product_id ID of the product added to the cart.
*
* @return void
*
* @codeCoverageIgnore Because we can't test XHR requests here.
*/
public function track_ajax_add_to_cart( $product_id ) {
$product = wc_get_product( $product_id );
$add_to_cart_data = [
'id' => $product_id,
'quantity' => $_POST[ 'quantity' ] ?? 1,
];

$this->track_add_to_cart( $product, $add_to_cart_data );
}

/**
* Track Remove from cart events.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public function do_request( $name = 'pageview', $domain = '', $url = '', $props
'u' => $url ?: wp_get_referer(),
];

// URL is required, so if no $url was set and no referer was found, attempt to create it from the REQUEST_URI server variable.
if ( empty( $body[ 'u' ] ) ) {
$body[ 'u' ] = $this->generate_event_url(); // @codeCoverageIgnore
}

// Revenue events use a different approach.
if ( isset( $props[ 'revenue' ] ) ) {
$body[ 'revenue' ] = reset( $props ); // @codeCoverageIgnore
Expand All @@ -124,6 +129,23 @@ public function do_request( $name = 'pageview', $domain = '', $url = '', $props
return $this->send_event( $request );
}

/**
* Attempts to generate the Event URL from available resources.
*
* @return string
*/
public function generate_event_url() {
$url = '';
$parts = parse_url( $_SERVER[ 'REQUEST_URI' ] );
$home_url_parts = parse_url( get_home_url() );

if ( isset( $home_url_parts[ 'scheme' ] ) && isset( $home_url_parts[ 'host' ] ) && isset( $parts[ 'path' ] ) ) {
$url = $home_url_parts[ 'scheme' ] . '://' . $home_url_parts [ 'host' ] . $parts[ 'path' ];
}

return $url;
}

/**
* Formats and sends $request to the Plausible API.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @package Plausible Analytics Unit Tests - ClientFactory
*/

namespace Plausible\Analytics\Tests\Unit;
namespace Plausible\Analytics\Tests\Integration;

use Plausible\Analytics\Tests\TestCase;
use Plausible\Analytics\WP\Client;
Expand Down
29 changes: 29 additions & 0 deletions tests/integration/ProxyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @package Plausible Analytics Unit Tests - ClientFactory
*/

namespace Plausible\Analytics\Tests\Integration;

use Plausible\Analytics\Tests\TestCase;
use Plausible\Analytics\WP\Proxy;

class ProxyTest extends TestCase {
/**
* @see Proxy::generate_event_url()
* @return void
*/
public function testGenerateEventUrl() {
$proxy = new Proxy( false );

$url = $proxy->generate_event_url();

$this->assertEquals( 'http://example.org', $url );

$_SERVER[ 'REQUEST_URI' ] = '/test';

$url = $proxy->generate_event_url();

$this->assertEquals( 'http://example.org/test', $url );
}
}