baidu_tongji-8.x-1.x-dev/baidu_analytics.module
baidu_analytics.module
<?php use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\File\FileSystemInterface; /** * @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, 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'), ); $drupal_messages = \Drupal::messenger()->all(); foreach ($drupal_messages 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::service('entity.repository')->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') && \Drupal::time()->getRequestTime() - $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', \Drupal::time()->getRequestTime())->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 = mb_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 && \Drupal::service('file_system')->prepareDirectory($path)) { // Save updated tracking code file to disk. \Drupal::service('file_system')->saveData($data, $file_destination, FileSystemInterface::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 (\Drupal::service('file_system')->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY)) { // There is no need to flush JS here as core refreshes JS caches // automatically, if new files are added. \Drupal::service('file_system')->saveData($data, $file_destination, FileSystemInterface::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 (\Drupal::service('file_system')->prepareDirectory($path)) { \Drupal::service('file_system')->scanDirectory($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 = mb_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 = mb_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; }