Configure Jetpack to Track Social Share And Traffic In Google Analytics

Jetpack is one of the most popular WordPress Plugin. It also features in my list of Top WordPress Plugins that every WordPress User must Install.

Jetpack has 35 free modules which are capable of handling most of the basic functionalities you need for your WordPress Blog. Jetpack Sharing is one of these Modules.

Though it very well does what it is supposed to, that is,  adding Social Share Buttons to your Blog, but it lacks the functionality to track your Social Shares and the traffic your get from it, in Google Analytics.

In this guide am going to explain to you how to track the following in Google Analytics using Jetpack Sharing Buttons

  1. Social Shares at your blog
  2. The traffic you get from the social shares.

We will have to write two different codes for tracking each of the above parameters. Let us first understand the need to track these parameters.

WHY TRACK SOCIAL SHARES AND TRAFFIC

The Importance of Social Media for getting traffic to your blog is growing with every passing day. Social media is one of the major sources of traffic for most Bloggers especially for new Bloggers who do not get enough Organic search traffic.

The number of shares you get is a very important factor to analyze what kind of posts are getting more social shares for you. Jetpack along with various other Plugins does allow you to calculate a number of social shares for each post, but it does not provide you details about the person sharing it. For better Analysis, you may want to have more detailed information about people sharing it.

Also, since a lot of Bloggers use multiple Social Media strategies to promote their content, it is also necessary to know which Social Media strategy is fetching you more traffic. So it is important for you to know how much amount of traffic you are getting from Direct shares from your blog along with in-depth data for same.

Tracking Jetpack Social Shares In Google Analytics

To Provide us with this information, Google Analytics started with a feature called Web Tracking. By default, all your Google +1s are tracked in Google Analytics if you use their widget code. However, with Jetpack, it doesn’t work.

Adrian at Statstory, has written a simple script which enables web tracking features for Jetpack sharing buttons. This script will enable you to track your Jetpack Shares for Facebook, Twitter, Google+ and LinkedIn in Google Analytics.

Just Copy the below script on your website either separately or along with some other script. Preferably it should be added before closing body tag.

<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);
});
});
});

Once you place the code, you can now track your Social Shares in Google Analytics under Acquisition> Social> Plug-Ins.

If you want to add other social networks, you may do so by using the same format as mentioned above.

Please note, as stated above, am not the author of this script, so in the case of any issues, you may directly contact Adrian.

Tracking Jetpack Social Traffic In Google Analytics

Now that we know who is sharing our Blog Posts, let us now add a code which will track the traffic the amount of traffic we are getting from these shares.

For this code, am really thankful to Jeremy Herves, for taking interest in my query at Support Forum and coming out with this working solution.

To achieve this, our code must accomplish the following

  1. Add UTM Parameters to each URL ( UTM parameters explained below)
  2. Remove Ampersands from the URL  created in Step 1 as Google Analytics cannot read the same.
  3. Since Twitter and Google+ sharing codes do not handle URL’s with UTM parameters, use goo.gl to shorten this URL.
  4. Check if the given URL already has a shortened version and in case if already a shortened URL is being used, return that short URL instead of creating a new one.
  5. In the case of any error, return the original non-trackable URL.

WHAT ARE UTM PARAMETERS

UTM parameters are simple tags that are added to a URL. When someone clicks on this URL, these tags are sent to Google Analytics, which it uses to identify the traffic source.

You can Add following UTM Parameters to URL

  1. Campaign Source: This is a Required parameter which returns the source website. e.g Google, Facebook, Twitter.
  2. Campaign Medium: This a Required Parameter used to track the medium of your Campaign. e.g Email, Marketing, Banner etc
  3. Campaign Term: This is an optional parameter used in case of CPC to track the paid keywords.
  4. Campaign Content: This is again optional and used in case of multiple Ads/Banners on the same web page.
  5. Campaign Name: This is a Required parameter which returns the Name of this Campaign. e.g Product Name, Service name etc.

