javascript - 我如何通过 Google Analytics 在我的网站上跟踪 Facebook?

标签 javascript facebook facebook-graph-api google-analytics

这件事让我发疯了。

我的电子商务商店上有类似 Facebook 的按钮。我已经使用 XFBML 和 JAvascript SDK 设置了它们。我正在使用正确的 Meta OG 属性。Facebook 一切正常。

现在这就是问题所在:我试图遵循本指南:http://www.websharedesign.com/blog/how-do-i-track-that-little-facebook-like-button-in-google-analytics.html但它对我不起作用,因为存在一些差异。首先,我使用 Google Analytics 的异步版本...其次,我的 facebook 之类的按钮会自动获取 URL(在本文中,用户手动输入了 href...)所以我更改了代码以添加到 gaq.push 中方法代替;

我尝试添加:

    FB.Event.subscribe('edge.create', function(href, widget) {
    _gaq.push(['_trackEvent', 'facebook', 'like', href]);
}); 

在主异步 Facebook 脚本中,我尝试将其单独放在页面底部。我什至尝试将其添加为实际 fb:like 上的 onclick= 。

我唯一一次让它注册任何东西是当我不小心将该代码添加到我的主要 Google 分析中,然后它为每个页面 View 生成一个报告作为一个事件......

请帮忙!!

编辑这是我的全部代码:我的页面顶部有 GA...

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'xxxxxxxxxxxxxxx']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

然后在正文的顶部定义此 FB:

<script>
  window.fbAsyncInit = function() {
    FB.init({appId: 'xxxxxxxxxxxxxxxxx', status: true, cookie: true,
             xfbml: true});  
   FB.Event.subscribe('edge.create', function(href, widget) {
    alert('Facebook Like');
    _gaq.push(['_trackEvent', 'facebook', 'Facebook like', 'test']);
   });                 
  };
  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
</script>
  • 你看,我把 gaq.push 放在上面的 facebook 代码中 - 带有警报。现在,当我点击“like”时会触发此警报,但 trackEvent 未发送到谷歌? (此外,理想情况下,如果它显示“测试”,我需要该页面的 URL...)

最佳答案

谢谢大家..7个小时后我找出了问题所在...我的IP在Google Analytics中被 Conceal 了,因为我没有通过测试访问来搞乱常规浏览量的统计数据..所以我了解到困难的是,它还会从跟踪事件中 Conceal 您的 IP 地址!作为引用,我之前提供的代码工作得很好。我唯一改变的是将“测试”更改为 url 以获取被喜欢的页面的 href,所以我最终的 Facebook 代码是:

<script>
  window.fbAsyncInit = function() {
    FB.init({appId: 'xxxxxxxxxxxxxxxxx', status: true, cookie: true,
             xfbml: true});  
   FB.Event.subscribe('edge.create', function(href, widget) {
    alert('Facebook Like');
    _gaq.push(['_trackEvent', 'facebook', 'Facebook like', href]);
   });                 
  };
  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
</script>

真是个布偶啊!不过还是感谢您的帮助!

关于javascript - 我如何通过 Google Analytics 在我的网站上跟踪 Facebook?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5363486/

相关文章:

android - Facebook 时间线,如 ListView

Facebook Graph API : FB. 在调用 FB.init() 之前调用了 login()

javascript - jQuery 尝试显示下一个元素

php - Facebook API : How to post to own application wall without login

javascript - HTML/JS -- 获取被点击的行

android - 将 android 联系人的生日复制到日历

Facebook API 检索 "All activity"

php - Graph API 新提要帖子对象 - 附件未显示

javascript - 嵌入式代码中的问题。视频不在共享平台上播放

javascript - 仅当没有数字时才捕获行