Merge pull request #30415 from woocommerce/add/wctracker-mobile-data
Add mobile data to WCTracker
This commit is contained in:
commit
aec78d9236
|
@ -166,6 +166,9 @@ class WC_Tracker {
|
||||||
// WooCommerce Admin info.
|
// WooCommerce Admin info.
|
||||||
$data['wc_admin_disabled'] = apply_filters( 'woocommerce_admin_disabled', false ) ? 'yes' : 'no';
|
$data['wc_admin_disabled'] = apply_filters( 'woocommerce_admin_disabled', false ) ? 'yes' : 'no';
|
||||||
|
|
||||||
|
// Mobile info.
|
||||||
|
$data['wc_mobile_usage'] = self::get_woocommerce_mobile_usage();
|
||||||
|
|
||||||
return apply_filters( 'woocommerce_tracker_data', $data );
|
return apply_filters( 'woocommerce_tracker_data', $data );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -756,6 +759,15 @@ class WC_Tracker {
|
||||||
'checkout_block_attributes' => $checkout_block_data['block_attributes'],
|
'checkout_block_attributes' => $checkout_block_data['block_attributes'],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get info about WooCommerce Mobile App usage
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function get_woocommerce_mobile_usage() {
|
||||||
|
return get_option( 'woocommerce_mobile_app_usage' );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WC_Tracker::init();
|
WC_Tracker::init();
|
||||||
|
|
|
@ -0,0 +1,142 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* REST API WC Telemetry controller
|
||||||
|
*
|
||||||
|
* Handles requests to the /wc-telemetry endpoint.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\RestApi
|
||||||
|
* @since 3.0.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Telemetry controller class.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\RestApi
|
||||||
|
* @extends WC_REST_Controller
|
||||||
|
*/
|
||||||
|
class WC_REST_Telemetry_Controller extends WC_REST_Controller {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint namespace.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $namespace = 'wc-telemetry';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Route base.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $rest_base = 'tracker';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register the route for /tracker
|
||||||
|
*/
|
||||||
|
public function register_routes() {
|
||||||
|
register_rest_route(
|
||||||
|
$this->namespace,
|
||||||
|
'/' . $this->rest_base,
|
||||||
|
array(
|
||||||
|
array(
|
||||||
|
'methods' => WP_REST_Server::CREATABLE,
|
||||||
|
'callback' => array( $this, 'record_usage_data' ),
|
||||||
|
'permission_callback' => array( $this, 'telemetry_permissions_check' ),
|
||||||
|
'args' => $this->get_collection_params(),
|
||||||
|
),
|
||||||
|
'schema' => array( $this, 'get_public_item_schema' ),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether a given request has permission to post telemetry data
|
||||||
|
*
|
||||||
|
* @param WP_REST_Request $request Full details about the request.
|
||||||
|
* @return WP_Error|boolean
|
||||||
|
*/
|
||||||
|
public function telemetry_permissions_check( $request ) {
|
||||||
|
if ( ! is_user_logged_in() ) {
|
||||||
|
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you post telemetry data.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Record WCTracker Data
|
||||||
|
*
|
||||||
|
* @param WP_REST_Request $request Full details about the request.
|
||||||
|
*/
|
||||||
|
public function record_usage_data( $request ) {
|
||||||
|
$new = $this->get_usage_data( $request );
|
||||||
|
if ( ! $new || ! $new['platform'] ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = get_option( 'woocommerce_mobile_app_usage' );
|
||||||
|
if ( ! $data ) {
|
||||||
|
$data = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$platform = $new['platform'];
|
||||||
|
if ( ! $data[ $platform ] || version_compare( $new['version'], $data[ $platform ]['version'], '>=' ) ) {
|
||||||
|
$data[ $platform ] = $new;
|
||||||
|
}
|
||||||
|
|
||||||
|
update_option( 'woocommerce_mobile_app_usage', $data );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get usage data from current request
|
||||||
|
*
|
||||||
|
* @param WP_REST_Request $request Full details about the request.
|
||||||
|
* @return Array
|
||||||
|
*/
|
||||||
|
public function get_usage_data( $request ) {
|
||||||
|
$platform = strtolower( $request->get_param( 'platform' ) );
|
||||||
|
switch ( $platform ) {
|
||||||
|
case 'ios':
|
||||||
|
case 'android':
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$version = $request->get_param( 'version' );
|
||||||
|
if ( ! $version ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'platform' => sanitize_text_field( $platform ),
|
||||||
|
'version' => sanitize_text_field( $version ),
|
||||||
|
'last_used' => gmdate( 'c' ),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get any query params needed.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get_collection_params() {
|
||||||
|
return array(
|
||||||
|
'platform' => array(
|
||||||
|
'description' => __( 'Platform to track.', 'woocommerce' ),
|
||||||
|
'required' => true,
|
||||||
|
'type' => 'string',
|
||||||
|
'sanitize_callback' => 'sanitize_text_field',
|
||||||
|
'validate_callback' => 'rest_validate_request_arg',
|
||||||
|
),
|
||||||
|
'version' => array(
|
||||||
|
'description' => __( 'Platform version to track.', 'woocommerce' ),
|
||||||
|
'required' => true,
|
||||||
|
'type' => 'string',
|
||||||
|
'sanitize_callback' => 'sanitize_text_field',
|
||||||
|
'validate_callback' => 'rest_validate_request_arg',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -52,9 +52,10 @@ class Server {
|
||||||
return apply_filters(
|
return apply_filters(
|
||||||
'woocommerce_rest_api_get_rest_namespaces',
|
'woocommerce_rest_api_get_rest_namespaces',
|
||||||
array(
|
array(
|
||||||
'wc/v1' => $this->get_v1_controllers(),
|
'wc/v1' => $this->get_v1_controllers(),
|
||||||
'wc/v2' => $this->get_v2_controllers(),
|
'wc/v2' => $this->get_v2_controllers(),
|
||||||
'wc/v3' => $this->get_v3_controllers(),
|
'wc/v3' => $this->get_v3_controllers(),
|
||||||
|
'wc-telemetry' => $this->get_telemetry_controllers(),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -179,6 +180,17 @@ class Server {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of controllers in the telemetry namespace.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function get_telemetry_controllers() {
|
||||||
|
return array(
|
||||||
|
'tracker' => 'WC_REST_Telemetry_Controller',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the path to the package.
|
* Return the path to the package.
|
||||||
*
|
*
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
/* eslint-disable jest/no-export, jest/no-disabled-tests */
|
||||||
|
/**
|
||||||
|
* Internal dependencies
|
||||||
|
*/
|
||||||
|
const { HTTPClientFactory, Coupon } = require( '@woocommerce/api' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* External dependencies
|
||||||
|
*/
|
||||||
|
const config = require( 'config' );
|
||||||
|
const {
|
||||||
|
it,
|
||||||
|
describe,
|
||||||
|
beforeAll,
|
||||||
|
} = require( '@jest/globals' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the default coupon and tests interactions with it via the API.
|
||||||
|
*/
|
||||||
|
const runTelemetryAPITest = () => {
|
||||||
|
describe( 'REST API > Telemetry', () => {
|
||||||
|
let client;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const admin = config.get( 'users.admin' );
|
||||||
|
const url = config.get( 'url' );
|
||||||
|
|
||||||
|
client = HTTPClientFactory.build( url )
|
||||||
|
.withBasicAuth( admin.username, admin.password )
|
||||||
|
.withIndexPermalinks()
|
||||||
|
.create();
|
||||||
|
} );
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
null,
|
||||||
|
{},
|
||||||
|
{ platform: 'ios' },
|
||||||
|
{ version: '1.1' },
|
||||||
|
])( 'errors for invalid request body - %p', async data => {
|
||||||
|
const response = await client
|
||||||
|
.post( `/wc-telemetry/tracker`, data )
|
||||||
|
.catch( err => {
|
||||||
|
expect( err.statusCode ).toBe( 400 );
|
||||||
|
} );
|
||||||
|
|
||||||
|
expect( response ).toBeUndefined();
|
||||||
|
} );
|
||||||
|
|
||||||
|
it( 'returns 200 with correct fields', async () => {
|
||||||
|
const response = await client
|
||||||
|
.post( `/wc-telemetry/tracker`, {
|
||||||
|
platform: 'ios',
|
||||||
|
version: '1.0',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect( response.statusCode ).toBe( 200 );
|
||||||
|
} );
|
||||||
|
} );
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = runTelemetryAPITest;
|
|
@ -53,6 +53,7 @@ const runCouponApiTest = require( './api/coupon.test' );
|
||||||
const runGroupedProductAPITest = require( './api/grouped-product.test' );
|
const runGroupedProductAPITest = require( './api/grouped-product.test' );
|
||||||
const runVariableProductAPITest = require( './api/variable-product.test' );
|
const runVariableProductAPITest = require( './api/variable-product.test' );
|
||||||
const runOrderApiTest = require( './api/order.test' );
|
const runOrderApiTest = require( './api/order.test' );
|
||||||
|
const runTelemetryAPITest = require( './api/telemetry.test' );
|
||||||
|
|
||||||
const runSetupOnboardingTests = () => {
|
const runSetupOnboardingTests = () => {
|
||||||
runActivationTest();
|
runActivationTest();
|
||||||
|
@ -108,6 +109,7 @@ const runApiTests = () => {
|
||||||
runVariableProductAPITest();
|
runVariableProductAPITest();
|
||||||
runCouponApiTest();
|
runCouponApiTest();
|
||||||
runOrderApiTest();
|
runOrderApiTest();
|
||||||
|
runTelemetryAPITest();
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -160,4 +162,5 @@ module.exports = {
|
||||||
runMyAccountCreateAccountTest,
|
runMyAccountCreateAccountTest,
|
||||||
runOrderEmailReceivingTest,
|
runOrderEmailReceivingTest,
|
||||||
runInitiateWccomConnectionTest,
|
runInitiateWccomConnectionTest,
|
||||||
|
runTelemetryAPITest,
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
/*
|
||||||
|
* Internal dependencies
|
||||||
|
*/
|
||||||
|
const { runTelemetryAPITest } = require( '@woocommerce/e2e-core-tests' );
|
||||||
|
|
||||||
|
runTelemetryAPITest();
|
Loading…
Reference in New Issue