PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.2
Jetpack – WP Security, Backup, Speed, & Growth v6.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / _inc / lib / tracks / client.php
client.php
131 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
11 if ( is_wp_error( $result ) ) {
12 // Handle the error in your app
13 }
14 ```
15 */
16
17 // Load the client classes
18 require_once( dirname(__FILE__) . '/class.tracks-event.php' );
19 require_once( dirname(__FILE__) . '/class.tracks-client.php' );
20
21 // Now, let's export a sprinkling of syntactic sugar!
22
23 /**
24 * Procedurally (vs. Object-oriented), track an event object (or flat array)
25 * NOTE: Use this only when the simpler jetpack_tracks_record_event() function won't work for you.
26 * @param \Jetpack_Tracks_Event $event The event object.
27 * @return \Jetpack_Tracks_Event|\WP_Error
28 */
29 function jetpack_tracks_record_event_raw( $event ) {
30 return Jetpack_Tracks_Client::record_event( $event );
31 }
32
33 /**
34 * Procedurally build a Tracks Event Object.
35 * NOTE: Use this only when the simpler jetpack_tracks_record_event() function won't work for you.
36 * @param $identity WP_user object
37 * @param string $event_name The name of the event
38 * @param array $properties Custom properties to send with the event
39 * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred
40 * @return \Jetpack_Tracks_Event|\WP_Error
41 */
42 function jetpack_tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
43
44 $identity = jetpack_tracks_get_identity( $user->ID );
45
46 $properties['user_lang'] = $user->get( 'WPLANG' );
47
48 $blog_details = array(
49 'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' )
50 );
51
52 $timestamp = ( $event_timestamp_millis !== false ) ? $event_timestamp_millis : round( microtime( true ) * 1000 );
53 $timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' );
54
55 return new Jetpack_Tracks_Event( array_merge( $blog_details, (array) $properties, $identity, array(
56 '_en' => $event_name,
57 '_ts' => $timestamp_string
58 ) ) );
59 }
60
61 /*
62 * Get the identity to send to tracks.
63 *
64 * @param int $user_id The user id of the local user
65 * @return array $identity
66 */
67 function jetpack_tracks_get_identity( $user_id ) {
68
69 // Meta is set, and user is still connected. Use WPCOM ID
70 $wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true );
71 if ( $wpcom_id && Jetpack::is_user_connected( $user_id ) ) {
72 return array(
73 '_ut' => 'wpcom:user_id',
74 '_ui' => $wpcom_id
75 );
76 }
77
78 // User is connected, but no meta is set yet. Use WPCOM ID and set meta.
79 if ( Jetpack::is_user_connected( $user_id ) ) {
80 $wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
81 add_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'], true );
82
83 return array(
84 '_ut' => 'wpcom:user_id',
85 '_ui' => $wpcom_user_data['ID']
86 );
87 }
88
89 // User isn't linked at all. Fall back to anonymous ID.
90 $anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
91 if ( ! $anon_id ) {
92 $anon_id = Jetpack_Tracks_Client::get_anon_id();
93 add_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id, false );
94 }
95
96 if ( ! isset( $_COOKIE[ 'tk_ai' ] ) && ! headers_sent() ) {
97 setcookie( 'tk_ai', $anon_id );
98 }
99
100 return array(
101 '_ut' => 'anon',
102 '_ui' => $anon_id
103 );
104
105 }
106
107 /**
108 * Record an event in Tracks - this is the preferred way to record events from PHP.
109 *
110 * @param mixed $identity username, user_id, or WP_user object
111 * @param string $event_name The name of the event
112 * @param array $properties Custom properties to send with the event
113 * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred
114 * @return bool true for success | \WP_Error if the event pixel could not be fired
115 */
116 function jetpack_tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
117
118 // We don't want to track user events during unit tests/CI runs.
119 if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) {
120 return false;
121 }
122
123 $event_obj = jetpack_tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis );
124
125 if ( is_wp_error( $event_obj->error ) ) {
126 return $event_obj->error;
127 }
128
129 return $event_obj->record();
130 }
131