| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This class handles event tracking functionality using OpenPanel analytics. |
| 5 |
* This class implements a singleton pattern to ensure only one instance |
| 6 |
* of the tracker exists throughout the application lifecycle. |
| 7 |
* |
| 8 |
* @author WPFunnels Team |
| 9 |
* @email [email protected] |
| 10 |
* @create date 2025-05-12 09:30:00 |
| 11 |
* @modify date 2024-05-12 11:03:17 |
| 12 |
* @package Mint\App\Internal\Tracking |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace WPFunnels\Tracking; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class EventTracker |
| 19 |
* |
| 20 |
* This class implements a singleton pattern to ensure only one instance |
| 21 |
* of the tracker exists throughout the application lifecycle. |
| 22 |
* |
| 23 |
* @package Mint\MRM\Internal\Tracking |
| 24 |
* @since 1.17.10 |
| 25 |
*/ |
| 26 |
class EventTracker{ |
| 27 |
|
| 28 |
/** |
| 29 |
* Holds the singleton instance of this class. |
| 30 |
* |
| 31 |
* @var EventTracker|null $instance The singleton instance of this class. |
| 32 |
* @since 1.17.10 |
| 33 |
*/ |
| 34 |
private static $instance; |
| 35 |
|
| 36 |
/** |
| 37 |
* Private constructor to prevent instantiation from outside the class. |
| 38 |
* |
| 39 |
* @since 1.17.10 |
| 40 |
*/ |
| 41 |
private function __construct(){ |
| 42 |
|
| 43 |
add_action( 'admin_init', array( $this, 'maybe_track_activation' ) ); |
| 44 |
add_action( 'wpfunnels_after_funnel_created', array( $this, 'on_after_funnel_created' ), 10, 2 ); |
| 45 |
add_action( 'wpfunnels_after_funnel_imported', array( $this, 'on_after_funnel_imported' ), 10, 3 ); |
| 46 |
add_action( 'wpfunnels_plugin_deactivated', array( $this, 'on_plugin_deactivated' ) ); |
| 47 |
add_action( 'wpfunnels_after_funnel_creation', array( $this, 'on_after_funnel_creation' ) ); |
| 48 |
add_action( 'wpfunnels/funnel_order_placed', array( $this, 'funnel_order_placed' ), 10, 3 ); |
| 49 |
add_action( 'wpfunnels_setup_wizard_complete', array( $this, 'on_setup_wizard_complete' ), 10, 3 ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Returns the singleton instance of this class. |
| 54 |
* |
| 55 |
* If the instance does not exist, it creates a new instance. |
| 56 |
* |
| 57 |
* @access public |
| 58 |
* |
| 59 |
* @return EventTracker The singleton instance of this class. |
| 60 |
* |
| 61 |
* @since 1.17.10 |
| 62 |
*/ |
| 63 |
public static function init(): void |
| 64 |
{ |
| 65 |
if (!self::$instance) { |
| 66 |
self::$instance = new self(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Checks if the plugin was just activated and sends the tracking event. |
| 72 |
* This runs on admin_init to ensure the full environment is available. |
| 73 |
* Fires regardless of the consent checkbox value. |
| 74 |
* |
| 75 |
* @access public |
| 76 |
* @return void |
| 77 |
* @since 1.17.10 |
| 78 |
*/ |
| 79 |
public function maybe_track_activation(): void |
| 80 |
{ |
| 81 |
if ( get_transient( 'wpfunnels_just_activated' ) ) { |
| 82 |
delete_transient( 'wpfunnels_just_activated' ); |
| 83 |
$this->on_plugin_activated(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function on_plugin_activated(): void |
| 88 |
{ |
| 89 |
coderex_telemetry_track( |
| 90 |
WPFNL_FILE, |
| 91 |
'signup', |
| 92 |
array( |
| 93 |
'site_url' => get_site_url(), |
| 94 |
'plugin_version' => WPFNL_VERSION, |
| 95 |
) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Sends a plugin deactivation event to OpenPanel. |
| 101 |
* |
| 102 |
* This method sends a plugin deactivation event to OpenPanel, including |
| 103 |
* the site URL and plugin version. |
| 104 |
* |
| 105 |
* @access public |
| 106 |
* |
| 107 |
* @return void |
| 108 |
* |
| 109 |
* @since 1.17.10 |
| 110 |
*/ |
| 111 |
public function on_plugin_deactivated(): void |
| 112 |
{ |
| 113 |
coderex_telemetry_track( |
| 114 |
WPFNL_FILE, |
| 115 |
'plugin_deactivation', |
| 116 |
array( |
| 117 |
'site_url' => get_site_url(), |
| 118 |
'plugin_version' => WPFNL_VERSION, |
| 119 |
) |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Sends an event to OpenPanel when a funnel is created. |
| 125 |
* |
| 126 |
* This method sends an event to OpenPanel when a funnel is created, |
| 127 |
* including the funnel ID and funnel type. |
| 128 |
* |
| 129 |
* @access public |
| 130 |
* |
| 131 |
* @param int $funnel_id The ID of the created funnel. |
| 132 |
* @param string $funnel_type The type of the created funnel. |
| 133 |
* |
| 134 |
* @return void |
| 135 |
* |
| 136 |
* @since 1.17.10 |
| 137 |
*/ |
| 138 |
public function on_after_funnel_created($funnel_id, $funnel_type): void |
| 139 |
{ |
| 140 |
$has_funnel_created = get_option('wpfunnels_funnel_created', false); |
| 141 |
if (!$has_funnel_created) { |
| 142 |
coderex_telemetry_track( |
| 143 |
WPFNL_FILE, |
| 144 |
'first_strike', |
| 145 |
array( |
| 146 |
'funnel_id' => $funnel_id, |
| 147 |
'funnel_type' => $funnel_type, |
| 148 |
'time' => current_time('mysql'), |
| 149 |
) |
| 150 |
); |
| 151 |
update_option('wpfunnels_funnel_created', true); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Sends an event to OpenPanel when a funnel is imported. |
| 157 |
* |
| 158 |
* This method sends an event to OpenPanel when a funnel is imported, |
| 159 |
* including the funnel type and builder name. |
| 160 |
* |
| 161 |
* @access public |
| 162 |
* |
| 163 |
* @param int $funnel_id The ID of the imported funnel. |
| 164 |
* @param string $funnel_type The type of the imported funnel. |
| 165 |
* @param string $builder The builder used for the imported funnel. |
| 166 |
* @return void |
| 167 |
* |
| 168 |
* @since 1.17.10 |
| 169 |
*/ |
| 170 |
public function on_after_funnel_imported($funnel_id, $funnel_type, $builder): void |
| 171 |
{ |
| 172 |
$has_funnel_imported = get_option('wpfunnels_funnel_imported', false); |
| 173 |
if (!$has_funnel_imported) { |
| 174 |
coderex_telemetry_track( |
| 175 |
WPFNL_FILE, |
| 176 |
'first_strike', |
| 177 |
array( |
| 178 |
'funnel_id' => $funnel_id, |
| 179 |
'builder' => $builder, |
| 180 |
'funnel_type' => $funnel_type, |
| 181 |
'time' => current_time('mysql'), |
| 182 |
) |
| 183 |
); |
| 184 |
update_option('wpfunnels_funnel_imported', true); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Tracks funnel creation event. |
| 190 |
* |
| 191 |
* This method sends an event to OpenPanel when a funnel is created, |
| 192 |
* including the funnel ID. |
| 193 |
* |
| 194 |
* @access public |
| 195 |
* |
| 196 |
* @param int $funnel_id The ID of the created funnel. |
| 197 |
* |
| 198 |
* @return void |
| 199 |
* |
| 200 |
* @since 1.17.10 |
| 201 |
*/ |
| 202 |
public function on_after_funnel_creation($funnel_id): void |
| 203 |
{ |
| 204 |
$funnel_id = absint( $funnel_id ); |
| 205 |
if ( ! $funnel_id ) { |
| 206 |
return; |
| 207 |
} |
| 208 |
$post = get_post( $funnel_id ); |
| 209 |
if ( empty( $post ) ) { |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
// Check if this is the first funnel created by the user |
| 214 |
$funnel_count = wp_count_posts( 'wpfunnels' ); |
| 215 |
$total_funnels = $funnel_count->publish + $funnel_count->draft + $funnel_count->pending; |
| 216 |
|
| 217 |
// Only send webhook if this is the first funnel (total count is 1) |
| 218 |
if ( $total_funnels === 1 ) { |
| 219 |
$current_user = wp_get_current_user(); |
| 220 |
$email = $current_user->user_email; |
| 221 |
$name = $current_user->display_name; |
| 222 |
|
| 223 |
$funnel_data = array( |
| 224 |
'funnel_id' => $funnel_id, |
| 225 |
'funnel_title' => $post->post_title, |
| 226 |
'funnel_status' => $post->post_status, |
| 227 |
'created_date' => $post->post_date, |
| 228 |
); |
| 229 |
|
| 230 |
$createContactInstance = new \WPFunnels\Admin\SetupWizard\CreateContact($email, $name); |
| 231 |
$createContactInstance->update_contact_via_webhook($funnel_data); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Tracks funnel order placed event. |
| 237 |
* |
| 238 |
* This method sends an event to OpenPanel when a funnel order is placed, |
| 239 |
* including the order ID, amount, and currency. |
| 240 |
* |
| 241 |
* @access public |
| 242 |
* |
| 243 |
* @param int $order_id The ID of the placed order. |
| 244 |
* @param float $funnel_id The amount of the placed order. |
| 245 |
* @param string $checkout_id The currency of the placed order. |
| 246 |
* |
| 247 |
* @return void |
| 248 |
* |
| 249 |
* @since 1.17.10 |
| 250 |
*/ |
| 251 |
public function funnel_order_placed($order_id, $funnel_id, $checkout_id){ |
| 252 |
$email = get_option('wpfunnels_activation_email'); |
| 253 |
if ( empty($email) ) { |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
global $wpdb; |
| 258 |
$table = $wpdb->prefix . 'wpfnl_stats'; |
| 259 |
|
| 260 |
$stats = $wpdb->get_row(" |
| 261 |
SELECT |
| 262 |
COUNT(id) as total_orders, |
| 263 |
SUM(total_sales) as total_revenue, |
| 264 |
MAX(date_created) as last_order_date |
| 265 |
FROM $table |
| 266 |
WHERE status IN ('processing','completed') |
| 267 |
"); |
| 268 |
|
| 269 |
$payload = array( |
| 270 |
'total_orders' => (int) $stats->total_orders, |
| 271 |
'total_revenue' => (float) $stats->total_revenue, |
| 272 |
'last_order_date' => $stats->last_order_date, |
| 273 |
); |
| 274 |
|
| 275 |
coderex_telemetry_track( |
| 276 |
WPFNL_FILE, |
| 277 |
'user_received_funnel_order', |
| 278 |
array( |
| 279 |
'total_orders' => (int) $stats->total_orders, |
| 280 |
'total_revenue' => (float) $stats->total_revenue, |
| 281 |
'last_order_date' => $stats->last_order_date, |
| 282 |
'time' => current_time('mysql'), |
| 283 |
) |
| 284 |
); |
| 285 |
|
| 286 |
$createContactInstance = new \WPFunnels\Admin\SetupWizard\CreateContact($email, ''); |
| 287 |
$createContactInstance->update_contact_order_via_webhook($payload); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Sends an event to OpenPanel when the setup wizard is completed. |
| 292 |
* |
| 293 |
* This method sends an event to OpenPanel when the setup wizard is completed, |
| 294 |
* including the funnel ID and action. |
| 295 |
* |
| 296 |
* @access public |
| 297 |
* |
| 298 |
* @param int $funnel_id The ID of the funnel associated with the setup wizard. |
| 299 |
* @param string $action The action taken during the setup wizard. |
| 300 |
* @param string $goal The goal achieved during the setup wizard. |
| 301 |
* |
| 302 |
* @return void |
| 303 |
* |
| 304 |
* @since 1.17.10 |
| 305 |
*/ |
| 306 |
public function on_setup_wizard_complete($funnel_id, $action, $goal): void |
| 307 |
{ |
| 308 |
coderex_telemetry_track( |
| 309 |
WPFNL_FILE, |
| 310 |
'setup', |
| 311 |
array( |
| 312 |
'funnel_id' => $funnel_id, |
| 313 |
'action' => $action, |
| 314 |
'goal' => $goal, |
| 315 |
'time' => current_time('mysql'), |
| 316 |
) |
| 317 |
); |
| 318 |
} |
| 319 |
} |
| 320 |
|