update
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking\AMP_Config_Injector
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Analytics_4
|
||||
* @copyright 2024 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @link https://sitekit.withgoogle.com
|
||||
*/
|
||||
|
||||
namespace Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking;
|
||||
|
||||
/**
|
||||
* Class for injecting JavaScript based on the registered event configurations.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
final class AMP_Config_Injector {
|
||||
|
||||
/**
|
||||
* Creates list of measurement event configurations and javascript to inject.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
* @since 1.121.0 Migrated from the Analytics (UA) namespace.
|
||||
*
|
||||
* @param array $gtag_amp_opt gtag config options for AMP.
|
||||
* @param array $events The map of Event objects, keyed by their unique ID.
|
||||
* @return array Filtered $gtag_amp_opt.
|
||||
*/
|
||||
public function inject_event_configurations( $gtag_amp_opt, $events ) {
|
||||
if ( empty( $events ) ) {
|
||||
return $gtag_amp_opt;
|
||||
}
|
||||
|
||||
if ( ! array_key_exists( 'triggers', $gtag_amp_opt ) ) {
|
||||
$gtag_amp_opt['triggers'] = array();
|
||||
}
|
||||
|
||||
foreach ( $events as $amp_trigger_key => $event ) {
|
||||
$event_config = $event->get_config();
|
||||
|
||||
$amp_trigger = array();
|
||||
if ( 'DOMContentLoaded' === $event_config['on'] ) {
|
||||
$amp_trigger['on'] = 'visible';
|
||||
} else {
|
||||
$amp_trigger['on'] = $event_config['on'];
|
||||
$amp_trigger['selector'] = $event_config['selector'];
|
||||
}
|
||||
|
||||
$amp_trigger['vars'] = array();
|
||||
$amp_trigger['vars']['event_name'] = $event_config['action'];
|
||||
if ( is_array( $event_config['metadata'] ) ) {
|
||||
foreach ( $event_config['metadata'] as $key => $value ) {
|
||||
$amp_trigger['vars'][ $key ] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$gtag_amp_opt['triggers'][ $amp_trigger_key ] = $amp_trigger;
|
||||
}
|
||||
|
||||
return $gtag_amp_opt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking\Event
|
||||
*
|
||||
* @package Google\Site_Kit
|
||||
* @copyright 2024 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @link https://sitekit.withgoogle.com
|
||||
*/
|
||||
|
||||
namespace Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class for representing a single tracking event that Advanced_Tracking tracks.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
* @since 1.121.0 Migrated from the Analytics (UA) namespace.
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
final class Event implements \JsonSerializable {
|
||||
|
||||
/**
|
||||
* The measurement event's configuration.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* Event constructor.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*
|
||||
* @param array $config {
|
||||
* The event's configuration.
|
||||
*
|
||||
* @type string $action Required. The event action / event name to send.
|
||||
* @type string $on Required. The DOM event to send the event for.
|
||||
* @type string $selector Required, unless $on is 'DOMContentLoaded'. The DOM selector on which to listen
|
||||
* to the $on event.
|
||||
* @type array|null $metadata Optional. Associative array of event metadata to send, such as 'event_category',
|
||||
* 'event_label' etc, or null to not send any extra event data.
|
||||
* }
|
||||
* @throws Exception Thrown when config param is undefined.
|
||||
*/
|
||||
public function __construct( $config ) {
|
||||
$this->config = $this->validate_config( $config );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an associative event containing the event attributes.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*
|
||||
* @return array The configuration in JSON-serializable format.
|
||||
*/
|
||||
public function jsonSerialize() {
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the measurement event configuration.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*
|
||||
* @return array The config.
|
||||
*/
|
||||
public function get_config() {
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the configuration keys and value types.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*
|
||||
* @param array $config The event's configuration.
|
||||
* @return array The event's configuration.
|
||||
* @throws Exception Thrown when invalid keys or value type.
|
||||
*/
|
||||
private function validate_config( $config ) {
|
||||
$valid_keys = array(
|
||||
'action',
|
||||
'selector',
|
||||
'on',
|
||||
'metadata',
|
||||
);
|
||||
foreach ( $config as $key => $value ) {
|
||||
if ( ! in_array( $key, $valid_keys, true ) ) {
|
||||
throw new Exception( 'Invalid configuration parameter: ' . $key );
|
||||
}
|
||||
}
|
||||
if ( ! array_key_exists( 'metadata', $config ) ) {
|
||||
$config['metadata'] = null;
|
||||
}
|
||||
if ( array_key_exists( 'on', $config ) && 'DOMContentLoaded' === $config['on'] ) {
|
||||
$config['selector'] = '';
|
||||
}
|
||||
foreach ( $valid_keys as $key ) {
|
||||
if ( ! array_key_exists( $key, $config ) ) {
|
||||
throw new Exception( 'Missed configuration parameter: ' . $key );
|
||||
}
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking\Event_List
|
||||
*
|
||||
* @package Google\Site_Kit
|
||||
* @copyright 2024 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @link https://sitekit.withgoogle.com
|
||||
*/
|
||||
|
||||
namespace Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking;
|
||||
|
||||
/**
|
||||
* Base class representing a tracking event list.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
* @since 1.121.0 Migrated from the Analytics (UA) namespace.
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
abstract class Event_List {
|
||||
|
||||
/**
|
||||
* Container for events.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
* @var array Map of events for this list, keyed by their unique ID.
|
||||
*/
|
||||
private $events = array();
|
||||
|
||||
/**
|
||||
* Adds events or registers WordPress hook callbacks to add events.
|
||||
*
|
||||
* Children classes should extend this to add their events, either generically or by dynamically collecting
|
||||
* metadata through WordPress hooks.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*/
|
||||
abstract public function register();
|
||||
|
||||
/**
|
||||
* Adds a measurement event to the measurement events array.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*
|
||||
* @param Event $event The measurement event object.
|
||||
*/
|
||||
protected function add_event( Event $event ) {
|
||||
$hash = md5( wp_json_encode( $event ) );
|
||||
$this->events[ $hash ] = $event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the measurement events array.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*
|
||||
* @return array The map of events for this list, keyed by their unique ID.
|
||||
*/
|
||||
public function get_events() {
|
||||
return $this->events;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking\Event_List_Registry
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Analytics_4
|
||||
* @copyright 2024 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @link https://sitekit.withgoogle.com
|
||||
*/
|
||||
|
||||
namespace Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking;
|
||||
|
||||
/**
|
||||
* Class for registering third party event lists.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
* @since 1.121.0 Migrated from the Analytics (UA) namespace.
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Event_List_Registry {
|
||||
|
||||
/**
|
||||
* The list of registered event lists.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
* @var Event_List[]
|
||||
*/
|
||||
private $event_lists = array();
|
||||
|
||||
/**
|
||||
* Registers an event list.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*
|
||||
* @param Event_List $event_list The event list to be registered.
|
||||
*/
|
||||
public function register_list( Event_List $event_list ) {
|
||||
$this->event_lists[] = $event_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of registered event lists.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*
|
||||
* @return Event_List[] The list of registered event lists.
|
||||
*/
|
||||
public function get_lists() {
|
||||
return $this->event_lists;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking\Script_Injector
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Analytics_4
|
||||
* @copyright 2024 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
* @link https://sitekit.withgoogle.com
|
||||
*/
|
||||
|
||||
namespace Google\Site_Kit\Modules\Analytics_4\Advanced_Tracking;
|
||||
|
||||
use Google\Site_Kit\Context;
|
||||
use Google\Site_Kit\Core\Assets\Manifest;
|
||||
use Google\Site_Kit\Core\Util\BC_Functions;
|
||||
|
||||
/**
|
||||
* Class for injecting JavaScript based on the registered event configurations.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
* @since 1.121.0 Migrated from the Analytics (UA) namespace.
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
final class Script_Injector {
|
||||
|
||||
/**
|
||||
* Plugin context.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
* @var Context
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*
|
||||
* @param Context $context Plugin context.
|
||||
*/
|
||||
public function __construct( Context $context ) {
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates list of measurement event configurations and javascript to inject.
|
||||
*
|
||||
* @since 1.18.0.
|
||||
*
|
||||
* @param array $events The map of Event objects, keyed by their unique ID.
|
||||
*/
|
||||
public function inject_event_script( $events ) {
|
||||
if ( empty( $events ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
list( $filename ) = Manifest::get( 'analytics-advanced-tracking' );
|
||||
if ( ! $filename ) {
|
||||
// Get file contents of script and add it to the page, injecting event configurations into it.
|
||||
$filename = 'analytics-advanced-tracking.js';
|
||||
|
||||
}
|
||||
|
||||
$script_path = $this->context->path( "dist/assets/js/{$filename}" );
|
||||
|
||||
// phpcs:ignore WordPress.WP.AlternativeFunctions, WordPressVIPMinimum.Performance.FetchingRemoteData
|
||||
$script_content = file_get_contents( $script_path );
|
||||
if ( ! $script_content ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data_var = sprintf(
|
||||
'var _googlesitekitAnalyticsTrackingData = %s;',
|
||||
wp_json_encode( array_values( $events ) )
|
||||
);
|
||||
BC_Functions::wp_print_inline_script_tag( $data_var . "\n" . $script_content );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user