class-wc-tracks.php
4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* PHP Tracks Client
*
* @package WooCommerce\Tracks
*/
/**
* WC_Tracks class.
*/
class WC_Tracks {
/**
* Tracks event name prefix.
*/
const PREFIX = 'wcadmin_';
/**
* Get total product counts.
*
* @return int Number of products.
*/
public static function get_products_count() {
$product_counts = WC_Tracker::get_product_counts();
return $product_counts['total'];
}
/**
* Gather blog related properties.
*
* @param int $user_id User id.
* @return array Blog details.
*/
public static function get_blog_details( $user_id ) {
$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 ),
'blog_id' => class_exists( 'Jetpack_Options' ) ? Jetpack_Options::get_option( 'id' ) : null,
'products_count' => self::get_products_count(),
'wc_version' => WC()->version,
);
set_transient( 'wc_tracks_blog_details', $blog_details, DAY_IN_SECONDS );
}
return $blog_details;
}
/**
* 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.
* Note: the event request won't be made if $properties has a member called `error`.
*
* @param string $event_name The name of the event.
* @param array $event_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, $event_properties = array() ) {
/**
* Don't track users who don't have tracking enabled.
*/
if ( ! WC_Site_Tracking::is_tracking_enabled() ) {
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;
}
$prefixed_event_name = self::PREFIX . $event_name;
$properties = self::get_properties( $prefixed_event_name, $event_properties );
$event_obj = new WC_Tracks_Event( $properties );
if ( is_wp_error( $event_obj->error ) ) {
return $event_obj->error;
}
return $event_obj->record();
}
/**
* Get all properties for the event including filtered and identity properties.
*
* @param string $event_name Event name.
* @param array $event_properties Event specific properties.
* @return array
*/
public static function get_properties( $event_name, $event_properties ) {
/**
* Allow event props to be filtered to enable adding site-wide props.
*
* @since 4.1.0
*/
$properties = apply_filters( 'woocommerce_tracks_event_properties', $event_properties, $event_name );
$user = wp_get_current_user();
$identity = WC_Tracks_Client::get_identity( $user->ID );
// Delete _ui and _ut protected properties.
unset( $properties['_ui'] );
unset( $properties['_ut'] );
$data = $event_name
? array(
'_en' => $event_name,
'_ts' => WC_Tracks_Client::build_timestamp(),
)
: array();
$server_details = self::get_server_details();
$blog_details = self::get_blog_details( $user->ID );
return array_merge( $properties, $data, $server_details, $identity, $blog_details );
}
}