This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathanalytics-output.php
121 lines (106 loc) · 3.42 KB
/
analytics-output.php
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
117
118
119
120
121
<?php
/**
* Adds an async attribute to specified scripts provided by
* `wp_enqueue`.
* Additions for scripts with async can be added using
* the `benenson_add_async_handlers` filter hook.
*
* @since 1.0.0
* @param string $tag `<script src=...` tag from wp_enqueue.
* @param string $handle Handle of the current enqueued script being used.
* @return string
*/
if ( ! function_exists( 'benenson_add_script_async' ) ) {
function benenson_add_script_async( $tag, $handle ) {
/**
* Only adds async to scripts with one of the following script
* handle names as defined by their wp_enqueue/register_script
* handler.
*
* @since 1.0.0
*
* @param array $handlers List of handlers to add `async`.
*/
$allowed_handles = apply_filters( 'benenson_add_async_handlers', [
'analytics-ga-script',
] );
if ( ! in_array( $handle, $allowed_handles, true ) ) {
return $tag;
}
return str_replace( ' src', ' async src', $tag );
}
}
add_filter( 'script_loader_tag', 'benenson_add_script_async', 10, 2 );
/**
* Enqueues Google Analytics script.
*
* @since 1.0.0
* @return void
*/
if ( ! function_exists( 'benenson_enqueue_ga' ) ) {
function benenson_enqueue_ga() {
$google_analytics = benenson_get_option( '_analytics_ga', '' );
if ( ! empty( $google_analytics ) ) {
wp_enqueue_script( 'analytics-ga-script', "https://www.googletagmanager.com/gtag/js?id={$google_analytics}", [], '1.0.0', false );
}
}
}
add_action( 'wp_enqueue_scripts', 'benenson_enqueue_ga' );
/**
* Adds scripts to `<head>` via `wp_head()` for
* Google Analytics and Google Tag Manager.
*
* @since 1.0.0
* @return void
*/
if ( ! function_exists( 'benenson_add_wp_head_scripts' ) ) {
function benenson_add_wp_head_scripts() {
$google_tag_manager = benenson_get_option( '_analytics_gtm' );
$google_analytics = benenson_get_option( '_analytics_ga' );
if ( ! empty( $google_tag_manager ) ) :
?>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','<?php echo esc_html( $google_tag_manager ); ?>');</script>
<!-- End Google Tag Manager -->
<?php
endif;
if ( ! empty( $google_analytics ) ) :
?>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
};
gtag('js', new Date());
gtag('config', '<?php echo esc_attr( $google_analytics ); ?>');
</script>
<?php
endif;
}
}
add_action( 'wp_head', 'benenson_add_wp_head_scripts' );
/**
* Adds fallback for Google Analytics to the `<body>`.
*
* @since 1.0.0
* @return void
*/
if ( ! function_exists( 'benenson_analytics_fallback' ) ) {
function benenson_analytics_fallback() {
$google_tag_manager = benenson_get_option( '_analytics_gtm' );
if ( ! empty( $google_tag_manager ) ) :
?>
<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=<?php echo esc_html( $google_tag_manager ); ?>" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->
<?php
endif;
}
}
add_action( 'benenson_after_body_open_tag', 'benenson_analytics_fallback' );