Tracks: Add a PHP client
This commit is contained in:
parent
5c414507f9
commit
851a2557f6
|
@ -64,6 +64,7 @@ class WC_Customer_Data_Store extends WC_Data_Store_WP implements WC_Customer_Dat
|
|||
'syntax_highlighting',
|
||||
'_order_count',
|
||||
'_money_spent',
|
||||
'_woo_tracks_anon_id',
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
|
@ -92,10 +92,49 @@ class WC_Tracks_Client {
|
|||
return number_format( $ts, 0, '', '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user's identity to send to Tracks. If Jetpack exists, default to its implementation.
|
||||
*
|
||||
* @param int $user_id User id.
|
||||
* @return array Identity properties.
|
||||
*/
|
||||
public static function get_identity( $user_id ) {
|
||||
if ( class_exists( 'Jetpack' ) ) {
|
||||
|
||||
include_once trailingslashit( WP_PLUGIN_DIR ) . 'jetpack/_inc/lib/tracks/client.php';
|
||||
|
||||
if ( function_exists( 'jetpack_tracks_get_identity' ) ) {
|
||||
return jetpack_tracks_get_identity( $user_id );
|
||||
}
|
||||
}
|
||||
|
||||
// Start with a previously set cookie.
|
||||
$anon_id = isset( $_COOKIE['tk_ai'] ) ? wp_unslash( $_COOKIE['tk_ai'] ) : false; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
|
||||
// If there is no cookie, apply a saved id.
|
||||
if ( ! $anon_id ) {
|
||||
$anon_id = get_user_meta( $user_id, '_woocommerce_tracks_anon_id', true );
|
||||
}
|
||||
|
||||
// If an id is still not found, create one and save it.
|
||||
if ( ! $anon_id ) {
|
||||
$anon_id = self::get_anon_id();
|
||||
update_user_meta( $user_id, '_woocommerce_tracks_anon_id', $anon_id );
|
||||
}
|
||||
|
||||
if ( ! isset( $_COOKIE['tk_ai'] ) ) {
|
||||
wc_setcookie( 'tk_ai', $anon_id );
|
||||
}
|
||||
|
||||
return array(
|
||||
'_ut' => 'anon',
|
||||
'_ui' => $anon_id,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grabs the user's anon id from cookies, or generates and sets a new one
|
||||
*
|
||||
* @todo: Determine the best way to identify sites/users with/without Jetpack connection.
|
||||
* @return string An anon id for the user
|
||||
*/
|
||||
public static function get_anon_id() {
|
||||
|
@ -104,25 +143,26 @@ class WC_Tracks_Client {
|
|||
if ( ! isset( $anon_id ) ) {
|
||||
|
||||
// Did the browser send us a cookie?
|
||||
if ( isset( $_COOKIE['tk_ai'] ) && preg_match( '#^[A-Za-z0-9+/=]{24}$#', wp_unslash( $_COOKIE['tk_ai'] ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
$anon_id = wp_unslash( $_COOKIE['tk_ai'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
if ( isset( $_COOKIE['tk_ai'] ) ) {
|
||||
$anon_id = sanitize_text_field( wp_unslash( $_COOKIE['tk_ai'] ) );
|
||||
} else {
|
||||
|
||||
$binary = '';
|
||||
|
||||
// Generate a new anonId and try to save it in the browser's cookies
|
||||
// Generate a new anonId and try to save it in the browser's cookies.
|
||||
// Note that base64-encoding an 18 character string generates a 24-character anon id.
|
||||
for ( $i = 0; $i < 18; ++$i ) {
|
||||
$binary .= chr( wp_rand( 0, 255 ) );
|
||||
}
|
||||
|
||||
$anon_id = 'jetpack:' . base64_encode( $binary );
|
||||
$anon_id = 'woo:' . base64_encode( $binary );
|
||||
|
||||
if ( ! headers_sent()
|
||||
&& ! ( defined( 'REST_REQUEST' ) && REST_REQUEST )
|
||||
&& ! ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
|
||||
// Don't set cookie on API requests.
|
||||
if (
|
||||
! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) &&
|
||||
! ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
|
||||
) {
|
||||
setcookie( 'tk_ai', $anon_id );
|
||||
wc_setcookie( 'tk_ai', $anon_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,54 +17,6 @@ class WC_Tracks {
|
|||
*/
|
||||
const PREFIX = 'wca_test_';
|
||||
|
||||
/**
|
||||
* Get the identity to send to tracks.
|
||||
*
|
||||
* @todo Determine the best way to identify sites/users with/without Jetpack connection.
|
||||
* @param int $user_id User id.
|
||||
* @return array Identity properties.
|
||||
*/
|
||||
public static function get_identity( $user_id ) {
|
||||
$has_jetpack = class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_active' ) && Jetpack::is_active();
|
||||
|
||||
// Meta is set, and user is still connected. Use WPCOM ID.
|
||||
$wpcom_id = $has_jetpack && get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true );
|
||||
if ( $wpcom_id && Jetpack::is_user_connected( $user_id ) ) {
|
||||
return array(
|
||||
'_ut' => 'wpcom:user_id',
|
||||
'_ui' => $wpcom_id,
|
||||
);
|
||||
}
|
||||
|
||||
// User is connected, but no meta is set yet. Use WPCOM ID and set meta.
|
||||
if ( $has_jetpack && Jetpack::is_user_connected( $user_id ) ) {
|
||||
$wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
|
||||
add_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'], true );
|
||||
|
||||
return array(
|
||||
'_ut' => 'wpcom:user_id',
|
||||
'_ui' => $wpcom_user_data['ID'],
|
||||
);
|
||||
}
|
||||
|
||||
// User isn't linked at all. Fall back to anonymous ID.
|
||||
$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
|
||||
if ( ! $anon_id ) {
|
||||
$anon_id = WC_Tracks_Client::get_anon_id();
|
||||
add_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id, false );
|
||||
}
|
||||
|
||||
if ( ! isset( $_COOKIE['tk_ai'] ) && ! headers_sent() ) {
|
||||
wc_setcookie( 'tk_ai', $anon_id );
|
||||
}
|
||||
|
||||
return array(
|
||||
'_ut' => 'anon',
|
||||
'_ui' => $anon_id,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gather blog related properties.
|
||||
*
|
||||
|
@ -76,7 +28,7 @@ class WC_Tracks {
|
|||
|
||||
return array(
|
||||
// @todo Add revenue info and url similar to wc-tracker.
|
||||
'url' => get_option( 'siteurl' ),
|
||||
'url' => home_url(),
|
||||
'blog_lang' => get_user_locale( $user_id ),
|
||||
'blog_id' => ( class_exists( 'Jetpack' ) && Jetpack_Options::get_option( 'id' ) ) || null,
|
||||
'products_count' => $product_counts['total'],
|
||||
|
@ -135,7 +87,7 @@ class WC_Tracks {
|
|||
);
|
||||
|
||||
$server_details = self::get_server_details();
|
||||
$identity = self::get_identity( $user->ID );
|
||||
$identity = WC_Tracks_Client::get_identity( $user->ID );
|
||||
$blog_details = self::get_blog_details( $user->ID );
|
||||
|
||||
$event_obj = new WC_Tracks_Event( array_merge( $data, $server_details, $identity, $blog_details, $properties ) );
|
||||
|
|
Loading…
Reference in New Issue