You are here

function hotjar_page_build in Hotjar 7

Implements hook_page_build().

Insert JavaScript to the <head> tag of the page.

File

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

Code

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