( 'googlesitekit_ads_init_tag' ) || did_action( 'googlesitekit_analytics-4_init_tag' ) ) || ! $this->conversion_tracking_settings->is_conversion_tracking_enabled() ) { return; } $active_providers = $this->get_active_providers(); array_walk( $active_providers, function ( Conversion_Events_Provider $active_provider ) { $script_asset = $active_provider->register_script(); if ( $script_asset instanceof Script ) { $script_asset->enqueue(); } } ); $gtag_event = ' window._googlesitekit = window._googlesitekit || {}; window._googlesitekit.throttledEvents = []; window._googlesitekit.gtagEvent = (name, data) => { var key = JSON.stringify( { name, data } ); if ( !! window._googlesitekit.throttledEvents[ key ] ) { return; } window._googlesitekit.throttledEvents[ key ] = true; setTimeout( () => { delete window._googlesitekit.throttledEvents[ key ]; }, 5 ); gtag( "event", name, { ...data, event_source: "site-kit" } ); }; '; if ( function_exists( 'edd_get_currency' ) ) { $gtag_event .= "window._googlesitekit.easyDigitalDownloadsCurrency = '" . edd_get_currency() . "';"; } if ( Feature_Flags::enabled( 'gtagUserData' ) ) { $gtag_event .= 'window._googlesitekit.gtagUserData = true;'; } wp_add_inline_script( GTag::HANDLE, preg_replace( '/\s+/', ' ', $gtag_event ) ); } /** * Gets the instances of active conversion event providers. * * @since 1.126.0 * * @return array List of active Conversion_Events_Provider instances. * @throws LogicException Thrown if an invalid conversion event provider class name is provided. */ public function get_active_providers() { $active_providers = array(); foreach ( self::$providers as $provider_slug => $provider_class ) { if ( ! is_string( $provider_class ) || ! $provider_class ) { throw new LogicException( sprintf( /* translators: %s: provider slug */ __( 'A conversion event provider class name is required to instantiate a provider: %s', 'google-site-kit' ), $provider_slug ) ); } if ( ! class_exists( $provider_class ) ) { throw new LogicException( sprintf( /* translators: %s: provider classname */ __( "The '%s' class does not exist", 'google-site-kit' ), $provider_class ) ); } if ( ! is_subclass_of( $provider_class, Conversion_Events_Provider::class ) ) { throw new LogicException( sprintf( /* translators: 1: provider classname 2: Conversion_Events_Provider classname */ __( "The '%1\$s' class must extend the base conversion event provider class: %2\$s", 'google-site-kit' ), $provider_class, Conversion_Events_Provider::class ) ); } $instance = new $provider_class( $this->context ); if ( $instance->is_active() ) { $active_providers[ $provider_slug ] = $instance; } } return $active_providers; } /** * Returns events supported by active providers from the conversion tracking infrastructure. * * @since 1.163.0 Moved this method here from the Ads class. * * @return array Array of supported conversion events, or empty array. */ public function get_supported_conversion_events() { $providers = $this->get_active_providers(); if ( empty( $providers ) ) { return array(); } $events = array(); foreach ( $providers as $provider ) { $events = array_merge( $events, array_values( $provider->get_event_names() ) ); } return array_unique( $events ); } /** * Gets an array of internal feature metrics. * * @since 1.163.0 * * @return array */ public function get_feature_metrics() { return array( 'conversion_tracking_enabled' => $this->conversion_tracking_settings->is_conversion_tracking_enabled(), 'conversion_tracking_providers' => array_keys( $this->get_active_providers() ), 'conversion_tracking_events' => $this->get_supported_conversion_events(), ); } }