-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaidu_analytics.module
More file actions
603 lines (548 loc) · 25.9 KB
/
Copy pathbaidu_analytics.module
File metadata and controls
603 lines (548 loc) · 25.9 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<?php
/**
* @file
* Drupal Module: Baidu Analytics
*
* Adds the required Javascript to all your Drupal pages to allow tracking by
* the Baidu Analytics statistics package.
*/
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Define the default file extension list that should be tracked as download.
*/
define('BAIDU_ANALYTICS_TRACKFILES_EXTENSIONS', '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip');
/**
* Define default path exclusion list to remove tracking from admin pages,
* see http://drupal.org/node/34970 for more information.
*/
define('BAIDU_ANALYTICS_PAGES', "admin\nadmin/*\nbatch\nnode/add*\nnode/*/*\nuser/*/*");
/**
* Define Asynchronous Tracker Code Library URL.
*/
define('BAIDU_ANALYTICS_ASYNC_LIBRARY_URL', 'hm.baidu.com/hm.js');
/**
* Define Standard Tracker Code Library URL.
*/
define('BAIDU_ANALYTICS_STANDARD_LIBRARY_URL', 'hm.baidu.com/h.js');
/**
* Implements hook_help().
*/
function baidu_analytics_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.baidu_analytics':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Baidu Analytics adds a web statistics tracking system to your website. This system incorporates numerous statistical features. For an extensive listing of these features see the <a href=":project">Baidu Analytics</a> project site. Beyond that, additional information can be found at the <a href=":documentation">Drupal - Baidu Analytics documentation</a>.', [':documentation' => 'https://www.drupal.org/node/2076737', ':project' => 'https://www.drupal.org/project/baidu_analytics']) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dt>' . t('Configuring Baidu Analytics') . '</dt>';
$output .= '<dd>' . t('All settings for this module can be found on the <a href=":ba_settings">Baidu Analytics settings</a> page. When entering the Baidu Analytics account number here, it will automatically add the required JavaScript to every page generated. The <em>General Settings</em> section on this page provides additional instruction about setting up tracking thru the Baidu account.', [':ba_settings' => Url::fromRoute('baidu_analytics.admin_settings_form')->toString()]) . '</dd>';
$output .= '<dt>' . t('Additional features') . '</dt>';
$output .= '<dd>' . t('The Baidu Analytics module offers a bit more than basic tracking. <em>Page Tracking</em> for instance allows you to provide a list of pages to track, or a list of pages not to track. Role and Link tracking features are also available. For a comprehensive discussion on the setup and use of its many feature see the <a href=":documentation">Drupal - Baidu Analytics documentation</a>.', [':documentation' => 'https://www.drupal.org/node/2076737']) . '</dd>';
return $output;
case 'baidu_analytics.admin_settings_form':
return t('<a href="@btj_url">Baidu Analytics</a> is a free (registration required) website traffic and marketing effectiveness service.', array('@btj_url' => 'http://tongji.baidu.com/'));
}
}
/**
* Implements hook_page_attachments().
*/
function baidu_analytics_page_attachments(&$page) {
$user = \Drupal::currentUser();
$config = \Drupal::config('baidu_analytics.settings');
// Get the web property ID for which the tracking code should be generated.
$id = $config->get('baidu_analytics_account');
$request = \Drupal::request();
// Get page http status code for visibility filtering.
$status = NULL;
if ($exception = $request->attributes->get('exception')) {
$status = $exception->getStatusCode();
}
$trackable_status_codes = array(
'403', // Forbidden
'404', // Not Found
);
// 1. Check if the Baidu account number has a value.
// 2. Track page views based on visibility value.
// 3. Check if we should track the currently active user's role.
// 4. Ignore pages visibility filter for 404 or 403 status codes.
if (!empty($id) && (_baidu_analytics_visibility_pages() || in_array($status, $trackable_status_codes)) && _baidu_analytics_visibility_user($user)) {
// Allow user to override the scope for script inclusion in the page.
$scope = $config->get('baidu_analytics_js_scope');
// Select recommended scope depending on the type of code to generate.
if ($scope == 'default') {
if ($config->get('baidu_analytics_code_type') == 'standard') {
// Standard code is recommended to be added in 'footer'.
$scope = 'footer';
}
else {
// Asynchronous code is recommended to be added in 'header'.
$scope = 'header';
}
}
// Add link tracking.
$link_settings = array();
if ($track_outbound = $config->get('baidu_analytics_trackoutbound')) {
$link_settings['trackOutbound'] = $track_outbound;
}
if ($track_mailto = $config->get('baidu_analytics_trackmailto')) {
$link_settings['trackMailto'] = $track_mailto;
}
if (($track_download = $config->get('baidu_analytics_trackfiles')) && ($trackfiles_extensions = $config->get('baidu_analytics_trackfiles_extensions'))) {
$link_settings['trackDownload'] = $track_download;
$link_settings['trackDownloadExtensions'] = $trackfiles_extensions;
}
if (!empty($link_settings)) {
$page['#attached']['drupalSettings']['baidu_analytics'] = $link_settings;
$page['#attached']['library'][] = 'baidu_analytics/baidu_analytics';
}
// Add messages tracking.
$message_events = '';
if ($message_types = $config->get('baidu_analytics_trackmessages')) {
$message_types = array_values(array_filter($message_types));
$status_heading = array(
'status' => t('Status message'),
'warning' => t('Warning message'),
'error' => t('Error message'),
);
foreach (drupal_get_messages(NULL, FALSE) as $type => $messages) {
// Track only the selected message types.
if (in_array($type, $message_types)) {
foreach ($messages as $message) {
$message_events .= '_hmt.push(["_trackEvent", ' . Json::encode(t('Messages')) . ', ' . Json::encode($status_heading[$type]) . ', ' . Json::encode($status_heading[$type] . ': ' . strip_tags($message)) . ']);';
}
}
}
}
// Site search tracking support.
$url_custom = '';
if (\Drupal::moduleHandler()->moduleExists('search') && $config->get('baidu_analytics_site_search') && (strpos(\Drupal::routeMatch()->getRouteName(), 'search.view') === 0) && $keys = ($request->query->has('keys') ? trim($request->get('keys')) : '')) {
global $pager_total_items;
$entity_id = \Drupal::routeMatch()->getParameter('entity')->id();
// If there are results, include the keys and the total count.
if ($pager_total_items != 0) {
$url_custom = Url::fromRoute('search.view_' . $entity_id, [], ['query' => ['search' => $keys, 'total-results' => intval($pager_total_items[0])]])->toString();
}
else {
// Make sure no results keys are included in the tracked URL.
$url_custom = Url::fromRoute('search.view_' . $entity_id, [], ['query' => ['search' => 'no-results:' . $keys, 'cat' => 'no-results']])->toString();
}
}
// If this node is translated from another one, pass the original instead.
if (\Drupal::moduleHandler()->moduleExists('content_translation') && $config->get('baidu_analytics_translation_set')) {
// Check we have a node object, it supports translation, and its
// translated node ID (tnid) doesn't match its own node ID.
if ($request->attributes->has('node')) {
$node = $request->attributes->get('node');
if ($node instanceof NodeInterface && \Drupal::entityManager()->getTranslationFromContext($node) !== $node->getUntranslated()) {
$url_custom = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], ['language' => $node->getUntranslated()->language()])->toString();
}
}
}
// Track access denied (403) and file not found (404) pages.
$referer_uri = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$current_uri = $request->getRequestUri() == '' ? '/' : $request->getRequestUri();
// Report a specific URL containing referer and current page URL.
if ($status == '403') {
$url_custom = "/403.html?page={$current_uri}&from={$referer_uri}";
}
elseif ($status == '404') {
$url_custom = "/404.html?page={$current_uri}&from={$referer_uri}";
}
// Add any custom code snippets if specified.
$codesnippet_before = $config->get('baidu_analytics_codesnippet_before');
$codesnippet_after = $config->get('baidu_analytics_codesnippet_after');
$baidu_analytics_custom_vars = $config->get('baidu_analytics_custom_var');
$custom_var = '';
for ($i = 1; $i < 6; $i++) {
$custom_var_name = !empty($baidu_analytics_custom_vars['slots'][$i]['name']) ? $baidu_analytics_custom_vars['slots'][$i]['name'] : '';
if (!empty($custom_var_name)) {
$custom_var_value = !empty($baidu_analytics_custom_vars['slots'][$i]['value']) ? $baidu_analytics_custom_vars['slots'][$i]['value'] : '';
$custom_var_scope = !empty($baidu_analytics_custom_vars['slots'][$i]['scope']) ? $baidu_analytics_custom_vars['slots'][$i]['scope'] : 3;
$types = array();
if ($request->attributes->has('node')) {
$node = $request->attributes->get('node');
if ($node instanceof NodeInterface) {
$types += ['node' => $node];
}
}
$custom_var_name = \Drupal::token()->replace($custom_var_name, $types, array('clear' => TRUE));
$custom_var_value = \Drupal::token()->replace($custom_var_value, $types, array('clear' => TRUE));
// Suppress empty custom names and/or variables.
if (!Unicode::strlen(trim($custom_var_name)) || !Unicode::strlen(trim($custom_var_value))) {
continue;
}
// The length of the string used for the 'name' and the one used for
// the 'value' must not exceed 128 bytes after url encoding.
$name_length = Unicode::strlenn(rawurlencode($custom_var_name));
$tmp_value = rawurlencode($custom_var_value);
$value_length = Unicode::strlen($tmp_value);
if ($name_length + $value_length > 128) {
// Trim value and remove fragments of url encoding.
$tmp_value = rtrim(substr($tmp_value, 0, 127 - $name_length), '%0..9A..F');
$custom_var_value = urldecode($tmp_value);
}
$custom_var_name = Json::encode($custom_var_name);
$custom_var_value = Json::encode($custom_var_value);
$custom_var .= "_hmt.push(['_setCustomVar', $i, $custom_var_name, $custom_var_value, $custom_var_scope]);";
}
}
// Start putting together the javascript code to be inserted in the page.
$script = 'var _hmt = _hmt || [];';
$script .= '_hmt.push(["_setAccount", ' . Json::encode($id) . ']);';
if (!empty($custom_var)) {
$script .= $custom_var;
}
if (!empty($codesnippet_before)) {
$script .= $codesnippet_before;
}
if (empty($url_custom)) {
$script .= '_hmt.push(["_trackPageview"]);';
}
else {
$script .= '_hmt.push(["_trackPageview", "' . $url_custom . '"]);';
}
if (!empty($message_events)) {
$script .= $message_events;
}
if (!empty($codesnippet_after)) {
$script .= $codesnippet_after;
}
// Determine which type of code should be used: async or standard.
if ($config->get('baidu_analytics_code_type') == 'standard') {
// Provide Standard BATC file URL on server if it is locally cached.
if ($config->get('baidu_analytics_cache') && $library_tracker_url = _baidu_analytics_cache(BAIDU_ANALYTICS_STANDARD_LIBRARY_URL)) {
// A dummy query-string is added to filenames, to gain control over
// browser-caching. The string changes on every update or full cache
// flush, forcing browsers to load a new copy of the files, as the
// URL changed.
$query_string = '?' . (\Drupal::state()->get('system.css_js_query_string') ?: '0');
$library_tracker_url .= $query_string;
}
else {
// If no cache is found, provide Standard Code Tracker library URL.
$library_tracker_url = '//' . BAIDU_ANALYTICS_STANDARD_LIBRARY_URL . '?' . $id;
}
$script .= 'document.write(unescape("%3Cscript src=\'' . $library_tracker_url . "' type='text/javascript'%3E%3C/script%3E\"));";
}
else {
// Provide Asynchronous BATC file URL on server if it is locally cached.
if ($config->get('baidu_analytics_cache') && $library_tracker_url = _baidu_analytics_cache(BAIDU_ANALYTICS_ASYNC_LIBRARY_URL)) {
// Add dummy query-string to filenames, same trick as the case above.
$query_string = '?' . (\Drupal::state()->get('system.css_js_query_string') ?: '0');
$library_tracker_url .= $query_string;
}
else {
// If no cache is found, provide Asynchronous Code Tracker library URL.
$library_tracker_url = '//' . BAIDU_ANALYTICS_ASYNC_LIBRARY_URL . '?' . $id;
}
// Asynchronous JavaScript tracking code.
$script .= "(function() {var hm = document.createElement('script');hm.src = '{$library_tracker_url}';hm.type = 'text/javascript';var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(hm, s);})()";
}
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#value' => $script,
'#scope' => $scope,
],
'baidu_analytics_tracking_script'
];
}
}
/**
* Implements hook_field_extra_fields().
*/
function baidu_analytics_entity_extra_field_info() {
$extra = array();
$extra['user']['user']['form']['baidu_analytics'] = array(
'label' => t('Baidu Analytics configuration'),
'description' => t('Baidu Analytics module form element.'),
'weight' => 3,
);
return $extra;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Allow users to decide if tracking code will be added to pages or not.
*/
function baidu_analytics_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$config = \Drupal::config('baidu_analytics.settings');
$account = $form_state->getFormObject()->getEntity();
//$category = $form['#user_category'];
if (\Drupal::currentUser()->hasPermission('opt-in or out of tracking') && ($visibility_user_account_mode = $config->get('baidu_analytics_custom')) != 0 && _baidu_analytics_visibility_roles($account)) {
$account_data_baidu_analytics = \Drupal::service('user.data')->get('baidu_analytics', $account->id());
$form['baidu_analytics'] = array(
'#type' => 'details',
'#title' => t('Baidu Analytics configuration'),
'#weight' => 3,
'#open' => TRUE,
);
switch ($visibility_user_account_mode) {
case 1:
$description = t('Users are tracked by default, but you are able to opt out.');
break;
case 2:
$description = t('Users are <em>not</em> tracked by default, but you are able to opt in.');
break;
}
// Disable tracking for visitors who have opted out from tracking via DNT
// (Do-Not-Track) header.
$disabled = FALSE;
if ($config->get('baidu_analytics_privacy_donottrack') && !empty($_SERVER['HTTP_DNT'])) {
$disabled = TRUE;
// Override settings value.
$account_data_baidu_analytics['baidu_analytics_custom'] = FALSE;
$description .= '<span class="admin-disabled">';
$description .= ' ' . t('You have opted out from tracking via browser privacy settings.');
$description .= '</span>';
}
$form['baidu_analytics']['baidu_analytics_custom'] = array(
'#type' => 'checkbox',
'#title' => t('Enable user tracking'),
'#description' => $description,
'#default_value' => isset($account_data_baidu_analytics['baidu_analytics_custom']) ? $account_data_baidu_analytics['baidu_analytics_custom'] : ($visibility_user_account_mode == 1),
'#disabled' => $disabled,
);
// hook_user_update() is missing in D8, add custom submit handler.
$form['actions']['submit']['#submit'][] = 'baidu_analytics_user_profile_form_submit';
}
}
/**
* Submit callback for user profile form to save the Baidu Analytics setting.
*/
function baidu_analytics_user_profile_form_submit($form, FormStateInterface $form_state) {
$account = $form_state->getFormObject()->getEntity();
if ($account->id() && $form_state->hasValue('baidu_analytics_custom')) {
\Drupal::service('user.data')->set('baidu_analytics', $account->id(), 'baidu_analytics_custom', (int) $form_state->getValue('baidu_analytics_custom'));
}
}
/**
* Implements hook_cron().
*/
function baidu_analytics_cron() {
$config = \Drupal::config('baidu_analytics.settings');
// Regenerate the tracking code file every day.
if ($config->get('baidu_analytics_cache') && REQUEST_TIME - $config->get('baidu_analytics_last_cache') >= 86400) {
// Depending on the type of code selected, provide the correct tracker url.
if ($config->get('baidu_analytics_code_type') == 'standard') {
$library_tracker_url = BAIDU_ANALYTICS_STANDARD_LIBRARY_URL;
}
else {
$library_tracker_url = BAIDU_ANALYTICS_ASYNC_LIBRARY_URL;
}
_baidu_analytics_cache($library_tracker_url, TRUE);
\Drupal::configFactory()->getEditable('baidu_analytics.settings')->set('baidu_analytics_last_cache', REQUEST_TIME)->save();
}
}
/**
* Helper function for grabbing search keys. Function is missing in D7.
*
* http://api.drupal.org/api/function/search_get_keys/6
*/
function baidu_analytics_search_get_keys() {
static $return;
if (!isset($return)) {
// Extract keys as remainder of path
// Note: support old GET format of searches for existing links.
$path = explode('/', $_GET['q'], 3);
$keys = empty($_REQUEST['keys']) ? '' : $_REQUEST['keys'];
$return = count($path) == 3 ? $path[2] : $keys;
}
return $return;
}
/**
* Download/Synchronize/Cache tracking code file locally.
*
* @param string $location
* The full URL to the external javascript file.
* @param bool $sync_cached_file
* Synchronize tracking code and update if remote file have changed.
*
* @return mixed
* The path to the local javascript file on success, boolean FALSE on failure.
*/
function _baidu_analytics_cache($location, $sync_cached_file = FALSE) {
$config = \Drupal::config('baidu_analytics.settings');
$path = 'public://baidu_analytics';
$baidu_analytics_account = $config->get('baidu_analytics_account');
// Use a tracker specific cache file to allow multiple codes on same site.
$tracker_hash = \Drupal\Component\Utility\Unicode::substr($baidu_analytics_account, 0, 7);
$file_destination = $path . '/' . (!empty($tracker_hash) ? $tracker_hash . '-' : '') . basename($location);
if (!file_exists($file_destination) || $sync_cached_file) {
global $base_url;
// Get current server's protocol to match with the request.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
$location = $protocol . $location . '?' . $baidu_analytics_account;
try {
$response = \Drupal::httpClient()->get($location, [
'headers' => [
'referer' => $base_url,
],
]);
$data = $response->getBody(TRUE);
if (file_exists($file_destination)) {
// Synchronize tracking code and and replace local file if outdated.
$data_hash_local = Crypt::hashBase64(file_get_contents($file_destination));
$data_hash_remote = Crypt::hashBase64($data);
// Check that the files directory is writable.
if ($data_hash_local != $data_hash_remote && file_prepare_directory($path)) {
// Save updated tracking code file to disk.
file_unmanaged_save_data($data, $file_destination, FILE_EXISTS_REPLACE);
\Drupal::logger('baidu_analytics')->info('Locally cached tracking code file has been updated.');
// Change query-strings on css/js files to enforce reload for all.
_drupal_flush_css_js();
}
}
else {
// Check that the files directory is writable.
if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
// There is no need to flush JS here as core refreshes JS caches
// automatically, if new files are added.
file_unmanaged_save_data($data, $file_destination, FILE_EXISTS_REPLACE);
\Drupal::logger('baidu_analytics')->info('Locally cached tracking code file has been saved.');
// Return the local JS file path.
return file_url_transform_relative(file_create_url($file_destination));
}
}
}
catch (RequestException $exception) {
watchdog_exception('baidu_analytics', $exception);
}
}
else {
// Return the local JS file path.
return file_url_transform_relative(file_create_url($file_destination));
}
}
/**
* Delete cached files and directory.
*/
function baidu_analytics_clear_js_cache() {
$path = 'public://baidu_analytics';
if (file_prepare_directory($path)) {
file_scan_directory($path, '/.*/', array('callback' => 'file_unmanaged_delete'));
\Drupal::service("file_system")->rmdir($path);
// Change query-strings on css/js files to enforce reload for all users.
_drupal_flush_css_js();
\Drupal::logger('baidu_analytics')->info('Local cache has been purged.', array());
}
}
/**
* Tracking visibility check for an user object.
*
* @param object $account
* A user object containing an array of roles to check.
*
* @return bool
* A decision on if the current user is being tracked by Baidu Analytics.
*/
function _baidu_analytics_visibility_user($account) {
$enabled = FALSE;
// Is current user a member of a role that should be tracked?
if (_baidu_analytics_visibility_header($account) && _baidu_analytics_visibility_roles($account)) {
// Use the user's block visibility setting, if necessary.
if (($custom = \Drupal::config('baidu_analytics.settings')->get('baidu_analytics_custom')) != 0) {
if ($account->uid && isset($account->data['baidu_analytics']['custom'])) {
$enabled = $account->data['baidu_analytics']['custom'];
}
else {
$enabled = ($custom == 1);
}
}
else {
$enabled = TRUE;
}
}
return $enabled;
}
/**
* Helper function to check if tracking code should display for current roles.
*
* Based on visibility setting this function returns TRUE if BA code should be
* added for the current role and otherwise FALSE.
*/
function _baidu_analytics_visibility_roles($account) {
$visibility = \Drupal::config('baidu_analytics.settings')->get('baidu_analytics_visibility_roles');
$enabled = $visibility;
$roles = array_filter(\Drupal::config('baidu_analytics.settings')->get('baidu_analytics_roles'));
if (count($roles) > 0) {
// One or more roles are selected.
foreach (array_values($account->getRoles()) as $user_role) {
// Is the current user a member of one of these roles?
if (in_array($user_role, $roles)) {
// Current user is a member of a role that should be tracked or
// excluded from tracking.
$enabled = !$visibility;
break;
}
}
}
else {
// No role is selected for tracking, therefore all roles should be tracked.
$enabled = TRUE;
}
return $enabled;
}
/**
* Helper function to check if tracking code should display for current page.
*
* Based on visibility setting this function returns TRUE if BA code should be
* added to the current page and otherwise FALSE.
*/
function _baidu_analytics_visibility_pages() {
$config = \Drupal::config('baidu_analytics.settings');
static $page_match;
// Cache visibility result if function is called more than once.
if (!isset($page_match)) {
$visibility = $config->get('baidu_analytics_visibility_pages');
$setting_pages = $config->get('baidu_analytics_pages');
// Match path if necessary.
if (!empty($setting_pages)) {
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = \Drupal\Component\Utility\Unicode::strtolower($setting_pages);
if ($visibility < 2) {
$current_path = \Drupal::service('path.current')->getPath();
$path = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
// Convert the Drupal path to lowercase.
$path = \Drupal\Component\Utility\Unicode::strtolower($path);
// Compare the lowercase internal and lowercase path alias (if any).
$page_match = \Drupal::service('path.matcher')->matchPath($path, $pages);
if ($path != $current_path) {
$page_match = $page_match || \Drupal::service('path.matcher')->matchPath($current_path, $pages);
}
// When $visibility has a value of 0, the tracking code is displayed on
// all pages except those listed in $pages. When set to 1, it
// is displayed only on those pages listed in $pages.
$page_match = !($visibility xor $page_match);
}
elseif (\Drupal::moduleHandler()->moduleExists('php')) {
$page_match = php_eval($setting_pages);
}
else {
$page_match = FALSE;
}
}
else {
$page_match = TRUE;
}
}
return $page_match;
}
/**
* Helper function to check if user's DNT is set and hide tracking code.
*
* Based on headers sent by clients this function returns TRUE if BA code
* should be added to the current page and otherwise FALSE.
*/
function _baidu_analytics_visibility_header($account) {
$config = \Drupal::config('baidu_analytics.settings');
if (($account->id() || \Drupal::config('system.performance')->get('cache.page.max_age') == 0) && $config->get('baidu_analytics_privacy_donottrack') && !empty($_SERVER['HTTP_DNT'])) {
// Disable tracking if caching is disabled or a visitors is logged in and
// have opted out from tracking via DNT (Do-Not-Track) header.
return FALSE;
}
return TRUE;
}