You are here

yandex_metrics.module in Yandex.Metrics 6.2

The main code of Yandex.Metrics Counter module.

File

yandex_metrics.module
View source
<?php

/**
 * @file
 *   The main code of Yandex.Metrics Counter module.
 */

/**
 * Remove counter from all administrative pages, see http://drupal.org/node/34970.
 */
define('YANDEX_METRICS_PAGES', "admin\nadmin/*\nbatch\nnode/add*\nnode/*/*\nuser/*/*");

/**
 * Implementation of hook_perm().
 */
function yandex_metrics_perm() {
  return array(
    'administer Yandex.Metrics settings',
  );
}

/**
 * Implementation of hook_menu().
 */
function yandex_metrics_menu() {
  $items['admin/settings/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.Metrics service.',
  );
  $items['admin/settings/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;
}

/**
 * The main module settings form.
 */
function yandex_metrics_main_settings() {
  $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.Metrics 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,
  );

  // Render the role overview.
  $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).'),
  );
  $authorisation_token = variable_get('yandex_metrics_auth_token', '');
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => !empty($authorisation_token) ? t('Save') : t('Save & Continue'),
  );
  return $form;
}

/**
 * Implementation of hook_submit for yandex_metrics_main_settings form.
 * @param array $form
 * @param array $form_state
 */
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."));
}

/**
 * Implementation of hook_footer.
 * Adds Yandex.Metrics counter code to the site footer.
 * @param int $main
 */
function yandex_metrics_footer($main = 0) {
  $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()) {
    return '<div class="ym-counter">' . $yandex_metrics_counter_code . '</div>';
  }
}

/**
 * Returns FALSE if we need to disable counter on page.
 * @return bool
 */
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']));

    // 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 $_GET['q'] then compare internal and $_GET['q'].
      $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;
  }
}

/**
 * Returns FALSE if we need to disable counter for role.
 * @return bool
 */
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) {

    // One or more roles are selected.
    foreach (array_keys($user->roles) 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;
}

/**
 * Implementation of 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/settings/yandex_metrics'),
      )) . '</dd>';
      $output .= '</dl>';
      return $output;
    case 'admin/settings/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

Namesort descending Description
yandex_metrics_footer Implementation of hook_footer. Adds Yandex.Metrics counter code to the site footer.
yandex_metrics_help Implementation of hook_help.
yandex_metrics_main_settings The main module settings form.
yandex_metrics_main_settings_submit Implementation of hook_submit for yandex_metrics_main_settings form.
yandex_metrics_menu Implementation of hook_menu().
yandex_metrics_perm Implementation of hook_perm().
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.

Constants

Namesort descending Description
YANDEX_METRICS_PAGES Remove counter from all administrative pages, see http://drupal.org/node/34970.