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 ()
报错截图:
初步判断与需求
- 初步判定
insertBefore()方法未被浏览器识别 - 伴随触发404资源加载失败问题
- 核心需求:找到适配方案,让Angular项目正常识别index.html中配置的GTAG
内容的提问来源于stack exchange,提问作者bbusdriver
相关产品推荐
相关产品推荐

