等待Facebook像素加载 [英] Wait for Facebook Pixel Load

查看:196
本文介绍了等待Facebook像素加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的JavaScript代码中检测到Facebook像素的加载已完成.这可能吗?

I want to detect in my JavaScript code that the load of a Facebook pixel has been completed. Is this possible?

以下是供参考的Facebook像素跟踪代码:

For reference here is the Facebook Pixel Tracking code:

<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','//connect.facebook.net/en_US/fbevents.js');
// Insert Your Facebook Pixel ID below. 
fbq('init', 'FB_PIXEL_ID');
fbq('track', 'PageView');
</script>
<!-- Insert Your Facebook Pixel ID below. --> 
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=FB_PIXEL_ID&amp;ev=PageView&amp;noscript=1"
/></noscript>

简而言之,似乎fbq('init', ...)会导致添加script标记并添加async属性并将src设置为//connect.facebook.net/en_US/fbevents.js.

Breaking it down, it seems that fbq('init', ...) causes a script tag to be added with the async attribute set and src set to //connect.facebook.net/en_US/fbevents.js.

随后,对fbq('track', ...)的调用以某种方式通过一个重定向导致对图像的HTTP GET.

Subsequently the call to fbq('track', ...) somehow causes an HTTP GET to an image via one redirect.

如何检测所有步骤是否已完成,尤其是最终图像加载是否已完成?

How to detect that all the steps are complete, especially that the final image load is complete?

推荐答案

我对此表示怀疑.给定名为myCallback的回调函数和名为myId的私有ID,请使用以下代码:

I took a stab at this. Given a callback function named myCallback and your private id named myId, use this code:

  (function wait() {
    // check for the generic script
    // easiest check to prevent race condition
    if (fbq.version > '2.0') {
      // now check for the custom script
      // `fbq.on` is now ready (`fbq.once` would be better but has a bug)
     fbq.on('configLoaded', function (name) {
        if (name === myId) {
          myCallback();
        }
      });
    } else {
      setTimeout(wait, 10);
    }
  })();

一切准备就绪后,将调用

myCallback.从fbq.version="2.7.17";

myCallback will be called once everything is ready. This is accurate as of fbq.version="2.7.17";

参考 https://github.com/poteto/ember-metrics/pull/151

这篇关于等待Facebook像素加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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