Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created December 21, 2015 17:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save devinsays/e9a5a42c1416b16f8bae to your computer and use it in GitHub Desktop.
Save devinsays/e9a5a42c1416b16f8bae to your computer and use it in GitHub Desktop.
Track JetPack Shares in Google Analytics
/**
* Javacript for loading custom Google Analytics events
*
* @since 1.0.0
*/
(function($) {
// GA Docs for Social Interactions:
// https://developers.google.com/analytics/devguides/collection/analyticsjs/social-interactions
if ( typeof(ga) == "function" ) {
// Twitter
$('a.share-twitter').on( 'click', function() {
ga( 'send', {
hitType: 'social',
socialNetwork: 'Twitter',
socialAction: 'share',
socialTarget: $(this).attr('href').substr( 0, $(this).attr('href').indexOf('?') )
});
});
// Facebook
$('a.share-facebook').on( 'click', function() {
ga( 'send', {
hitType: 'social',
socialNetwork: 'Facebook',
socialAction: 'share',
socialTarget: $(this).attr('href').substr( 0, $(this).attr('href').indexOf('?') )
});
});
}
})(jQuery);
@gregoryfoster
Copy link

Nice gist! I've adapted it into a basic WordPress plugin here crediting your work in the README and code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment