client.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * PHP Tracks Client
  4. * @autounit nosara tracks-client
  5. * Example Usage:
  6. *
  7. ```php
  8. include( plugin_dir_path( __FILE__ ) . 'lib/tracks/client.php');
  9. $result = jetpack_tracks_record_event( $user, $event_name, $properties );
  10. if ( is_wp_error( $result ) ) {
  11. // Handle the error in your app
  12. }
  13. ```
  14. */
  15. // Load the client classes
  16. require_once( dirname(__FILE__) . '/class.tracks-event.php' );
  17. require_once( dirname(__FILE__) . '/class.tracks-client.php' );
  18. // Now, let's export a sprinkling of syntactic sugar!
  19. /**
  20. * Procedurally (vs. Object-oriented), track an event object (or flat array)
  21. * NOTE: Use this only when the simpler jetpack_tracks_record_event() function won't work for you.
  22. * @param \Jetpack_Tracks_Event $event The event object.
  23. * @return \Jetpack_Tracks_Event|\WP_Error
  24. */
  25. function jetpack_tracks_record_event_raw( $event ) {
  26. return Jetpack_Tracks_Client::record_event( $event );
  27. }
  28. /**
  29. * Procedurally build a Tracks Event Object.
  30. * NOTE: Use this only when the simpler jetpack_tracks_record_event() function won't work for you.
  31. * @param $identity WP_user object
  32. * @param string $event_name The name of the event
  33. * @param array $properties Custom properties to send with the event
  34. * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred
  35. * @return \Jetpack_Tracks_Event|\WP_Error
  36. */
  37. function jetpack_tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
  38. $identity = jetpack_tracks_get_identity( $user->ID );
  39. $properties['user_lang'] = $user->get( 'WPLANG' );
  40. $blog_details = array(
  41. 'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' )
  42. );
  43. $timestamp = ( $event_timestamp_millis !== false ) ? $event_timestamp_millis : round( microtime( true ) * 1000 );
  44. $timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' );
  45. return new Jetpack_Tracks_Event( array_merge( $blog_details, (array) $properties, $identity, array(
  46. '_en' => $event_name,
  47. '_ts' => $timestamp_string
  48. ) ) );
  49. }
  50. /*
  51. * Get the identity to send to tracks.
  52. *
  53. * @param int $user_id The user id of the local user
  54. * @return array $identity
  55. */
  56. function jetpack_tracks_get_identity( $user_id ) {
  57. // Meta is set, and user is still connected. Use WPCOM ID
  58. $wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true );
  59. if ( $wpcom_id && Jetpack::is_user_connected( $user_id ) ) {
  60. return array(
  61. '_ut' => 'wpcom:user_id',
  62. '_ui' => $wpcom_id
  63. );
  64. }
  65. // User is connected, but no meta is set yet. Use WPCOM ID and set meta.
  66. if ( Jetpack::is_user_connected( $user_id ) ) {
  67. $wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
  68. add_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'], true );
  69. return array(
  70. '_ut' => 'wpcom:user_id',
  71. '_ui' => $wpcom_user_data['ID']
  72. );
  73. }
  74. // User isn't linked at all. Fall back to anonymous ID.
  75. $anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
  76. if ( ! $anon_id ) {
  77. $anon_id = Jetpack_Tracks_Client::get_anon_id();
  78. add_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id, false );
  79. }
  80. if ( ! isset( $_COOKIE[ 'tk_ai' ] ) && ! headers_sent() ) {
  81. setcookie( 'tk_ai', $anon_id );
  82. }
  83. return array(
  84. '_ut' => 'anon',
  85. '_ui' => $anon_id
  86. );
  87. }
  88. /**
  89. * Record an event in Tracks - this is the preferred way to record events from PHP.
  90. *
  91. * @param mixed $identity username, user_id, or WP_user object
  92. * @param string $event_name The name of the event
  93. * @param array $properties Custom properties to send with the event
  94. * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred
  95. * @return bool true for success | \WP_Error if the event pixel could not be fired
  96. */
  97. function jetpack_tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
  98. // We don't want to track user events during unit tests/CI runs.
  99. if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) {
  100. return false;
  101. }
  102. $event_obj = jetpack_tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis );
  103. if ( is_wp_error( $event_obj->error ) ) {
  104. return $event_obj->error;
  105. }
  106. return $event_obj->record();
  107. }