69°F

Aaron Parecki

  • Articles
  • Notes
  • Photos
  • Tracking Facebook Likes in Google Analytics

    December 24, 2012

    Google Analytics provides some hooks for tracking Facebook likes on your web pages as well as other social interactions. This lets you see how many people have "liked" your pages, and which pages are most popular on Facebook!

    This is a quick guide on how to set up Facebook like tracking in Google Analytics.


    First, in your <head> tag, put your Google Analytics tracking code as normal.

    <head>
      ...
    
      <script type="text/javascript">
    
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
        _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>
    </head>
    

    Then, immediately after your <body> tag, add the Facebook script according to their documentation.

    <body>
      <div id="fb-root"></div>
      <script>(function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=XXXXXXXXXXXXXXX";
        fjs.parentNode.insertBefore(js, fjs);
      }(document, 'script', 'facebook-jssdk'));</script>
      ...
    

    Now here comes the magic. Immediately after the Facebook script, add a new script tag with the following:

    <script>
      window.fbAsyncInit = function() {
        FB.Event.subscribe('edge.create', function(targetUrl) {
          _gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
        });
        FB.Event.subscribe('edge.remove', function(targetUrl) {
          _gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
        });
        FB.Event.subscribe('message.send', function(targetUrl) {
          _gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
        });
      };
    </script>
    

    This will track likes, unlikes and shares of all your pages and send the data to Google Analytics.

    Don't forget to actually add the "Like" button somewhere on your page!

    <div class="fb-like" data-send="false" data-width="300"></div>
    

    Now, in Google Analytics, you will be able to find the reports by clicking Traffic Sources -> Social -> Plugins.

    Mon, Dec 24, 2012 10:48am -08:00 #google #analytics #facebook
Posted in /articles

Hi, I'm Aaron Parecki, Director of Identity Standards at Okta, and co-founder of IndieWebCamp. I maintain oauth.net, write and consult about OAuth, and participate in the OAuth Working Group at the IETF. I also help people learn about video production and livestreaming. (detailed bio)

I've been tracking my location since 2008 and I wrote 100 songs in 100 days. I've spoken at conferences around the world about owning your data, OAuth, quantified self, and explained why R is a vowel. Read more.

  • Director of Identity Standards at Okta
  • IndieWebCamp Founder
  • OAuth WG Editor
  • OpenID Board Member

  • 🎥 YouTube Tutorials and Reviews
  • 🏠 We're building a triplex!
  • ⭐️ Life Stack
  • ⚙️ Home Automation
  • All
  • Articles
  • Bookmarks
  • Notes
  • Photos
  • Replies
  • Reviews
  • Trips
  • Videos
  • Contact
© 1999-2025 by Aaron Parecki. Powered by p3k. This site supports Webmention.
Except where otherwise noted, text content on this site is licensed under a Creative Commons Attribution 3.0 License.
IndieWebCamp Microformats Webmention W3C HTML5 Creative Commons
WeChat ID
aaronpk_tv