You are here

function hotjar_add_js in Hotjar 6

Insert JavaScript to the appropriate scope/region of the page.

1 string reference to 'hotjar_add_js'
hotjar_theme_registry_alter in ./hotjar.module
Implements hook_theme_registry_alter().

File

./hotjar.module, line 82
Drupal Module: Hotjar.

Code

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');
}