• Social Tracking with WordPress Jetpack & Google Analytics

    by  • Last modified:October 6, 2015 • Google Analytics • 6 Comments

    social analytics google analyticsIn Google Analytics, while event tracking measures general user-interactions very well, “Social Analytics” provides a consistent framework for recording social interactions. Essentially, this method allows you to add social interactions to the “Social Plugin” section of Google Analytics reports.

    I was recently working on a WordPress project and was trying to get this to work with the Jetpack plugin. I thought it would be simple (and amazing) to track all shares into Google Analytics, but I kept hitting the following error: "Error: $ is not a function". After much research, I happened upon this post which pointed out that jQuery runs in a ‘no-conflicts’ mode in WordPress. With this lead, I was able to come up with the following code.This makes pushing social shares to Google Analytics a breeze.

    Please note in the code, I only wrote it for Facebook, Twitter, Linkedin and Google+. The clicks on the email button will be an event, because technically it’s not a social share.

    <script>
    
    jQuery(document).on( 'ready post-load', function(){
     jQuery('a.share-facebook').on('click', function() {
      ga('send', 'social', 'facebook', 'share', window.location.hostname+window.location.pathname);
      });
     });
    
      jQuery(document).on( 'ready post-load', function(){
        jQuery( 'a.share-twitter' ).on( 'click', function() {
      ga('send', 'social', 'twitter', 'tweet', window.location.hostname+window.location.pathname);
      });
     });
    
    jQuery(document).on( 'ready post-load', function(){
     jQuery('a.share-google-plus-1').on('click', function() {
      ga('send', 'social', 'google', 'share', window.location.hostname+window.location.pathname);
      });
     });
     
    jQuery(document).on( 'ready post-load', function(){
     jQuery('a.share-linkedin').on('click', function() {
      ga('send', 'social', 'linkedin', 'share', window.location.hostname+window.location.pathname);
      });
     });
     
    jQuery(document).on( 'ready post-load', function(){
     jQuery('a.share-email').on('click', function() {
        ga('send', 'event', 'share', 'email', window.location.hostname+window.location.pathname, {
        nonInteraction: true});});
      });
    </script>
    

    You can modify to add the social buttons you use from Jetpack (or not), but keep in mind to use the Social Plugin syntax as follows:

    ga('send', 'social', [socialNetwork], [socialAction], [socialTarget]);
    

    Definitions:

    • socialNetwork: The network on which the action occurs (e.g. Facebook, Twitter)
    • socialAction: The type of action that happens (e.g. Like, Send, Tweet).
    • socialTarget: Specifies the target of a social interaction. This value is typically a URL.

    About

    About The Author: Adrian has over 12 years experience in Digital Marketing and Analytics. He currently works as a Marketing Head of Community at Vanilla Forums, a modern forum software platform that allows clients to connect and engage their communities and customers. Adrian has extensive experience with Google and Piwik (Matomo) Analytics. He lives and works in Montreal.