init
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Tracker
|
||||
*
|
||||
* @package WooCommerce\Payments
|
||||
*/
|
||||
|
||||
namespace WCPay;
|
||||
|
||||
defined( 'ABSPATH' ) || exit; // block direct access.
|
||||
|
||||
/**
|
||||
* An API for adding track events that will get unloaded
|
||||
* at a later stage and pushed to WP.com.
|
||||
*/
|
||||
class Tracker {
|
||||
/**
|
||||
* A key value array event_name => properties.
|
||||
*
|
||||
* @var array $admin_events
|
||||
*/
|
||||
protected static $admin_events = [];
|
||||
|
||||
/**
|
||||
* Record an event in Tracks
|
||||
*
|
||||
* This only sends track events for admin logged in users. This is a limitation of the
|
||||
* WC_Tracks and related classes.
|
||||
*
|
||||
* The event name will be prefixed before sending it see Core_Tracks_Wrapper.
|
||||
*
|
||||
* @param string $event_name The name of the event.
|
||||
* @param array $properties Custom properties to send with the event.
|
||||
*/
|
||||
public static function track_admin( $event_name, $properties = [] ) {
|
||||
self::$admin_events[ $event_name ] = $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a track event.
|
||||
*
|
||||
* @param string $event_name The name of the event that should be removed.
|
||||
*/
|
||||
public static function remove_admin_event( $event_name ) {
|
||||
if ( isset( self::$admin_events ) ) {
|
||||
unset( self::$admin_events[ $event_name ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a track event.
|
||||
*/
|
||||
public static function get_admin_events() {
|
||||
return self::$admin_events;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Core Tracks Wrapper
|
||||
*
|
||||
* @package WooCommerce\Payments
|
||||
*/
|
||||
|
||||
namespace WCPay;
|
||||
|
||||
use WC_Tracks;
|
||||
|
||||
defined( 'ABSPATH' ) || exit; // block direct access.
|
||||
|
||||
/**
|
||||
* Moves all events from Tracker to Tracks loader
|
||||
*/
|
||||
function record_tracker_events() {
|
||||
foreach ( Tracker::get_admin_events() as $event => $properties ) {
|
||||
WC_Tracks::record_event( $event, $properties );
|
||||
Tracker::remove_admin_event( $event );
|
||||
}
|
||||
}
|
||||
|
||||
// Loaded on admin_init to ensure that we are in admin and that WC_Tracks is loaded.
|
||||
add_action(
|
||||
'admin_init',
|
||||
function () {
|
||||
if ( ! class_exists( 'WC_Tracks' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Move all events with priority 1 just before the admin_footer hook adds footer pixels.
|
||||
add_action( 'admin_footer', 'WCPay\record_tracker_events', 1 );
|
||||
|
||||
/**
|
||||
* Send all events that were not handled in `admin_footer`.
|
||||
*
|
||||
* Between shutdown and admin footer many things can happen. Admin footer loads
|
||||
* scripts in the markup images that will call tracks from the browsers
|
||||
* side (which means it's faster as we're not doing network calls to wp.com server
|
||||
* side). Anything that is added afterward must be sent from the
|
||||
* server, but doing it on shutdown means it's not blocking anything.
|
||||
*/
|
||||
add_action( 'shutdown', __NAMESPACE__ . '\\record_tracker_events', 1 );
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user