| 1 |
<?php |
| 2 |
/** |
| 3 |
* Global helper functions for Linno Telemetry SDK |
| 4 |
* |
| 5 |
* @package LinnoSDK\Telemetry |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if (!function_exists('linno_telemetry')) { |
| 10 |
/** |
| 11 |
* Get the Telemetry Client instance for a specific plugin |
| 12 |
* |
| 13 |
* This function returns the Client instance for the specified plugin file. |
| 14 |
* The Client must be initialized elsewhere in the plugin before calling this function. |
| 15 |
* |
| 16 |
* @param string $plugin_file The main plugin file path (use __FILE__ from your main plugin file) |
| 17 |
* @return \LinnoSDK\Telemetry\Client|null The Client instance or null if not initialized |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
function linno_telemetry(string $plugin_file) { |
| 21 |
return \LinnoSDK\Telemetry\Client::getInstance($plugin_file); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
if (!function_exists('linno_telemetry_track')) { |
| 26 |
/** |
| 27 |
* Track a telemetry event for a specific plugin |
| 28 |
* |
| 29 |
* This is a convenience function that calls the track() method on the |
| 30 |
* plugin's Client instance. If the Client is not initialized or opt-in |
| 31 |
* is not enabled, the event will not be sent. |
| 32 |
* |
| 33 |
* @param string $plugin_file The main plugin file path (use __FILE__ from your main plugin file) |
| 34 |
* @param string $event The event name (alphanumeric and underscores only) |
| 35 |
* @param array $properties Optional array of event properties |
| 36 |
* |
| 37 |
* @return bool True if event was sent successfully, false otherwise |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
function linno_telemetry_track(string $plugin_file, string $event, array $properties = []): bool { |
| 41 |
$client = linno_telemetry($plugin_file); |
| 42 |
|
| 43 |
if ($client === null) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
return $client->track($event, $properties); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if (!function_exists('linno_telemetry_generate_profile_id')) { |
| 52 |
/** |
| 53 |
* Generate a UUID v4 for profile identification |
| 54 |
* |
| 55 |
* This function generates a unique identifier that can be used as a profileId |
| 56 |
* in the __identify property when tracking events with user profiles. |
| 57 |
* |
| 58 |
* @return string UUID v4 string |
| 59 |
* @since 1.0.0 |
| 60 |
*/ |
| 61 |
function linno_telemetry_generate_profile_id(): string { |
| 62 |
return \LinnoSDK\Telemetry\Helpers\Utils::generateProfileId(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if (!function_exists('linno_telemetry_update_last_action')) { |
| 67 |
/** |
| 68 |
* Update the last core action for a plugin |
| 69 |
* |
| 70 |
* This updates the last core action performed, which will be included |
| 71 |
* in the deactivation event to understand what the user was doing |
| 72 |
* before deactivating the plugin. |
| 73 |
* |
| 74 |
* @param string $plugin_file The main plugin file path |
| 75 |
* @param string $action The action name (e.g., 'feed_created', 'settings_saved') |
| 76 |
* |
| 77 |
* @return bool True on success, false on failure |
| 78 |
* @since 1.0.0 |
| 79 |
*/ |
| 80 |
function linno_telemetry_update_last_action(string $plugin_file, string $action): bool { |
| 81 |
$client = linno_telemetry($plugin_file); |
| 82 |
|
| 83 |
if ($client === null) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
$client->updateLastCoreAction($action); |
| 88 |
return true; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if (!function_exists('linno_telemetry_sync_consent_state')) { |
| 93 |
/** |
| 94 |
* Synchronize telemetry state after custom onboarding consent updates |
| 95 |
* |
| 96 |
* Use this helper when your onboarding flow stores consent directly |
| 97 |
* (outside of Client::set_optin_state()). It ensures post-consent |
| 98 |
* setup is completed, including pending activation tracking. |
| 99 |
* |
| 100 |
* @param string $plugin_file The main plugin file path |
| 101 |
* |
| 102 |
* @return bool True on success, false if client is unavailable |
| 103 |
* @since 1.0.1 |
| 104 |
*/ |
| 105 |
function linno_telemetry_sync_consent_state(string $plugin_file): bool { |
| 106 |
$client = linno_telemetry($plugin_file); |
| 107 |
|
| 108 |
if ($client === null) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
$client->sync_consent_state(); |
| 113 |
return true; |
| 114 |
} |
| 115 |
} |
| 116 |
|