2019-02-01 01:26:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHP Tracks Client
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tracks
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Tracks class.
|
|
|
|
*/
|
|
|
|
class WC_Tracks {
|
|
|
|
|
|
|
|
/**
|
2019-02-25 18:57:44 +00:00
|
|
|
* Tracks event name prefix.
|
2019-02-01 01:26:38 +00:00
|
|
|
*/
|
2019-02-25 18:57:44 +00:00
|
|
|
const PREFIX = 'wcadmin_';
|
2019-02-01 01:26:38 +00:00
|
|
|
|
2019-02-21 18:46:58 +00:00
|
|
|
/**
|
|
|
|
* Get total product counts.
|
|
|
|
*
|
2019-02-26 03:34:43 +00:00
|
|
|
* @return int Number of products.
|
2019-02-21 18:46:58 +00:00
|
|
|
*/
|
|
|
|
public static function get_products_count() {
|
|
|
|
$product_counts = WC_Tracker::get_product_counts();
|
|
|
|
return $product_counts['total'];
|
|
|
|
}
|
|
|
|
|
2019-02-01 01:26:38 +00:00
|
|
|
/**
|
|
|
|
* Gather blog related properties.
|
|
|
|
*
|
|
|
|
* @param int $user_id User id.
|
|
|
|
* @return array Blog details.
|
|
|
|
*/
|
|
|
|
public static function get_blog_details( $user_id ) {
|
2019-03-13 13:12:07 +00:00
|
|
|
$blog_details = get_transient( 'wc_tracks_blog_details' );
|
|
|
|
if ( false === $blog_details ) {
|
|
|
|
$blog_details = array(
|
|
|
|
'url' => home_url(),
|
|
|
|
'blog_lang' => get_user_locale( $user_id ),
|
2019-06-27 20:15:17 +00:00
|
|
|
'blog_id' => class_exists( 'Jetpack_Options' ) ? Jetpack_Options::get_option( 'id' ) : null,
|
2019-03-13 13:12:07 +00:00
|
|
|
'products_count' => self::get_products_count(),
|
|
|
|
);
|
|
|
|
set_transient( 'wc_tracks_blog_details', $blog_details, DAY_IN_SECONDS );
|
|
|
|
}
|
|
|
|
return $blog_details;
|
2019-02-01 01:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gather details from the request to the server.
|
|
|
|
*
|
|
|
|
* @return array Server details.
|
|
|
|
*/
|
|
|
|
public static function get_server_details() {
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
$data['_via_ua'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
|
|
|
|
$data['_via_ip'] = isset( $_SERVER['REMOTE_ADDR'] ) ? wc_clean( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
|
|
|
|
$data['_lg'] = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) : '';
|
|
|
|
$data['_dr'] = isset( $_SERVER['HTTP_REFERER'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '';
|
|
|
|
|
|
|
|
$uri = isset( $_SERVER['REQUEST_URI'] ) ? wc_clean( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
|
|
|
|
$host = isset( $_SERVER['HTTP_HOST'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
|
|
|
|
$data['_dl'] = isset( $_SERVER['REQUEST_SCHEME'] ) ? wc_clean( wp_unslash( $_SERVER['REQUEST_SCHEME'] ) ) . '://' . $host . $uri : '';
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record an event in Tracks - this is the preferred way to record events from PHP.
|
|
|
|
*
|
|
|
|
* @param string $event_name The name of the event.
|
|
|
|
* @param array $properties Custom properties to send with the event.
|
|
|
|
* @return bool|WP_Error True for success or WP_Error if the event pixel could not be fired.
|
|
|
|
*/
|
|
|
|
public static function record_event( $event_name, $properties = array() ) {
|
|
|
|
/**
|
2019-05-21 00:20:25 +00:00
|
|
|
* Don't track users who don't have tracking enabled.
|
2019-02-01 01:26:38 +00:00
|
|
|
*/
|
2019-05-21 00:20:25 +00:00
|
|
|
if ( ! WC_Site_Tracking::is_tracking_enabled() ) {
|
2019-02-01 01:26:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = wp_get_current_user();
|
|
|
|
|
|
|
|
// We don't want to track user events during unit tests/CI runs.
|
|
|
|
if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-09 19:46:40 +00:00
|
|
|
$prefixed_event_name = self::PREFIX . $event_name;
|
2019-02-01 01:26:38 +00:00
|
|
|
|
|
|
|
$data = array(
|
2020-03-09 19:46:40 +00:00
|
|
|
'_en' => $prefixed_event_name,
|
2019-02-01 01:26:38 +00:00
|
|
|
'_ts' => WC_Tracks_Client::build_timestamp(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$server_details = self::get_server_details();
|
2019-02-01 01:26:38 +00:00
|
|
|
$identity = WC_Tracks_Client::get_identity( $user->ID );
|
2019-02-01 01:26:38 +00:00
|
|
|
$blog_details = self::get_blog_details( $user->ID );
|
|
|
|
|
2020-03-09 19:46:40 +00:00
|
|
|
// Allow event props to be filtered to enable adding site-wide props.
|
2020-03-17 00:00:25 +00:00
|
|
|
$filtered_properties = apply_filters( 'woocommerce_tracks_event_properties', $properties, $prefixed_event_name );
|
|
|
|
|
|
|
|
// Delete _ui and _ut protected properties.
|
|
|
|
unset( $filtered_properties['_ui'] );
|
|
|
|
unset( $filtered_properties['_ut'] );
|
2020-03-09 19:46:40 +00:00
|
|
|
|
|
|
|
$event_obj = new WC_Tracks_Event( array_merge( $data, $server_details, $identity, $blog_details, $filtered_properties ) );
|
2019-02-01 01:26:38 +00:00
|
|
|
|
|
|
|
if ( is_wp_error( $event_obj->error ) ) {
|
|
|
|
return $event_obj->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $event_obj->record();
|
|
|
|
}
|
|
|
|
}
|