Code For Tracking Traffic

Please note, in this code, I have added my Google Shortener API Key. I did so to make sure in case you forget to add your API key, your website should not break.

Please replace my API Key with yours. If you don’t know how to create one, you can check out my article which explains in detail How To Create Google URL Shortener API Key

Also do not forget to change the UTM parameters, except for UTM source which displays the name of the social network by default. Currently, other values are set as “XXX”.

Once you replace your API key and UTM parameters in the below code, just copy it to your Theme’s Functions.php file. Alternately you can also use Functionality Plugin to insert the code. This will make sure, in case you update your theme, you do not lose any changes you made.

/**
 * Overwrite the links used in Jetpack's Sharing module.
 *
 * @see https://wordpress.org/support/topic/how-to-use-utm-codes-with-jetpack-sharing
 *
 * @param string $url
 * @param int $post_id
 * @param int $sharing_id
 * @filter sharing_permalink
 * @return string
 */
function jeherve_custom_sharedaddy_link( $url, $post_id, $sharing_id ) {

    // Let's see if we have a shortlink for this post
    $tracked_url = get_post_meta( $post_id, '_tracked_googl_shortlink', true );

    if ( $tracked_url ) {
        return $tracked_url;
    } else {
        // Let's build our tracking parameters
        $ga_params = array(
            'utm_source'   => $sharing_id,
            'utm_medium'   => 'XXX',
            'utm_term'     => 'XXX',
            'utm_content'  => 'XXX',
            'utm_campaign' => 'XXX',
        );

        // Add them to our post permalink
        $tracked_url = esc_url( add_query_arg( $ga_params, $url ) );

        // Do not encode ampersands, GA doesn't like it.
        $tracked_url = str_replace( '&#038;', '&', $tracked_url );

        /**
         * Create a short Goo.gl URL redirecting to the full URL with tracking parameters.
         *
         * @see https://goo.gl/
         *
         * This uses my own API key by default.
         * if you want to use your own to have stats attached to your own Goo.gl account,
         * get your own key here: https://developers.google.com/url-shortener/v1/getting_started?hl=en
         */
        $googl_key = 'my_api_key';

        // Let's query Google and get a Short URL
        $googl_result = wp_remote_post(
            add_query_arg(
                'key',
                $googl_key,
                'https://www.googleapis.com/urlshortener/v1/url'
            ),
            array(
                'body' => json_encode( array( 'longUrl' => esc_url_raw( $tracked_url ) ) ),
                'headers' => array( 'Content-Type' => 'application/json' ),
            )
        );

        // Return the default URL if the request got an error.
        if ( is_wp_error( $googl_result ) ) {
            return esc_url( $url );
        }

        $googl_result = json_decode( $googl_result['body'] );
        $googl_link = $googl_result->id;

        if ( $googl_link ) {
            // Let's save that in post meta for the next call
            add_post_meta( $post_id, '_tracked_googl_shortlink', $googl_link, true );

            // And boom! return a short tracked URL.
            return $googl_link;
        } else {
            return esc_url( $url );
        }

        // Final default, just in case
        return esc_url( $url );
    }
}
add_filter( 'sharing_permalink', 'jeherve_custom_sharedaddy_link', 10, 3 );

That’s it, all the traffic from Jetpack sharing can now be tracked in Google Analytics by navigating to Acquisition > Campaigns.

You can also check out Jeremy’s Post for the same.

FINAL WORDS

You do not have to shell out a big amount for buying Plugins which offer Google Analytics tracking feature. By adding the above two codes to your website, you will be able to push all your blog’s Social share data to Google Analytics and use the same for better Analysis.

I strongly recommend you to create a backup before adding the above code. Should you face any problem, you can contact me.

Aquif Shaikh
 

Aquif Shaikh is a Blogger from Baroda. He loves writing, traveling and sharing his knowledge and Ideas with the world. At Blogging Ocean, he writes mostly about blogging tips, content marketing, and SEO.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: