analytics-events.php
| 1 | <?php |
| 2 | /** |
| 3 | * Analytics Events helper for one-time milestone tracking. |
| 4 | * |
| 5 | * Tracks events temporarily, sends them once via BSF Analytics, |
| 6 | * then cleans up. Only a minimal dedup flag remains. |
| 7 | * |
| 8 | * @package sureforms. |
| 9 | * @since 2.5.1 |
| 10 | */ |
| 11 | |
| 12 | namespace SRFM\Inc; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Analytics Events Class. |
| 20 | * |
| 21 | * @since 2.5.1 |
| 22 | */ |
| 23 | class Analytics_Events { |
| 24 | /** |
| 25 | * Track a one-time event. Skips if already tracked or pending. |
| 26 | * Only stores temporary data — cleaned up after analytics send. |
| 27 | * |
| 28 | * @param string $event_name Event identifier. |
| 29 | * @param string $event_value Primary value (version, form ID, mode, etc.). |
| 30 | * @param array<string, mixed> $properties Additional context as key-value pairs. |
| 31 | * @since 2.5.1 |
| 32 | * @return void |
| 33 | */ |
| 34 | public static function track( $event_name, $event_value = '', $properties = [] ) { |
| 35 | // Check dedup flag — already sent in a previous cycle. |
| 36 | $pushed = Helper::get_srfm_option( 'usage_events_pushed', [] ); |
| 37 | $pushed = is_array( $pushed ) ? $pushed : []; |
| 38 | if ( in_array( $event_name, $pushed, true ) ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // Check if already queued in current cycle. |
| 43 | $pending = Helper::get_srfm_option( 'usage_events_pending', [] ); |
| 44 | $pending = is_array( $pending ) ? $pending : []; |
| 45 | if ( in_array( $event_name, array_column( $pending, 'event_name' ), true ) ) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Add to pending queue. |
| 50 | $pending[] = [ |
| 51 | 'event_name' => sanitize_text_field( $event_name ), |
| 52 | 'event_value' => sanitize_text_field( (string) $event_value ), |
| 53 | 'properties' => ! empty( $properties ) ? $properties : new \stdClass(), |
| 54 | 'date' => current_time( 'mysql' ), |
| 55 | ]; |
| 56 | Helper::update_srfm_option( 'usage_events_pending', $pending ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Flush pending events: returns them for the payload, then cleans up. |
| 61 | * |
| 62 | * After this call: |
| 63 | * - usage_events_pending is EMPTY (full event data deleted). |
| 64 | * - usage_events_pushed has event_name strings added (minimal dedup). |
| 65 | * |
| 66 | * @since 2.5.1 |
| 67 | * @return array Pending events to include in payload. Empty if none. |
| 68 | */ |
| 69 | public static function flush_pending() { |
| 70 | $pending = Helper::get_srfm_option( 'usage_events_pending', [] ); |
| 71 | if ( empty( $pending ) || ! is_array( $pending ) ) { |
| 72 | return []; |
| 73 | } |
| 74 | |
| 75 | // Add event names to dedup flag (minimal — just strings). |
| 76 | $pushed = Helper::get_srfm_option( 'usage_events_pushed', [] ); |
| 77 | $pushed = is_array( $pushed ) ? $pushed : []; |
| 78 | $pushed = array_unique( |
| 79 | array_merge( $pushed, array_column( $pending, 'event_name' ) ) |
| 80 | ); |
| 81 | Helper::update_srfm_option( 'usage_events_pushed', $pushed ); |
| 82 | |
| 83 | // DELETE all temporary event data. |
| 84 | Helper::update_srfm_option( 'usage_events_pending', [] ); |
| 85 | |
| 86 | return $pending; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Remove specific event names from the pushed dedup flag, allowing them to be re-tracked. |
| 91 | * |
| 92 | * Pass an array of event names to remove only those entries. |
| 93 | * Pass an empty array (or omit) to clear all pushed events. |
| 94 | * |
| 95 | * @param array<string> $event_names Event names to remove. Empty = clear all. |
| 96 | * @since 2.6.0 |
| 97 | * @return void |
| 98 | */ |
| 99 | public static function flush_pushed( $event_names = [] ) { |
| 100 | $pushed = Helper::get_srfm_option( 'usage_events_pushed', [] ); |
| 101 | $pushed = is_array( $pushed ) ? $pushed : []; |
| 102 | |
| 103 | if ( empty( $event_names ) ) { |
| 104 | Helper::update_srfm_option( 'usage_events_pushed', [] ); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | $pushed = array_values( array_diff( $pushed, $event_names ) ); |
| 109 | Helper::update_srfm_option( 'usage_events_pushed', $pushed ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Check if an event has already been tracked (sent or pending). |
| 114 | * |
| 115 | * @param string $event_name Event identifier. |
| 116 | * @since 2.5.1 |
| 117 | * @return bool |
| 118 | */ |
| 119 | public static function is_tracked( $event_name ) { |
| 120 | $pushed = Helper::get_srfm_option( 'usage_events_pushed', [] ); |
| 121 | $pushed = is_array( $pushed ) ? $pushed : []; |
| 122 | if ( in_array( $event_name, $pushed, true ) ) { |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | $pending = Helper::get_srfm_option( 'usage_events_pending', [] ); |
| 127 | $pending = is_array( $pending ) ? $pending : []; |
| 128 | return in_array( $event_name, array_column( $pending, 'event_name' ), true ); |
| 129 | } |
| 130 | } |
| 131 |