英文:
dataLayer.push() data isn't shown in GTM preview
问题
我正在使用GTM设置时遇到问题。我尝试在点击按钮时传递自定义事件,但是GTM预览没有显示此事件被跟踪的迹象。
我已经通过在GTM中添加特殊操作-> donate_button_click 来添加触发器。
然后在我的HTML中,我通过以下方式初始化数据层:
<script>
dataLayer = [];
</script>
在主要的GTM标签之前。之后,我尝试在锚点标签中推送我的动作,使用以下代码:
<a href="#XJKDHSTQ" style="display: none" onclick="dataLayer.push({'event':'donate_button_click'});"></a>
但是GTM没有显示任何被跟踪的事件。
英文:
I am pushing my head against the wall with GTM setup. I am trying to pass custom event upon clicking a button but GTM preview shows no signs of this event being tracked.
I've added trigger by adding special action -> donate_button_click in GTM
Then in my HTML i am initialising datalayer with
<script>
dataLayer = [];
</script>
Before the main GTM tag. After that i am trying to push my action in anchor tag by using
<a href="#XJKDHSTQ" style="display: none" onclick="dataLayer.push({'event':'donate_button_click'});"></a>
But GTM shows none of the events tracked.
答案1
得分: 3
Google Tag Manager(GTM)的设置有时可能会有些棘手。以下是一些您可以检查和可能修复以排除此问题的事项。
脚本的顺序:确保dataLayer脚本位于您的HTML中Google Tag Manager容器脚本之上。
<script>
window.dataLayer = window.dataLayer || [];
</script>
<!-- Google Tag Manager -->
<!-- ... -->
<!-- End Google Tag Manager -->
dataLayer.push()的语法:确保您使用了正确的语法。在您的示例中看起来是正确的,但始终要仔细检查。
确保GTM容器已正确安装:确保Google Tag Manager代码段已正确安装在您的页面上。使用Google的Tag Assistant验证它,或者检查页面源代码。
检查GTM中的触发器设置:验证GTM中的触发器是否已正确设置。触发器中的事件名称应与您推送到dataLayer的事件名称完全匹配。在您的情况下,它应该是'donate_button_click'。
元素的可见性:您的锚标签上有"display: none",这可能会阻止按钮被点击,从而阻止事件触发。确保元素可见且可点击。
使用控制台日志进行调试:在您的onclick事件中,在dataLayer.push()之前,您可以添加一个console.log语句,以验证点击事件是否起作用。如果控制台日志语句显示在浏览器的控制台中,但dataLayer.push()仍然不起作用,那么问题很可能出在GTM或dataLayer.push()语句上。
<a href="#XJKDHSTQ" style="display: none" onclick="console.log('button clicked'); dataLayer.push({'event':'donate_button_click'});"></a>
在JavaScript控制台中检查:打开您的网站并转到开发工具中的JavaScript控制台(右键单击页面上的任何位置,然后单击检查)。键入dataLayer并按回车键。这将输出dataLayer的当前状态。每次单击按钮时,都应将新对象添加到此数组中。如果没有添加,那么您推送事件到dataLayer的方式可能有问题。
英文:
Google Tag Manager (GTM) setup can sometimes be a bit tricky. Here are a few things you can check and possibly fix to troubleshoot this issue.
Order of scripts: Make sure the dataLayer script is placed above the Google Tag Manager container script in your HTML.
<script>
window.dataLayer = window.dataLayer || [];
</script>
<!-- Google Tag Manager -->
<!-- ... -->
<!-- End Google Tag Manager -->
Syntax of dataLayer.push(): Make sure you are using the correct syntax. It looks correct in your example, but always double-check.
Ensure GTM Container is Installed Correctly: It's important to make sure that the Google Tag Manager snippet is installed correctly on your page. Verify it using Tag Assistant by Google or check the page source.
Check the 'Trigger' settings in GTM: Verify if the trigger is set correctly in GTM. The Event name in the trigger should exactly match the one you're pushing to the dataLayer. In your case, it should be 'donate_button_click'.
Visibility of the element: You have display: none on your anchor tag, which might prevent the button from being clicked and thereby the event from firing. Make sure the element is visible and clickable.
Use console logs for debugging: In your onclick event, you could add a console.log statement before the dataLayer.push() to verify that the click event is working. If the console log statement shows up in the browser's console, but the dataLayer.push() is still not working, then the issue is most likely with GTM or the dataLayer.push() statement.
<a href="#XJKDHSTQ" style="display: none" onclick="console.log('button clicked'); dataLayer.push({'event':'donate_button_click'});"></a>
Check in JavaScript console: Open your website and go to the JavaScript console in developer tools (right-click anywhere on the webpage and click inspect). Type dataLayer and hit enter. This will output the current state of dataLayer. Every time you click on the button, a new object should be added to this array. If it's not, there's something wrong with the way you're pushing events to the dataLayer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论