hotjar.module in Hotjar 7
Same filename and directory in other branches
Drupal Module: Hotjar.
Adds the required Javascript to all your Drupal pages to allow tracking by hotjar (https://www.hotjar.com/).
File
hotjar.moduleView 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/*/*");
define('HOTJAR_ACCESS_ALLOW', TRUE);
define('HOTJAR_ACCESS_DENY', FALSE);
define('HOTJAR_ACCESS_IGNORE', NULL);
/**
* Implements hook_help().
*/
function hotjar_help($path, $arg) {
switch ($path) {
case 'admin/help#hotjar':
case 'admin/config/system/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_permission() {
return array(
'administer hotjar' => array(
'title' => t('Administer Hotjar'),
'description' => t('Perform maintenance tasks for Hotjar.'),
),
);
}
/**
* Implements hook_menu().
*/
function hotjar_menu() {
$items['admin/config/system/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;
}
/**
* 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(),
'hotjar_snippet_version' => 6,
);
if (empty($settings['hotjar_snippet_version'])) {
$settings['hotjar_snippet_version'] = 6;
}
foreach ($settings as $name => $value) {
if (array_key_exists($name, $GLOBALS['conf'])) {
$settings[$name] = $GLOBALS['conf'][$name];
}
}
return $settings;
}
/**
* Implements hook_page_build().
*
* Insert JavaScript to the <head> tag of the page.
*/
function hotjar_page_build(&$page) {
$settings = hotjar_get_settings();
$id = $settings['hotjar_account'];
$version = $settings['hotjar_snippet_version'];
if (!$id || !_hotjar_access()) {
return;
}
// Use escaped HotjarID.
$clean_id = drupal_json_encode($id);
$clean_version = drupal_json_encode($version);
// 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">$clean_id</span>},hjsv:{<span class="php-variable">$clean_version</span>}};
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, array(
'type' => 'inline',
'scope' => 'header',
'weight' => -99,
'group' => JS_LIBRARY - 1,
));
}
/**
* Determines whether we add tracking code to page.
*/
function _hotjar_access() {
$settings = hotjar_get_settings();
$id = $settings['hotjar_account'];
$access = array(
'hotjar_id' => (bool) $id,
'hotjar_status' => _hotjar_check_status(),
'hotjar_path' => _hotjar_should_be_added(),
'hotjar_role' => _hotjar_check_user(),
);
foreach (module_implements('hotjar_access') as $module) {
$access[$module] = module_invoke($module, 'hotjar_access');
}
drupal_alter('hotjar_access', $access);
return !in_array(HOTJAR_ACCESS_DENY, $access, TRUE);
}
/**
* Check Hotjar code should be added.
*/
function _hotjar_should_be_added() {
$page_match =& drupal_static(__FUNCTION__);
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.
$status = drupal_get_http_header('Status');
$not_tracked_status_codes = array(
'403 Forbidden',
'404 Not Found',
);
return !in_array($status, $not_tracked_status_codes);
}
Functions
Name | Description |
---|---|
hotjar_get_settings | Get Hotjar settings. |
hotjar_help | Implements hook_help(). |
hotjar_menu | Implements hook_menu(). |
hotjar_page_build | Implements hook_page_build(). |
hotjar_permission | Implements hook_permission(). |
_hotjar_access | Determines whether we add tracking code to page. |
_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_should_be_added | Check Hotjar code should be added. |
Constants
Name | Description |
---|---|
HOTJAR_ACCESS_ALLOW | |
HOTJAR_ACCESS_DENY | |
HOTJAR_ACCESS_IGNORE | |
HOTJAR_PAGES | Define default path exclusion list to remove tracking from admin pages. |