简体   繁体   English

dataLayer.push()肯定会在锚点上触发时向谷歌发送数据吗?

[英]Will dataLayer.push() definitely send data to google when triggered on an anchor?

This may seem like a simple question, but it doesn't seem to be answered anywhere that i can find. 这可能看起来像一个简单的问题,但似乎没有任何我能找到的答案。

I am writing an onClick event handler that simply calls dataLayer.push() when an anchor is clicked. 我正在编写一个onClick事件处理程序,只需在单击一个锚点时调用dataLayer.push()

Is dataLayer.push() a synchronous operation? dataLayer.push()是同步操作吗?

Will the GET request to google definitely be sent, even though the browser has unloaded the page it was requested from due to the link being followed? 是否肯定会发送谷歌的GET请求,即使浏览器已经卸载了请求的页面,因为链接被跟踪?

Some browsers show the connection get cancelled, some show it success. 一些浏览器显示连接被取消,一些显示它成功。

My question is if the computer is slow, is it possible for the page to get unloaded before the request is sent? 我的问题是,如果计算机速度慢,是否可以在发送请求之前卸载页面?

This is why i assume that google started using the eventCallback property to redirect the user after the link has been followed. 这就是为什么我假设谷歌开始使用eventCallback属性在链接被跟踪后重定向用户。 eg https://developers.google.com/tag-manager/enhanced-ecommerce#product-clicks 例如https://developers.google.com/tag-manager/enhanced-ecommerce#product-clicks

This source code does not include the click handler, but implies that the onClick event should stop propogation and let the eventCallback function set document.location . 此源代码不包含单击处理程序,但暗示onClick事件应该停止传播并让eventCallback函数设置document.location

However, as soon as you cancel the event, all its information has gone. 但是,一旦取消该活动,其所有信息都已消失。

This (in my opinion) is just the wrong way to do it. 这(在我看来)只是错误的做法。 eg (CTRL or COMMAND) + Click opens a new tab on browsers. 例如(CTRL或COMMAND)+ Click在浏览器上打开一个新选项卡。 This will not work unless the onClick event handler allows the prorogation to continue. 除非onClick事件处理程序允许prorogation继续,否则这将不起作用。

Relying on eventCallback also means that if the google scrips didn't load for one of the many reasons it could (but is still unlikely), your links don't work. 依赖eventCallback也意味着如果google scrips没有加载其中的一个原因(但仍然不太可能),你的链接不起作用。 And your site is broken. 而且你的网站坏了。

So this leaves the correct way to do it for the onClick event handler to allow the event to propagate and return true. 因此,这为onClick事件处理程序提供了正确的方法,以允许事件传播并返回true。

Which also means that dataLayer.push() would need return after the GET request was sent for any of this to work properly. 这也意味着dataLayer.push()需要在发送GET请求后才能返回,以使其中的任何一个正常工作。

Code example: NOTE: You will get mixed results in mixed environments. 代码示例:注意:您将在混合环境中获得混合结果。

<a href="/somewhere-else">Link</a>


$(document).on('click', 'a', function(event) {

    // Is dataLayer.push() guaranteed to fire a GET ?
    // data set externally
    dataLayer.push(data);

    return true;

}); });

Is there anyone out there that can guarantee that the GET request will get fired to the google server? 是否有人可以保证GET请求将被解雇到谷歌服务器?

Have the google developers forgotten something here? 谷歌开发者忘记了什么吗?

EDIT: Updated title to be more relevant to the question. 编辑:更新标题与问题更相关。

datalayer.push does not send anything to Google. datalayer.push不会向Google发送任何内容。 It pushes objects with key/value pairs to the datalayer array. 它将具有键/值对的对象推送到数据层阵列。 This might contain an event which in turn fires a tag. 这可能包含一个事件,该事件反过来触发标记。 Whether the tag is sent depends on the setup of the tag, not on the dataLayer.push. 是否发送标签取决于标签的设置,而不是dataLayer.push。

As a consequence, when you write your own click handlers your are yourself responsible to make sure your tags are actually fired. 因此,当您编写自己的点击处理程序时,您自己负责确保您的标记实际被触发。

If you use the built-in click handler you can configure a delay to make sure your tag has time to fire before the link redirects : 如果您使用内置点击处理程序,则可以配置延迟以确保您的标记在链接重定向之前有时间触发

Since link clicks usually cause the browser to load a new page and interrupt any pending HTTP request, you have the option to add a small delay to allow tags fired by Tag Manager to execute properly before redirecting to the next page. 由于链接点击通常会导致浏览器加载新页面并中断任何挂起的HTTP请求,因此您可以选择添加一个小延迟,以允许Tag Manager在重定向到下一页之前正确执行标记。 Checking the “Wait For Tags” option will delay opening of links until all tags have fired or the specified timeout has elapsed, whichever comes first. 选中“等待标记”选项将延迟链接的打开,直到所有标记都已触发或指定的超时已过,以先到者为准。

You should be able to mix both methods (push data on the click, but still use the "native" link click handler for the event). 您应该能够混合使用这两种方法(在点击上推送数据,但仍然使用事件的“本机”链接点击处理程序)。

You can also try to specify "beacon" as the transport method in your Google Analytics tags, on browsers that support this (which I think is only Chrome at the moment) GA will then use the navigator.sendBeacon interface, which sends the data even in case the page unloads. 您还可以尝试在Google Analytics代码中指定“beacon”作为传输方法 ,在支持此功能的浏览器上(我认为目前只有Chrome)GA将使用navigator.sendBeacon接口,该接口甚至会发送数据以防页面卸载。

You might think that Google's solution is not very elegant (but the simple delay has the advantage that it works for all tags, not just for GA), but they have not "forgotten" the problem. 您可能认为Google的解决方案不是很优雅(但简单延迟的优势在于它适用于所有标签,而不仅仅适用于GA),但他们并没有“忘记”这个问题。

Also solutions that combine GA hit callbacks with timeouts that redirects if the callback fails as proposed ie by Simo Ahava somewhere should be be doable with GTM, even if they are probably more cumbersome to implement in GA. 此外,如果回调失败,如果由Simo Ahava在某处提出,那么结合GA命中回调和重定向的重定向的解决方案应该是GTM可行的,即使它们在GA中实现可能更麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM