Skip to content

Commit

Permalink
Adds WC store_id to body of woopay tracker requests (#10412)
Browse files Browse the repository at this point in the history
  • Loading branch information
FangedParakeet authored Feb 22, 2025
1 parent b5c7cf9 commit fd6b540
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions changelog/add-10382-adds-store-id-to-tracker-request-body
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Adds store_id property to body of WooPay tracker events.
14 changes: 14 additions & 0 deletions includes/class-woopay-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ private function tracks_build_event_obj( $user, $event_name, $properties = [] )
$properties['blog_url'] = $site_url;
$properties['blog_id'] = \Jetpack_Options::get_option( 'id' );
$properties['user_lang'] = $user->get( 'WPLANG' );
$properties['store_id'] = $this->get_wc_store_id();

// Add event property for test mode vs. live mode events.
$properties['test_mode'] = WC_Payments::mode()->is_test() ? 1 : 0;
Expand Down Expand Up @@ -379,6 +380,19 @@ private function tracks_build_event_obj( $user, $event_name, $properties = [] )
);
}

/**
* Returns WC store_id value, if available.
* store_id introduced in WC 8.4.
*
* @return string|null
*/
public function get_wc_store_id() {
if ( defined( \WC_Install::STORE_ID_OPTION ) ) {
return get_option( \WC_Install::STORE_ID_OPTION, null );
}
return null;
}

/**
* Get the identity to send to tracks.
*
Expand Down
23 changes: 15 additions & 8 deletions tests/unit/test-class-woopay-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class WooPay_Tracker_Test extends WCPAY_UnitTestCase {
/**
* @var WooPay_Tracker
*/
private $tracker;
private $mock_tracker;

/**
* @var WC_Payments_Http
Expand Down Expand Up @@ -43,7 +43,12 @@ public function setUp(): void {
$this->http_client_stub->method( 'is_user_connected' )->willReturn( true );
$this->http_client_stub->method( 'get_connected_user_data' )->willReturn( [ 'ID' => 1234 ] );

$this->tracker = new WooPay_Tracker( $this->http_client_stub );
$this->mock_tracker = $this->getMockBuilder( WooPay_Tracker::class )
->setConstructorArgs( [ $this->http_client_stub ] )
->onlyMethods( [ 'get_wc_store_id' ] )
->getMock();
$this->mock_tracker->method( 'get_wc_store_id' )
->willReturn( 'mock_store_id' );

$this->cache = WC_Payments::get_database_cache();
$this->mock_cache = $this->createMock( WCPay\Database_Cache::class );
Expand All @@ -63,15 +68,15 @@ public function test_tracks_obeys_woopay_flag(): void {
$is_account_connected = true;

$this->setup_woopay_environment( $is_woopay_eligible, $is_account_connected );
$this->assertFalse( $this->tracker->should_enable_tracking() );
$this->assertFalse( $this->mock_tracker->should_enable_tracking() );
}

public function test_does_not_track_admin_pages(): void {
$is_woopay_eligible = true;
$is_account_connected = true;
$is_admin_page = true;
$this->setup_woopay_environment( $is_woopay_eligible, $is_account_connected, $is_admin_page );
$this->assertFalse( $this->tracker->should_enable_tracking() );
$this->assertFalse( $this->mock_tracker->should_enable_tracking() );
}

public function test_does_track_non_admins(): void {
Expand All @@ -84,25 +89,26 @@ public function test_does_track_non_admins(): void {

foreach ( $all_roles as $role ) {
wp_get_current_user()->set_role( $role );
$this->assertTrue( $this->tracker->should_enable_tracking() );
$this->assertTrue( $this->mock_tracker->should_enable_tracking() );
}
}

public function test_does_not_track_when_account_not_connected(): void {
$is_woopay_eligible = true;
$is_account_connected = false;
$this->setup_woopay_environment( $is_woopay_eligible, $is_account_connected );
$this->assertFalse( $this->tracker->should_enable_tracking() );
$this->assertFalse( $this->mock_tracker->should_enable_tracking() );
}

public function test_tracks_build_event_obj_for_admin_events(): void {
$this->set_account_connected( true );
$event_name = 'wcadmin_test_event';
$properties = [ 'test_property' => 'value' ];

$event_obj = $this->invoke_method( $this->tracker, 'tracks_build_event_obj', [ wp_get_current_user(), $event_name, $properties ] );
$event_obj = $this->invoke_method( $this->mock_tracker, 'tracks_build_event_obj', [ wp_get_current_user(), $event_name, $properties ] );
$this->assertEquals( 'value', $event_obj->test_property );
$this->assertEquals( 1234, $event_obj->_ui );
$this->assertEquals( 'mock_store_id', $event_obj->store_id );
$this->assertEquals( $event_name, $event_obj->_en );
}

Expand All @@ -111,11 +117,12 @@ public function test_tracks_build_event_obj_for_shopper_events() {
$event_name = 'wcpay_test_event';
$properties = [ 'test_property' => 'value' ];

$event_obj = $this->invoke_method( $this->tracker, 'tracks_build_event_obj', [ wp_get_current_user(), $event_name, $properties ] );
$event_obj = $this->invoke_method( $this->mock_tracker, 'tracks_build_event_obj', [ wp_get_current_user(), $event_name, $properties ] );

$this->assertInstanceOf( Jetpack_Tracks_Event::class, $event_obj );
$this->assertEquals( 'value', $event_obj->test_property );
$this->assertEquals( 1234, $event_obj->_ui );
$this->assertEquals( 'mock_store_id', $event_obj->store_id );
$this->assertEquals( $event_name, $event_obj->_en );
}

Expand Down

0 comments on commit fd6b540

Please sign in to comment.