yandex_metrics.module in Yandex.Metrics 8.2
Same filename and directory in other branches
The main code of Yandex.Metrics Counter module.
File
yandex_metrics.moduleView source
<?php
/**
* @file
* The main code of Yandex.Metrics Counter module.
*/
/**
* Implements hook_permission().
*/
function yandex_metrics_permission() {
return array(
'administer Yandex.Metrics settings' => array(
'title' => t('Administer Yandex.Metrics Settings'),
),
);
}
/**
* Implements hook_page_build().
* Adds Yandex.Metrics counter code to the site footer.
*/
function yandex_metrics_page_build(&$page) {
$yandex_metrics_counter_code = Drupal::config('yandex_metrics.settings')
->get('counter_code');
if (!empty($yandex_metrics_counter_code) && yandex_metrics_show_counter() && yandex_metrics_show_counter_for_role()) {
$path = drupal_get_path('module', 'yandex_metrics');
$page['page_bottom']['yandex_metrics'] = array(
'#type' => 'markup',
'#markup' => '<div class="ym-counter">' . $yandex_metrics_counter_code . '</div>',
'#attached' => array(
'css' => array(
$path . '/css/yandex_metrics.css',
),
),
);
}
}
/**
* Returns FALSE if we need to disable counter on page.
* @return bool
*/
function yandex_metrics_show_counter() {
$pages = Drupal::config('yandex_metrics.settings')
->get('visibility.path.pages');
$visibility = Drupal::config('yandex_metrics.settings')
->get('visibility.path.visibility');
$urls_equal = FALSE;
if (!empty($pages)) {
$pages_in_lowcase = drupal_strtolower($pages);
$current_path = Drupal::service('path.alias_manager.cached')
->getPathAlias(current_path());
$current_path = drupal_strtolower($current_path);
// Compare internal and path alias.
$path_match = drupal_match_path($current_path, $pages_in_lowcase);
if ($path_match) {
$urls_equal = TRUE;
}
else {
// If path alias doesn't equal with current_path() then compare internal and current_path().
$path_match = drupal_match_path(current_path(), $pages_in_lowcase);
if ($current_path != current_path() && $path_match) {
$urls_equal = TRUE;
}
}
}
if (!$visibility && $urls_equal) {
return FALSE;
}
elseif (!$visibility && !$urls_equal) {
return TRUE;
}
elseif ($visibility && $urls_equal) {
return TRUE;
}
elseif ($visibility && !$urls_equal) {
return FALSE;
}
}
/**
* Returns FALSE if we need to disable counter for role.
* @return bool
*/
function yandex_metrics_show_counter_for_role() {
$user = \Drupal::currentUser();
$visibility = (bool) Drupal::config('yandex_metrics.settings')
->get('visibility.role.visibility');
$enabled = (bool) $visibility;
$roles = Drupal::config('yandex_metrics.settings')
->get('visibility.role.roles');
$has_active_role = FALSE;
foreach ($roles as $key => $value) {
if ($key === $value) {
$has_active_role = TRUE;
break;
}
}
if ($has_active_role) {
// One or more roles are selected.
foreach ($user
->getRoles() as $rid) {
// Is the current user a member of one of these roles?
if (isset($roles[$rid]) && $rid === $roles[$rid]) {
// Current user is a member of a role that should be tracked/excluded from tracking.
$enabled = !$visibility;
break;
}
}
}
else {
// No role is selected for tracking, therefore all roles should be tracked.
$enabled = TRUE;
}
return $enabled;
}
/**
* Implements hook_help().
*/
function yandex_metrics_help($path, $arg) {
switch ($path) {
case 'admin/help#yandex_metrics':
$output = '';
$output .= '<h3>' . t('About the module') . '</h3>';
$output .= '<p>' . t('The <a href="@yandex_metrika" target="_blank">Yandex.Metrics</a> service is European alternative of Google Analytics. This is a free tool that helps you to increase the conversion rate of your site.', array(
'@yandex_metrika' => 'http://metrika.yandex.ru/',
)) . '</p>';
$output .= '<p>' . t('The Yandex.Metrics Counter module allows to install <a href="@yandex_metrika" target="_blank">Yandex.Metrics</a> counter code on the site pages.', array(
'@yandex_metrika' => 'http://metrika.yandex.ru/',
)) . '</p>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Installing counter') . '</dt>';
$output .= '<dd>' . t('Yandex.Metrics counter is JavaScript code that monitors user behavior on your website. You should <a href="@create_counter" target="_blank">create</a> and <a href="@install_counter">install</a> the counter to work with Yandex.Metrics service.', array(
'@create_counter' => 'http://metrika.yandex.ru/',
'@install_counter' => url('admin/config/system/yandex_metrics'),
)) . '</dd>';
$output .= '</dl>';
return $output;
case 'admin/config/system/yandex_metrics':
$output = '<p>' . t('Yandex.Metrics counter is JavaScript code that monitors user behavior on your website. You should <a href="@create_counter" target="_blank">create</a> and install the counter code to work with Yandex.Metrics service.', array(
'@create_counter' => 'http://metrika.yandex.ru/',
)) . '</p>';
return $output;
}
}
Functions
Name | Description |
---|---|
yandex_metrics_help | Implements hook_help(). |
yandex_metrics_page_build | Implements hook_page_build(). Adds Yandex.Metrics counter code to the site footer. |
yandex_metrics_permission | Implements hook_permission(). |
yandex_metrics_show_counter | Returns FALSE if we need to disable counter on page. |
yandex_metrics_show_counter_for_role | Returns FALSE if we need to disable counter for role. |