JetpackTracking

The WordPress Core JetpackTracking class.

Defined (1)

The class is defined in the following location(s).

/class.jetpack-tracks.php  
  1. class JetpackTracking { 
  2. static $product_name = 'jetpack'; 
  3.  
  4. static function track_jetpack_usage() { 
  5. if ( ! Jetpack::is_active() ) { 
  6. return; 
  7.  
  8. // For tracking stuff via js/ajax 
  9. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_tracks_scripts' ) ); 
  10.  
  11. add_action( 'jetpack_activate_module', array( __CLASS__, 'track_activate_module'), 1, 1 ); 
  12. add_action( 'jetpack_deactivate_module', array( __CLASS__, 'track_deactivate_module'), 1, 1 ); 
  13. add_action( 'jetpack_user_authorized', array( __CLASS__, 'track_user_linked' ) ); 
  14.  
  15. static function enqueue_tracks_scripts() { 
  16. wp_enqueue_script( 'jptracks', plugins_url( '_inc/lib/tracks/tracks-ajax.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); 
  17. wp_localize_script( 'jptracks', 'jpTracksAJAX', array( 
  18. 'ajaxurl' => admin_url( 'admin-ajax.php' ),  
  19. 'jpTracksAJAX_nonce' => wp_create_nonce( 'jp-tracks-ajax-nonce' ),  
  20. ) ); 
  21.  
  22. /** User has linked their account */ 
  23. static function track_user_linked() { 
  24. $user_id = get_current_user_id(); 
  25. $anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true ); 
  26.  
  27. if ( $anon_id ) { 
  28. self::record_user_event( '_aliasUser', array( 'anonId' => $anon_id ) ); 
  29. delete_user_meta( $user_id, 'jetpack_tracks_anon_id' ); 
  30. if ( ! headers_sent() ) { 
  31. setcookie( 'tk_ai', 'expired', time() - 1000 ); 
  32.  
  33. $wpcom_user_data = Jetpack::get_connected_user_data( $user_id ); 
  34. update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] ); 
  35.  
  36. self::record_user_event( 'wpa_user_linked', array() ); 
  37.  
  38. /** Activated module */ 
  39. static function track_activate_module( $module ) { 
  40. self::record_user_event( 'module_activated', array( 'module' => $module ) ); 
  41.  
  42. /** Deactivated module */ 
  43. static function track_deactivate_module( $module ) { 
  44. self::record_user_event( 'module_deactivated', array( 'module' => $module ) ); 
  45.  
  46. static function record_user_event( $event_type, $data= array() ) { 
  47.  
  48. $user = wp_get_current_user(); 
  49. $site_url = get_option( 'siteurl' ); 
  50.  
  51. $data['_via_ua'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; 
  52. $data['_via_ip'] = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : ''; 
  53. $data['_lg'] = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''; 
  54. $data['blog_url'] = $site_url; 
  55. $data['blog_id'] = Jetpack_Options::get_option( 'id' ); 
  56.  
  57. // Top level events should not be namespaced 
  58. if ( '_aliasUser' != $event_type ) { 
  59. $event_type = self::$product_name . '_' . $event_type; 
  60.  
  61. $data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '0'; 
  62.  
  63. jetpack_tracks_record_event( $user, $event_type, $data );