/** * Procedurally build a Tracks Event Object. * NOTE: Use this only when the simpler tracks_record_event() function won't work for you. * @param $identity WP_user object * @param string $event_name The name of the event * @param array $properties Custom properties to send with the event * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred * @return \Tracks_Event|\WP_Error */ function tracks_build_event_obj($user, $event_name, $properties = array(), $event_timestamp_millis = false) { $anon_id = get_user_meta($user->ID, 'jpo_tracks_anon_id', true); if (!$anon_id) { $anon_id = Tracks_Client::get_anon_id(); add_user_meta($user->ID, 'jpo_tracks_anon_id', $anon_id, false); } if (!isset($_COOKIE['tk_ai']) && !headers_sent()) { setcookie('tk_ai', $anon_id); } $identity = array('_ut' => 'anon', '_ui' => $anon_id); $properties['user_lang'] = $user->get('WPLANG'); $blog_details = array('blog_lang' => isset($properties['blog_lang']) ? $properties['blog_lang'] : get_bloginfo('language')); $timestamp = $event_timestamp_millis !== false ? $event_timestamp_millis : round(microtime(true) * 1000); $timestamp_string = is_string($timestamp) ? $timestamp : number_format($timestamp, 0, '', ''); return new Tracks_Event(array_merge($blog_details, (array) $properties, $identity, array('_en' => $event_name, '_ts' => $timestamp_string))); }
/** * Annotate the event with all relevant info. * @param mixed $event Object or (flat) array * @return mixed The transformed event array or WP_Error on failure. */ static function validate_and_sanitize($event) { $event = (object) $event; // Required if (!$event->_en) { return new WP_Error('invalid_event', 'A valid event must be specified via `_en`', 400); } // delete non-routable addresses otherwise geoip will discard the record entirely if (property_exists($event, '_via_ip') && preg_match('/^192\\.168|^10\\./', $event->_via_ip)) { unset($event->_via_ip); } $validated = array('browser_type' => Tracks_Client::BROWSER_TYPE, '_aua' => Tracks_Client::get_user_agent()); $_event = (object) array_merge((array) $event, $validated); // If you want to blacklist property names, do it here. // Make sure we have an event timestamp. if (!isset($_event->_ts)) { $_event->_ts = Tracks_Client::build_timestamp(); } return $_event; }
function tracks_get_identity($user_id) { // Meta is set, and user is still connected. Use WPCOM ID $wpcom_id = 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 (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 = Tracks_Client::get_anon_id(); add_user_meta($user_id, 'jetpack_tracks_anon_id', $anon_id, false); } if (!isset($_COOKIE['tk_ai']) && !headers_sent()) { setcookie('tk_ai', $anon_id); } return array('_ut' => 'anon', '_ui' => $anon_id); }