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

Track used gateway when placing the order #8717

Merged
merged 3 commits into from
Apr 29, 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
5 changes: 5 additions & 0 deletions changelog/add-track-gateway-type
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: dev
Comment: Record gateway name when placing the order


68 changes: 59 additions & 9 deletions includes/class-woopay-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use WC_Payments_Features;
use WCPay\Constants\Country_Code;
use WP_Error;
use Exception;

defined( 'ABSPATH' ) || exit; // block direct access.

Expand Down Expand Up @@ -42,6 +43,13 @@ class WooPay_Tracker extends Jetpack_Tracks_Client {
*/
private $http;

/**
* Base URL for stats counter.
*
* @var string
*/
private static $pixel_base_url = 'https://pixel.wp.com/g.gif';


/**
* Constructor.
Expand All @@ -63,8 +71,8 @@ public function __construct( $http ) {
add_action( 'woocommerce_after_single_product', [ $this, 'classic_product_page_view' ] );
add_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after', [ $this, 'blocks_checkout_start' ] );
add_action( 'woocommerce_blocks_enqueue_cart_block_scripts_after', [ $this, 'blocks_cart_page_view' ] );
add_action( 'woocommerce_checkout_order_processed', [ $this, 'checkout_order_processed' ] );
add_action( 'woocommerce_blocks_checkout_order_processed', [ $this, 'checkout_order_processed' ] );
add_action( 'woocommerce_checkout_order_processed', [ $this, 'checkout_order_processed' ], 10, 2 );
add_action( 'woocommerce_blocks_checkout_order_processed', [ $this, 'checkout_order_processed' ], 10, 2 );
add_action( 'woocommerce_payments_save_user_in_woopay', [ $this, 'must_save_payment_method_to_platform' ] );
add_action( 'before_woocommerce_pay_form', [ $this, 'pay_for_order_page_view' ] );
add_action( 'woocommerce_thankyou', [ $this, 'thank_you_page_view' ] );
Expand Down Expand Up @@ -370,7 +378,6 @@ public function tracks_get_identity() {
];
}


/**
* Record a Tracks event that the classic checkout page has loaded.
*/
Expand Down Expand Up @@ -445,13 +452,56 @@ public function pay_for_order_page_view() {
}

/**
* Record a Tracks event that the order has been processed.
* Bump a counter. No user identifiable information is sent.
*
* @param string $group The group to bump the stat in.
* @param string $stat_name The name of the stat to bump.
*
* @return bool
*/
public function bump_stats( $group, $stat_name ) {
$pixel_url = sprintf(
self::$pixel_base_url . '?v=wpcom-no-pv&x_%s=%s',
$group,
$stat_name
);

$response = wp_remote_get( esc_url_raw( $pixel_url ) );

if ( is_wp_error( $response ) ) {
return false;
}

if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return false;
}

return true;
}

/**
* Record that the order has been processed.
*/
public function checkout_order_processed() {
$is_woopay_order = ( isset( $_SERVER['HTTP_USER_AGENT'] ) && 'WooPay' === $_SERVER['HTTP_USER_AGENT'] );
// Don't track WooPay orders. They will be tracked on WooPay side with more flow specific details.
if ( ! $is_woopay_order ) {
$this->maybe_record_wcpay_shopper_event( 'checkout_order_placed' );
public function checkout_order_processed( $order_id ) {

$payment_gateway = wc_get_payment_gateway_by_order( $order_id );
$properties = [ 'payment_title' => 'other' ];

// If the order was placed using WooCommerce Payments, record the payment title using Tracks.
if (strpos( $payment_gateway->id, 'woocommerce_payments') === 0 ) {
$order = wc_get_order( $order_id );
$payment_title = $order->get_payment_method_title();
$properties = [ 'payment_title' => $payment_title ];

$is_woopay_order = ( isset( $_SERVER['HTTP_USER_AGENT'] ) && 'WooPay' === $_SERVER['HTTP_USER_AGENT'] );

// Don't track WooPay orders. They will be tracked on WooPay side with more details.
if ( ! $is_woopay_order ) {
$this->maybe_record_wcpay_shopper_event( 'checkout_order_placed', $properties );
}
// If the order was placed using a different payment gateway, just increment a counter.
} else {
$this->bump_stats( 'wcpay_order_completed_gateway', 'other' );
}
}

Expand Down
Loading