1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tracking analytics.js class. |
4
|
|
|
* |
5
|
|
|
* @since 6.0.0 |
6
|
|
|
* |
7
|
|
|
* @package MonsterInsights |
8
|
|
|
* @author Chris Christoff |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
// Exit if accessed directly |
12
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
13
|
|
|
exit; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
class MonsterInsights_Tracking_Analytics extends MonsterInsights_Tracking_Abstract { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Holds the name of the tracking type. |
20
|
|
|
* |
21
|
|
|
* @since 6.0.0 |
22
|
|
|
* @access public |
23
|
|
|
* |
24
|
|
|
* @var string $name Name of the tracking type. |
25
|
|
|
*/ |
26
|
|
|
public $name = 'analytics'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Version of the tracking class. |
30
|
|
|
* |
31
|
|
|
* @since 6.0.0 |
32
|
|
|
* @access public |
33
|
|
|
* |
34
|
|
|
* @var string $version Version of the tracking class. |
35
|
|
|
*/ |
36
|
|
|
public $version = '1.0.0'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Primary class constructor. |
40
|
|
|
* |
41
|
|
|
* @since 6.0.0 |
42
|
|
|
* @access public |
43
|
|
|
*/ |
44
|
|
|
public function __construct() { |
45
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get frontend tracking options. |
50
|
|
|
* |
51
|
|
|
* This function is used to return an array of parameters |
52
|
|
|
* for the frontend_output() function to output. These are |
53
|
|
|
* generally dimensions and turned on GA features. |
54
|
|
|
* |
55
|
|
|
* @since 6.0.0 |
56
|
|
|
* @access public |
57
|
|
|
* |
58
|
|
|
* @return array Array of the options to use. |
59
|
|
|
*/ |
60
|
|
|
public function frontend_tracking_options( ) { |
61
|
|
|
global $wp_query; |
62
|
|
|
$options = array(); |
63
|
|
|
|
64
|
|
|
$ua_code = monsterinsights_get_ua_to_output(); |
65
|
|
|
if ( empty( $ua_code ) ) { |
66
|
|
|
return $options; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$track_user = monsterinsights_track_user(); |
70
|
|
|
|
71
|
|
|
if ( ! $track_user ) { |
72
|
|
|
$options['create'] = "'create', '" . esc_js( $ua_code ) . "', '" . esc_js( 'auto' ) . "'"; |
73
|
|
|
$options['forceSSL'] = "'set', 'forceSSL', true"; |
74
|
|
|
$options['send'] = "'send','pageview'"; |
75
|
|
|
return $options; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$domain = esc_attr( monsterinsights_get_option( 'subdomain_tracking', 'auto' ) ); |
79
|
|
|
|
80
|
|
|
$cross_domains = monsterinsights_get_option( 'cross_domains', array() ); |
81
|
|
|
$allow_anchor = monsterinsights_get_option( 'allow_anchor', false ); |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
$create = array(); |
85
|
|
|
if ( $allow_anchor ) { |
86
|
|
|
$create['allowAnchor'] = true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ( is_array( $cross_domains ) && ! empty( $cross_domains ) ) { |
|
|
|
|
90
|
|
|
$create['allowLinker'] = true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ( class_exists( 'MonsterInsights_AMP' ) ) { |
94
|
|
|
$create['useAmpClientId'] = true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$create = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_create', $create ); |
98
|
|
|
|
99
|
|
|
if ( $create && ! empty( $create ) && is_array( $create ) ) { |
100
|
|
|
$create = json_encode( $create ); |
101
|
|
|
$create = str_replace( '"', "'", $create ); |
102
|
|
|
$options['create'] = "'create', '" . esc_js( $ua_code ). "', '" . esc_js( $domain ) . "', " . $create; |
103
|
|
|
} else { |
104
|
|
|
$options['create'] = "'create', '" . esc_js( $ua_code ) . "', '" . esc_js( $domain ) . "'"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$options['forceSSL'] = "'set', 'forceSSL', true"; |
108
|
|
|
|
109
|
|
|
$code = monsterinsights_get_option( 'custom_code', false ); |
110
|
|
|
if ( ! empty( $code ) ) { |
111
|
|
|
// Add custom code to the view |
112
|
|
|
$options['custom_code'] = array( |
113
|
|
|
'type' => 'custom_code', |
114
|
|
|
'value' => stripslashes( $code ), |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Anonymous data |
119
|
|
|
if ( monsterinsights_get_option( 'anonymize_ips', false ) ) { |
120
|
|
|
$options['anonymize_ips'] = "'set', 'anonymizeIp', true"; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$options = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_before_scripts', $options ); |
124
|
|
|
|
125
|
|
|
// add demographics |
126
|
|
|
if ( monsterinsights_get_option( 'demographics', false ) ) { |
127
|
|
|
$options['demographics'] = "'require', 'displayfeatures'"; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Add Enhanced link attribution. |
131
|
|
|
if ( monsterinsights_get_option( 'link_attribution', false ) ) { |
132
|
|
|
$options['enhanced_link_attribution'] = "'require', 'linkid', 'linkid.js'"; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Add cross-domain tracking. |
136
|
|
|
if ( is_array( $cross_domains ) && ! empty( $cross_domains ) ) { |
137
|
|
|
$options['cross_domain_tracking'] = "'require', 'linker'"; |
138
|
|
|
$cross_domains_strings = array(); |
139
|
|
|
foreach ( $cross_domains as $cross_domain ) { |
140
|
|
|
if ( ! isset( $cross_domain['domain'] ) ) { |
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
$cross_domains_strings[] = '\'' . $cross_domain['domain'] . '\''; |
144
|
|
|
} |
145
|
|
|
if ( ! empty( $cross_domains_strings ) ) { |
146
|
|
|
$cross_domains_strings = implode( ',', $cross_domains_strings ); |
147
|
|
|
$options['cross_domains'] = "'linker:autoLink', [$cross_domains_strings]"; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$options = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_before_pageview', $options ); |
152
|
|
|
$options = apply_filters( 'monsterinsights_frontend_tracking_options_before_pageview', $options, $this->name, $this->version ); |
153
|
|
|
|
154
|
|
|
if ( is_404() ) { |
155
|
|
|
if ( monsterinsights_get_option( 'hash_tracking', false ) ) { |
156
|
|
|
$options['send'] = "'send','pageview','/404.html?page=' + document.location.pathname + document.location.search + location.hash + '&from=' + document.referrer"; |
157
|
|
|
} else { |
158
|
|
|
$options['send'] = "'send','pageview','/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer"; |
159
|
|
|
} |
160
|
|
|
} else if ( $wp_query->is_search ) { |
161
|
|
|
$pushstr = "'send','pageview','/?s="; |
162
|
|
|
if ( (int) $wp_query->found_posts === 0 ) { |
163
|
|
|
$options['send'] = $pushstr . 'no-results:' . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=no-results'"; |
164
|
|
|
} else if ( (int) $wp_query->found_posts === 1 ) { |
165
|
|
|
$options['send'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=1-result'"; |
166
|
|
|
} else if ( $wp_query->found_posts > 1 && $wp_query->found_posts < 6 ) { |
167
|
|
|
$options['send'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=2-5-results'"; |
168
|
|
|
} else { |
169
|
|
|
$options['send'] = $pushstr . rawurlencode( $wp_query->query_vars['s'] ) . "&cat=plus-5-results'"; |
170
|
|
|
} |
171
|
|
|
} else if ( monsterinsights_get_option( 'hash_tracking', false ) ) { |
172
|
|
|
$options['send'] = "'send','pageview', location.pathname + location.search + location.hash"; |
173
|
|
|
} else { |
174
|
|
|
$options['send'] = "'send','pageview'"; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$options = apply_filters( 'monsterinsights_frontend_tracking_options_analytics_end', $options ); |
178
|
|
|
return $options; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get frontend output. |
183
|
|
|
* |
184
|
|
|
* This function is used to return the Javascript |
185
|
|
|
* to output in the head of the page for the given |
186
|
|
|
* tracking method. |
187
|
|
|
* |
188
|
|
|
* @since 6.0.0 |
189
|
|
|
* @access public |
190
|
|
|
* |
191
|
|
|
* @return string Javascript to output. |
192
|
|
|
*/ |
193
|
|
|
public function frontend_output( ) { |
194
|
|
|
$options = $this->frontend_tracking_options(); |
195
|
|
|
$src = apply_filters( 'monsterinsights_frontend_output_analytics_src', '//www.google-analytics.com/analytics.js' ); |
196
|
|
|
$compat_mode = monsterinsights_get_option( 'gatracker_compatibility_mode', false ); |
197
|
|
|
$compat = $compat_mode ? 'window.ga = __gaTracker;' : ''; |
198
|
|
|
$track_user = monsterinsights_track_user(); |
199
|
|
|
$ua = monsterinsights_get_ua(); |
200
|
|
|
$output = ''; |
201
|
|
|
$reason = ''; |
202
|
|
|
$attr_string = monsterinsights_get_frontend_analytics_script_atts(); |
203
|
|
|
ob_start(); |
204
|
|
|
?> |
205
|
|
|
<!-- This site uses the Google Analytics by MonsterInsights plugin v<?php echo MONSTERINSIGHTS_VERSION; ?> - Using Analytics tracking - https://www.monsterinsights.com/ --> |
206
|
|
|
<?php if ( ! $track_user ) { |
207
|
|
|
if ( empty( $ua ) ) { |
208
|
|
|
$reason = __( 'Note: MonsterInsights is not currently configured on this site. The site owner needs to authenticate with Google Analytics in the MonsterInsights settings panel.', 'google-analytics-for-wordpress' ); |
209
|
|
|
$output .= '<!-- ' . esc_html( $reason ) . ' -->' . PHP_EOL; |
210
|
|
|
} else if ( current_user_can( 'monsterinsights_save_settings' ) ) { |
211
|
|
|
$reason = __( 'Note: MonsterInsights does not track you as a logged-in site administrator to prevent site owners from accidentally skewing their own Google Analytics data.'. PHP_EOL . 'If you are testing Google Analytics code, please do so either logged out or in the private browsing/incognito mode of your web browser.', 'google-analytics-for-wordpress' ); |
212
|
|
|
$output .= '<!-- ' . esc_html( $reason ) . ' -->' . PHP_EOL; |
213
|
|
|
} else { |
214
|
|
|
$reason = __( 'Note: The site owner has disabled Google Analytics tracking for your user role.', 'google-analytics-for-wordpress' ); |
215
|
|
|
$output .= '<!-- ' . esc_html( $reason ) . ' -->' . PHP_EOL; |
216
|
|
|
} |
217
|
|
|
echo $output; |
218
|
|
|
} ?> |
219
|
|
|
<?php if ( $ua ) { ?> |
220
|
|
|
<script<?php echo $attr_string;?>> |
221
|
|
|
(window.gaDevIds=window.gaDevIds||[]).push("dZGIzZG"); |
222
|
|
|
var mi_version = '<?php echo MONSTERINSIGHTS_VERSION; ?>'; |
223
|
|
|
var mi_track_user = <?php echo ( $track_user ? 'true' : 'false' ); ?>; |
224
|
|
|
var mi_no_track_reason = <?php echo ( $reason ? "'" . esc_js( $reason) . "'": "''" ); ?>; |
225
|
|
|
<?php do_action( 'monsterinsights_tracking_analytics_frontend_output_after_mi_track_user' ); ?> |
226
|
|
|
|
227
|
|
|
<?php if ( $this->should_do_optout() ) { ?> |
228
|
|
|
var disableStr = 'ga-disable-<?php echo monsterinsights_get_ua(); ?>'; |
229
|
|
|
|
230
|
|
|
/* Function to detect opted out users */ |
231
|
|
|
function __gaTrackerIsOptedOut() { |
232
|
|
|
return document.cookie.indexOf(disableStr + '=true') > -1; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/* Disable tracking if the opt-out cookie exists. */ |
236
|
|
|
if ( __gaTrackerIsOptedOut() ) { |
237
|
|
|
window[disableStr] = true; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/* Opt-out function */ |
241
|
|
|
function __gaTrackerOptout() { |
242
|
|
|
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/'; |
243
|
|
|
window[disableStr] = true; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if ( 'undefined' === typeof gaOptout ) { |
247
|
|
|
function gaOptout() { |
248
|
|
|
__gaTrackerOptout(); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
<?php } ?> |
252
|
|
|
|
253
|
|
|
if ( mi_track_user ) { |
254
|
|
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
255
|
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
256
|
|
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
257
|
|
|
})(window,document,'script','<?php echo $src; ?>','__gaTracker'); |
258
|
|
|
|
259
|
|
|
<?php |
260
|
|
|
echo $compat; |
261
|
|
|
|
262
|
|
|
if ( count( $options ) >= 1 ) { |
263
|
|
|
foreach ( $options as $item ) { |
264
|
|
|
if ( ! is_array( $item ) ) { |
265
|
|
|
echo ' __gaTracker(' . $item . ");\n"; |
266
|
|
|
} else if ( ! empty ( $item['value'] ) ) { |
267
|
|
|
echo ' ' . $item['value'] . "\n"; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
if ( $compat_mode ) { |
272
|
|
|
// Ensure that GA is fully loaded and assign to ga. |
273
|
|
|
echo " __gaTracker( function() { window.ga = __gaTracker; } );\n"; |
274
|
|
|
} |
275
|
|
|
?> |
276
|
|
|
} else { |
277
|
|
|
<?php if ( $this->should_do_optout() ) { ?> |
278
|
|
|
console.log( "<?php echo esc_js( $reason );?>" ); |
279
|
|
|
(function() { |
280
|
|
|
/* https://developers.google.com/analytics/devguides/collection/analyticsjs/ */ |
281
|
|
|
var noopfn = function() { |
282
|
|
|
return null; |
283
|
|
|
}; |
284
|
|
|
var noopnullfn = function() { |
285
|
|
|
return null; |
286
|
|
|
}; |
287
|
|
|
var Tracker = function() { |
288
|
|
|
return null; |
289
|
|
|
}; |
290
|
|
|
var p = Tracker.prototype; |
291
|
|
|
p.get = noopfn; |
292
|
|
|
p.set = noopfn; |
293
|
|
|
p.send = noopfn; |
294
|
|
|
var __gaTracker = function() { |
295
|
|
|
var len = arguments.length; |
296
|
|
|
if ( len === 0 ) { |
297
|
|
|
return; |
298
|
|
|
} |
299
|
|
|
var f = arguments[len-1]; |
300
|
|
|
if ( typeof f !== 'object' || f === null || typeof f.hitCallback !== 'function' ) { |
301
|
|
|
console.log( '<?php echo esc_js( __("Not running function", "google-analytics-for-wordpress" ) );?> __gaTracker(' + arguments[0] + " ....) <?php echo esc_js( __( "because you are not being tracked.", 'google-analytics-for-wordpress' ) );?> " + mi_no_track_reason ); |
302
|
|
|
return; |
303
|
|
|
} |
304
|
|
|
try { |
305
|
|
|
f.hitCallback(); |
306
|
|
|
} catch (ex) { |
307
|
|
|
|
308
|
|
|
} |
309
|
|
|
}; |
310
|
|
|
__gaTracker.create = function() { |
311
|
|
|
return new Tracker(); |
312
|
|
|
}; |
313
|
|
|
__gaTracker.getByName = noopnullfn; |
314
|
|
|
__gaTracker.getAll = function() { |
315
|
|
|
return []; |
316
|
|
|
}; |
317
|
|
|
__gaTracker.remove = noopfn; |
318
|
|
|
window['__gaTracker'] = __gaTracker; |
319
|
|
|
<?php echo $compat; ?> |
320
|
|
|
})(); |
321
|
|
|
<?php } ?> |
322
|
|
|
} |
323
|
|
|
</script> |
324
|
|
|
<?php } else { ?> |
325
|
|
|
<!-- No UA code set --> |
326
|
|
|
<?php } ?> |
327
|
|
|
<!-- / Google Analytics by MonsterInsights --> |
328
|
|
|
<?php |
329
|
|
|
$output = ob_get_contents(); |
330
|
|
|
ob_end_clean(); |
331
|
|
|
return $output; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function should_do_optout() { |
335
|
|
|
return ! ( defined( 'MI_NO_TRACKING_OPTOUT' ) && MI_NO_TRACKING_OPTOUT ); |
|
|
|
|
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|