GoogleAnalytics.php
5.95 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
namespace AIOSEO\Plugin\Common\Main;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Utils;
/**
* Outputs the Google Analytics to the head.
*
* @since 4.0.0
*/
class GoogleAnalytics {
/**
* Retrieves the script to output.
*
* @since 4.0.0
*/
public function canShowScript() {
$pluginUpgrader = new Utils\PluginUpgraderSilentAjax();
$miLite = $pluginUpgrader->pluginSlugs['miLite'];
$miPro = $pluginUpgrader->pluginSlugs['miPro'];
$emLite = $pluginUpgrader->pluginSlugs['emLite'];
$emPro = $pluginUpgrader->pluginSlugs['emPro'];
$activePlugins = get_option( 'active_plugins' );
if (
in_array( $miLite, $activePlugins, true ) ||
in_array( $miPro, $activePlugins, true ) ||
in_array( $emLite, $activePlugins, true ) ||
in_array( $emPro, $activePlugins, true )
) {
return false;
}
$googleAnalyticsId = aioseo()->options->deprecated->webmasterTools->googleAnalytics->id;
$gtmContainerId = aioseo()->options->deprecated->webmasterTools->googleAnalytics->gtmContainerId;
if (
in_array( 'googleAnalytics', aioseo()->internalOptions->internal->deprecatedOptions, true ) &&
! $googleAnalyticsId &&
! $gtmContainerId
) {
return false;
}
return ! $this->userIsExcluded();
}
/**
* Checks if the user is excluded from tracking.
*
* @since 4.0.0
*
* @return boolean True if the user is excluded from tracking.
*/
public function userIsExcluded() {
// Check whether we should exclude tracking for specific user roles.
$excludeUsers = aioseo()->options->deprecated->webmasterTools->googleAnalytics->excludeUsers;
if (
aioseo()->options->deprecated->webmasterTools->googleAnalytics->advanced &&
! empty( $excludeUsers ) &&
is_user_logged_in()
) {
$currentUser = wp_get_current_user();
if ( ! empty( $currentUser ) ) {
$intersect = array_intersect( $excludeUsers, $currentUser->roles );
if ( ! empty( $intersect ) ) {
return true;
}
}
}
return false;
}
/**
* Get analytics options.
*
* @since 4.0.0
*
* @return array An array of options.
*/
public function getOptions() {
$allowLinker = '';
$cookieDomain = '';
$domain = '';
$additionalDomains = '';
$domainList = [];
$advancedOptions = aioseo()->options->deprecated->webmasterTools->googleAnalytics->advanced;
if ( $advancedOptions ) {
$trackingDomain = aioseo()->options->deprecated->webmasterTools->googleAnalytics->trackingDomain;
$cookieDomain = ! empty( $trackingDomain ) ? aioseo()->helpers->sanitizeDomain( $trackingDomain ) : '';
if ( aioseo()->options->deprecated->webmasterTools->googleAnalytics->multipleDomains ) {
$allowLinker = '\'allowLinker\': true';
$optionDomains = aioseo()->options->deprecated->webmasterTools->googleAnalytics->additionalDomains;
if ( ! empty( $optionDomains ) ) {
$additionalDomains = trim( $optionDomains );
$additionalDomains = preg_split( '/[\s,]+/', $additionalDomains );
if ( ! empty( $additionalDomains ) ) {
foreach ( $additionalDomains as $d ) {
$d = aioseo()->helpers->sanitizeDomain( $d );
if ( ! empty( $d ) ) {
$domainList[] = $d;
}
}
}
}
}
}
if ( ! empty( $cookieDomain ) ) {
$cookieDomain = esc_js( $cookieDomain );
$cookieDomain = '\'cookieDomain\': \'' . $cookieDomain . '\'';
}
if ( empty( $cookieDomain ) ) {
$domain = ', \'auto\'';
}
$options = [];
if ( ! empty( $domainList ) ) {
$options[] = [
'require',
'linker'
];
$options[] = [
'linker:autoLink',
$domainList
];
}
if ( $advancedOptions ) {
if ( aioseo()->options->deprecated->webmasterTools->googleAnalytics->displayAdvertiserTracking ) {
$options[] = [
'require',
'displayfeatures'
];
}
if ( aioseo()->options->deprecated->webmasterTools->googleAnalytics->enhancedEcommerce ) {
$options[] = [
'require',
'ec'
];
}
if ( aioseo()->options->deprecated->webmasterTools->googleAnalytics->enhancedLinkAttribution ) {
$options[] = [
'require',
'linkid',
'linkid.js'
];
}
if ( aioseo()->options->deprecated->webmasterTools->googleAnalytics->anonymizeIp ) {
$options[] = [
'set',
'anonymizeIp',
true
];
}
if ( aioseo()->options->deprecated->webmasterTools->googleAnalytics->trackOutboundLinks ) {
$options[] = [
'require',
'outboundLinkTracker'
];
}
}
$jsOptions = [];
if ( ! empty( $cookieDomain ) ) {
$jsOptions[] = $cookieDomain;
}
if ( ! empty( $allowLinker ) ) {
$jsOptions[] = $allowLinker;
}
$jsOptions = empty( $jsOptions )
? ''
: ', { ' . implode( ',', $jsOptions ) . ' } ';
return [
'options' => $options,
'domain' => $domain,
'jsOptions' => $jsOptions
];
}
/**
* Retrieve any attributes needed for the script tag.
*
* @since 4.0.0
*
* @return string The attributes as a string.
*/
public function getScriptAttributes() {
return ' ' . trim( preg_replace( '/\s+/', ' ', apply_filters( 'aioseo_ga_attributes', '' ) ) );
}
/**
* Get the URL for autotrack.js
*
* @since 4.0.0
*
* @return string The autotrack.js URL.
*/
public function autoTrackUrl() {
return apply_filters( 'aioseo_google_autotrack', plugin_dir_url( AIOSEO_FILE ) . 'app/Common/Assets/js/autotrack.js' );
}
/**
* Check if autotrack JS should be included.
*
* @since 4.0.0
*
* @return boolean True if so, false if not.
*/
public function autoTrack() {
if (
in_array( 'googleAnalytics', aioseo()->internalOptions->internal->deprecatedOptions, true ) &&
! aioseo()->options->deprecated->webmasterTools->googleAnalytics->id
) {
return false;
}
if ( ! aioseo()->options->deprecated->webmasterTools->googleAnalytics->advanced ) {
return false;
}
if (
aioseo()->options->deprecated->webmasterTools->googleAnalytics->trackOutboundLinks
) {
return true;
}
return false;
}
}