1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nosara Tracks for Jetpack |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Automattic\Jetpack; |
7
|
|
|
|
8
|
|
|
class Tracking { |
9
|
|
|
public $product_name; |
10
|
|
|
|
11
|
|
|
function __construct( $product_name = 'jetpack' ) { |
12
|
|
|
$this->product_name = $product_name; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
View Code Duplication |
static function enqueue_tracks_scripts() { |
16
|
|
|
wp_enqueue_script( 'jptracks', plugins_url( '_inc/lib/tracks/tracks-ajax.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); |
17
|
|
|
wp_localize_script( |
18
|
|
|
'jptracks', |
19
|
|
|
'jpTracksAJAX', |
20
|
|
|
array( |
21
|
|
|
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
22
|
|
|
'jpTracksAJAX_nonce' => wp_create_nonce( 'jp-tracks-ajax-nonce' ), |
23
|
|
|
) |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
static function record_user_event( $event_type, $data = array(), $user = null ) { |
28
|
|
|
if ( ! $user ) { |
29
|
|
|
$user = wp_get_current_user(); |
30
|
|
|
} |
31
|
|
|
$site_url = get_option( 'siteurl' ); |
32
|
|
|
|
33
|
|
|
$data['_via_ua'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
34
|
|
|
$data['_via_ip'] = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : ''; |
35
|
|
|
$data['_lg'] = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''; |
36
|
|
|
$data['blog_url'] = $site_url; |
37
|
|
|
$data['blog_id'] = \Jetpack_Options::get_option( 'id' ); |
38
|
|
|
|
39
|
|
|
// Top level events should not be namespaced |
40
|
|
|
if ( '_aliasUser' != $event_type ) { |
41
|
|
|
$event_type = self::$product_name . '_' . $event_type; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '0'; |
45
|
|
|
|
46
|
|
|
return self::jetpack_tracks_record_event( $user, $event_type, $data ); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Procedurally build a Tracks Event Object. |
51
|
|
|
* NOTE: Use this only when the simpler \Automattic\Jetpack\Tracking::jetpack_tracks_record_event() function won't work for you. |
52
|
|
|
* |
53
|
|
|
* @param $identity WP_user object |
54
|
|
|
* @param string $event_name The name of the event |
55
|
|
|
* @param array $properties Custom properties to send with the event |
56
|
|
|
* @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
|
|
|
|
57
|
|
|
* @return \Jetpack_Tracks_Event|\WP_Error |
58
|
|
|
*/ |
59
|
|
|
function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
60
|
|
|
|
61
|
|
|
$tracks = new \Automattic\Jetpack\Tracking(); |
62
|
|
|
$identity = $tracks->tracks_get_identity( $user->ID ); |
63
|
|
|
|
64
|
|
|
$properties['user_lang'] = $user->get( 'WPLANG' ); |
65
|
|
|
|
66
|
|
|
$blog_details = array( |
67
|
|
|
'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' ), |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$timestamp = ( $event_timestamp_millis !== false ) ? $event_timestamp_millis : round( microtime( true ) * 1000 ); |
71
|
|
|
$timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' ); |
72
|
|
|
|
73
|
|
|
return new \Jetpack_Tracks_Event( |
74
|
|
|
array_merge( |
75
|
|
|
$blog_details, |
76
|
|
|
(array) $properties, |
77
|
|
|
$identity, |
78
|
|
|
array( |
79
|
|
|
'_en' => $event_name, |
80
|
|
|
'_ts' => $timestamp_string, |
81
|
|
|
) |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/* |
87
|
|
|
* Get the identity to send to tracks. |
88
|
|
|
* |
89
|
|
|
* @param int $user_id The user id of the local user |
90
|
|
|
* @return array $identity |
91
|
|
|
*/ |
92
|
|
|
function tracks_get_identity( $user_id ) { |
93
|
|
|
|
94
|
|
|
// Meta is set, and user is still connected. Use WPCOM ID |
95
|
|
|
$wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true ); |
96
|
|
|
if ( $wpcom_id && \Jetpack::is_user_connected( $user_id ) ) { |
|
|
|
|
97
|
|
|
return array( |
98
|
|
|
'_ut' => 'wpcom:user_id', |
99
|
|
|
'_ui' => $wpcom_id, |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// User is connected, but no meta is set yet. Use WPCOM ID and set meta. |
104
|
|
|
if ( \Jetpack::is_user_connected( $user_id ) ) { |
|
|
|
|
105
|
|
|
$wpcom_user_data = Jetpack::get_connected_user_data( $user_id ); |
106
|
|
|
update_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'] ); |
107
|
|
|
|
108
|
|
|
return array( |
109
|
|
|
'_ut' => 'wpcom:user_id', |
110
|
|
|
'_ui' => $wpcom_user_data['ID'], |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// User isn't linked at all. Fall back to anonymous ID. |
115
|
|
|
$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true ); |
116
|
|
|
if ( ! $anon_id ) { |
117
|
|
|
$anon_id = \Jetpack_Tracks_Client::get_anon_id(); |
118
|
|
|
add_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id, false ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ( ! isset( $_COOKIE['tk_ai'] ) && ! headers_sent() ) { |
122
|
|
|
setcookie( 'tk_ai', $anon_id ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return array( |
126
|
|
|
'_ut' => 'anon', |
127
|
|
|
'_ui' => $anon_id, |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Record an event in Tracks - this is the preferred way to record events from PHP. |
134
|
|
|
* |
135
|
|
|
* @param mixed $identity username, user_id, or WP_user object |
|
|
|
|
136
|
|
|
* @param string $event_name The name of the event |
137
|
|
|
* @param array $properties Custom properties to send with the event |
138
|
|
|
* @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
|
|
|
|
139
|
|
|
* @return bool true for success | \WP_Error if the event pixel could not be fired |
140
|
|
|
*/ |
141
|
|
|
function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
142
|
|
|
|
143
|
|
|
// We don't want to track user events during unit tests/CI runs. |
144
|
|
|
if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) { |
|
|
|
|
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$event_obj = self::tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis ); |
149
|
|
|
|
150
|
|
|
if ( is_wp_error( $event_obj->error ) ) { |
151
|
|
|
return $event_obj->error; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $event_obj->record(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.