如何处理由Google Tag Manager(GTM.JS)引起的TypeError?
我有一个使用Google Analytics(分析)和Google标签管理器的网站。它已经存在了几天,但是今天GTM脚本(GTM.JS)开始在控制台中给我错误:“ und offerate typeError:分配到常量变量”。反过来,这导致我的一些自定义JS无法按预期工作。
如下所示,GTM脚本作为第二个标签以及在开场字体之后的第二个标签包括:[https://developers.google.com/tag-platform/tag-manager/web] 1 [1
]我在控制台中遇到的错误是:
Uncaught TypeError: Assignment to constant variable.
at <anonymous>:1:157
at <anonymous>:1:260
at gtm.js?id=GTM-XXX:271:414
at gtm.js?id=GTM-XXX:272:118
at b (gtm.js?id=GTM-XXX:272:337)
at dc (gtm.js?id=GTM-XXX:47:276)
at e (gtm.js?id=GTM-XXX:132:34)
at gtm.js?id=GTM-XXX:31:130
at Array.<anonymous> (gtm.js?id=GTM-XXX:133:454)
at Object.execute (gtm.js?id=GTM-XXX:133:194)
有人认识到这个问题吗?此错误发生在Chrome(MAC和Windows),边缘,但不在Firefox(Mac和Windows)中。 Safari给我一个类似的错误“ TypeError:试图分配给ReadOnly属性”。
I have a website which uses Google Analytics and Google Tag Manager. It has been live for a couple of days but today the GTM script (gtm.js) started giving me errors in the console: "Uncaught TypeError: Assignment to constant variable". This, in turn, is causing some of my custom JS to not work as intended.
The GTM script is included as the second tag in as well as a immediately after the opening body tag, as instructed here : [https://developers.google.com/tag-platform/tag-manager/web][1]
This is the error that I get in the console:
Uncaught TypeError: Assignment to constant variable.
at <anonymous>:1:157
at <anonymous>:1:260
at gtm.js?id=GTM-XXX:271:414
at gtm.js?id=GTM-XXX:272:118
at b (gtm.js?id=GTM-XXX:272:337)
at dc (gtm.js?id=GTM-XXX:47:276)
at e (gtm.js?id=GTM-XXX:132:34)
at gtm.js?id=GTM-XXX:31:130
at Array.<anonymous> (gtm.js?id=GTM-XXX:133:454)
at Object.execute (gtm.js?id=GTM-XXX:133:194)
Does anyone recognize this issue? This error occurs in Chrome (Mac and Windows), Edge but not in Firefox (neither Mac nor Windows). Safari gives me a similar error "TypeError: Attempted to assign to readonly property."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,如果您有GTM,通常不需要GA脚本。 GTM完全有能力管理GA。
现在,要正确调试它,请打开DevTools中的抽屉,然后转到网络请求阻止器工具。将新的阻止规则添加到阻塞
GTM
。重新加载页面,查看错误是否消失。如果不是,那么问题就不在GTM中。如果它确实消失了,我们将切换到GTM调试。转到GTM,打开站点预览,在预览窗口中获取错误,查看在错误发生之前触发的标签,然后尝试暂停标签。查看是否暂停它们,您可以指出哪个引起问题。
或者,您可以使用重定向器扩展名(或类似的扩展程序)用一个完全空的(但已发布)容器替换GTM容器,并查看是否发生了该错误。如果仍然使用一个空容器发生,并且您没有在CHTML标签/CJS vars/模板中重写const(例如重写const),则可能是您的前端通过某些globals或覆盖的本地功能干扰GTM,或好吧,请进一步调试,以便我们知道在哪里挖掘。
First of all, you typically don't need a GA script if you have GTM. GTM is perfectly capable of managing GA.
Now to debug it properly, open the drawer in your devtools and go to the Network Request Blocker tool. Add a new blocking rule to block
gtm
. Reload the page, see if the error disappears. If it doesn't, then the issue is not in gtm. If it does disappear then we switch to gtm debugging.Go to GTM, open site preview, get the error in the preview window, see what tags fired right before the error happened and try pausing the tags. See if by pausing them you can indicate which one causes the issue.
Or you could use a redirector extension (or a similar one) to replace your gtm container with a completely empty (but published) container and see if that error occurs. If it still occurs with an empty container, and you didn't make any trivial mistakes like rewriting a const in your CHTML tags/CJS vars/templates, it's likely your front-end interfering with GTM through some globals or overriding native functions, or, well, debug it further so we would know where to dig.
如果您删除自定义JS标签,我怀疑您会发现错误已解决。
给出的错误是非常具体的 - 常数试图分配。我怀疑这是在您自己提供的堆栈跟踪底部执行的您自己的代码中发生的。
简而言之,在您的代码中的某个地方,我怀疑您有以下内容:
这会失败,因为
const
关键字指示两个变量都不能重新启动或重新分配在同一块范围内。相反,它应该阅读以下内容:
使用'var'语句
var 声明一个名为'myvar'的变量,并将值'Hello'分配给它。可以在同一范围内对变量进行其他任务。
使用'let'语句
let 也声明一个名为“ MyVar”的变量,并为其分配了“ Hello”的值。可以在同一块范围内对该变量进行其他分配,但是变量本身不能重新定义。
将“ const”的使用与任何变量交换,其中您将新值分配给变量,最初使用
LET
关键字而不是解决您的错误。If you remove your custom JS tags, I suspect you'll see the error is resolved.
The error given is quite specific - there's an attempted assignment to a constant. I suspect this is happening within your own code as executed by GTM per the bottom of the stack trace you provided.
Put simply, somewhere in your code, I suspect you have something like the following:
This fails because the
const
keyword indicates that the variable both cannot be redeclared nor reassigned in the same block scope.It should instead read either of the following:
Using a 'var' statement
Var declares a variable named 'myVar' and assigns the value 'hello' to it. Other assignments can be made to the variable in the same scope.
Using a 'let' statement
Let also declares a variable named 'myVar' and assigns a value of 'hello' to it. Other assignments can be made to this variable in the same block scope, but the variable itself cannot be redeclared.
Swapping out the use of 'const' with any variable where you're assigning a new value to a variable and initially using the
let
keyword instead should resolve your error.