assets = $assets; $this->experiments = $experiments; $this->site_kit = $site_kit; $this->settings = $settings; $this->preferences = $preferences; $this->woocommerce = $woocommerce; } /** * Initializes tracking. * * Registers the setting in WordPress. * * @since 1.0.0 */ public function register(): void { // By not passing an actual script src we can print only the inline script. $this->assets->register_script( self::SCRIPT_HANDLE, false, [], WEBSTORIES_VERSION, false ); wp_add_inline_script( self::SCRIPT_HANDLE, 'window.webStoriesTrackingSettings = ' . wp_json_encode( $this->get_settings() ) . ';' ); } /** * Get the action to use for registering the service. * * @since 1.6.0 * * @return string Registration action to use. */ public static function get_registration_action(): string { return 'admin_init'; } /** * Returns tracking settings to pass to the inline script. * * @since 1.0.0 * * @return array|bool|string> Tracking settings. */ public function get_settings(): array { return [ 'trackingAllowed' => $this->is_active(), 'trackingId' => self::TRACKING_ID, 'trackingIdGA4' => self::TRACKING_ID_GA4, // This doesn't seem to be fully working for web properties. // So we send it as both app_version and a user property. // See https://support.google.com/analytics/answer/9268042. 'appVersion' => WEBSTORIES_VERSION, 'userProperties' => $this->get_user_properties(), ]; } /** * Is tracking active for the current user? * * @return bool True if tracking enabled, and False if not. */ public function is_active(): bool { return (bool) $this->preferences->get_preference( get_current_user_id(), Preferences::OPTIN_META_KEY ); } /** * Returns a list of user properties. * * @since 1.4.0 * * @return array User properties. */ private function get_user_properties(): array { /** * Current user. * * @var null|WP_User $current_user */ $current_user = wp_get_current_user(); $roles = $current_user instanceof WP_User ? $current_user->roles : []; $role = ! empty( $roles ) && \is_array( $roles ) ? array_shift( $roles ) : ''; $experiments = implode( ',', $this->experiments->get_enabled_experiments() ); $active_plugins = []; $woocommerce_status = $this->woocommerce->get_plugin_status(); if ( $woocommerce_status['active'] ) { $active_plugins[] = 'woocommerce'; } $site_kit_status = $this->site_kit->get_plugin_status(); $analytics = $site_kit_status['analyticsActive'] ? 'google-site-kit' : ! empty( $this->settings->get_setting( $this->settings::SETTING_NAME_TRACKING_ID ) ); if ( $site_kit_status['active'] ) { $active_plugins[] = 'google-site-kit'; } /** * Ad network type. * * @var string $ad_network */ $ad_network = $this->settings->get_setting( $this->settings::SETTING_NAME_AD_NETWORK, 'none' ); return [ 'siteLocale' => get_locale(), 'userLocale' => get_user_locale(), 'userRole' => $role, 'enabledExperiments' => $experiments, 'wpVersion' => (string) get_bloginfo( 'version' ), 'phpVersion' => (string) PHP_VERSION, 'isMultisite' => (int) is_multisite(), 'serverEnvironment' => wp_get_environment_type(), 'adNetwork' => $ad_network, 'analytics' => $analytics, 'activePlugins' => implode( ',', $active_plugins ), // This doesn't seem to be fully working for web properties. // So we send it as both app_version and a user property. // See https://support.google.com/analytics/answer/9268042. 'pluginVersion' => WEBSTORIES_VERSION, ]; } }