settings = $settings; } /** * Initializes all hooks. * * @since 1.0.0 */ public function register(): void { add_action( 'web_stories_print_analytics', [ $this, 'print_analytics_tag' ] ); } /** * Returns the Google Analytics tracking ID. * * @since 1.0.0 * * @return string Tracking ID. */ public function get_tracking_id(): string { /** * Tracking ID. * * @var string $tracking_id */ $tracking_id = $this->settings->get_setting( $this->settings::SETTING_NAME_TRACKING_ID ); // For some reasons, some sites use the plugin's own tracking ID (as used in the admin) // for their stories. Prevent accidental erroneous tracking in such a case. if ( Tracking::TRACKING_ID === $tracking_id ) { return ''; } return $tracking_id; } /** * Returns the default analytics configuration. * * Note: variables in single quotes will be substituted by . * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * * @see https://github.com/ampproject/amphtml/blob/main/docs/spec/amp-var-substitutions.md * * @param string $tracking_id Tracking ID. * @return array> configuration. */ public function get_default_configuration( string $tracking_id ): array { $config = [ 'vars' => [ 'gtag_id' => $tracking_id, 'config' => [ $tracking_id => [ 'groups' => 'default' ], ], ], 'triggers' => [ // Fired when a story page becomes visible. 'storyProgress' => [ 'on' => 'story-page-visible', 'request' => 'event', 'vars' => [ 'event_name' => 'custom', 'event_action' => 'story_progress', 'event_category' => '${title}', 'event_label' => '${storyPageIndex}', 'event_value' => '${storyProgress}', 'send_to' => $tracking_id, ], ], // Fired when the last page in the story is shown to the user. // This can be used to measure completion rate. 'storyEnd' => [ 'on' => 'story-last-page-visible', 'request' => 'event', 'vars' => [ 'event_name' => 'custom', 'event_action' => 'story_complete', 'event_category' => '${title}', 'event_label' => '${storyPageCount}', 'send_to' => $tracking_id, ], ], // Fired when clicking an element that opens a tooltip ( or ). 'trackFocusState' => [ 'on' => 'story-focus', 'tagName' => 'a', 'request' => 'click ', 'vars' => [ 'event_name' => 'custom', 'event_action' => 'story_focus', 'event_category' => '${title}', 'send_to' => $tracking_id, ], ], // Fired when clicking on a tooltip. 'trackClickThrough' => [ 'on' => 'story-click-through', 'tagName' => 'a', 'request' => 'click ', 'vars' => [ 'event_name' => 'custom', 'event_action' => 'story_click_through', 'event_category' => '${title}', 'send_to' => $tracking_id, ], ], // Fired when opening a drawer or dialog inside a story (e.g. page attachment). 'storyOpen' => [ 'on' => 'story-open', 'request' => 'event', 'vars' => [ 'event_name' => 'custom', 'event_action' => 'story_open', 'event_category' => '${title}', 'send_to' => $tracking_id, ], ], // Fired when closing a drawer or dialog inside a story (e.g. page attachment). 'storyClose' => [ 'on' => 'story-close', 'request' => 'event', 'vars' => [ 'event_name' => 'custom', 'event_action' => 'story_close', 'event_category' => '${title}', 'send_to' => $tracking_id, ], ], // Fired when the user initiates an interaction to mute the audio for the current story. 'audioMuted' => [ 'on' => 'story-audio-muted', 'request' => 'event', 'vars' => [ 'event_name' => 'custom', 'event_action' => 'story_audio_muted', 'event_category' => '${title}', 'send_to' => $tracking_id, ], ], // Fired when the user initiates an interaction to unmute the audio for the current story. 'audioUnmuted' => [ 'on' => 'story-audio-unmuted', 'request' => 'event', 'vars' => [ 'event_name' => 'custom', 'event_action' => 'story_audio_unmuted', 'event_category' => '${title}', 'send_to' => $tracking_id, ], ], // Fired when a page attachment is opened by the user. 'pageAttachmentEnter' => [ 'on' => 'story-page-attachment-enter', 'request' => 'event', 'vars' => [ 'event_name' => 'custom', 'event_action' => 'story_page_attachment_enter', 'event_category' => '${title}', 'send_to' => $tracking_id, ], ], // Fired when a page attachment is dismissed by the user. 'pageAttachmentExit' => [ 'on' => 'story-page-attachment-exit', 'request' => 'event', 'vars' => [ 'event_name' => 'custom', 'event_action' => 'story_page_attachment_exit', 'event_category' => '${title}', 'send_to' => $tracking_id, ], ], ], ]; /** * Filters the Web Stories Google Analytics configuration. * * Only used when not using , which is the default. * * @param array $config Analytics configuration. */ return apply_filters( 'web_stories_analytics_configuration', $config ); } /** * Prints the analytics tag for single stories. * * @since 1.0.0 */ public function print_analytics_tag(): void { $tracking_id = $this->get_tracking_id(); if ( ! $tracking_id ) { return; } if ( $this->settings->get_setting( $this->settings::SETTING_NAME_USING_LEGACY_ANALYTICS ) ) { $this->print_amp_analytics_tag( $tracking_id ); } else { $this->print_amp_story_auto_analytics_tag( $tracking_id ); } } /** * Prints the legacy tag for single stories. * * @since 1.12.0 * * @param string $tracking_id Tracking ID. */ private function print_amp_analytics_tag( string $tracking_id ): void { ?> tag for single stories. * * @since 1.12.0 * * @param string $tracking_id Tracking ID. */ private function print_amp_story_auto_analytics_tag( string $tracking_id ): void { ?>