You need to enable JavaScript to run this app. Angular无法识别index.html中配置的Google gtag报404错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Angular无法识别index.html中配置的Google gtag报404错误如何解决

Angular项目配置GTM运行异常问题

按照谷歌官方指引,在项目index.html的head区块中添加GTAG配置后,运行时触发报错。

修改后的index.html代码

<!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>

控制台报错详情

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

配套报错提示:
Failed to load resource: the server responded with a status of 404 ()

报错截图:
enter image description here

初步判断与需求

  • 初步判定insertBefore()方法未被浏览器识别
  • 伴随触发404资源加载失败问题
  • 核心需求:找到适配方案,让Angular项目正常识别index.html中配置的GTAG

内容的提问来源于stack exchange,提问作者bbusdriver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.02 16:15:08