Log tracks events after they have been validated (#34030)

* Log events after they have been validated

* Add changelog entry

* Remove old tracks file

* Fix lint errors
This commit is contained in:
Joshua T Flowers 2022-07-27 14:01:18 -04:00 committed by GitHub
parent 30de9ccb19
commit 345b8886e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 130 additions and 77 deletions

View File

@ -1,29 +1,44 @@
<?php
/**
* API initialization for beta testing.
*
* @package WC_Beta_Tester
*/
/**
* Register the test helper route.
*
* @param string $route Route path.
* @param string $callback Callback function name.
* @param array $additional_options Additional options passed to route registration.
*/
function register_woocommerce_admin_test_helper_rest_route( $route, $callback, $additional_options = array() ) {
add_action( 'rest_api_init', function() use ( $route, $callback, $additional_options ) {
add_action(
'rest_api_init',
function() use ( $route, $callback, $additional_options ) {
$default_options = array(
'methods' => 'POST',
'callback' => $callback,
'permission_callback' => function( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new \WP_Error(
'woocommerce_rest_cannot_edit',
__( 'Sorry, you cannot perform this action', 'woocommerce' )
);
}
return true;
},
);
$default_options = array(
'methods' => 'POST',
'callback' => $callback,
'permission_callback' => function( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) {
return new \WP_Error(
'woocommerce_rest_cannot_edit',
__( 'Sorry, you cannot perform this action', 'woocommerce-admin-test-helper' )
);
}
return true;
},
);
$default_options = array_merge( $default_options, $additional_options );
$default_options = array_merge( $default_options, $additional_options );
register_rest_route(
'wc-admin-test-helper',
$route,
$default_options
);
} );
register_rest_route(
'wc-admin-test-helper',
$route,
$default_options
);
}
);
}
require( 'admin-notes/delete-all-notes.php' );
@ -32,10 +47,10 @@ require( 'tools/trigger-wca-install.php' );
require( 'tools/trigger-cron-job.php' );
require( 'tools/run-wc-admin-daily.php' );
require( 'options/rest-api.php' );
require( 'tools/delete-all-products.php');
require( 'tools/delete-all-products.php' );
require( 'tools/disable-wc-email.php' );
require( 'tools/trigger-update-callbacks.php' );
require( 'tracks/tracks-debug-log.php' );
require( 'tracks/class-tracks-debug-log.php' );
require( 'features/features.php' );
require( 'rest-api-filters/rest-api-filters.php' );
require( 'rest-api-filters/hook.php' );

View File

@ -0,0 +1,87 @@
<?php
/**
* A class for logging tracked events.
*
* @package WC_Beta_Tester
*/
/**
* Class Tracks_Debug_Log.
*/
class Tracks_Debug_Log {
/**
* Logger class to use.
*
* @var WC_Logger_Interface|null
*/
private $logger;
/**
* Logger source.
*
* @var string logger source.
*/
private $source = 'tracks';
/**
* Initialize hooks.
*/
public function __construct() {
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-client.php';
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-footer-pixel.php';
$logger = wc_get_logger();
$this->logger = $logger;
add_action( 'admin_footer', array( $this, 'log_footer_pixels' ), 5 );
add_action( 'pre_http_request', array( $this, 'log_remote_pixels' ), 10, 3 );
}
/**
* Log the event.
*
* @param string $event_name Event name.
* @param array $properties Event properties.
*/
public function log_event( $event_name, $properties ) {
$this->logger->debug(
$event_name,
array( 'source' => $this->source )
);
foreach ( $properties as $key => $property ) {
$this->logger->debug(
" - {$key}: {$property}",
array( 'source' => $this->source )
);
}
}
/**
* Log events passed as footer pixels.
*/
public function log_footer_pixels() {
$events = WC_Tracks_Footer_Pixel::get_events();
foreach ( $events as $event ) {
$this->log_event( $event->_en, $event );
}
}
/**
* Log events that are retrieved by remote request.
*
* @param false|array|WP_Error $preempt A preemptive return value of an HTTP request. Default false.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
*/
public function log_remote_pixels( $preempt, $parsed_args, $url ) {
if ( strpos( $url, WC_Tracks_Client::PIXEL ) === 0 ) {
$parsed_url = wp_parse_url( $url );
parse_str( $parsed_url['query'], $params );
$this->log_event( $params['_en'], $params );
}
return $preempt;
}
}
new Tracks_Debug_Log();

View File

@ -1,53 +0,0 @@
<?php
/**
* A class for logging tracked events.
*/
class TracksDebugLog {
/**
* Logger class to use.
*
* @var WC_Logger_Interface|null
*/
private $logger;
/**
* Logger source.
*
* @var string logger source.
*/
private $source = 'tracks';
/**
* Initialize hooks.
*/
public function __construct() {
add_filter( 'woocommerce_tracks_event_properties', array( $this, 'log_event' ), 10, 2 );
$logger = wc_get_logger();
$this->logger = $logger;
$this->logger = $logger;
}
/**
* Log the event.
*
* @param array $properties Event properties.
* @param string $event_name Event name.
*/
public function log_event( $properties, $event_name ) {
$this->logger->debug(
$event_name,
array( 'source' => $this->source )
);
foreach ( $properties as $key => $property ) {
$this->logger->debug(
" - {$key}: {$property}",
array( 'source' => $this->source )
);
}
return $properties;
}
}
new TracksDebugLog();

View File

@ -0,0 +1,4 @@
Significance: minor
Type: dev
Update tracks debugging to only log after events are validated