Skip to content

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 4 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions inc/Editor/BlockManagement/BlockRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
defined( 'ABSPATH' ) || exit();

use RemoteDataBlocks\Editor\Assets\Assets;
use RemoteDataBlocks\Telemetry\TracksTelemetry;
use RemoteDataBlocks\Telemetry\Telemetry;
use RemoteDataBlocks\Editor\BlockPatterns\BlockPatterns;
use RemoteDataBlocks\REST\RemoteDataController;
use function register_block_type;
Expand Down Expand Up @@ -66,7 +66,7 @@ public static function register_container_blocks(): void {
wp_localize_script( $script_handle, 'REMOTE_DATA_BLOCKS', [
'config' => $all_remote_block_configs,
'rest_url' => RemoteDataController::get_url(),
'tracks_global_properties' => TracksTelemetry::get_global_properties(),
'tracks_global_properties' => Telemetry::get_global_properties(),
] );
}
}
Expand Down
7 changes: 4 additions & 3 deletions inc/Telemetry/DataSourceTelemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace RemoteDataBlocks\Telemetry;

use RemoteDataBlocks\Telemetry\TracksTelemetry;
use RemoteDataBlocks\Store\DataSource\DataSourceConfigManager;
use function do_action;

defined( 'ABSPATH' ) || exit();

class DataSourceTelemetry {
Expand All @@ -23,7 +24,7 @@ private static function get_interaction_track_props( array $config ): array {
}

private static function track_interaction( array $config, string $action ): void {
TracksTelemetry::record_event( self::DATA_SOURCE_INTERACTION_EVENT_NAME, array_merge( [
do_action( 'remote_data_blocks_track_event', self::DATA_SOURCE_INTERACTION_EVENT_NAME, array_merge( [
'data_source_type' => $config['service'],
'action' => $action,
], self::get_interaction_track_props( $config ) ) );
Expand Down Expand Up @@ -61,7 +62,7 @@ function ( $config ) {
}
) );

TracksTelemetry::record_event( self::DATA_SOURCE_VIEW_EVENT_NAME, [
do_action( 'remote_data_blocks_track_event', self::DATA_SOURCE_VIEW_EVENT_NAME, [
'total_data_sources_count' => count( $configs ),
'code_configured_data_sources_count' => $code_configured_count,
'ui_configured_data_sources_count' => $storage_configured_count,
Expand Down
79 changes: 0 additions & 79 deletions inc/Telemetry/EnvironmentConfig.php

This file was deleted.

151 changes: 151 additions & 0 deletions inc/Telemetry/Telemetry.php
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 );
Copy link
Contributor

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 vs remotedatablocks)

Any reason we can't make those consistent?

Copy link
Member Author

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 using remotedatablocks as the prefix. It's a bit painful to change, otherwise I would have done it.

}

/**
* 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;
}
}
Loading
Loading