tracks-callables.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * This was abstracted from wp-calypso's analytics lib: https://github.com/Automattic/wp-calypso/blob/master/client/lib/analytics/README.md
  3. * Some stuff was removed like GA tracking and other things not necessary for Jetpack tracking.
  4. *
  5. * This library should only be used and loaded if the Jetpack site is connected.
  6. */
  7. // Load tracking scripts
  8. window._tkq = window._tkq || [];
  9. function buildQuerystring( group, name ) {
  10. var uriComponent = '';
  11. if ( 'object' === typeof group ) {
  12. for ( var key in group ) {
  13. uriComponent += '&x_' + encodeURIComponent( key ) + '=' + encodeURIComponent( group[ key ] );
  14. }
  15. } else {
  16. uriComponent = '&x_' + encodeURIComponent( group ) + '=' + encodeURIComponent( name );
  17. }
  18. return uriComponent;
  19. }
  20. var analytics = {
  21. initialize: function( userId, username ) {
  22. analytics.setUser( userId, username );
  23. analytics.identifyUser();
  24. },
  25. mc: {
  26. bumpStat: function( group, name ) {
  27. var uriComponent = buildQuerystring( group, name ); // prints debug info
  28. new Image().src = document.location.protocol + '//pixel.wp.com/g.gif?v=wpcom-no-pv' + uriComponent + '&t=' + Math.random();
  29. }
  30. },
  31. tracks: {
  32. recordEvent: function( eventName, eventProperties ) {
  33. eventProperties = eventProperties || {};
  34. if ( eventName.indexOf( 'jetpack_' ) !== 0 ) {
  35. debug( '- Event name must be prefixed by "jetpack_"' );
  36. return;
  37. }
  38. window._tkq.push( [ 'recordEvent', eventName, eventProperties ] );
  39. },
  40. recordPageView: function( urlPath ) {
  41. analytics.tracks.recordEvent( 'jetpack_page_view', {
  42. 'path': urlPath
  43. } );
  44. }
  45. },
  46. setUser: function( userId, username ) {
  47. _user = { ID: userId, username: username };
  48. },
  49. identifyUser: function() {
  50. // Don't identify the user if we don't have one
  51. if ( _user ) {
  52. window._tkq.push( [ 'identifyUser', _user.ID, _user.username ] );
  53. }
  54. },
  55. clearedIdentity: function() {
  56. window._tkq.push( [ 'clearIdentity' ] );
  57. }
  58. };