function testIdentify() { $identified = Analytics::identify("some_user", array( "name" => "Calvin", "loves_php" => false, "birthday" => time(), )); $this->assertTrue($identified); }
public static function identify($user_id = 0, $traits = array()) { if (!$user_id && is_user_logged_in()) { $user_id = get_current_user_id(); } // A user is necessary if (!$user_id) { return; } $user = get_userdata($user_id); $user_traits = array(); // fill in any traits not passed to the method if (is_a($user, 'WP_User')) { $user_traits = array('name' => $user->first_name . ' ' . $user->last_name, 'email' => $user->user_email); } // merge with existing $traits = wp_parse_args($traits, $user_traits); // traits are necessary if (empty($traits)) { return; } self::load_segment_api(); Analytics::identify(array('userId' => $user_id, 'traits' => $traits)); }
/** * Identify the user in the analytics system * * @param array $identification The array of data for Segment.io's \Analytics::identify() (Empty * generates an anonymousId.) * @param bool $js Set this to generate the content as JS once you run render() * @param bool $render_safe Set this to true to enable running \Security::htmlentities() on all traits. * @param array $js_options The array of options to set for the JS "options" parameter - "integrations" * options get specified in $identification, but may be overridden here. * @param string $js_callback If you want to use a callback function with "identify," specify it here. * * @return mixed True or false depending on if the PHP version succeeded, or the JS code if we compiled the JS code */ public function identify(array $identification = array(), $js = true, array $js_options = array(), $js_callback = null) { // Don't track a customer if they don't want to be tracked. if ($this->dnt === true) { return; } $identification['userId'] = empty($identification['userId']) ? $this->identity['userId'] : $identification['userId']; /** * Set this for anywhere in the system that needs access to it. It's already part of $identification, * so we don't merge it in. */ if (!empty($identification['userId'])) { $this->set_user_id($identification['userId']); } else { $identification['anonymousId'] = !empty($identification['anonymousId']) ? $identification['anonymousId'] : $this->identity['anonymousId']; } $identification = \Arr::merge($this->_get_context($js), $identification); if ($js !== true) { return \Analytics::identify($identification); } else { // User ID (JS generates an anonymous ID if we don't send this.) $js_params[] = !empty($identification['userId']) ? "'" . $identification['userId'] . "'" : "null"; // Traits $js_params[] = !empty($identification['traits']) ? json_encode($identification['traits']) : "{}"; // Integrations $js_params[] = $this->_set_js_options($js_options, $identification); // Callback function $js_params[] = !empty($js_callback) ? $js_callback : "null"; // Add it to the queue. return $this->_js_scripts['identify'] = 'analytics.identify(' . implode(',', $js_params) . ');'; } }
<?php /** * Initialize the library */ date_default_timezone_set('America/Los_Angeles'); require_once "./analytics/lib/Segment.php"; class_alias('Segment', 'Analytics'); Analytics::init("testsecret", array("debug" => true, "error_handler" => function ($code, $msg) { error_log("error_log: " . $code . " " . $msg); })); /** * Create a random user to identify. */ $user = "******" . rand(); Analytics::identify(array("userId" => $user, "traits" => array("name" => "Michael Bolton", "email" => "*****@*****.**"))); echo "User: {$user}";
public function wp_footer() { // Identify the user if the current user merits it. $identify = $this->get_current_user_identify(); if ($identify) { Analytics::identify($identify['user_id'], $identify['traits']); } // Track a custom page view event if the current page merits it. $track = $this->get_current_page_track(); if ($track) { Analytics::track($track['event'], $track['properties']); } }