Add logger for tracks events

This commit is contained in:
Joshua Flowers 2022-01-27 11:34:34 -05:00
parent 981f477d7f
commit c25e092c6d
2 changed files with 54 additions and 0 deletions

View File

@ -35,3 +35,4 @@ require( 'options/rest-api.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' );

View File

@ -0,0 +1,53 @@
<?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();