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

发布于 2024-10-24 08:21:49 字数 2176 浏览 2 评论 0原文

这件事让我发疯了。

我的电子商务商店上有类似 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= 。

我唯一一次让它注册任何东西是当我不小心在我的主谷歌分析中添加了该代码,然后它为每个页面视图生成了一个报告作为一个事件......

请帮助!

编辑这是我的全部代码:我的页面顶部有 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>
  • 你看我在上面的 facebook 代码中放置了 gaq.push - 带有一个警报。现在,当我点击“like”时会触发此警报,但 trackEvent 未发送到谷歌? (另外,理想情况下,如果它说“测试”,我想要该页面的 URL...)

Going out of my mind on this one.

I have Facebook like buttons on my ecommerce store. I have set them up using the XFBML and JAvascript SDK.. I am using the correct Meta OG properties.. everything work fine for facebook.

Now this is where it goes wrong: I was trying to follow this guide: http://www.websharedesign.com/blog/how-do-i-track-that-little-facebook-like-button-in-google-analytics.html but it doesn't work for me as there a couple of differences. Firstly Im using the Async version of Google analytics... Secondly, my facebook like buttons get the URL automatically (In this article the user has manually put in the href...) So i changed the code to add in the gaq.push method instead;

I have tried adding:

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

into the main Async Facebook script, i have tried putting this on its own at the bottom of the page.. I have even tried adding this as an onclick= on the actual fb:like.

The only time i got it to register anything was when i accidentally added that code in my main Google analytics and then it generated a report for every page view as an event...

Please help!!

EDIT this is all my code: I have GA at the top of my page...

<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>

Then below this FB is defined at the top of the body:

<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>
  • You see I have a put the gaq.push in the facebook code above - with an alert. Now this alert is fired when i click like, but the trackEvent is not sent to google? (Also, ideally where it says 'test' I would like the URL of the page...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-10-31 08:21:49

谢谢大家.. 7个小时后我找出了问题所在...我的IP在Google Analytics中被隐藏了,因为我没有通过测试访问来搞乱常规浏览量的统计数据..所以我学到了惨痛的教训它还会在跟踪事件中隐藏您的 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>

真是个布偶!不过还是谢谢你的帮助!

Thanks guys.. after 7 hours i worked out what the problem was... I had my IP hidden from Google Analytics as i didn't my test visits to mess up the stats of th regular pageviews.. So i learned the hard way that it also hides your IP addres from track Events! For reference the code that I supplied before works perfectly.. the only thing I changed was the changing the 'test' to url to get the href of the page being liked, so my final Facebook code was:

<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>

What a muppet! Thanks for your help though!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文