You are here

hotjar.module in Hotjar 6

Same filename and directory in other branches
  1. 8.2 hotjar.module
  2. 8 hotjar.module
  3. 7 hotjar.module

Drupal Module: Hotjar.

Adds the required Javascript to all your Drupal pages to allow tracking by hotjar (https://www.hotjar.com/).

File

hotjar.module
View source
<?php

/**
 * @file
 * Drupal Module: Hotjar.
 *
 * Adds the required Javascript to all your Drupal pages to allow
 * tracking by hotjar (https://www.hotjar.com/).
 */

/**
 * Define default path exclusion list to remove tracking from admin pages.
 *
 * See http://drupal.org/node/34970 for more information.
 */
define('HOTJAR_PAGES', "admin\nadmin/*\nbatch\nnode/add*\nnode/*/*\nuser/*/*");

/**
 * Implements hook_help().
 */
function hotjar_help($path, $arg) {
  switch ($path) {
    case 'admin/help#hotjar':
    case 'admin/settings/hotjar':
      return t('<a href="@hotjar_url">Hotjar</a> is a new powerful way to reveal true website user behaviour and experiences in one central tool – giving you the big picture of how you can improve your site\'s UX and conversion rates. All your data is securely stored in the cloud and is accessible at lightning speed.', array(
        '@hotjar_url' => 'https://www.hotjar.com/',
      ));
  }
}

/**
 * Implements hook_permission().
 */
function hotjar_perm() {
  return array(
    'administer hotjar',
  );
}

/**
 * Implements hook_menu().
 */
function hotjar_menu() {
  $items['admin/settings/hotjar'] = array(
    'title' => 'Hotjar',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'hotjar_admin_settings_form',
    ),
    'access arguments' => array(
      'administer hotjar',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'hotjar.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme_registry_alter().
 *
 * Borrowed from googleanalytics.
 */
function hotjar_theme_registry_alter(&$registry) {

  // Add custom preprocess function to the beginning of the stack.
  // This allows use of the drupal_add_js() function before $vars is populated.
  array_unshift($registry['page']['preprocess functions'], 'hotjar_add_js');
}

/**
 * Get Hotjar settings.
 */
function hotjar_get_settings() {
  $settings = variable_get('hotjar_settings', array());
  $settings += array(
    'hotjar_account' => NULL,
    'hotjar_visibility_pages' => 0,
    'hotjar_pages' => HOTJAR_PAGES,
    'hotjar_visibility_roles' => 0,
    'hotjar_roles' => array(),
  );
  return $settings;
}

/**
 * Insert JavaScript to the appropriate scope/region of the page.
 */
function hotjar_add_js(&$variables, $hook) {
  $settings = hotjar_get_settings();
  $id = $settings['hotjar_account'];
  if (!$id || !_hotjar_check_status() || !_hotjar_should_be_added() || !_hotjar_check_user()) {
    return;
  }

  // Quote from the Hotjar dashboard:
  // "The Tracking Code below should be placed in the <head> tag of
  // every page you want to track on your site."
  $tracking_code = <<<HJ
(function(h,o,t,j,a,r){
  h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
  h._hjSettings={hjid:'{<span class="php-variable">$id</span>}',hjsv:5};
  a=o.getElementsByTagName('head')[0];
  r=o.createElement('script');r.async=1;
  r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
  a.appendChild(r);
})(window,document,'//static.hotjar.com/c/hotjar-','.js?sv=');
HJ;
  drupal_add_js($tracking_code, 'inline', 'header');
}

/**
 * Check Hotjar code should be added.
 */
function _hotjar_should_be_added() {
  static $page_match;
  if (isset($page_match)) {
    return $page_match;
  }
  $settings = hotjar_get_settings();
  $visibility = $settings['hotjar_visibility_pages'];
  $setting_pages = $settings['hotjar_pages'];
  if (empty($setting_pages)) {
    $page_match = TRUE;
    return $page_match;
  }
  $pages = drupal_strtolower($setting_pages);
  if ($visibility < 2) {
    $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
    $page_match = drupal_match_path($path, $pages);
    if ($path != $_GET['q']) {
      $page_match = $page_match || drupal_match_path($_GET['q'], $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);
  }
  else {
    $page_match = FALSE;
  }
  return $page_match;
}

/**
 * Check Hotjar code should be added for user.
 */
function _hotjar_check_user($account = NULL) {
  if (!isset($account)) {
    $account = $GLOBALS['user'];
  }
  $enabled = FALSE;
  if (_hotjar_check_roles($account)) {
    $enabled = TRUE;
  }
  return $enabled;
}

/**
 * Check user role.
 */
function _hotjar_check_roles($account = NULL) {
  if (!isset($account)) {
    $account = $GLOBALS['user'];
  }
  $settings = hotjar_get_settings();
  $visibility = $settings['hotjar_visibility_roles'];
  $enabled = $visibility;
  $roles = $settings['hotjar_roles'];
  $checked_roles = array_filter($roles);
  if (empty($checked_roles)) {

    // No role is selected for tracking, therefore all roles should be tracked.
    return TRUE;
  }

  // The hotjar_roles stores the selected roles as an array where
  // the keys are the role IDs. When the role is not selected the
  // value is 0. If a role is selected the value is the role ID.
  if (count(array_intersect_key($account->roles, $checked_roles))) {
    $enabled = !$visibility;
  }
  return $enabled;
}

/**
 * Check current request HTTP status.
 */
function _hotjar_check_status() {

  // Get page status code for visibility filtering.
  $headers = _hotjar_get_headers();
  $status = $headers[':status'];
  $not_tracked_status_codes = array(
    '403 Forbidden',
    '404 Not Found',
  );
  return !in_array($status, $not_tracked_status_codes);
}

/**
 * Get all headers.
 */
function _hotjar_get_headers() {
  $header_array = array();
  $headers = drupal_get_headers();
  $exploded_headers = explode("\n", $headers);
  foreach ($exploded_headers as $h) {
    $matches = array();
    preg_match('/^(.+):(.*)$/', $h, $matches);
    if (count($matches) > 2) {
      $header_array[strtolower($matches[1])] = trim($matches[2]);
    }
  }
  return $header_array;
}

Functions

Namesort descending Description
hotjar_add_js Insert JavaScript to the appropriate scope/region of the page.
hotjar_get_settings Get Hotjar settings.
hotjar_help Implements hook_help().
hotjar_menu Implements hook_menu().
hotjar_perm Implements hook_permission().
hotjar_theme_registry_alter Implements hook_theme_registry_alter().
_hotjar_check_roles Check user role.
_hotjar_check_status Check current request HTTP status.
_hotjar_check_user Check Hotjar code should be added for user.
_hotjar_get_headers Get all headers.
_hotjar_should_be_added Check Hotjar code should be added.

Constants

Namesort descending Description
HOTJAR_PAGES Define default path exclusion list to remove tracking from admin pages.