You are here

protected function SnippetBuilder::buildSnippet in Hotjar 8

Same name and namespace in other branches
  1. 8.2 src/SnippetBuilder.php \Drupal\hotjar\SnippetBuilder::buildSnippet()

Get Hotjar snippet code.

Return value

mixed|string Hotjar snippet.

1 call to SnippetBuilder::buildSnippet()
SnippetBuilder::saveSnippets in src/SnippetBuilder.php
Saves JS snippet files based on current settings.

File

src/SnippetBuilder.php, line 176

Class

SnippetBuilder
Class SnippetBuilder.

Namespace

Drupal\hotjar

Code

protected function buildSnippet() {

  // Use escaped HotjarID.
  $clean_id = $this
    ->escapeValue($this->settings
    ->getSetting('account'));
  $clean_version = $this
    ->escapeValue($this->settings
    ->getSetting('snippet_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.
  $script = <<<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;

  // Compact script if core aggregation or advagg module are enabled.
  if ($this
    ->isJsPreprocessEnabled() || $this->moduleHandler
    ->moduleExists('advagg')) {
    $script = str_replace([
      "\n",
      '  ',
    ], '', $script);
  }
  return $script;
}