Automattic\WooCommerce\Blocks\Domain\Services

GoogleAnalytics::enqueue_scripts()publicWC 1.0

Enqueue the Google Tag Manager script if prerequisites are met.

Метод класса: GoogleAnalytics{}

Хуки из метода

Возвращает

null. Ничего.

Использование

$GoogleAnalytics = new GoogleAnalytics();
$GoogleAnalytics->enqueue_scripts();

Код GoogleAnalytics::enqueue_scripts() WC 7.1.0

public function enqueue_scripts() {
	$settings = $this->get_google_analytics_settings();
	$prefix   = strstr( strtoupper( $settings['ga_id'] ), '-', true );

	// Require tracking to be enabled with a valid GA ID.
	if ( ! in_array( $prefix, [ 'G', 'GT' ], true ) ) {
		return;
	}

	/**
	 * Filter to disable Google Analytics tracking.
	 *
	 * @internal Matches filter name in GA extension.
	 * @since 4.9.0
	 *
	 * @param boolean $disable_tracking If true, tracking will be disabled.
	 */
	if ( apply_filters( 'woocommerce_ga_disable_tracking', ! wc_string_to_bool( $settings['ga_event_tracking_enabled'] ) ) ) {
		return;
	}

	if ( ! wp_script_is( 'google-tag-manager', 'registered' ) ) {
		// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
		wp_register_script( 'google-tag-manager', 'https://www.googletagmanager.com/gtag/js?id=' . $settings['ga_id'], [], null, false );
		wp_add_inline_script(
			'google-tag-manager',
			"
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '" . esc_js( $settings['ga_id'] ) . "', { 'send_page_view': false });"
		);
	}
	wp_enqueue_script( 'wc-blocks-google-analytics' );
}