| 1 |
<?php |
| 2 |
/** |
| 3 |
* Analytics class helps to connect BSF Analytics. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SureDonation\Inc\Admin; |
| 9 |
|
| 10 |
use SureDonation\Inc\Helper; |
| 11 |
use SureDonation\Inc\Payments\Offline\Offline_Helper; |
| 12 |
use SureDonation\Inc\Payments\Payment_Helper; |
| 13 |
use SureDonation\Inc\Payments\PayPal\PayPal_Helper; |
| 14 |
use SureDonation\Inc\Payments\Stripe\Stripe_Helper; |
| 15 |
use SureDonation\Inc\Traits\Get_Instance; |
| 16 |
|
| 17 |
// Exit if accessed directly. |
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Analytics class. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
*/ |
| 27 |
class Analytics { |
| 28 |
use Get_Instance; |
| 29 |
|
| 30 |
/** |
| 31 |
* BSF_Analytics_Events instance for one-time event tracking. |
| 32 |
* |
| 33 |
* @var \BSF_Analytics_Events|null |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
private static $events = null; |
| 37 |
|
| 38 |
/** |
| 39 |
* Request-cached donation aggregates. |
| 40 |
* |
| 41 |
* @var array<string, int>|null |
| 42 |
* @since 1.0.0 |
| 43 |
*/ |
| 44 |
private static $donation_aggregates = null; |
| 45 |
|
| 46 |
/** |
| 47 |
* Class constructor. |
| 48 |
* |
| 49 |
* @return void |
| 50 |
* @since 1.0.0 |
| 51 |
*/ |
| 52 |
public function __construct() { |
| 53 |
/* |
| 54 |
* Entity registration is deferred to init priority 0 so that add-on |
| 55 |
* plugins (e.g. SureDonation Pro) registering filters such as |
| 56 |
* suredonation_deactivation_survey_data on plugins_loaded are in |
| 57 |
* place before the deactivation survey data is filtered, and the |
| 58 |
* entity is still set before the BSF Analytics loader consumes it on |
| 59 |
* init priority 10. |
| 60 |
*/ |
| 61 |
add_action( 'init', [ $this, 'register_entity' ], 0 ); |
| 62 |
|
| 63 |
add_filter( 'bsf_core_stats', [ $this, 'add_suredonation_analytics_data' ] ); |
| 64 |
|
| 65 |
// Keep analytics sends (and their stat queries) off the frontend. |
| 66 |
add_filter( 'suredonation_tracking_enabled', [ $this, 'restrict_tracking_to_admin' ] ); |
| 67 |
|
| 68 |
// Event tracking hooks. Registered outside is_admin() on purpose — |
| 69 |
// onboarding completion and campaign publishes fire during REST requests. |
| 70 |
add_action( 'suredonation_onboarding_user_details_saved', [ $this, 'track_onboarding_completed' ] ); |
| 71 |
add_action( 'transition_post_status', [ $this, 'track_first_campaign_published' ], 10, 3 ); |
| 72 |
add_action( 'current_screen', [ $this, 'track_first_campaign_editor_opened' ] ); |
| 73 |
|
| 74 |
// Detect state-based events (daily throttle; dedup prevents repeat |
| 75 |
// tracking). Admin-only so the detection never runs on the frontend. |
| 76 |
if ( is_admin() ) { |
| 77 |
$this->detect_state_events(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Register the SureDonation entity with the BSF Analytics loader. |
| 83 |
* |
| 84 |
* Runs on init priority 0 — after add-on plugins have registered their |
| 85 |
* filters on plugins_loaded, and before the loader's own init callback |
| 86 |
* loads the analytics library. |
| 87 |
* |
| 88 |
* @return void |
| 89 |
* @since 1.0.0 |
| 90 |
*/ |
| 91 |
public function register_entity() { |
| 92 |
if ( ! class_exists( 'BSF_Analytics_Loader' ) ) { |
| 93 |
require_once SUREDONATION_DIR . 'inc/lib/bsf-analytics/class-bsf-analytics-loader.php'; |
| 94 |
} |
| 95 |
|
| 96 |
if ( ! class_exists( 'BSF_Admin_Notices' ) ) { |
| 97 |
require_once SUREDONATION_DIR . 'inc/lib/astra-notices/class-bsf-admin-notices.php'; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* The loader's get_instance() carries no return type. |
| 102 |
* |
| 103 |
* @var \BSF_Analytics_Loader $suredonation_bsf_analytics |
| 104 |
*/ |
| 105 |
$suredonation_bsf_analytics = \BSF_Analytics_Loader::get_instance(); |
| 106 |
|
| 107 |
$suredonation_bsf_analytics->set_entity( |
| 108 |
[ |
| 109 |
'suredonation' => [ |
| 110 |
'product_name' => 'SureDonation', |
| 111 |
'path' => SUREDONATION_DIR . 'inc/lib/bsf-analytics', |
| 112 |
'author' => 'SureDonation', |
| 113 |
'time_to_display' => '+24 hours', |
| 114 |
'deactivation_survey' => apply_filters( |
| 115 |
'suredonation_deactivation_survey_data', |
| 116 |
[ |
| 117 |
[ |
| 118 |
'id' => 'deactivation-survey-suredonation', |
| 119 |
'popup_logo' => SUREDONATION_URL . 'images/suredonation-icon.svg', |
| 120 |
'plugin_slug' => 'suredonation', |
| 121 |
'popup_title' => __( 'Quick Feedback', 'suredonation' ), |
| 122 |
'support_url' => 'https://suredonation.com/support/', |
| 123 |
'popup_description' => __( 'If you have a moment, please share why you are deactivating SureDonation:', 'suredonation' ), |
| 124 |
'show_on_screens' => [ 'plugins' ], |
| 125 |
'plugin_version' => SUREDONATION_VER, |
| 126 |
], |
| 127 |
] |
| 128 |
), |
| 129 |
'hide_optin_checkbox' => true, |
| 130 |
], |
| 131 |
] |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get the shared BSF_Analytics_Events instance. |
| 137 |
* |
| 138 |
* Uses SureDonation's Helper option methods so the event data stays |
| 139 |
* inside the consolidated suredonation_options row. |
| 140 |
* |
| 141 |
* @return \BSF_Analytics_Events|null Events instance, or null when the library is unavailable. |
| 142 |
* @since 1.0.0 |
| 143 |
*/ |
| 144 |
public static function events() { |
| 145 |
if ( null === self::$events ) { |
| 146 |
if ( ! class_exists( 'BSF_Analytics_Events' ) ) { |
| 147 |
$events_file = SUREDONATION_DIR . 'inc/lib/bsf-analytics/class-bsf-analytics-events.php'; |
| 148 |
if ( file_exists( $events_file ) ) { |
| 149 |
require_once $events_file; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if ( ! class_exists( 'BSF_Analytics_Events' ) ) { |
| 154 |
return null; |
| 155 |
} |
| 156 |
|
| 157 |
self::$events = new \BSF_Analytics_Events( |
| 158 |
'suredonation', |
| 159 |
[ |
| 160 |
'get' => [ Helper::class, 'get_suredonation_option' ], |
| 161 |
'update' => [ Helper::class, 'update_suredonation_option' ], |
| 162 |
] |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
return self::$events; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Callback function to add SureDonation specific analytics data. |
| 171 |
* |
| 172 |
* @param array<string, mixed> $stats_data Existing stats data. |
| 173 |
* @return array<string, mixed> |
| 174 |
* @since 1.0.0 |
| 175 |
*/ |
| 176 |
public function add_suredonation_analytics_data( $stats_data ) { |
| 177 |
$aggregates = $this->get_donation_aggregates(); |
| 178 |
$campaign_counts = wp_count_posts( 'suredonation_cmpgn' ); |
| 179 |
$form_counts = wp_count_posts( 'suredonation_form' ); |
| 180 |
|
| 181 |
$bsf_internal_referrer = get_option( 'bsf_product_referers', [] ); |
| 182 |
$internal_referer = is_array( $bsf_internal_referrer ) && ! empty( $bsf_internal_referrer['suredonation'] ) |
| 183 |
? sanitize_text_field( (string) $bsf_internal_referrer['suredonation'] ) |
| 184 |
: 'self'; |
| 185 |
|
| 186 |
$plugin_data = [ |
| 187 |
'free_version' => SUREDONATION_VER, |
| 188 |
'numeric_values' => [ |
| 189 |
'total_campaigns' => absint( $campaign_counts->publish ?? 0 ), |
| 190 |
'total_donation_forms' => absint( $form_counts->publish ?? 0 ), |
| 191 |
'total_donations' => $aggregates['total'], |
| 192 |
'completed_donations' => $aggregates['completed'], |
| 193 |
'recurring_donations' => $aggregates['recurring'], |
| 194 |
'total_donors' => $this->get_total_donors(), |
| 195 |
], |
| 196 |
'boolean_values' => [ |
| 197 |
'stripe_enabled' => Stripe_Helper::is_stripe_connected(), |
| 198 |
'paypal_enabled' => PayPal_Helper::is_paypal_connected(), |
| 199 |
'offline_enabled' => Offline_Helper::is_offline_enabled(), |
| 200 |
], |
| 201 |
'internal_referer' => $internal_referer, |
| 202 |
]; |
| 203 |
|
| 204 |
// Add KPI tracking data. |
| 205 |
$kpi_data = $this->get_kpi_tracking_data(); |
| 206 |
if ( ! empty( $kpi_data ) ) { |
| 207 |
$plugin_data['kpi_records'] = $kpi_data; |
| 208 |
} |
| 209 |
|
| 210 |
// Flush pending events into payload (only if any exist). |
| 211 |
$events = self::events(); |
| 212 |
if ( null !== $events ) { |
| 213 |
$pending_events = $events->flush_pending(); |
| 214 |
if ( ! empty( $pending_events ) ) { |
| 215 |
$plugin_data['events_record'] = $pending_events; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if ( ! isset( $stats_data['plugin_data'] ) || ! is_array( $stats_data['plugin_data'] ) ) { |
| 220 |
$stats_data['plugin_data'] = []; |
| 221 |
} |
| 222 |
|
| 223 |
$stats_data['plugin_data']['suredonation'] = $plugin_data; |
| 224 |
|
| 225 |
return $stats_data; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Keep analytics sends off the frontend. |
| 230 |
* |
| 231 |
* Filter callback for `suredonation_tracking_enabled`. The library |
| 232 |
* evaluates this on every request via `is_tracking_enabled()`; gating on |
| 233 |
* is_admin() means the stats queries never run on frontend page loads. |
| 234 |
* Deliberately NOT narrowed further (e.g. to plugin screens): the library |
| 235 |
* also consults this filter from `register_usage_tracking_setting()` on |
| 236 |
* admin_init, where returning false aborts settings registration for all |
| 237 |
* registered BSF products. |
| 238 |
* |
| 239 |
* @param bool $is_enabled Whether tracking is enabled (opt-in state). |
| 240 |
* @return bool |
| 241 |
* @since 1.0.0 |
| 242 |
*/ |
| 243 |
public function restrict_tracking_to_admin( $is_enabled ) { |
| 244 |
return $is_enabled && is_admin(); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Track onboarding completion when lead-capture details are saved. |
| 249 |
* |
| 250 |
* The payload contains PII (name/email) — only the opt-in flag is |
| 251 |
* forwarded to analytics. |
| 252 |
* |
| 253 |
* @param array<string, mixed> $payload Sanitized onboarding payload. |
| 254 |
* @return void |
| 255 |
* @since 1.0.0 |
| 256 |
*/ |
| 257 |
public function track_onboarding_completed( $payload ) { |
| 258 |
$events = self::events(); |
| 259 |
if ( null === $events ) { |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
$payload = is_array( $payload ) ? $payload : []; |
| 264 |
|
| 265 |
$events->track( |
| 266 |
'onboarding_completed', |
| 267 |
'', |
| 268 |
[ |
| 269 |
'opted_in' => ! empty( $payload['opted_in'] ) ? 'yes' : 'no', |
| 270 |
] |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Track first time a campaign is published (activation event). |
| 276 |
* |
| 277 |
* @param string $new_status New post status. |
| 278 |
* @param string $old_status Old post status. |
| 279 |
* @param \WP_Post $post Post object. |
| 280 |
* @return void |
| 281 |
* @since 1.0.0 |
| 282 |
*/ |
| 283 |
public function track_first_campaign_published( $new_status, $old_status, $post ) { |
| 284 |
if ( 'publish' !== $new_status || 'publish' === $old_status || ! $post instanceof \WP_Post || 'suredonation_cmpgn' !== $post->post_type ) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
$events = self::events(); |
| 289 |
if ( null === $events ) { |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
$meta = Helper::get_campaign_meta( $post->ID ); |
| 294 |
$goal_amount = isset( $meta['goal_amount'] ) && is_numeric( $meta['goal_amount'] ) ? (float) $meta['goal_amount'] : 0.0; |
| 295 |
$goal_type = isset( $meta['goal_type'] ) && is_scalar( $meta['goal_type'] ) ? sanitize_text_field( (string) $meta['goal_type'] ) : ''; |
| 296 |
|
| 297 |
$events->track( |
| 298 |
'first_campaign_published', |
| 299 |
(string) $post->ID, |
| 300 |
[ |
| 301 |
'goal_type' => $goal_type, |
| 302 |
'has_goal' => $goal_amount > 0 ? '1' : '0', |
| 303 |
] |
| 304 |
); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Track first time a user opens the campaign editor. |
| 309 |
* |
| 310 |
* @param \WP_Screen $screen Current screen object. |
| 311 |
* @return void |
| 312 |
* @since 1.0.0 |
| 313 |
*/ |
| 314 |
public function track_first_campaign_editor_opened( $screen ) { |
| 315 |
if ( ! $screen instanceof \WP_Screen || 'suredonation_cmpgn' !== $screen->post_type || 'post' !== $screen->base ) { |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
$events = self::events(); |
| 320 |
if ( null === $events ) { |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
$events->track( 'first_campaign_editor_opened' ); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Get donation aggregates in a single request-cached query. |
| 329 |
* |
| 330 |
* Feeds both the stats numeric values and the state-event detection so |
| 331 |
* the donations table is only ever hit once per request. |
| 332 |
* |
| 333 |
* @return array<string, int> Aggregate counts. |
| 334 |
* @since 1.0.0 |
| 335 |
*/ |
| 336 |
private function get_donation_aggregates() { |
| 337 |
if ( null !== self::$donation_aggregates ) { |
| 338 |
return self::$donation_aggregates; |
| 339 |
} |
| 340 |
|
| 341 |
global $wpdb; |
| 342 |
|
| 343 |
$defaults = [ |
| 344 |
'total' => 0, |
| 345 |
'completed' => 0, |
| 346 |
'recurring' => 0, |
| 347 |
'anonymous_completed' => 0, |
| 348 |
'fees_covered_completed' => 0, |
| 349 |
'refunded' => 0, |
| 350 |
]; |
| 351 |
|
| 352 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Single aggregate query on a custom table, request-cached in a static. |
| 353 |
$row = $wpdb->get_row( |
| 354 |
$wpdb->prepare( |
| 355 |
"SELECT COUNT(*) AS total, |
| 356 |
COALESCE(SUM(payment_status = 'completed'),0) AS completed, |
| 357 |
COALESCE(SUM(subscription_id IS NOT NULL AND subscription_id <> ''),0) AS recurring, |
| 358 |
COALESCE(SUM(is_anonymous = 1 AND payment_status = 'completed'),0) AS anonymous_completed, |
| 359 |
COALESCE(SUM(fees_covered > 0 AND payment_status = 'completed'),0) AS fees_covered_completed, |
| 360 |
COALESCE(SUM(payment_status IN ('refunded','partially_refunded')),0) AS refunded |
| 361 |
FROM %i", |
| 362 |
$wpdb->prefix . 'suredonation_donations' |
| 363 |
), |
| 364 |
ARRAY_A |
| 365 |
); |
| 366 |
|
| 367 |
self::$donation_aggregates = is_array( $row ) |
| 368 |
? array_map( 'absint', array_merge( $defaults, $row ) ) |
| 369 |
: $defaults; |
| 370 |
|
| 371 |
return self::$donation_aggregates; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Get the total number of donors. |
| 376 |
* |
| 377 |
* @return int |
| 378 |
* @since 1.0.0 |
| 379 |
*/ |
| 380 |
private function get_total_donors() { |
| 381 |
global $wpdb; |
| 382 |
|
| 383 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Single COUNT query on a custom table, runs only at analytics send time. |
| 384 |
$count = $wpdb->get_var( |
| 385 |
$wpdb->prepare( |
| 386 |
'SELECT COUNT(*) FROM %i', |
| 387 |
$wpdb->prefix . 'suredonation_donors' |
| 388 |
) |
| 389 |
); |
| 390 |
|
| 391 |
return absint( $count ); |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Get KPI tracking data for the last 2 full days (excluding today). |
| 396 |
* |
| 397 |
* Single grouped query; raw revenue never enters the payload — only |
| 398 |
* the donation count and a coarse revenue tier per day. |
| 399 |
* |
| 400 |
* Date boundaries use GMT because `created_at` is written with |
| 401 |
* current_time( 'mysql', true ). |
| 402 |
* |
| 403 |
* @return array<string, array<string, array<string, mixed>>> KPI data keyed by Y-m-d date. |
| 404 |
* @since 1.0.0 |
| 405 |
*/ |
| 406 |
private function get_kpi_tracking_data() { |
| 407 |
global $wpdb; |
| 408 |
|
| 409 |
$start = gmdate( 'Y-m-d', strtotime( '-2 days' ) ) . ' 00:00:00'; |
| 410 |
$end = gmdate( 'Y-m-d' ) . ' 00:00:00'; |
| 411 |
|
| 412 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Single grouped query on a custom table, runs only at analytics send time. |
| 413 |
$rows = $wpdb->get_results( |
| 414 |
$wpdb->prepare( |
| 415 |
"SELECT DATE(created_at) AS day, COUNT(*) AS donations, COALESCE(SUM(amount),0) AS revenue |
| 416 |
FROM %i |
| 417 |
WHERE payment_status = 'completed' AND created_at >= %s AND created_at < %s |
| 418 |
GROUP BY day", |
| 419 |
$wpdb->prefix . 'suredonation_donations', |
| 420 |
$start, |
| 421 |
$end |
| 422 |
), |
| 423 |
ARRAY_A |
| 424 |
); |
| 425 |
|
| 426 |
$kpi_data = []; |
| 427 |
|
| 428 |
// Seed both days so dates with zero donations are still reported. |
| 429 |
for ( $i = 2; $i >= 1; $i-- ) { |
| 430 |
$day = gmdate( 'Y-m-d', strtotime( '-' . $i . ' days' ) ); |
| 431 |
|
| 432 |
$kpi_data[ $day ] = [ |
| 433 |
'numeric_values' => [ |
| 434 |
'donations' => 0, |
| 435 |
], |
| 436 |
'string_values' => [ |
| 437 |
'donation_revenue_tier' => '0', |
| 438 |
], |
| 439 |
]; |
| 440 |
} |
| 441 |
|
| 442 |
$rows = is_array( $rows ) ? $rows : []; |
| 443 |
|
| 444 |
foreach ( $rows as $row ) { |
| 445 |
if ( empty( $row['day'] ) || ! isset( $kpi_data[ $row['day'] ] ) ) { |
| 446 |
continue; |
| 447 |
} |
| 448 |
|
| 449 |
$kpi_data[ $row['day'] ] = [ |
| 450 |
'numeric_values' => [ |
| 451 |
'donations' => absint( $row['donations'] ?? 0 ), |
| 452 |
], |
| 453 |
'string_values' => [ |
| 454 |
'donation_revenue_tier' => $this->get_revenue_tier( (float) ( $row['revenue'] ?? 0 ) ), |
| 455 |
], |
| 456 |
]; |
| 457 |
} |
| 458 |
|
| 459 |
return $kpi_data; |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* Map a raw daily revenue amount to a coarse reporting tier. |
| 464 |
* |
| 465 |
* @param float $revenue Daily revenue. |
| 466 |
* @return string Revenue tier label. |
| 467 |
* @since 1.0.0 |
| 468 |
*/ |
| 469 |
private function get_revenue_tier( float $revenue ): string { |
| 470 |
if ( $revenue <= 0 ) { |
| 471 |
return '0'; |
| 472 |
} |
| 473 |
if ( $revenue < 100 ) { |
| 474 |
return '1-100'; |
| 475 |
} |
| 476 |
if ( $revenue < 500 ) { |
| 477 |
return '100-500'; |
| 478 |
} |
| 479 |
if ( $revenue < 1000 ) { |
| 480 |
return '500-1000'; |
| 481 |
} |
| 482 |
if ( $revenue < 5000 ) { |
| 483 |
return '1000-5000'; |
| 484 |
} |
| 485 |
return '5000+'; |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Detect state-based events that can't use direct hooks. |
| 490 |
* |
| 491 |
* Throttled by a daily transient; uses the request-cached donation |
| 492 |
* aggregates plus option reads only — no extra queries. The events |
| 493 |
* tracker dedups, so repeated calls are safe. |
| 494 |
* |
| 495 |
* @return void |
| 496 |
* @since 1.0.0 |
| 497 |
*/ |
| 498 |
private function detect_state_events() { |
| 499 |
if ( get_transient( 'suredonation_state_events_checked' ) ) { |
| 500 |
return; |
| 501 |
} |
| 502 |
|
| 503 |
$events = self::events(); |
| 504 |
if ( null === $events ) { |
| 505 |
return; // Tracker unavailable — retry on next admin load. |
| 506 |
} |
| 507 |
|
| 508 |
// Set only after the tracker is confirmed available. |
| 509 |
set_transient( 'suredonation_state_events_checked', true, DAY_IN_SECONDS ); |
| 510 |
|
| 511 |
$aggregates = $this->get_donation_aggregates(); |
| 512 |
$mode = Payment_Helper::get_payment_mode(); |
| 513 |
|
| 514 |
// plugin_activated: dedup ensures this fires only once. |
| 515 |
$bsf_referrers = get_option( 'bsf_product_referers', [] ); |
| 516 |
$source = is_array( $bsf_referrers ) && ! empty( $bsf_referrers['suredonation'] ) |
| 517 |
? sanitize_text_field( (string) $bsf_referrers['suredonation'] ) |
| 518 |
: 'self'; |
| 519 |
$events->track( 'plugin_activated', SUREDONATION_VER, [ 'source' => $source ] ); |
| 520 |
|
| 521 |
// plugin_updated: re-track on every version change. |
| 522 |
$tracked_version = get_option( 'suredonation_tracked_version', '' ); |
| 523 |
if ( SUREDONATION_VER !== $tracked_version ) { |
| 524 |
if ( ! empty( $tracked_version ) && is_string( $tracked_version ) ) { |
| 525 |
$events->flush_pushed( [ 'plugin_updated' ] ); |
| 526 |
$events->track( 'plugin_updated', SUREDONATION_VER, [ 'from_version' => $tracked_version ] ); |
| 527 |
} |
| 528 |
update_option( 'suredonation_tracked_version', SUREDONATION_VER, false ); |
| 529 |
} |
| 530 |
|
| 531 |
// stripe_connected: detect connection state. |
| 532 |
if ( Stripe_Helper::is_stripe_connected() ) { |
| 533 |
$events->track( 'stripe_connected', $mode ); |
| 534 |
} |
| 535 |
|
| 536 |
// paypal_connected: detect connection state. |
| 537 |
if ( PayPal_Helper::is_paypal_connected() ) { |
| 538 |
$events->track( 'paypal_connected', $mode ); |
| 539 |
} |
| 540 |
|
| 541 |
// payment_mode_live: site switched to live payments. |
| 542 |
if ( 'live' === $mode ) { |
| 543 |
$events->track( 'payment_mode_live' ); |
| 544 |
} |
| 545 |
|
| 546 |
// first_donation_received: time-to-value milestone. |
| 547 |
if ( $aggregates['completed'] > 0 ) { |
| 548 |
$install_time_raw = get_site_option( 'suredonation_usage_installed_time', 0 ); |
| 549 |
$install_time = is_numeric( $install_time_raw ) ? (int) $install_time_raw : 0; |
| 550 |
$days_since_install = $install_time > 0 ? (int) floor( ( time() - $install_time ) / DAY_IN_SECONDS ) : 0; |
| 551 |
|
| 552 |
$events->track( |
| 553 |
'first_donation_received', |
| 554 |
Payment_Helper::get_currency(), |
| 555 |
[ |
| 556 |
'days_since_install' => (string) $days_since_install, |
| 557 |
'payment_mode' => $mode, |
| 558 |
] |
| 559 |
); |
| 560 |
} |
| 561 |
|
| 562 |
// anonymous_donation_submitted: at least one completed anonymous donation. |
| 563 |
if ( $aggregates['anonymous_completed'] > 0 ) { |
| 564 |
$events->track( 'anonymous_donation_submitted' ); |
| 565 |
} |
| 566 |
|
| 567 |
// cover_fees_used: at least one completed donation covered fees. |
| 568 |
if ( $aggregates['fees_covered_completed'] > 0 ) { |
| 569 |
$events->track( 'cover_fees_used' ); |
| 570 |
} |
| 571 |
|
| 572 |
// first_refund_processed: at least one (partially) refunded donation. |
| 573 |
if ( $aggregates['refunded'] > 0 ) { |
| 574 |
$events->track( 'first_refund_processed' ); |
| 575 |
} |
| 576 |
|
| 577 |
// webhook_configured: a Stripe webhook secret is stored for the current mode. |
| 578 |
if ( '' !== Stripe_Helper::get_webhook_secret() ) { |
| 579 |
$events->track( 'webhook_configured', $mode ); |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
|