Jetpack_Tracks_Event

The WordPress Core Jetpack Tracks Event class.

Defined (1)

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

/_inc/lib/tracks/class.tracks-event.php  
  1. class Jetpack_Tracks_Event { 
  2. const EVENT_NAME_REGEX = '/^(([a-z0-9]+)_) {2}([a-z0-9_]+)$/'; 
  3. const PROP_NAME_REGEX = '/^[a-z_][a-z0-9_]*$/'; 
  4. public $error; 
  5.  
  6. function __construct( $event ) { 
  7. $_event = self::validate_and_sanitize( $event ); 
  8. if ( is_wp_error( $_event ) ) { 
  9. $this->error = $_event; 
  10. return; 
  11.  
  12. foreach( $_event as $key => $value ) { 
  13. $this->{$key} = $value; 
  14.  
  15. function record() { 
  16. return Jetpack_Tracks_Client::record_event( $this ); 
  17.  
  18. /** 
  19. * Annotate the event with all relevant info. 
  20. * @param mixed $event Object or (flat) array 
  21. * @return mixed The transformed event array or WP_Error on failure. 
  22. */ 
  23. static function validate_and_sanitize( $event ) { 
  24. $event = (object) $event; 
  25.  
  26. // Required 
  27. if ( ! $event->_en ) { 
  28. return new WP_Error( 'invalid_event', 'A valid event must be specified via `_en`', 400 ); 
  29.  
  30. // delete non-routable addresses otherwise geoip will discard the record entirely 
  31. if ( property_exists( $event, '_via_ip' ) && preg_match( '/^192\.168|^10\./', $event->_via_ip ) ) { 
  32. unset($event->_via_ip); 
  33.  
  34. $validated = array( 
  35. 'browser_type' => Jetpack_Tracks_Client::BROWSER_TYPE,  
  36. '_aua' => Jetpack_Tracks_Client::get_user_agent(),  
  37. ); 
  38.  
  39. $_event = (object) array_merge( (array) $event, $validated ); 
  40.  
  41. // If you want to blacklist property names, do it here. 
  42.  
  43. // Make sure we have an event timestamp. 
  44. if ( ! isset( $_event->_ts ) ) { 
  45. $_event->_ts = Jetpack_Tracks_Client::build_timestamp(); 
  46.  
  47. return $_event; 
  48.  
  49. /** 
  50. * Build a pixel URL that will send a Tracks event when fired. 
  51. * On error, returns an empty string (''). 
  52. * @return string A pixel URL or empty string ('') if there were invalid args. 
  53. */ 
  54. function build_pixel_url() { 
  55. if ( $this->error ) { 
  56. return ''; 
  57.  
  58. $args = get_object_vars( $this ); 
  59.  
  60. // Request Timestamp and URL Terminator must be added just before the HTTP request or not at all. 
  61. unset( $args['_rt'] ); 
  62. unset( $args['_'] ); 
  63.  
  64. $validated = self::validate_and_sanitize( $args ); 
  65.  
  66. if ( is_wp_error( $validated ) ) 
  67. return ''; 
  68.  
  69. return Jetpack_Tracks_Client::PIXEL . '?' . http_build_query( $validated ); 
  70.  
  71. static function event_name_is_valid( $name ) { 
  72. return preg_match( Jetpack_Tracks_Event::EVENT_NAME_REGEX, $name ); 
  73.  
  74. static function prop_name_is_valid( $name ) { 
  75. return preg_match( Jetpack_Tracks_Event::PROP_NAME_REGEX, $name ); 
  76.  
  77. static function scrutinize_event_names( $event ) { 
  78. if ( ! Jetpack_Tracks_Event::event_name_is_valid( $event->_en ) ) { 
  79. return; 
  80.  
  81. $whitelisted_key_names = array( 
  82. 'anonId',  
  83. 'Browser_Type',  
  84. ); 
  85.  
  86. foreach ( array_keys( (array) $event ) as $key ) { 
  87. if ( in_array( $key, $whitelisted_key_names ) ) { 
  88. continue; 
  89. if ( ! Jetpack_Tracks_Event::prop_name_is_valid( $key ) ) { 
  90. return;