Facebook跟踪和谷歌跟踪脚本

时间:2014-10-10 15:44:15

标签: javascript ajax facebook google-analytics tracking

我已经完成了谷歌跟踪转换。现在我也需要Facebook像素跟踪。

我的Google Head Script看起来像这样:

<script>
 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

 ga('create', 'XX-XXXXXXXX-X' 'auto');
 ga('send', 'pageview');

</script>

我对Google的JavaScript调用如下所示:

if (typeof ga !== 'undefined')
 {
   ga('send', 'event', 'Blub', 'blub');
 }

现在我需要添加Facebook跟踪,我从Facebook获得此脚本:

<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', 'XXXXXXXXXXXXX', {'value':'0.00','currency’:'XXX'}]);
</script>

如何在不释放Google代码的情况下整合Facebook代码? 如果我同时使用它们会有问题吗?

BR

1 个答案:

答案 0 :(得分:0)

您可以毫无问题地添加它。

<script>
/* Facebook tracking */
(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;
 }
})();

/* Google Analytics tracking */
 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

 ga('create', 'XX-XXXXXXXX-X' 'auto');
 ga('send', 'pageview');

</script>

对于活动:

if (typeof ga !== 'undefined')
 {
   ga('send', 'event', 'Blub', 'blub');
 }
window._fbq = window._fbq || [];
window._fbq.push(['track', 'XXXXXXXXXXXXX', {'value':'0.00','currency’:'XXX'}]);