Html Facebook 转换像素代码:BODY 与 HEAD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21440813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:45:10  来源:igfitidea点击:

Facebook Conversion Pixel code: BODY vs HEAD

htmlfacebooktracking

提问by cockypup

Can I place the Facebook Conversion Pixel inside the BODY (as opposed to inside the HEAD, which is what Facebook suggest in their specs?

我可以将 Facebook 转换像素放在 BODY 内(而不是在 HEAD 内,这是 Facebook 在其规范中建议的内容?

I don't see why not. Has anybody tried it?

我不明白为什么不。有人试过吗?

Here is an example of the code

这是代码的示例

<script type="text/javascript">
        var fb_param = {};
        fb_param.pixel_id = '123456789';
        fb_param.value = '10';
        fb_param.currency = 'USD';
        (function(){
            var fpw = document.createElement('script');
            fpw.async = true;
            fpw.src = '//connect.facebook.net/en_US/fp.js';
            var ref = document.getElementsByTagName('script')[0];
            ref.parentNode.insertBefore(fpw, ref);
        })();
    </script>
    <noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/offsite_event.php?id=1234566&amp;value=10&amp;currency=USD" /></noscript>

采纳答案by Michal Trojanowski

According to this answer here: https://www.facebook.com/help/community/question/?id=10200354561858276

根据这里的答案:https: //www.facebook.com/help/community/question/?id=10200354561858276

you can place it inside the <BODY>element but FB recommends to keep it at the beginning of <BODY>or in the <HEAD>as the conversion will be then counted even if the page does not load fully or the user closes the page.

您可以将它放在<BODY>元素内,但 FB 建议将其保留在元素的开头<BODY>或元素中,<HEAD>因为即使页面未完全加载或用户关闭页面,转换也会被计算在内。

回答by Vaibhav Kumar

You can put fb pixel code anywhere in the doc. It will work. Tested

您可以将 fb 像素代码放在文档中的任何位置。它会起作用。已测试

If you put in the end of the body, there is one drawback, you won't get the pixel count if the user closed the browser (case - the page is not loaded fully).

如果放在正文的末尾,则有一个缺点,如果用户关闭浏览器(情况 - 页面未完全加载),您将无法获得像素数。

But if it is present in the head, you will get the pixel count in case of the page is not loaded properly.

但是,如果它出现在头部,您将在页面未正确加载的情况下获得像素数。

回答by cockypup

Apparently, yes. It works in the BODY. Tested.

显然,是的。它适用于身体。测试。

回答by Mladen B.

What I did is to place the <script>part of the code in the HEADsection and <noscript>part at the end of the BODYsection. Why?

我所做的是<script>将代码的一部分放在HEAD节中,将<noscript>部分放在节的末尾BODY。为什么?

If the Javascript is enabled, and if it is important to you to count every hit to your page, no matter if the page has been fully loaded or not, this will still work.

如果启用了 Javascript,并且如果您对页面的每次点击进行计数很重要,无论页面是否已完全加载,这仍然有效。

If the Javascript is not enabled, the <noscript>part will also be executed, without causing the disturbance in the dom model layout (on my page, it added some bogus height to that 1px image, for god knows what reason) and, most importantly, it WILL validate in the W3C HTML validator. If you leave the noscript part in the head section, the img tag within noscript will cause a validation error.

如果未启用 Javascript,该<noscript>部分也将被执行,而不会造成 dom 模型布局的干扰(在我的页面上,它为该 1px 图像添加了一些虚假高度,天知道是什么原因),最重要的是,它将在 W3C HTML 验证器中进行验证。如果将 noscript 部分留在 head 部分,noscript 中的 img 标签将导致验证错误。

回答by Tobi

The provided code will place the reference to the FB script before the first occurence of a tag anyway, see:

无论如何,提供的代码将在第一次出现标记之前放置对 FB 脚本的引用,请参阅:

var ref = document.getElementsByTagName('script')[0];
ref.parentNode.insertBefore(fpw, ref);

What exactly is the reason why you want to change this?

你想改变这个的确切原因是什么?