| 1 |
<?php |
| 2 |
/** |
| 3 |
* Main orchestrator for the feature usage tracking module. |
| 4 |
* |
| 5 |
* @package WpVr\Tracking |
| 6 |
* @since 8.5.37 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Wpvr\Admin\Tracking; |
| 10 |
|
| 11 |
// Exit if accessed directly. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
use Wpvr\Admin\Tracking\Events\AhaMomentEvents; |
| 17 |
use Wpvr\Admin\Tracking\Events\HabitMomentEvents; |
| 18 |
use Wpvr\Admin\Tracking\Events\SetupEvents; |
| 19 |
use Wpvr\Admin\Tracking\Events\SignupEvents; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class Tracker |
| 23 |
* |
| 24 |
* Initializes and manages the tracking system. It checks if tracking is |
| 25 |
* enabled and, if so, instantiates and initializes event handlers. |
| 26 |
* |
| 27 |
* @package WpVr\Tracking |
| 28 |
* @since 8.5.37 |
| 29 |
*/ |
| 30 |
class Tracker { |
| 31 |
|
| 32 |
/** |
| 33 |
* Instance of the PosthogClient. |
| 34 |
* |
| 35 |
* @var PosthogClient |
| 36 |
* @since 8.5.37 |
| 37 |
*/ |
| 38 |
private $client; |
| 39 |
|
| 40 |
/** |
| 41 |
* Array of event handlers. |
| 42 |
* |
| 43 |
* @var array |
| 44 |
* @since 8.5.37 |
| 45 |
*/ |
| 46 |
private $event_handlers = array(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Whether tracking is enabled. |
| 50 |
* |
| 51 |
* @var bool |
| 52 |
* @since 8.5.37 |
| 53 |
*/ |
| 54 |
private $is_tracking_enabled = false; |
| 55 |
|
| 56 |
/** |
| 57 |
* Constructor. |
| 58 |
* |
| 59 |
* @since 8.5.37 |
| 60 |
*/ |
| 61 |
public function __construct() { |
| 62 |
$this->check_if_tracking_enabled(); |
| 63 |
if ( $this->is_tracking_enabled ) { |
| 64 |
$this->init_client(); |
| 65 |
$this->init_event_handlers(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Check if tracking is enabled. |
| 71 |
* |
| 72 |
* @since 8.5.37 |
| 73 |
*/ |
| 74 |
private function check_if_tracking_enabled() { |
| 75 |
$this->is_tracking_enabled = apply_filters( 'wpvr_tracking_enabled', 'yes' ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Initialize the PostHog client. |
| 80 |
* |
| 81 |
* @since 8.5.37 |
| 82 |
*/ |
| 83 |
private function init_client() { |
| 84 |
$this->client = new PosthogClient(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Initialize backend tracking. |
| 89 |
* |
| 90 |
* @since 8.5.37 |
| 91 |
*/ |
| 92 |
private function init_backend_tracking() { |
| 93 |
if ( $this->client ) { |
| 94 |
$this->client->init_backend_tracking(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Initialize event handlers. |
| 100 |
* |
| 101 |
* @since 8.5.37 |
| 102 |
*/ |
| 103 |
private function init_event_handlers() { |
| 104 |
if ( ! $this->client ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// Initialize event handlers. |
| 109 |
$this->event_handlers = array( |
| 110 |
'signup' => new SignupEvents( $this->client ), |
| 111 |
'setup' => new SetupEvents( $this->client ), |
| 112 |
'aha' => new AhaMomentEvents( $this->client ), |
| 113 |
'habit' => new HabitMomentEvents( $this->client ), |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get the PostHog client. |
| 119 |
* |
| 120 |
* @return PosthogClient|null The PostHog client or null if tracking is disabled. |
| 121 |
* @since 8.5.37 |
| 122 |
*/ |
| 123 |
public function get_client() { |
| 124 |
return $this->client; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get an event handler by type. |
| 129 |
* |
| 130 |
* @param string $type The event handler type. |
| 131 |
* |
| 132 |
* @return object|null The event handler or null if it doesn't exist. |
| 133 |
* @since 8.5.37 |
| 134 |
*/ |
| 135 |
public function get_event_handler( $type ) { |
| 136 |
return isset( $this->event_handlers[ $type ] ) ? $this->event_handlers[ $type ] : null; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Check if tracking is enabled. |
| 141 |
* |
| 142 |
* @return bool Whether tracking is enabled. |
| 143 |
* @since 8.5.37 |
| 144 |
*/ |
| 145 |
public function is_enabled() { |
| 146 |
return $this->is_tracking_enabled; |
| 147 |
} |
| 148 |
} |
| 149 |
|