You are here

crazyegg.module in Crazy Egg Integration 8

Same filename and directory in other branches
  1. 6 crazyegg.module
  2. 7 crazyegg.module

File

crazyegg.module
View source
<?php

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\user\Entity\User;

/**
 * Implements hook_permission().
 */
function crazyegg_permission() {
  return [
    'administer crazy egg' => [
      'title' => t('Administer Crazy Egg'),
      'description' => t('Administer account settings and visibility of Crazy Egg on your site.'),
    ],
  ];
}

/**
 * Implements hook_help().
 * Shows help info on `/admin/help/crazyegg` page
 */
function crazyegg_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.crazyegg':
      return t('<img src="@logo" style="float: right;" alt="Crazy Egg">' . '<strong>Crazy Egg</strong> is an analytics tool that provides website heatmaps and eye tracking.<br/><br/>' . 'Drupal module repository: <a href="@module_url">www.drupal.org/project/crazyegg</a><br/>' . 'Official website: <a href="@ce_url">crazyegg.com</a><br/>' . 'Support: <a href="mailto:@support_email">support@crazyegg.com</a><br/><br/><hr/><br/>', [
        '@ce_url' => 'https://www.crazyegg.com',
        '@module_url' => 'https://www.drupal.org/project/crazyegg',
        '@support_email' => 'support@crazyegg.com',
        '@logo' => 'https://ceblog.s3.amazonaws.com/wp-content/uploads/2015/06/Crazy-Egg-logo-small.png',
      ]);
  }
}

/**
 * Implements hook_library_info_build().
 * Includes Crazy Egg tracking script as a library (but doesn't insert it to the page yet)
 */
function crazyegg_library_info_build() {
  $libs = [
    'crazyegg' => [],
  ];
  $account_path = crazyegg_get_account_path();
  if ($account_path) {
    $scope = \Drupal::config('crazyegg.settings')
      ->get('crazyegg_js_scope');
    $libs['crazyegg'] = [
      'header' => $scope === 'header',
      'js' => [
        'https://script.crazyegg.com/pages/scripts/' . $account_path . '.js' => [
          'type' => 'external',
          'minified' => TRUE,
          'weight' => 0,
          'attributes' => [
            'async' => TRUE,
          ],
        ],
      ],
    ];
  }
  return $libs;
}

/**
 * Implements hook_page_attachments().
 * Checks all conditions and if they are met, injects Crazy Egg tracking script into the page
 */
function crazyegg_page_attachments(array &$attachments) {
  $crazyegg_enabled = \Drupal::config('crazyegg.settings')
    ->get('crazyegg_enabled');
  $account_path = crazyegg_get_account_path();

  // if module is enabled, there is an account ID, user role is allowed and current page matches page targeting setting
  if ($crazyegg_enabled > 0 && $account_path && crazyegg_is_page_allowed() && crazyegg_is_role_allowed()) {
    $attachments['#attached']['library'][] = 'crazyegg/crazyegg';
  }
}

/**
 * Build account path from account number/id, e.g:
 *  "1234567" => "0123/4567"
 * @return bool|string
 */
function crazyegg_get_account_path() {
  $account_id = \Drupal::config('crazyegg.settings')
    ->get('crazyegg_account_id');
  if (is_numeric($account_id)) {
    $account_path = str_pad($account_id, 8, '0', STR_PAD_LEFT);
    $account_path = substr($account_path, 0, 4) . '/' . substr($account_path, 4, 8);
    return $account_path;
  }
  return FALSE;
}

/**
 * Check if actual page URL matches patterns listed in `crazyegg_paths` variable. If the variable is empty, return true.
 * @return bool
 */
function crazyegg_is_page_allowed() {
  $paths = \Drupal::config('crazyegg.settings')
    ->get('crazyegg_paths');
  $url = Url::fromRoute('<current>')
    ->toString();
  return empty($paths) or \Drupal::service('path.matcher')
    ->matchPath($url, $paths);
}

/**
 * Check if actual user role is not mentioned in `crazyegg_roles_excluded` variable
 * @return bool
 */
function crazyegg_is_role_allowed() {
  $current_user = User::load(\Drupal::currentUser()
    ->id());
  $roles_excluded = \Drupal::config('crazyegg.settings')
    ->get('crazyegg_roles_excluded');
  if ($roles_excluded) {
    foreach ($roles_excluded as $role) {
      if (!empty($role) and $current_user
        ->hasRole($role)) {
        return FALSE;
      }
    }
  }
  return TRUE;
}

Functions

Namesort descending Description
crazyegg_get_account_path Build account path from account number/id, e.g: "1234567" => "0123/4567"
crazyegg_help Implements hook_help(). Shows help info on `/admin/help/crazyegg` page
crazyegg_is_page_allowed Check if actual page URL matches patterns listed in `crazyegg_paths` variable. If the variable is empty, return true.
crazyegg_is_role_allowed Check if actual user role is not mentioned in `crazyegg_roles_excluded` variable
crazyegg_library_info_build Implements hook_library_info_build(). Includes Crazy Egg tracking script as a library (but doesn't insert it to the page yet)
crazyegg_page_attachments Implements hook_page_attachments(). Checks all conditions and if they are met, injects Crazy Egg tracking script into the page
crazyegg_permission Implements hook_permission().