Google跟踪代码管理器延迟window.load? [英] Google Tag Manager delaying window.load?

查看:174
本文介绍了Google跟踪代码管理器延迟window.load?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面在加载时触发ajax调用

I have a page that fires an ajax call on load

$(window).load(function() {
  updateDeliverySlots();
});

我的正文顶部还有Google跟踪代码管理器javascript(我知道当前的建议是将其置于 head 部分,但我们还没有更新代码,并且认为这不是问题的根源。

I also have Google Tag Manager javascript at the top of my body (I know the current suggestion is to place this in the head section, but we haven't updated the code yet, and don't think that is the source of the problem).

<body>
        <!-- Google Tag Manager -->
<noscript>
    <iframe src="//www.googletagmanager.com/ns.html?id=XXXX" height="0" width="0" style="display:none;visibility:hidden">
    </iframe>
</noscript>
<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='//www.googletagmanager.com/gtm.jsid='+i+dl;f.parentNode.insertBefore(j,f);
})
(window,document,'script','dataLayer','XXXX');
</script>
<!-- End Google Tag Manager -->
....
</body>

我对GTM标记中包含的脚本之一进行的调用有问题。该脚本向第三方发送请求。如果请求因任何原因无法连接(例如服务器关闭),则在请求超时(60秒)之前,我的updateDeliverySlots()函数(以及其中的ajax调用)不会被触发。我还发现页面仍在加载(根据浏览器图标)。

I have a problem with one of the calls made by a script included in a GTM tag. This script sends a request to a third party. If the request can't connect for whatever reason (eg. server being down) my updateDeliverySlots() function (and the ajax call therein) is not fired until the request times out (60 seconds). I also find that the page is still 'loading' (according to the browser icons).

我的GTM实现中有什么东西我做错了吗?据我了解,Google Tags背面的所有内容都应该是异步的,并且与页面的准备就绪无关。

Is there something in my GTM implementation that I am doing wrong? It was my understanding that everything fired on the back of Google Tags should be asynchronous and would have no bearing on the 'readiness' of the page.

推荐答案

同步脚本必须获取并在处理到达时运行,异步脚本 可能在处理完毕后随时运行。

Synchronous scripts must be fetched and run when processing reaches them, async scripts may run at any time after processing reaches them.

因此,根据服务器/页面控件之外的时间,异步脚本可以:

Depending on timing that is outside of the server/pages control, an async script can therefore:


  • 在文档完成之前添加同步资源,其时间延迟加载事件

  • 在处理页面后添加同步资源,不会影响加载事件。

  • 添加一个不会对页面产生重大影响的异步资源(除非它反过来向页面添加同步资源)

您可以选择以下几个选项之一来解决这个问题:

You may choose one of several options to resolve this in GTM:


  1. 修复自定义脚本以使用异步资源

  2. 将触发触发器更改为页面视图 - >已加载窗口

  3. 使用谷歌提供的标签(如果可用)替换脚本,因为它们应构建为仅使用异步资源

  4. 在GTM中阻止自定义脚本(这也会阻止相关跟踪)

  1. fix the custom scripts to use async resources
  2. change the firing trigger to page view -> window loaded
  3. replace the scripts with google provided tags (if available) as they should be constructed to use only async resources
  4. Block custom scripts in the GTM (which will also prevent the related tracking)

将异步更改为推迟您的GTM代码段。

Change async to defer on your GTM snippet.

即:

  <!-- 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.defer=true;j.src=
  'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  })(window,document,'script','dataLayer','GTM-XXXXX');</script>
  <!-- End Google Tag Manager -->

由于这会将整个GTM流程移至页面完全解析后,因此会破坏Google Optimizer 标签和早期页面跟踪。

Since this moves the entire GTM process to after the page is fully parsed, it will break "Google Optimizer" tags and early page tracking.

这篇关于Google跟踪代码管理器延迟window.load?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