events
6 years ago
class-wc-site-tracking.php
6 years ago
class-wc-tracks-client.php
6 years ago
class-wc-tracks-event.php
6 years ago
class-wc-tracks-footer-pixel.php
6 years ago
class-wc-tracks.php
6 years ago
class-wc-tracks.php
116 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PHP Tracks Client |
| 4 | * |
| 5 | * @package WooCommerce\Tracks |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * WC_Tracks class. |
| 10 | */ |
| 11 | class WC_Tracks { |
| 12 | |
| 13 | /** |
| 14 | * Tracks event name prefix. |
| 15 | */ |
| 16 | const PREFIX = 'wcadmin_'; |
| 17 | |
| 18 | /** |
| 19 | * Get total product counts. |
| 20 | * |
| 21 | * @return int Number of products. |
| 22 | */ |
| 23 | public static function get_products_count() { |
| 24 | $product_counts = WC_Tracker::get_product_counts(); |
| 25 | return $product_counts['total']; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Gather blog related properties. |
| 30 | * |
| 31 | * @param int $user_id User id. |
| 32 | * @return array Blog details. |
| 33 | */ |
| 34 | public static function get_blog_details( $user_id ) { |
| 35 | $blog_details = get_transient( 'wc_tracks_blog_details' ); |
| 36 | if ( false === $blog_details ) { |
| 37 | $blog_details = array( |
| 38 | 'url' => home_url(), |
| 39 | 'blog_lang' => get_user_locale( $user_id ), |
| 40 | 'blog_id' => class_exists( 'Jetpack_Options' ) ? Jetpack_Options::get_option( 'id' ) : null, |
| 41 | 'products_count' => self::get_products_count(), |
| 42 | ); |
| 43 | set_transient( 'wc_tracks_blog_details', $blog_details, DAY_IN_SECONDS ); |
| 44 | } |
| 45 | return $blog_details; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Gather details from the request to the server. |
| 50 | * |
| 51 | * @return array Server details. |
| 52 | */ |
| 53 | public static function get_server_details() { |
| 54 | $data = array(); |
| 55 | |
| 56 | $data['_via_ua'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; |
| 57 | $data['_via_ip'] = isset( $_SERVER['REMOTE_ADDR'] ) ? wc_clean( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; |
| 58 | $data['_lg'] = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) : ''; |
| 59 | $data['_dr'] = isset( $_SERVER['HTTP_REFERER'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; |
| 60 | |
| 61 | $uri = isset( $_SERVER['REQUEST_URI'] ) ? wc_clean( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; |
| 62 | $host = isset( $_SERVER['HTTP_HOST'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; |
| 63 | $data['_dl'] = isset( $_SERVER['REQUEST_SCHEME'] ) ? wc_clean( wp_unslash( $_SERVER['REQUEST_SCHEME'] ) ) . '://' . $host . $uri : ''; |
| 64 | |
| 65 | return $data; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Record an event in Tracks - this is the preferred way to record events from PHP. |
| 70 | * |
| 71 | * @param string $event_name The name of the event. |
| 72 | * @param array $properties Custom properties to send with the event. |
| 73 | * @return bool|WP_Error True for success or WP_Error if the event pixel could not be fired. |
| 74 | */ |
| 75 | public static function record_event( $event_name, $properties = array() ) { |
| 76 | /** |
| 77 | * Don't track users who don't have tracking enabled. |
| 78 | */ |
| 79 | if ( ! WC_Site_Tracking::is_tracking_enabled() ) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | $user = wp_get_current_user(); |
| 84 | |
| 85 | // We don't want to track user events during unit tests/CI runs. |
| 86 | if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) { |
| 87 | return false; |
| 88 | } |
| 89 | $prefixed_event_name = self::PREFIX . $event_name; |
| 90 | |
| 91 | $data = array( |
| 92 | '_en' => $prefixed_event_name, |
| 93 | '_ts' => WC_Tracks_Client::build_timestamp(), |
| 94 | ); |
| 95 | |
| 96 | $server_details = self::get_server_details(); |
| 97 | $identity = WC_Tracks_Client::get_identity( $user->ID ); |
| 98 | $blog_details = self::get_blog_details( $user->ID ); |
| 99 | |
| 100 | // Allow event props to be filtered to enable adding site-wide props. |
| 101 | $filtered_properties = apply_filters( 'woocommerce_tracks_event_properties', $properties, $prefixed_event_name ); |
| 102 | |
| 103 | // Delete _ui and _ut protected properties. |
| 104 | unset( $filtered_properties['_ui'] ); |
| 105 | unset( $filtered_properties['_ut'] ); |
| 106 | |
| 107 | $event_obj = new WC_Tracks_Event( array_merge( $data, $server_details, $identity, $blog_details, $filtered_properties ) ); |
| 108 | |
| 109 | if ( is_wp_error( $event_obj->error ) ) { |
| 110 | return $event_obj->error; |
| 111 | } |
| 112 | |
| 113 | return $event_obj->record(); |
| 114 | } |
| 115 | } |
| 116 |