gtag_config_add_optimize_id( $gtag_config ); } ); $print_amp_optimize_experiment = function() { $this->print_amp_optimize_experiment(); }; add_action( 'wp_footer', $print_amp_optimize_experiment ); // For AMP Native and Transitional. add_action( 'amp_post_template_footer', $print_amp_optimize_experiment ); // For AMP Reader. add_filter( // Load amp-experiment component for AMP Reader. 'amp_post_template_data', function( $data ) { return $this->amp_data_load_experiment_component( $data ); } ); // Optimize tag placement logic. add_action( 'template_redirect', $this->get_method_proxy( 'register_tag' ) ); } /** * Checks whether the module is connected. * * A module being connected means that all steps required as part of its activation are completed. * * @since 1.0.0 * * @return bool True if module is connected, false otherwise. */ public function is_connected() { $settings = $this->get_settings()->get(); if ( ! $settings['optimizeID'] ) { return false; } return parent::is_connected(); } /** * Cleans up when the module is deactivated. * * @since 1.0.0 */ public function on_deactivation() { $this->get_settings()->delete(); } /** * Expands gtag config options with optimize ID. * * @since 1.0.0 * * @param array $gtag_config Associative array of gtag config options. * @return array Filtered $gtag_config. */ protected function gtag_config_add_optimize_id( $gtag_config ) { $settings = $this->get_settings()->get(); if ( $settings['optimizeID'] ) { $gtag_config['optimize_id'] = $settings['optimizeID']; } return $gtag_config; } /** * Outputs Optimize experiment script in AMP if opted in. * * @since 1.0.0 */ protected function print_amp_optimize_experiment() { if ( ! $this->context->is_amp() ) { return; } $settings = $this->get_settings()->get(); if ( ! $settings['ampExperimentJSON'] ) { return; } ?> get_settings()->get(); return array( 'optimize_id' => array( 'label' => __( 'Optimize ID', 'google-site-kit' ), 'value' => $settings['optimizeID'], 'debug' => Debug_Data::redact_debug_value( $settings['optimizeID'], 7 ), ), ); } /** * Adds AMP experiment script if opted in. * * @since 1.0.0 * * @param array $data AMP template data. * @return array Filtered $data. */ protected function amp_data_load_experiment_component( $data ) { $settings = $this->get_settings()->get(); if ( $settings['ampExperimentJSON'] ) { $data['amp_component_scripts']['amp-experiment'] = 'https://cdn.ampproject.org/v0/amp-experiment-0.1.js'; } return $data; } /** * Sets up information about the module. * * @since 1.0.0 * * @return array Associative array of module info. */ protected function setup_info() { return array( 'slug' => 'optimize', 'name' => _x( 'Optimize', 'Service name', 'google-site-kit' ), 'description' => __( 'Create free A/B tests that help you drive metric-based design solutions to your site', 'google-site-kit' ), 'order' => 5, 'homepage' => __( 'https://optimize.google.com/optimize/home/', 'google-site-kit' ), 'depends_on' => array( 'analytics' ), ); } /** * Sets up the Google services the module should use. * * This method is invoked once by {@see Module::get_service()} to lazily set up the services when one is requested * for the first time. * * @since 1.0.0 * @since 1.2.0 Now requires Google_Site_Kit_Client instance. * * @param Google_Site_Kit_Client $client Google client instance. * @return array Google services as $identifier => $service_instance pairs. Every $service_instance must be an * instance of Google_Service. */ protected function setup_services( Google_Site_Kit_Client $client ) { return array(); } /** * Sets up the module's settings instance. * * @since 1.2.0 * * @return Module_Settings */ protected function setup_settings() { return new Settings( $this->options ); } /** * Sets up the module's assets to register. * * @since 1.10.0 * * @return Asset[] List of Asset objects. */ protected function setup_assets() { $base_url = $this->context->url( 'dist/assets/' ); return array( new Script( 'googlesitekit-modules-optimize', array( 'src' => $base_url . 'js/googlesitekit-modules-optimize.js', 'dependencies' => array( 'googlesitekit-vendor', 'googlesitekit-api', 'googlesitekit-data', 'googlesitekit-modules', 'googlesitekit-datastore-site', 'googlesitekit-datastore-forms', ), ) ), ); } /** * Registers the Optimize tag. * * @since 1.39.0 */ private function register_tag() { $is_amp = $this->context->is_amp(); $module_settings = $this->get_settings(); $settings = $module_settings->get(); if ( $is_amp ) { return false; } $tag = new Web_Tag( $settings['optimizeID'], self::MODULE_SLUG ); if ( ! $tag->is_tag_blocked() ) { $tag->use_guard( new Tag_Guard( $module_settings ) ); if ( $tag->can_register() ) { $tag->register(); } } } /** * Checks if the current user has access to the current configured service entity. * * @since 1.70.0 * * @return boolean|WP_Error */ public function check_service_entity_access() { // TODO: For Optimize module, there is no API to check service entity access. This is a no-op for now that always returns true. return true; } }