1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nosara Tracks for Jetpack |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-tracking |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The Tracking class, used to record events in wpcom |
12
|
|
|
*/ |
13
|
|
|
class Tracking { |
14
|
|
|
/** |
15
|
|
|
* The assets version. |
16
|
|
|
* |
17
|
|
|
* @since 9.4.0 |
18
|
|
|
* |
19
|
|
|
* @var string Assets version. |
20
|
|
|
*/ |
21
|
|
|
const ASSETS_VERSION = '1.0.0'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Slug of the product that we are tracking. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $product_name; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Connection manager object. |
32
|
|
|
* |
33
|
|
|
* @var Object |
34
|
|
|
*/ |
35
|
|
|
private $connection; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates the Tracking object. |
39
|
|
|
* |
40
|
|
|
* @param String $product_name the slug of the product that we are tracking. |
41
|
|
|
* @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function __construct( $product_name = 'jetpack', $connection = null ) { |
44
|
|
|
$this->product_name = $product_name; |
45
|
|
|
$this->connection = $connection; |
46
|
|
|
if ( is_null( $this->connection ) ) { |
47
|
|
|
// TODO We should always pass a Connection. |
48
|
|
|
$this->connection = new Connection\Manager(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Universal method for for all tracking events triggered via the JavaScript client. |
54
|
|
|
* |
55
|
|
|
* @access public |
56
|
|
|
*/ |
57
|
|
|
public function ajax_tracks() { |
58
|
|
|
// Check for nonce. |
59
|
|
|
if ( |
60
|
|
|
empty( $_REQUEST['tracksNonce'] ) |
61
|
|
|
|| ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) |
62
|
|
|
) { |
63
|
|
|
wp_send_json_error( |
64
|
|
|
__( 'You aren’t authorized to do that.', 'jetpack' ), |
65
|
|
|
403 |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) { |
70
|
|
|
wp_send_json_error( |
71
|
|
|
__( 'No valid event name or type.', 'jetpack' ), |
72
|
|
|
403 |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$tracks_data = array(); |
77
|
|
|
if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) { |
78
|
|
|
if ( is_array( $_REQUEST['tracksEventProp'] ) ) { |
79
|
|
|
$tracks_data = $_REQUEST['tracksEventProp']; |
80
|
|
|
} else { |
81
|
|
|
$tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] ); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->record_user_event( $_REQUEST['tracksEventName'], $tracks_data ); |
86
|
|
|
|
87
|
|
|
wp_send_json_success(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Enqueue script necessary for tracking. |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function enqueue_tracks_scripts() { |
94
|
|
|
wp_enqueue_script( |
95
|
|
|
'jptracks', |
96
|
|
|
Assets::get_file_url_for_environment( 'js/tracks-ajax.js', 'js/tracks-ajax.js', __FILE__ ), |
97
|
|
|
array(), |
98
|
|
|
self::ASSETS_VERSION, |
99
|
|
|
true |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
wp_localize_script( |
103
|
|
|
'jptracks', |
104
|
|
|
'jpTracksAJAX', |
105
|
|
|
array( |
106
|
|
|
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
107
|
|
|
'jpTracksAJAX_nonce' => wp_create_nonce( 'jp-tracks-ajax-nonce' ), |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Send an event in Tracks. |
114
|
|
|
* |
115
|
|
|
* @param string $event_type Type of the event. |
116
|
|
|
* @param array $data Data to send with the event. |
117
|
|
|
* @param mixed $user username, user_id, or WP_user object. |
118
|
|
|
*/ |
119
|
|
|
public function record_user_event( $event_type, $data = array(), $user = null ) { |
120
|
|
|
if ( ! $user ) { |
121
|
|
|
$user = wp_get_current_user(); |
122
|
|
|
} |
123
|
|
|
$site_url = get_option( 'siteurl' ); |
124
|
|
|
|
125
|
|
|
$data['_via_ua'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
126
|
|
|
$data['_via_ip'] = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : ''; |
127
|
|
|
$data['_lg'] = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''; |
128
|
|
|
$data['blog_url'] = $site_url; |
129
|
|
|
$data['blog_id'] = \Jetpack_Options::get_option( 'id' ); |
130
|
|
|
|
131
|
|
|
// Top level events should not be namespaced. |
132
|
|
|
if ( '_aliasUser' !== $event_type ) { |
133
|
|
|
$event_type = $this->product_name . '_' . $event_type; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '0'; |
137
|
|
|
|
138
|
|
|
return $this->tracks_record_event( $user, $event_type, $data ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Record an event in Tracks - this is the preferred way to record events from PHP. |
143
|
|
|
* |
144
|
|
|
* @param mixed $user username, user_id, or WP_user object. |
145
|
|
|
* @param string $event_name The name of the event. |
146
|
|
|
* @param array $properties Custom properties to send with the event. |
147
|
|
|
* @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred. |
|
|
|
|
148
|
|
|
* |
149
|
|
|
* @return bool true for success | \WP_Error if the event pixel could not be fired |
150
|
|
|
*/ |
151
|
|
|
public function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
152
|
|
|
|
153
|
|
|
// We don't want to track user events during unit tests/CI runs. |
154
|
|
|
if ( $user instanceof \WP_User && 'wptests_capabilities' === $user->cap_key ) { |
|
|
|
|
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
$terms_of_service = new Terms_Of_Service(); |
158
|
|
|
$status = new Status(); |
159
|
|
|
// Don't track users who have not agreed to our TOS. |
160
|
|
|
if ( ! $this->should_enable_tracking( $terms_of_service, $status ) ) { |
|
|
|
|
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$event_obj = $this->tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis ); |
165
|
|
|
|
166
|
|
|
if ( is_wp_error( $event_obj->error ) ) { |
167
|
|
|
return $event_obj->error; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $event_obj->record(); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Determines whether tracking should be enabled. |
175
|
|
|
* |
176
|
|
|
* @param Automattic\Jetpack\Terms_Of_Service $terms_of_service A Terms_Of_Service object. |
177
|
|
|
* @param Automattic\Jetpack\Status $status A Status object. |
178
|
|
|
* |
179
|
|
|
* @return boolean True if tracking should be enabled, else false. |
180
|
|
|
*/ |
181
|
|
|
public function should_enable_tracking( $terms_of_service, $status ) { |
182
|
|
|
if ( $status->is_offline_mode() ) { |
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $terms_of_service->has_agreed() || $this->connection->is_user_connected(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Procedurally build a Tracks Event Object. |
191
|
|
|
* NOTE: Use this only when the simpler Automattic\Jetpack\Tracking->jetpack_tracks_record_event() function won't work for you. |
192
|
|
|
* |
193
|
|
|
* @param WP_user $user WP_user object. |
194
|
|
|
* @param string $event_name The name of the event. |
195
|
|
|
* @param array $properties Custom properties to send with the event. |
196
|
|
|
* @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred. |
|
|
|
|
197
|
|
|
* |
198
|
|
|
* @return \Jetpack_Tracks_Event|\WP_Error |
199
|
|
|
*/ |
200
|
|
|
private function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
201
|
|
|
$identity = $this->tracks_get_identity( $user->ID ); |
202
|
|
|
|
203
|
|
|
$properties['user_lang'] = $user->get( 'WPLANG' ); |
204
|
|
|
|
205
|
|
|
$blog_details = array( |
206
|
|
|
'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' ), |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
$timestamp = ( false !== $event_timestamp_millis ) ? $event_timestamp_millis : round( microtime( true ) * 1000 ); |
210
|
|
|
$timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' ); |
211
|
|
|
|
212
|
|
|
return new \Jetpack_Tracks_Event( |
213
|
|
|
array_merge( |
|
|
|
|
214
|
|
|
$blog_details, |
215
|
|
|
(array) $properties, |
216
|
|
|
$identity, |
217
|
|
|
array( |
218
|
|
|
'_en' => $event_name, |
219
|
|
|
'_ts' => $timestamp_string, |
220
|
|
|
) |
221
|
|
|
) |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get the identity to send to tracks. |
227
|
|
|
* |
228
|
|
|
* @param int $user_id The user id of the local user. |
229
|
|
|
* |
230
|
|
|
* @return array $identity |
231
|
|
|
*/ |
232
|
|
|
public function tracks_get_identity( $user_id ) { |
233
|
|
|
|
234
|
|
|
// Meta is set, and user is still connected. Use WPCOM ID. |
235
|
|
|
$wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true ); |
236
|
|
|
if ( $wpcom_id && $this->connection->is_user_connected( $user_id ) ) { |
237
|
|
|
return array( |
238
|
|
|
'_ut' => 'wpcom:user_id', |
239
|
|
|
'_ui' => $wpcom_id, |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// User is connected, but no meta is set yet. Use WPCOM ID and set meta. |
244
|
|
|
if ( $this->connection->is_user_connected( $user_id ) ) { |
245
|
|
|
$wpcom_user_data = $this->connection->get_connected_user_data( $user_id ); |
246
|
|
|
update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] ); |
247
|
|
|
|
248
|
|
|
return array( |
249
|
|
|
'_ut' => 'wpcom:user_id', |
250
|
|
|
'_ui' => $wpcom_user_data['ID'], |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
// User isn't linked at all. Fall back to anonymous ID. |
255
|
|
|
$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true ); |
256
|
|
|
if ( ! $anon_id ) { |
257
|
|
|
$anon_id = \Jetpack_Tracks_Client::get_anon_id(); |
258
|
|
|
add_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id, false ); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
if ( ! isset( $_COOKIE['tk_ai'] ) && ! headers_sent() ) { |
262
|
|
|
setcookie( 'tk_ai', $anon_id ); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return array( |
266
|
|
|
'_ut' => 'anon', |
267
|
|
|
'_ui' => $anon_id, |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.