JavaScript和Facebook像素

时间:2015-06-18 15:25:44

标签: javascript facebook variables

我想知道这个脚本是否有效?

我必须在15页上实现这个facebook脚本并在2个位置替换FacebookId变量 - 在window._fbq.push()和URL中。感谢

                <script type="text/javascript">
            if(window.location.pathname == '/test') {
            FacebookId = '123123123';
            }
            if(window.location.pathname == '/test2') {
            FacebookId = '34534534534';
            }

            <script>(function() {
            var _fbq = window._fbq || (window._fbq = []);
            if (!_fbq.loaded) {
            var fbds = document.createElement('script');
            fbds.async = true;
            fbds.src = '//connect.facebook.net/en_US/fbds.js';
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(fbds, s);
            _fbq.loaded = true;
            }
            })();
            window._fbq = window._fbq || [];
            window._fbq.push(['track', FacebookId , {'value':'0.01','currency':'USD'}]);
            </script>
            <noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev="+FacebookId+"&amp;cd[value]=0.01&amp;cd[currency]=USD&amp;noscript=1" /></noscript>

            }
            </script>

1 个答案:

答案 0 :(得分:0)

这是JavaScript部分的解决方案。 noscript部分不适合你

<script type="text/javascript">
var path = window.location.pathname, FacebookId="";
if(path.indexOf('/test/') !=-1) {
  FacebookId = '123123123';
}
if(path.indexOf('/test2/') !=-1) {
  FacebookId = '34534534534';
}
(function() {
  var _fbq = window._fbq || (window._fbq = []);
  if (!_fbq.loaded) {
    var fbds = document.createElement('script');
    fbds.async = true;
    fbds.src = '//connect.facebook.net/en_US/fbds.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(fbds, s);
    _fbq.loaded = true;
  }
})();
window._fbq = window._fbq || [];
window._fbq.push(['track', FacebookId , {'value':'0.01','currency':'USD'}]);
</script>

如果您可以更改noscript内容,可以使用以下内容

<noscript><img height="1" width="1" alt="" style="display:none" 
    src="https://www.facebook.com/tr?ev=12121212121&amp;cd[value]=0.01&amp;cd[currency]=USD&amp;noscript=1" /></noscript>

<script>
    window._fbq = window._fbq || [];
    var fb = document.getElementsByTagName("noscript")[0].innerHTML.split("?ev=")[1].split("&amp")[0];
    window._fbq.push(['track',fb, {'value':'0.01','currency':'USD'}]);
</script>