Google标签管理器中的代码不起作用
我有一个测试任务,而我完成了2个。 但是我不明白我需要做什么?!
我设法找到了语法错误: 首先应该是:...函数tomefunctionName(){...}
或者 (function(){...})()
...第二,它是匿名函数...
任务: 该脚本是在GTM中执行的,并通过自定义任务在Google Analytics(分析)中实现。
该脚本将有关用户行为的信息发送到Optimozg服务器,然后将其发送到BigQuery(bq.php文件进程并转发数据)。 Optimozg服务器数据正确输入,但是Google Analytics(分析)中的数据尚未到达。
什么原因?
您如何修复它?
暗示: (使用GTM在您的网站实例上测试代码)
function(){return function(tracker){if("undefined"===typeof tracker.get("BigQueryStreaming")){var f=tracker.get("sendHitTask"),h=function(){function d(c){var a=!1;try{document.createElement("img").src=e(!0)+"?"+c,a=!0}catch(k){}
return a}
function e(c){var a="https://test.optimozg.com/bq/bq-test.php";c||(a+="?tid="+encodeURIComponent(tracker.get("trackingId")));return a}
return{send:function(c){var a;if(!(a=2036>=c.length&&d(c))){a=!1;try{a=navigator.sendBeacon&&navigator.sendBeacon(e(),c)}catch(g){}}
if(!a){a=!1;var b;try{window.XMLHttpRequest&&"withCredentials" in(b=new XMLHttpRequest)&&(b.open("POST",e(),!0),b.setRequestHeader("Content-Type","text/plain"),b.send(c),a=!0)}catch(g){}}
return a||d(c)}}}();tracker.set("sendHitTask",function(d){h.send(d.get("hitPayload"));tracker.set("BigQueryStreaming",!0)})}}}
I have a test tasks and 2 from 3 I've done.
But this one I don't understand how and what I need to do?!
I managed to find syntax error:
At first should be:...function someFunctionName() {...}
or(function() {...})()
...second it's anonymous function...
TASK:
This script is executed in GTM and implemented in Google analytics by custom Task.
The script sends information about user behavior to Optimozg server and then to Bigquery (bq.php file processes and forwards data). Optimozg server data is coming in correctly, but the data in Google Analytics does not reach.
What is the reason?
How do you fix it?
Hint:
(test the code on your site instance with GTM)
function(){return function(tracker){if("undefined"===typeof tracker.get("BigQueryStreaming")){var f=tracker.get("sendHitTask"),h=function(){function d(c){var a=!1;try{document.createElement("img").src=e(!0)+"?"+c,a=!0}catch(k){}
return a}
function e(c){var a="https://test.optimozg.com/bq/bq-test.php";c||(a+="?tid="+encodeURIComponent(tracker.get("trackingId")));return a}
return{send:function(c){var a;if(!(a=2036>=c.length&&d(c))){a=!1;try{a=navigator.sendBeacon&&navigator.sendBeacon(e(),c)}catch(g){}}
if(!a){a=!1;var b;try{window.XMLHttpRequest&&"withCredentials" in(b=new XMLHttpRequest)&&(b.open("POST",e(),!0),b.setRequestHeader("Content-Type","text/plain"),b.send(c),a=!0)}catch(g){}}
return a||d(c)}}}();tracker.set("sendHitTask",function(d){h.send(d.get("hitPayload"));tracker.set("BigQueryStreaming",!0)})}}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道为什么JS开发人员应该对GTM了解任何知识。他们通常不去那里。
但是,是的,要了解如何正确使用给定代码,只需阅读本文: https ://www.simoahava.com/analytics/customtask-the-guide/ 它描述了什么是自定义任务以及如何使用它们。
好的,所以首先创建一个GTM帐户。在您的网站上部署GTM代码。也可以使用本地站点。或者,相反,将局部扩展名注入GTM代码到尚无GTM的随机站点。或者,也许使用重定向器扩展名将其GTM请求重定向到您的,取决于您。
之后,您只需在GTM中制作一个标签,该标签将发送通用分析页面浏览量。不幸的是,GA4决定不打扰自定义任务。然后,您可以在PageView上进行触发。您将触发器分配给标签。不要忘记至少发布一次工作空间以进行测试。然后您预览。预览是右上角的GTM中的CTA,在出版物附近。基本上是一个整洁的GTM调试器。输入部署/注入GTM片段的站点。确保预览会在页面加载上看到您的标签启动。那意味着您正确做准备。
顺便说一句,我们在这里进行提示部分。现在,您需要在GTM中制作自定义的JavaScript变量,并按照其中的形式粘贴代码段。它想要在匿名函数中代码的原因是因为它会自行运行它作为封闭。因此,他们有点消除了额外()()的需求。这主要是为不认识JS的人做的,所以不要感到惊讶。
好的,您已经制作了CJS var,现在转到您的标签,并完全按照Simo在他的文章中显示的范围设置您的惯例:
好,现在发布您的容器,转到您拥有的网站它部署了,打开网络选项卡并重新加载页面。
检查对BQ的呼叫和OptimozG端点。我相信,现在他们问的是为什么标签要发送的原始电话不会发送。因此,如果您删除了“范围”的设置,然后发布并重新加载页面,则应看到
collect
端点的请求,这是GA的数据跟踪的端点。如果您重新添加了“定制码”代码,则可以防止执行正常标签的功能,因此无collect
呼叫。他们想从您那里听到的是如何使标签与他们的Optimzg和BQ呼叫一起发射原始事件。
最有可能的答案非常简单,优雅,但需要很多调试才能到达。阅读Simo的文章将有助于理解设定各种任务的重要性。
嗯,好的,我不是要真正调试它,但是看起来我发现了这个错误。它在
var f = tracker.get(“ sendhittask”)
中用于存储原始的sendhittask函数,但永远不会使用。这是为什么?基本上,您只需要在最后一行中设置的新sendhittask函数中调用该函数即可。我不会在我的GTM中调试它,但我很确定这是问题所在。有点乞求在那里找到。另外,这并不是一项初级JS开发任务。这是一项高级跟踪实施任务。基本上,加拿大和美国约为110美元/小时。我想,Junior JS开发人员约为35美元/小时。他们只是想省钱,呵呵。我正在考虑雇用Junior JS开发人员而不是跟踪实现者,但是很难教授数据分析如何在所有不同工具中起作用。
Not sure why JS devs should know anything about GTM. They typically don't go there.
But yes, to understand how to use the given code properly, just read this article: https://www.simoahava.com/analytics/customtask-the-guide/ it describes what custom tasks are and how to use them.
Ok, so first make a GTM account. Deploy the GTM code on your site. May as well use a local site. Or, rather, have the GTM code being injected by a local extension to a random site that doesn't have GTM yet. Or maybe use a redirector extension to redirect the request for their GTM to yours, up to you.
After that, you just make a tag in GTM that would send a Universal Analytics pageview. GA4 decided not to bother with custom tasks, unfortunately, so UA only. Then you make a trigger on pageview. You assign the trigger to the tag. Don't forget to publish the workspace at least once for it to be testable. Then you preview. Preview is a CTA in GTM in top right corner, near the publish. Basically a neat GTM debugger. Enter the site where you have your GTM snippet deployed/injected. Make sure preview sees your tag firing on page load. That would mean you did the preparation correctly.
We're doing the Hint section here, by the way. Now you need to make a custom javascript variable in GTM, paste the code snippet as is in there. The reason why it wants the code in an anonymous function is because it will run it as a closure on it's own. So they kinda remove the need of the extra ()(). It's mostly done for people who don't know JS, so don't be surprised.
Ok, you've made the CJS var, now go to your tag, and set your customTag exactly as Simo shows in his article:
Good, now publish your container, go to the site where you have it deployed, open the network tab and reload the page.
Inspect the calls to the BQ and Optimozg endpoints. Now what they ask is, I believe, why the original call that is meant to be sent by the tag is not being send. So if you remove the setting of the customTask, then publish and reload the page, you should see a request to the
collect
endpoint, which is the GA's endpoint for data tracking. If you re-add the customTask code, it will prevent the normal tag's functionality from execution, so nocollect
call.What they want to hear from you is how to make the tag fire the original event alongside their optimozg and bq calls.
Most likely, the answer is pretty simple and elegant, but requires a lot of debugging to reach to. Reading Simo's article will help understanding the significance of setting various tasks.
Uh, ok, I didn't mean to really debug it, but it looks like I found the bug. It's in the
var f = tracker.get("sendHitTask")
It's being used to store the original sendHitTask function, but it never gets used. Why is that? Basically, you just need to call the function in the new sendHitTask function that you set in the last line. I'm not going to debug it in my GTM, but I'm pretty sure that's the issue. It's kinda begging to be found there.Also, this is not quite a junior JS dev task. It's a senior tracking implementation task. Basically, about $110/hr in Canada and US. Junior JS devs are around $35/hr, I guess. They're just trying to save money, heh. I was thinking of hiring junior JS devs instead of tracking implementators too, but it's hard to teach how data analysis works in all the different tools.