PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Tracking / McpTracking.php

McpTracking.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at classes/Tracking/McpTracking.php

155 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace Imagify\Tracking;
5
6 /**
7 * Tracking class for Imagify MCP ability events.
8 *
9 * Fires the same Mixpanel events as the UI path but with context `wp_plugin_mcp`
10 * so Mixpanel can aggregate across both surfaces or filter by source.
11 *
12 * @since 2.3.0
13 */
14 class McpTracking extends BaseTracking {
15
16 /**
17 * Returns the default event properties shared by every MCP tracked event.
18 *
19 * Overrides context to `wp_plugin_mcp` so Mixpanel can distinguish MCP
20 * executions from UI-initiated ones.
21 *
22 * @return array<string, mixed>
23 */
24 protected function get_default_event_properties(): array {
25 $defaults = parent::get_default_event_properties();
26 $defaults['context'] = 'wp_plugin_mcp';
27 return $defaults;
28 }
29
30 /**
31 * Track a "Media Optimized" event triggered via the MCP ability.
32 *
33 * Only fires for the `imagify/optimize-media` ability and only on success.
34 *
35 * @param string $ability_id Ability slug, e.g. `imagify/optimize-media`.
36 * @param array $result Return value of OptimizeMedia::execute().
37 * @param float $start_time microtime(true) captured before execute() ran.
38 * @param array $input_params Raw input params passed to execute().
39 * @return void
40 */
41 public function track_media_optimized( string $ability_id, array $result, float $start_time, array $input_params ): void {
42 if ( ! $this->can_track() ) {
43 return;
44 }
45
46 if ( 'imagify/optimize-media' !== $ability_id ) {
47 return;
48 }
49
50 if ( 'success' !== ( $result['status'] ?? '' ) ) {
51 return;
52 }
53
54 $media_id = isset( $input_params['media_id'] ) ? (int) $input_params['media_id'] : 0;
55 $execution_time_ms = round( ( microtime( true ) - $start_time ) * 1000, 2 );
56
57 $optimization_level = isset( $input_params['optimization_level'] )
58 ? (int) $input_params['optimization_level']
59 : (int) get_imagify_option( 'optimization_level' );
60
61 $event_data = array_merge(
62 $this->get_default_event_properties(),
63 [
64 'initiated_via' => 'mcp',
65 'optimization_level' => $optimization_level,
66 'media_type' => $media_id > 0 ? (string) get_post_mime_type( $media_id ) : '',
67 'original_size' => null !== $result['original_size'] ? (int) $result['original_size'] : null,
68 'optimized_size' => null !== $result['optimized_size'] ? (int) $result['optimized_size'] : null,
69 'savings_percent' => null !== $result['savings_percent'] ? (float) $result['savings_percent'] : null,
70 'next_gen_format' => $this->resolve_next_gen_format(),
71 'execution_time_ms' => $execution_time_ms,
72 ]
73 );
74
75 $this->mixpanel->track_direct( 'Media Optimized', $event_data );
76 }
77
78 /**
79 * Track a generic "MCP Ability Executed" event for any ability invocation.
80 *
81 * Fires for every ability (including optimize-media) so Mixpanel dashboards
82 * can aggregate all MCP calls by ability_id regardless of outcome.
83 *
84 * @param string $ability_id Ability slug, e.g. `imagify/get-stats`.
85 * @param string $ability_name Human-readable ability label.
86 * @param float $start_time microtime(true) captured before execute() ran.
87 * @return void
88 */
89 public function track_ability_executed( string $ability_id, string $ability_name, float $start_time ): void {
90 if ( ! $this->can_track() ) {
91 return;
92 }
93
94 $event_data = array_merge(
95 $this->get_default_event_properties(),
96 [
97 'ability_id' => $ability_id,
98 'ability_name' => $ability_name,
99 'execution_time_ms' => round( ( microtime( true ) - $start_time ) * 1000, 2 ),
100 ]
101 );
102
103 $this->mixpanel->track_direct( 'MCP Ability Executed', $event_data );
104 }
105
106 /**
107 * Track an "MCP Ability Permission Denied" event.
108 *
109 * @param string $ability_id Ability slug.
110 * @param string $ability_name Human-readable ability label.
111 * @param string $required_capability The capability that was missing.
112 * @return void
113 */
114 public function track_permission_denied( string $ability_id, string $ability_name, string $required_capability ): void {
115 if ( ! $this->can_track() ) {
116 return;
117 }
118
119 $user = wp_get_current_user();
120 $user_role = ! empty( $user->roles ) ? (string) $user->roles[0] : '';
121
122 $event_data = array_merge(
123 $this->get_default_event_properties(),
124 [
125 'ability_id' => $ability_id,
126 'ability_name' => $ability_name,
127 'required_capability' => $required_capability,
128 'user_role' => $user_role,
129 ]
130 );
131
132 $this->mixpanel->track_direct( 'MCP Ability Permission Denied', $event_data );
133 }
134
135 /**
136 * Resolve the next-gen format from the site's conversion settings.
137 *
138 * Returns 'avif' when AVIF conversion is enabled (highest priority), 'webp'
139 * when only WebP is enabled, or null when next-gen conversion is off.
140 *
141 * @return string|null
142 */
143 protected function resolve_next_gen_format(): ?string {
144 if ( (bool) get_imagify_option( 'convert_to_avif' ) ) {
145 return 'avif';
146 }
147
148 if ( (bool) get_imagify_option( 'convert_to_webp' ) ) {
149 return 'webp';
150 }
151
152 return null;
153 }
154 }
155