Help in tracking Facebook likes

Discussion in 'Google Analytics' started by Voimis, Jun 29, 2012.

  1. #1
    Hello

    I'm trying to track Facebook likes in Google Analytics but I cannot get it done.

    I've implemented GA code like this in header:

    	<script type="text/javascript">
    
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'xxxxxxx']);
      _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>
    Code (markup):
    I've also got <script src="fb_ga.js"></script> in header calling fb_ga.js whic is:

    var _ga = _ga || {};
    
    var _gaq = _gaq || [];
    
    
    
    _ga.getSocialActionTrackers_ = function(
        network, socialAction, opt_target, opt_pagePath) {
      return function() {
        var trackers = _gat._getTrackers();
        for (var i = 0, tracker; tracker = trackers[i]; i++) {
          tracker._trackSocial(network, socialAction, opt_target, opt_pagePath);
        }
      };
    };
    
    
    
    
    _ga.trackFacebook = function(opt_pagePath) {
      try {
        if (FB && FB.Event && FB.Event.subscribe) {
          FB.Event.subscribe('edge.create', function(opt_target) {
            _gaq.push(_ga.getSocialActionTrackers_('facebook', 'like',
                opt_target, opt_pagePath));
          });
          FB.Event.subscribe('edge.remove', function(opt_target) {
            _gaq.push(_ga.getSocialActionTrackers_('facebook', 'unlike',
                opt_target, opt_pagePath));
          });
          FB.Event.subscribe('message.send', function(opt_target) {
            _gaq.push(_ga.getSocialActionTrackers_('facebook', 'send',
                opt_target, opt_pagePath));
          });
        }
      } catch (e) {}
    };
    
    
    
    _ga.extractParamFromUri_ = function(uri, paramName) {
      if (!uri) {
        return;
      }
      var regex = new RegExp('[\\?&#]' + paramName + '=([^&#]*)');
      var params = regex.exec(uri);
      if (params != null) {
        return unescape(params[1]);
      }
      return;
    };
    Code (markup):
    Right after body I've got the FB XFBML code with no site info because I don't use it to log in to my site via any app on FB:

    <div id="fb-root"></div>
    
      <script>
        window.fbAsyncInit = function() {
          FB.init({
            appId      : '123456', // ENTER your FB App ID
            //channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
            status     : true, // check login status
            cookie     : true, // enable cookies to allow the server to access the session
            xfbml      : true  // parse XFBML
          });
    
          _ga.trackFacebook(); //Google Analytics tracking
    
        };
    
        // Load the Facebook SDK Asynchronously
        (function(d){
          var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
          if (d.getElementById(id)) {return;}
          js = d.createElement('script'); js.id = id; js.async = true;
          js.src = "//connect.facebook.net/en_US/all.js";
          ref.parentNode.insertBefore(js, ref);
        }(document));
      </script>
    Code (markup):
    And the like button is:

    <fb:like href="url is here vie GET_URI php script" send="true" width="450" show_faces="false" action="recommend"></fb:like>
    Code (markup):
    And this doesn't work. People can like and send the page, yes, but nothing shows up in GA.

    What should I change, what am I missing? What did I do wrong?

    Thanks a lot in advance
     
    Voimis, Jun 29, 2012 IP
  2. nkboss2

    nkboss2 Member

    Messages:
    133
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    thank you for this good information.
     
    nkboss2, Jul 6, 2012 IP