* @since 1.3.0 */ public const TRACKED_EVENTS = [ 'configure_gateway' => 'configure_gateway_notice_react_cta', 'webhook' => 'webhook_notice_react_cta', 'test_mode' => 'test_mode_notice_react_cta', 'quick_access' => 'quick_access_configure_gateway_react_cta', // The PayPal settings panel warns when PayPal has told us the connected // account cannot be paid. The CTA sends the merchant to PayPal to finish // setup; the dismiss says they saw it and moved on, which is worth // separating from never having seen it. 'paypal_account' => 'paypal_account_warning_react_cta', 'paypal_account_x' => 'paypal_account_warning_react_dismiss', 'paypal_webhook_x' => 'paypal_webhook_error_react_dismiss', // The Stripe settings panel warns when Stripe has told us the connected // account cannot charge cards. The CTA sends the site owner to their // Stripe dashboard to resolve it; the dismiss says they saw it and moved // on, which is worth separating from never having seen it. 'stripe_account' => 'stripe_account_warning_react_cta', 'stripe_account_x' => 'stripe_account_warning_react_dismiss', // The form-side capability notices link here with ?sd_notice=, so the // settings screen records that one of them is what brought the admin // over. Their own page is a public donation form, which is no place to // be loading a tracker. 'stripe_capability' => 'stripe_capability_notice_cta', // The other three donation-form notices reach the settings screen the // same way. Their own page is a public donation form, which is no place // to be loading a tracker, so the CTA carries a marker and the arrival // is what gets recorded. 'frontend_test_mode' => 'frontend_test_mode_notice_cta', 'frontend_gateway_unavailable' => 'frontend_gateway_unavailable_notice_cta', 'frontend_gateway_setup' => 'frontend_gateway_setup_notice_cta', ]; /** * BSF_Analytics_Events instance for one-time event tracking. * * @var \BSF_Analytics_Events|null * @since 1.0.0 */ private static $events = null; /** * Request-cached donation aggregates. * * @var array|null * @since 1.0.0 */ private static $donation_aggregates = null; /** * Class constructor. * * @return void * @since 1.0.0 */ public function __construct() { /* * Entity registration is deferred to init priority 0 so that add-on * plugins (e.g. SureDonation Pro) registering filters such as * suredonation_deactivation_survey_data on plugins_loaded are in * place before the deactivation survey data is filtered, and the * entity is still set before the BSF Analytics loader consumes it on * init priority 10. */ add_action( 'init', [ $this, 'register_entity' ], 0 ); // REST route the React admin app calls to record notice/UI clicks. add_action( 'rest_api_init', [ $this, 'register_routes' ] ); add_filter( 'bsf_core_stats', [ $this, 'add_suredonation_analytics_data' ] ); // Keep analytics sends (and their stat queries) off the frontend. add_filter( 'suredonation_tracking_enabled', [ $this, 'restrict_tracking_to_admin' ] ); // Event tracking hooks. Registered outside is_admin() on purpose — // onboarding completion and campaign publishes fire during REST requests. add_action( 'suredonation_onboarding_user_details_saved', [ $this, 'track_onboarding_completed' ] ); add_action( 'suredonation_campaign_tour_shown', [ $this, 'track_campaign_tour_shown' ] ); add_action( 'suredonation_campaign_tour_outcome', [ $this, 'track_campaign_tour_outcome' ], 10, 2 ); add_action( 'suredonation_campaign_template_picker_opened', [ $this, 'track_campaign_template_picker_opened' ] ); add_action( 'suredonation_campaign_created_from_template', [ $this, 'track_campaign_created_from_template' ] ); add_action( 'transition_post_status', [ $this, 'track_first_campaign_published' ], 10, 3 ); add_action( 'current_screen', [ $this, 'track_first_campaign_editor_opened' ] ); add_action( 'suredonation_privacy_data_exported', [ $this, 'track_privacy_data_exported' ] ); add_action( 'suredonation_privacy_data_erased', [ $this, 'track_privacy_data_erased' ] ); // Detect state-based events (daily throttle; dedup prevents repeat // tracking). Admin-only so the detection never runs on the frontend. if ( is_admin() ) { $this->detect_state_events(); } } /** * Register the SureDonation entity with the BSF Analytics loader. * * Runs on init priority 0 — after add-on plugins have registered their * filters on plugins_loaded, and before the loader's own init callback * loads the analytics library. * * @return void * @since 1.0.0 */ public function register_entity() { if ( ! class_exists( 'BSF_Analytics_Loader' ) ) { require_once SUREDONATION_DIR . 'inc/lib/bsf-analytics/class-bsf-analytics-loader.php'; } if ( ! class_exists( 'BSF_Admin_Notices' ) ) { require_once SUREDONATION_DIR . 'inc/lib/astra-notices/class-bsf-admin-notices.php'; } /** * The loader's get_instance() carries no return type. * * @var \BSF_Analytics_Loader $suredonation_bsf_analytics */ $suredonation_bsf_analytics = \BSF_Analytics_Loader::get_instance(); $suredonation_bsf_analytics->set_entity( [ 'suredonation' => [ 'product_name' => 'SureDonation', 'path' => SUREDONATION_DIR . 'inc/lib/bsf-analytics', 'author' => 'SureDonation', 'time_to_display' => '+24 hours', 'deactivation_survey' => apply_filters( 'suredonation_deactivation_survey_data', [ [ 'id' => 'deactivation-survey-suredonation', 'popup_logo' => SUREDONATION_URL . 'images/suredonation-icon.svg', 'plugin_slug' => 'suredonation', 'popup_title' => __( 'Quick Feedback', 'suredonation' ), 'support_url' => 'https://suredonation.com/support/', 'popup_description' => __( 'If you have a moment, please share why you are deactivating SureDonation:', 'suredonation' ), 'show_on_screens' => [ 'plugins' ], 'plugin_version' => SUREDONATION_VER, ], ] ), 'hide_optin_checkbox' => true, ], ] ); } /** * Get the shared BSF_Analytics_Events instance. * * Uses SureDonation's Helper option methods so the event data stays * inside the consolidated suredonation_options row. * * @return \BSF_Analytics_Events|null Events instance, or null when the library is unavailable. * @since 1.0.0 */ public static function events() { if ( null === self::$events ) { if ( ! class_exists( 'BSF_Analytics_Events' ) ) { $events_file = SUREDONATION_DIR . 'inc/lib/bsf-analytics/class-bsf-analytics-events.php'; if ( file_exists( $events_file ) ) { require_once $events_file; } } if ( ! class_exists( 'BSF_Analytics_Events' ) ) { return null; } self::$events = new \BSF_Analytics_Events( 'suredonation', [ 'get' => [ Helper::class, 'get_suredonation_option' ], 'update' => [ Helper::class, 'update_suredonation_option' ], ] ); } return self::$events; } /** * Register REST routes. * * Hooked - rest_api_init * * @return void * @since 1.3.0 */ public function register_routes() { register_rest_route( 'suredonation/v1', '/track-notice-event', [ 'methods' => \WP_REST_Server::CREATABLE, 'callback' => [ $this, 'handle_track_notice_event' ], // A named method rather than a closure: the capability guard in // tests/unit/inc/test-rest-api.php introspects every write // route's permission_callback, and a closure is opaque to it. 'permission_callback' => [ $this, 'check_permissions' ], 'args' => [ 'event' => [ 'type' => 'string', 'required' => true, ], ], ] ); } /** * Whether the current user may record notice events. * * Named check_permissions to match the other REST controllers, which is also * what the write-route capability guard asserts on. * * @return bool True when the user can manage options. * @since 1.4.0 */ public function check_permissions() { return current_user_can( 'manage_options' ); } /** * Record a React notice/UI interaction event. * * Validates the event against an allowlist (so arbitrary events cannot be * injected) and records it via the shared analytics events, respecting the * usage-tracking opt-in. Event names are suffixed `_react` to keep them * distinct from the wp-admin notice events. * * @param \WP_REST_Request> $request REST request. * @return \WP_REST_Response * @since 1.3.0 */ public function handle_track_notice_event( $request ) { $event = sanitize_key( (string) $request->get_param( 'event' ) ); if ( ! in_array( $event, self::TRACKED_EVENTS, true ) ) { return new \WP_REST_Response( [ 'success' => false ], 400 ); } $events = self::events(); if ( null !== $events ) { $events->track( $event ); } return new \WP_REST_Response( [ 'success' => true ], 200 ); } /** * Callback function to add SureDonation specific analytics data. * * @param array $stats_data Existing stats data. * @return array * @since 1.0.0 */ public function add_suredonation_analytics_data( $stats_data ) { $aggregates = $this->get_donation_aggregates(); $campaign_counts = wp_count_posts( 'suredonation_cmpgn' ); $form_counts = wp_count_posts( 'suredonation_form' ); $bsf_internal_referrer = get_option( 'bsf_product_referers', [] ); $internal_referer = is_array( $bsf_internal_referrer ) && ! empty( $bsf_internal_referrer['suredonation'] ) ? sanitize_text_field( (string) $bsf_internal_referrer['suredonation'] ) : 'self'; $privacy_settings = Privacy_Settings::get_settings(); // Computed once: the headline total below is derived from the same rows. $template_usage = $this->get_campaign_template_usage(); $plugin_data = [ 'free_version' => SUREDONATION_VER, 'numeric_values' => [ 'total_campaigns' => absint( $campaign_counts->publish ?? 0 ), 'total_donation_forms' => absint( $form_counts->publish ?? 0 ), 'total_donations' => $aggregates['total'], 'completed_donations' => $aggregates['completed'], 'recurring_donations' => $aggregates['recurring'], 'total_donors' => $this->get_total_donors(), 'forms_with_image_block' => $this->get_image_block_form_count(), 'posts_with_social_sharing_block' => $this->get_social_sharing_block_count(), 'stripe_accounts_count' => count( Stripe_Helper::get_all_accounts() ), // Campaigns started from a gallery template — excludes both the // scratch path and the `general` fallback, so this is the count // of campaigns that actually adopted a cause template. 'campaigns_from_template' => array_sum( array_diff_key( $template_usage, [ 'scratch' => 0, Campaign_Templates::GENERAL => 0, ] ) ), ], 'boolean_values' => [ 'stripe_enabled' => Stripe_Helper::is_stripe_connected(), 'paypal_enabled' => PayPal_Helper::is_paypal_connected(), 'offline_enabled' => Offline_Helper::is_offline_enabled(), // True only when the OttoKit plugin is active AND authenticated, // so this implies the plugin is active. 'ottokit_connected' => Helper::is_suretriggers_ready(), 'contact_consent_enabled' => ! empty( $privacy_settings['contact_consent_field'] ), 'privacy_policy_field_enabled' => ! empty( $privacy_settings['privacy_policy_field'] ), 'terms_field_enabled' => ! empty( $privacy_settings['terms_conditions_field'] ), ], 'data_retention_period' => isset( $privacy_settings['minimum_data_retention_period'] ) ? Helper::get_string_value( $privacy_settings['minimum_data_retention_period'] ) : 'none', 'block_usage' => $this->get_block_usage(), 'campaign_template_usage' => $template_usage, 'elementor_widget_usage' => $this->get_elementor_widget_usage(), 'bricks_element_usage' => $this->get_bricks_element_usage(), 'internal_referer' => $internal_referer, ]; // Add KPI tracking data. $kpi_data = $this->get_kpi_tracking_data(); if ( ! empty( $kpi_data ) ) { $plugin_data['kpi_records'] = $kpi_data; } // Flush pending events into payload (only if any exist). $events = self::events(); if ( null !== $events ) { $pending_events = $events->flush_pending(); if ( ! empty( $pending_events ) ) { $plugin_data['events_record'] = $pending_events; } } if ( ! isset( $stats_data['plugin_data'] ) || ! is_array( $stats_data['plugin_data'] ) ) { $stats_data['plugin_data'] = []; } $stats_data['plugin_data']['suredonation'] = $plugin_data; return $stats_data; } /** * Keep analytics sends off the frontend. * * Filter callback for `suredonation_tracking_enabled`. The library * evaluates this on every request via `is_tracking_enabled()`; gating on * is_admin() means the stats queries never run on frontend page loads. * Deliberately NOT narrowed further (e.g. to plugin screens): the library * also consults this filter from `register_usage_tracking_setting()` on * admin_init, where returning false aborts settings registration for all * registered BSF products. * * @param bool $is_enabled Whether tracking is enabled (opt-in state). * @return bool * @since 1.0.0 */ public function restrict_tracking_to_admin( $is_enabled ) { return $is_enabled && is_admin(); } /** * Track onboarding completion when lead-capture details are saved. * * The payload contains PII (name/email) — only the opt-in flag is * forwarded to analytics. * * @param array $payload Sanitized onboarding payload. * @return void * @since 1.0.0 */ public function track_onboarding_completed( $payload ) { $events = self::events(); if ( null === $events ) { return; } $payload = is_array( $payload ) ? $payload : []; $events->track( 'onboarding_completed', '', [ 'opted_in' => ! empty( $payload['opted_in'] ) ? 'yes' : 'no', ] ); } /** * Track the first time the campaign guided tour is shown to a user. * * Fired from the REST layer on the tour's first render. The events tracker * dedups by name, so this is recorded once per site regardless of how many * users see the tour or how often it re-triggers. * * @return void * @since 1.5.0 */ public function track_campaign_tour_shown() { $events = self::events(); if ( null === $events ) { return; } $events->track( 'campaign_tour_shown' ); } /** * Track how a campaign guided-tour run ended. * * `campaign_tour_shown` tells us the tour was seen; these tell us whether it * worked. Four outcomes, each recorded under its own event name: * * - `completed` — the user reached the final step. * - `dismissed` — closed part-way; the step key rides along as the * event value so we can see where runs are abandoned. * - `opted_out` — ticked "Don't show this again". * - `manual_started` — replayed deliberately via "Take a tour". * * `dismissed` is tracked with $force so the most recent drop-off step wins * rather than only the first one ever recorded on the site; the others keep * the default once-per-site semantics. * * @param string $outcome How the run ended. Unknown values are ignored. * @param string $step Step key the run ended on. Only used for `dismissed`. * @return void * @since 1.5.0 */ public function track_campaign_tour_outcome( $outcome, $step = '' ) { $events = self::events(); if ( null === $events ) { return; } $outcome = Helper::get_string_value( $outcome ); $step = Helper::get_string_value( $step ); switch ( $outcome ) { case 'completed': $events->track( 'campaign_tour_completed' ); break; case 'dismissed': // Retrackable: the latest abandonment point is the useful one. $events->track( 'campaign_tour_dismissed', $step, [], true ); break; case 'opted_out': $events->track( 'campaign_tour_opted_out', $step ); break; case 'manual_started': $events->track( 'campaign_tour_manual_started' ); break; } } /** * Track the first time the campaign template picker is opened. * * Deduped, so it answers "did this site ever discover the picker?" — the * denominator for template adoption, since `campaign_template_usage` in the * stats payload only counts campaigns that were actually created from one. * * @return void * @since 1.5.0 */ public function track_campaign_template_picker_opened() { $events = self::events(); if ( null === $events ) { return; } $events->track( 'campaign_template_picker_opened' ); } /** * Track the first campaign a site creates from a template. * * Deduped, so the event value is the template the site reached for *first* — * the running per-template totals live in `campaign_template_usage` on the * stats payload, which is recomputed on every send. * * @param string $template_id Template the campaign was created from. * @return void * @since 1.5.0 */ public function track_campaign_created_from_template( $template_id ) { $events = self::events(); if ( null === $events ) { return; } $template_id = Helper::get_string_value( $template_id ); if ( '' === $template_id ) { return; } $events->track( 'first_campaign_from_template', $template_id ); } /** * Track first personal-data export that included SureDonation data * (adoption event — deduped, sent once). * * @since 1.2.0 * @return void */ public function track_privacy_data_exported() { $events = self::events(); if ( null === $events ) { return; } $events->track( 'privacy_data_export_used' ); } /** * Track first personal-data erasure processed for SureDonation data * (adoption event — deduped, sent once). * * @since 1.2.0 * @param array $outcome Erasure outcome flags. * @return void */ public function track_privacy_data_erased( $outcome ) { $events = self::events(); if ( null === $events ) { return; } $outcome = is_array( $outcome ) ? $outcome : []; $events->track( 'privacy_data_erasure_used', '', [ 'items_removed' => ! empty( $outcome['items_removed'] ) ? 'yes' : 'no', 'items_retained' => ! empty( $outcome['items_retained'] ) ? 'yes' : 'no', 'erase_failed' => ! empty( $outcome['erase_failed'] ) ? 'yes' : 'no', ] ); } /** * Track first time a campaign is published (activation event). * * @param string $new_status New post status. * @param string $old_status Old post status. * @param \WP_Post $post Post object. * @return void * @since 1.0.0 */ public function track_first_campaign_published( $new_status, $old_status, $post ) { if ( 'publish' !== $new_status || 'publish' === $old_status || ! $post instanceof \WP_Post || 'suredonation_cmpgn' !== $post->post_type ) { return; } $events = self::events(); if ( null === $events ) { return; } $meta = Helper::get_campaign_meta( $post->ID ); $goal_amount = isset( $meta['goal_amount'] ) && is_numeric( $meta['goal_amount'] ) ? (float) $meta['goal_amount'] : 0.0; $goal_type = isset( $meta['goal_type'] ) && is_scalar( $meta['goal_type'] ) ? sanitize_text_field( (string) $meta['goal_type'] ) : ''; $events->track( 'first_campaign_published', (string) $post->ID, [ 'goal_type' => $goal_type, 'has_goal' => $goal_amount > 0 ? '1' : '0', ] ); } /** * Track first time a user opens the campaign editor. * * @param \WP_Screen $screen Current screen object. * @return void * @since 1.0.0 */ public function track_first_campaign_editor_opened( $screen ) { if ( ! $screen instanceof \WP_Screen || 'suredonation_cmpgn' !== $screen->post_type || 'post' !== $screen->base ) { return; } $events = self::events(); if ( null === $events ) { return; } $events->track( 'first_campaign_editor_opened' ); } /** * Get donation aggregates in a single request-cached query. * * Feeds both the stats numeric values and the state-event detection so * the donations table is only ever hit once per request. * * @return array Aggregate counts. * @since 1.0.0 */ private function get_donation_aggregates() { if ( null !== self::$donation_aggregates ) { return self::$donation_aggregates; } global $wpdb; $defaults = [ 'total' => 0, 'completed' => 0, 'completed_live' => 0, 'recurring' => 0, 'anonymous_completed' => 0, 'fees_covered_completed' => 0, 'refunded' => 0, ]; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Single aggregate query on a custom table, request-cached in a static. $row = $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) AS total, COALESCE(SUM(payment_status = 'completed'),0) AS completed, COALESCE(SUM(payment_status = 'completed' AND payment_mode = 'live'),0) AS completed_live, COALESCE(SUM(subscription_id IS NOT NULL AND subscription_id <> ''),0) AS recurring, COALESCE(SUM(is_anonymous = 1 AND payment_status = 'completed'),0) AS anonymous_completed, COALESCE(SUM(fees_covered > 0 AND payment_status = 'completed'),0) AS fees_covered_completed, COALESCE(SUM(payment_status IN ('refunded','partially_refunded')),0) AS refunded FROM %i", $wpdb->prefix . 'suredonation_donations' ), ARRAY_A ); self::$donation_aggregates = is_array( $row ) ? array_map( 'absint', array_merge( $defaults, $row ) ) : $defaults; return self::$donation_aggregates; } /** * Get the total number of donors. * * @return int * @since 1.0.0 */ private function get_total_donors() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Single COUNT query on a custom table, runs only at analytics send time. $count = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $wpdb->prefix . 'suredonation_donors' ) ); return absint( $count ); } /** * How many published posts use each SureDonation campaign/donation block. * * A privacy-preserving usage count (no content leaves the site) so we can see * which blocks are actually adopted. One conditional-SUM query (a single table * scan), run only at analytics send time. Elementor/Bricks placements are not * counted here (they store their config outside the block grammar) — see * get_elementor_widget_usage(). * * @return array Block key => number of published posts using it. * @since 1.2.0 */ private function get_block_usage() { global $wpdb; $blocks = [ 'campaign_goal' => 'suredonation/campaign-goal', 'campaign_stats' => 'suredonation/campaign-stats', 'campaign_donations' => 'suredonation/campaign-donations', 'campaign_donors' => 'suredonation/campaign-donors', 'campaign_donor_comments' => 'suredonation/campaign-donor-comments', 'campaign_donate_button' => 'suredonation/campaign-donate-button', 'campaign_social_sharing' => 'suredonation/campaign-social-sharing', 'donation_form' => 'suredonation/donation-form', ]; // prepare() fills placeholders in SQL order: the SELECT-list %s LIKEs // first, then the FROM %i, then the status %s. $selects = []; $values = []; foreach ( array_keys( $blocks ) as $key ) { $selects[] = "SUM(post_content LIKE %s) AS {$key}"; // The space after the block name is the delimiter the serializer always // emits (before attrs JSON, "-->" or "/-->"), so a future // "campaign-goal-x" block can't prefix-match campaign-goal. $values[] = '%' . $wpdb->esc_like( '