Jetpack_Tracks_Client

The WordPress Core Jetpack Tracks Client class.

Defined (1)

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

/_inc/lib/tracks/class.tracks-client.php  
  1. class Jetpack_Tracks_Client { 
  2. const PIXEL = 'https://pixel.wp.com/t.gif'; 
  3. const BROWSER_TYPE = 'php-agent'; 
  4. const USER_AGENT_SLUG = 'tracks-client'; 
  5. const VERSION = '0.3'; 
  6.  
  7. /** 
  8. * record_event 
  9. * @param mixed $event Event object to send to Tracks. An array will be cast to object. Required. 
  10. * Properties are included directly in the pixel query string after light validation. 
  11. * @return mixed True on success, WP_Error on failure 
  12. */ 
  13. static function record_event( $event ) { 
  14. if ( ! $event instanceof Jetpack_Tracks_Event ) { 
  15. $event = new Jetpack_Tracks_Event( $event ); 
  16. if ( is_wp_error( $event ) ) { 
  17. return $event; 
  18.  
  19. $pixel = $event->build_pixel_url( $event ); 
  20.  
  21. if ( ! $pixel ) { 
  22. return new WP_Error( 'invalid_pixel', 'cannot generate tracks pixel for given input', 400 ); 
  23.  
  24. return self::record_pixel( $pixel ); 
  25.  
  26. /** 
  27. * Synchronously request the pixel 
  28. */ 
  29. static function record_pixel( $pixel ) { 
  30. // Add the Request Timestamp and URL terminator just before the HTTP request. 
  31. $pixel .= '&_rt=' . self::build_timestamp() . '&_=_'; 
  32.  
  33. $response = wp_remote_get( $pixel, array( 
  34. 'blocking' => true, // The default, but being explicit here :) 
  35. 'timeout' => 1,  
  36. 'redirection' => 2,  
  37. 'httpversion' => '1.1',  
  38. 'user-agent' => self::get_user_agent(),  
  39. ) ); 
  40.  
  41. if ( is_wp_error( $response ) ) { 
  42. return $response; 
  43.  
  44. $code = isset( $response['response']['code'] ) ? $response['response']['code'] : 0; 
  45.  
  46. if ( $code !== 200 ) { 
  47. return new WP_Error( 'request_failed', 'Tracks pixel request failed', $code ); 
  48.  
  49. return true; 
  50.  
  51. static function get_user_agent() { 
  52. return Jetpack_Tracks_Client::USER_AGENT_SLUG . '-v' . Jetpack_Tracks_Client::VERSION; 
  53.  
  54. /** 
  55. * Build an event and return its tracking URL 
  56. * @deprecated Call the `build_pixel_url` method on a Jetpack_Tracks_Event object instead. 
  57. * @param array $event Event keys and values 
  58. * @return string URL of a tracking pixel 
  59. */ 
  60. static function build_pixel_url( $event ) { 
  61. $_event = new Jetpack_Tracks_Event( $event ); 
  62. return $_event->build_pixel_url(); 
  63.  
  64. /** 
  65. * Validate input for a tracks event. 
  66. * @deprecated Instantiate a Jetpack_Tracks_Event object instead 
  67. * @param array $event Event keys and values 
  68. * @return mixed Validated keys and values or WP_Error on failure 
  69. */ 
  70. private static function validate_and_sanitize( $event ) { 
  71. $_event = new Jetpack_Tracks_Event( $event ); 
  72. if ( is_wp_error( $_event ) ) { 
  73. return $_event; 
  74. return get_object_vars( $_event ); 
  75.  
  76. // Milliseconds since 1970-01-01 
  77. static function build_timestamp() { 
  78. $ts = round( microtime( true ) * 1000 ); 
  79. return number_format( $ts, 0, '', '' ); 
  80.  
  81. /** 
  82. * Grabs the user's anon id from cookies, or generates and sets a new one 
  83. * @return string An anon id for the user 
  84. */ 
  85. static function get_anon_id() { 
  86. static $anon_id = null; 
  87.  
  88. if ( ! isset( $anon_id ) ) { 
  89.  
  90. // Did the browser send us a cookie? 
  91. if ( isset( $_COOKIE[ 'tk_ai' ] ) && preg_match( '#^[A-Za-z0-9+/=]{24}$#', $_COOKIE[ 'tk_ai' ] ) ) { 
  92. $anon_id = $_COOKIE[ 'tk_ai' ]; 
  93. } else { 
  94.  
  95. $binary = ''; 
  96.  
  97. // Generate a new anonId and try to save it in the browser's cookies 
  98. // Note that base64-encoding an 18 character string generates a 24-character anon id 
  99. for ( $i = 0; $i < 18; ++$i ) { 
  100. $binary .= chr( mt_rand( 0, 255 ) ); 
  101.  
  102. $anon_id = 'jetpack:' . base64_encode( $binary ); 
  103.  
  104. if ( ! headers_sent() ) { 
  105. setcookie( 'tk_ai', $anon_id ); 
  106.  
  107. return $anon_id; 
  108.  
  109. /** 
  110. * Gets the WordPress.com user's Tracks identity, if connected. 
  111. * @return array|bool 
  112. */ 
  113. static function get_connected_user_tracks_identity() { 
  114. if ( ! $user_data = Jetpack::get_connected_user_data() ) { 
  115. return false; 
  116.  
  117. return array( 
  118. 'userid' => $user_data['ID'],  
  119. 'username' => $user_data['login'],  
  120. );