GoogleAnalytics.php
3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Automattic\WooCommerce\Blocks\Domain\Services;
use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
/**
* Service class to integrate Blocks with the Google Analytics extension,
*/
class GoogleAnalytics {
/**
* Instance of the asset API.
*
* @var AssetApi
*/
protected $asset_api;
/**
* Constructor.
*
* @param AssetApi $asset_api Instance of the asset API.
*/
public function __construct( AssetApi $asset_api ) {
$this->asset_api = $asset_api;
$this->init();
}
/**
* Hook into WP.
*/
protected function init() {
add_action( 'init', array( $this, 'register_assets' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_filter( 'script_loader_tag', array( $this, 'async_script_loader_tags' ), 10, 3 );
}
/**
* Register scripts.
*/
public function register_assets() {
$this->asset_api->register_script( 'wc-blocks-google-analytics', 'build/wc-blocks-google-analytics.js', [ 'google-tag-manager' ] );
}
/**
* Enqueue the Google Tag Manager script if prerequisites are met.
*/
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' );
}
/**
* Get settings from the GA integration extension.
*
* @return array
*/
private function get_google_analytics_settings() {
return wp_parse_args(
get_option( 'woocommerce_google_analytics_settings' ),
[
'ga_id' => '',
'ga_event_tracking_enabled' => 'no',
]
);
}
/**
* Add async to script tags with defined handles.
*
* @param string $tag HTML for the script tag.
* @param string $handle Handle of script.
* @param string $src Src of script.
* @return string
*/
public function async_script_loader_tags( $tag, $handle, $src ) {
if ( ! in_array( $handle, array( 'google-tag-manager' ), true ) ) {
return $tag;
}
// If script was output manually in wp_head, abort.
if ( did_action( 'woocommerce_gtag_snippet' ) ) {
return '';
}
return str_replace( '<script src', '<script async src', $tag );
}
}