You are here

protected function Container::scriptSnippet in GoogleTagManager 8

Returns JavaScript script snippet.

Return value

array The script snippet.

1 call to Container::scriptSnippet()
Container::snippets in src/Entity/Container.php
Returns array of JavaScript snippets.

File

src/Entity/Container.php, line 257

Class

Container
Defines the container configuration entity.

Namespace

Drupal\google_tag\Entity

Code

protected function scriptSnippet() {

  // Gather data.
  $container_id = $this
    ->variableClean('container_id');
  $data_layer = $this
    ->variableClean('data_layer');
  $query = $this
    ->environmentQuery();

  // Build script snippet.
  $script = <<<EOS
(function(w,d,s,l,i){
  w[l]=w[l]||[];
  w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});
  var f=d.getElementsByTagName(s)[0];
  var j=d.createElement(s);
  var dl=l!='dataLayer'?'&l='+l:'';
  j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl+'{<span class="php-variable">$query</span>}';
  j.async=true;
  f.parentNode.insertBefore(j,f);
})(window,document,'script','{<span class="php-variable">$data_layer</span>}','{<span class="php-variable">$container_id</span>}');
EOS;
  return $this
    ->compactSnippet($script);
}