You are here

protected function GTMContainer::scriptSnippet in GoogleTagManager 7.2

Returns JavaScript script snippet.

Return value

array The script snippet.

1 call to GTMContainer::scriptSnippet()
GTMContainer::snippets in includes/entity/container.inc
Returns array of JavaScript snippets.

File

includes/entity/container.inc, line 228

Class

GTMContainer
Defines the container configuration entity.

Code

protected function scriptSnippet() {

  // Gather data.
  $compact = \GTMSettings::getInstance()
    ->get('compact_snippet');
  $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;
  if ($compact) {
    $script = str_replace(array(
      "\n",
      '  ',
    ), '', $script);
  }
  return $script;
}