function gtm_page_attachments in GoogleTag Manager 8
Implements hook_page_attachments().
File
- ./
gtm.module, line 11 - Module gtm.module.
Code
function gtm_page_attachments(array &$page) {
$config = \Drupal::config('gtm.settings');
$is_admin = \Drupal::service('router.admin_context')
->isAdminRoute();
$admin_pages = $config
->get('admin-pages') || !$is_admin;
if (\Drupal::currentUser()
->id() == 1 && $config
->get('admin-disable')) {
$admin_pages = FALSE;
}
if ($config
->get('enable') && $config
->get('google-tag') && $admin_pages) {
$google_tag = $config
->get('google-tag');
$script = "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':\nnew Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],\nj=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=\n'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);\n})(window,document,'script','dataLayer','{$google_tag}');\n";
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#value' => $script,
],
'gtm',
];
}
}