-
Notifications
You must be signed in to change notification settings - Fork 7
Use new Telemetry class from VIP mu-plugins #452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace RemoteDataBlocks\Telemetry; | ||
|
||
use RemoteDataBlocks\Editor\BlockManagement\ConfigStore; | ||
use WP_Post; | ||
|
||
defined( 'ABSPATH' ) || exit(); | ||
|
||
/** | ||
* Class to implement telemetry on WordPress VIP. | ||
*/ | ||
class Telemetry { | ||
private const EVENT_PREFIX = 'remotedatablocks_'; | ||
private static ?self $instance = null; | ||
|
||
final private function __construct( private string $plugin_path, private ?object $telemetry = null ) {} | ||
|
||
/** | ||
* Initialize telemetry for the plugin. The Telemetry library is provided | ||
* by VIP mu-plugins and only runs in approved environments. | ||
* | ||
* @psalm-suppress UndefinedClass | ||
* | ||
* @param string $plugin_path Plugin path. | ||
* @param object|null $telemetry Telemetry instance. If null, we will attempt to create one. | ||
*/ | ||
public static function init( string $plugin_path, ?object $telemetry = null ): void { | ||
if ( isset( self::$instance ) ) { | ||
return; | ||
} | ||
|
||
if ( null === $telemetry && class_exists( 'Automattic\VIP\Telemetry\Telemetry' ) ) { | ||
$telemetry = new \Automattic\VIP\Telemetry\Telemetry( self::EVENT_PREFIX, self::get_global_properties() ); | ||
} | ||
|
||
self::$instance = new self( $plugin_path, $telemetry ); | ||
self::$instance->setup_tracking_via_hooks(); | ||
} | ||
|
||
/** | ||
* Get global event properties. These properties are sent with each event. | ||
*/ | ||
public static function get_global_properties(): array { | ||
$plugin_version = defined( 'REMOTE_DATA_BLOCKS__PLUGIN_VERSION' ) ? constant( 'REMOTE_DATA_BLOCKS__PLUGIN_VERSION' ) : 'unknown'; | ||
|
||
return [ | ||
'plugin_version' => $plugin_version, | ||
]; | ||
} | ||
|
||
private function setup_tracking_via_hooks(): void { | ||
// WordPress hooks. | ||
add_action( 'activated_plugin', [ $this, 'track_plugin_activation' ], 10, 1 ); | ||
add_action( 'deactivated_plugin', [ $this, 'track_plugin_deactivation' ], 10, 1 ); | ||
add_action( 'save_post', [ $this, 'track_remote_data_blocks_usage' ], 10, 2 ); | ||
|
||
// Custom hook to allow other plugin code to track events | ||
add_action( 'remote_data_blocks_track_event', [ $this, 'record_event' ], 10, 2 ); | ||
} | ||
|
||
/** | ||
* Activation hook. | ||
* | ||
* @param string $plugin_path Path of the plugin that was activated. | ||
*/ | ||
public function track_plugin_activation( string $plugin_path ): void { | ||
if ( ! str_ends_with( $this->plugin_path, $plugin_path ) ) { | ||
return; | ||
} | ||
|
||
$this->record_event( 'plugin_toggle', [ 'action' => 'activate' ] ); | ||
} | ||
|
||
/** | ||
* Deactivation hook. | ||
* | ||
* @param string $plugin_path Path of the plugin that was deactivated. | ||
*/ | ||
public function track_plugin_deactivation( string $plugin_path ): void { | ||
if ( ! str_ends_with( $this->plugin_path, $plugin_path ) ) { | ||
return; | ||
} | ||
|
||
$this->record_event( 'plugin_toggle', [ 'action' => 'deactivate' ] ); | ||
} | ||
|
||
/** | ||
* Track usage of Remote Data Blocks. | ||
* | ||
* @param int $post_id Post ID. | ||
* @param WP_Post $post Post object. | ||
*/ | ||
public function track_remote_data_blocks_usage( int $post_id, WP_Post $post ): void { | ||
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { | ||
return; | ||
} | ||
|
||
$post_status = $post->post_status; | ||
if ( 'publish' !== $post_status ) { | ||
return; | ||
} | ||
|
||
// Regular expression to match all remote data blocks present in the post content. | ||
$reg_exp = '/<!--\s{1}wp:remote-data-blocks\/([^\s]+)\s/'; | ||
preg_match_all( $reg_exp, $post->post_content, $matches ); | ||
if ( count( $matches[1] ) === 0 ) { | ||
return; | ||
} | ||
|
||
// Get data source and track usage. | ||
$track_props = [ | ||
'post_status' => $post_status, | ||
'post_type' => $post->post_type, | ||
]; | ||
foreach ( $matches[1] as $match ) { | ||
$data_source_type = ConfigStore::get_data_source_type( 'remote-data-blocks/' . $match ); | ||
if ( ! $data_source_type ) { | ||
continue; | ||
} | ||
|
||
// Calculate stats of each remote data source. | ||
$key = $data_source_type . '_data_source_count'; | ||
$track_props[ $key ] = ( $track_props[ $key ] ?? 0 ) + 1; | ||
$track_props['remote_data_blocks_total_count'] = ( $track_props['remote_data_blocks_total_count'] ?? 0 ) + 1; | ||
} | ||
|
||
$this->record_event( 'blocks_usage_stats', $track_props ); | ||
} | ||
|
||
/** | ||
* Record a telemetry event. | ||
* | ||
* @param string $event_name The name of the event. | ||
* @param array $props The properties to send with the event. | ||
*/ | ||
public function record_event( string $event_name, array $props ): void { | ||
if ( null === $this->telemetry ) { | ||
return; | ||
} | ||
|
||
$this->telemetry->record_event( $event_name, $props ); | ||
} | ||
|
||
/** | ||
* Reset class properties. | ||
*/ | ||
public static function reset(): void { | ||
self::$instance = null; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're inconsistent between the action and the
EVENT_PREFIX
value (remote_data_blocks
vsremotedatablocks
)Any reason we can't make those consistent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the namespacing we've done in the WordPress plugin uses
remote_data_blocks
. However, Tracks is external to WordPress and the event names were already registered months ago usingremotedatablocks
as the prefix. It's a bit painful to change, otherwise I would have done it.