View source
<?php
define('YANDEX_METRICS_PAGES', "admin\nadmin/*\nbatch\nnode/add*\nnode/*/*\nuser/*/*");
function yandex_metrics_permission() {
return array(
'administer Yandex.Metrics settings' => array(
'title' => t('Administer Yandex.Metrics Settings'),
'restrict access' => TRUE,
),
);
}
function yandex_metrics_menu() {
$items = array();
$items['admin/config/system/yandex_metrics'] = array(
'access arguments' => array(
'administer Yandex.Metrics settings',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'yandex_metrics_main_settings',
),
'title' => 'Yandex.Metrics',
'type' => MENU_NORMAL_ITEM,
'description' => 'Integrate your site with Yandex.Metrica service.',
);
$items['admin/config/system/yandex_metrics/counter'] = array(
'access arguments' => array(
'administer Yandex.Metrics settings',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'yandex_metrics_main_settings',
),
'title' => 'Counter Code',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
);
return $items;
}
function yandex_metrics_main_settings($form, $form_state) {
if (module_exists('domain_variable')) {
return variable_module_form($form, $form_state, 'yandex_metrics');
}
$form['yandex_metrics_counter_code'] = array(
'#default_value' => variable_get('yandex_metrics_counter_code', ''),
'#title' => t('Counter Code'),
'#type' => 'textarea',
'#rows' => 10,
'#description' => t('Paste Yandex.Metrica counter code for your site here.'),
);
$form['visibility'] = array(
'#type' => 'fieldset',
'#title' => t('Page specific tracking settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$visibility = variable_get('yandex_metrics_visibility', 0);
$options = array(
t('Add to every page except the listed pages.'),
t('Add to the listed pages only.'),
);
$form['visibility']['yandex_metrics_visibility'] = array(
'#type' => 'radios',
'#title' => t('Add code to specific pages'),
'#options' => $options,
'#default_value' => $visibility,
);
$pages = variable_get('yandex_metrics_pages', YANDEX_METRICS_PAGES);
$description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
'%blog' => 'blog',
'%blog-wildcard' => 'blog/*',
'%front' => '<front>',
));
$form['visibility']['yandex_metrics_pages'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#default_value' => $pages,
'#description' => $description,
'#wysiwyg' => FALSE,
'#rows' => 10,
);
$form['role_visibility'] = array(
'#type' => 'fieldset',
'#title' => t('Role specific tracking settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['role_visibility']['yandex_metrics_visibility_roles'] = array(
'#type' => 'radios',
'#title' => t('Add tracking for specific roles'),
'#options' => array(
t('Add to the selected roles only'),
t('Add to every role except the selected ones'),
),
'#default_value' => variable_get('yandex_metrics_visibility_roles', 0),
);
$roles = user_roles();
$role_options = array();
foreach ($roles as $rid => $name) {
$role_options[$rid] = $name;
}
$form['role_visibility']['yandex_metrics_roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#default_value' => variable_get('yandex_metrics_roles', array()),
'#options' => $role_options,
'#description' => t('If none of the roles are selected, all users will be tracked. If a user has any of the roles checked, that user will be tracked (or excluded, depending on the setting above).'),
);
$form['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function yandex_metrics_main_settings_submit($form, &$form_state) {
$counter_code = $form_state['values']['yandex_metrics_counter_code'];
$visibility = $form_state['values']['yandex_metrics_visibility'];
$pages = $form_state['values']['yandex_metrics_pages'];
$visibility_roles = $form_state['values']['yandex_metrics_visibility_roles'];
$roles = $form_state['values']['yandex_metrics_roles'];
variable_set('yandex_metrics_counter_code', $counter_code);
variable_set('yandex_metrics_visibility', $visibility);
variable_set('yandex_metrics_pages', $pages);
variable_set('yandex_metrics_visibility_roles', $visibility_roles);
variable_set('yandex_metrics_roles', $roles);
drupal_set_message(t("The Counter Code settings has been saved successfully."));
}
function yandex_metrics_page_build(&$page) {
$yandex_metrics_counter_code = variable_get('yandex_metrics_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');
drupal_add_css($path . '/css/yandex_metrics.css');
$page['page_bottom']['yandex_metrics'] = array(
'#type' => 'markup',
'#markup' => '<div class="ym-counter">' . $yandex_metrics_counter_code . '</div>',
);
}
}
function yandex_metrics_show_counter() {
$pages = variable_get('yandex_metrics_pages', YANDEX_METRICS_PAGES);
$visibility = variable_get('yandex_metrics_visibility', 0);
$urls_equal = FALSE;
if (!empty($pages)) {
$pages_in_lowcase = drupal_strtolower($pages);
$current_path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
$path_match = drupal_match_path($current_path, $pages_in_lowcase);
if ($path_match) {
$urls_equal = TRUE;
}
else {
$path_match = drupal_match_path($_GET['q'], $pages_in_lowcase);
if ($current_path != $_GET['q'] && $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;
}
}
function yandex_metrics_show_counter_for_role() {
global $user;
$visibility = variable_get('yandex_metrics_visibility_roles', 0);
$enabled = $visibility;
$roles = variable_get('yandex_metrics_roles', array());
if (array_sum($roles) > 0) {
foreach (array_keys($user->roles) as $rid) {
if (isset($roles[$rid]) && $rid == $roles[$rid]) {
$enabled = !$visibility;
break;
}
}
}
else {
$enabled = TRUE;
}
return $enabled;
}
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.Metrica</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.Metrica</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.Metrica 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.Metrica service.', array(
'@create_counter' => 'http://metrika.yandex.ru/',
)) . '</p>';
return $output;
}
}