Search code examples
angulargoogle-tag-manager

Angular doesn't recognize Google gtag in index.html


Followed the instruction from google and added gtag in index.html in head section. However, I'm getting an error.

<!doctype html>
<html lang="en">

<head>
  <base href="/">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Hello</title>
  <!-- Favicon -->
  <link href="./assets/img/favicon.ico" rel="icon" type="image/png">
  <!-- Fonts -->
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
  <!-- Google Tag Manager -->
  <script>(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],
        j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
          'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
    })(window, document, 'script', 'dataLayer', 'GTM-01jd9k');</script>
  <!-- End Google Tag Manager -->
</head>

<body>
  <app-root></app-root>
</body>

</html>

Looking at the console,

(index):30 GET https://www.googletagmanager.com/gtm.js?id=GT..... net::ERR_ABORTED 404

Looks like insertBefore() is not recognized.

Failed to load resource: the server responded with a status of 404 ()

enter image description here

How can angular recognize gtag in index.html? Any help is appreciated!


Solution

  • Use angular-google-tag-manager npm package for google tag manager. I have used it. It is awesome package.