开发者

How can i track Facebook like on my website via Google Analytics?

开发者 https://www.devze.com 2023-02-18 01:25 出处:网络
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 wo

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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消