You are here

function gtm_page_top in GoogleTag Manager 8

Implements hook_page_top().

File

./gtm.module, line 36
Module gtm.module.

Code

function gtm_page_top(array &$page_top) {
  $config = \Drupal::config('gtm.settings');
  $is_admin = \Drupal::service('router.admin_context')
    ->isAdminRoute();
  $admin_pages = $config
    ->get('admin-pages') || !$is_admin;
  if ($config
    ->get('enable') && $config
    ->get('google-tag') && $admin_pages) {
    $google_tag = $config
      ->get('google-tag');
    $src = "https://www.googletagmanager.com/ns.html?id={$google_tag}";
    $page_top['gtm'] = [
      '#noscript' => TRUE,
      '#type' => 'html_tag',
      '#tag' => 'iframe',
      '#attributes' => [
        'src' => $src,
        'height' => 0,
        'width' => 0,
        'style' => 'display:none;visibility:hidden;',
      ],
    ];
  }
}