You are here

public function GTMContainer::dataLayerSnippet in GoogleTagManager 7.2

Returns JavaScript data layer snippet or adds items to data layer.

Return value

string|null The data layer snippet or NULL.

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

File

includes/entity/container.inc, line 289

Class

GTMContainer
Defines the container configuration entity.

Code

public function dataLayerSnippet(array &$classes = array()) {

  // Gather data.
  $data_layer = $this
    ->variableClean('data_layer');
  $whitelist = $this
    ->get('whitelist_classes');
  $blacklist = $this
    ->get('blacklist_classes');
  $classes = array();
  $names = array(
    'whitelist',
    'blacklist',
  );
  foreach ($names as $name) {

    // Inline code from google_tag_text_clean($$name, 'array');
    ${$name} = explode("\n", ${$name});
    ${$name} = array_map('trim', ${$name});
    ${$name} = array_filter(${$name}, 'trim');
    if (empty(${$name})) {
      continue;
    }
    $classes["gtm.{$name}"] = ${$name};
  }
  if ($classes) {

    // Build data layer snippet.
    $script = "var {$data_layer} = [" . drupal_json_encode($classes) . '];';
    return $script;
  }
}