init
This commit is contained in:
@@ -0,0 +1 @@
|
||||
<?php
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
class Base_MixpanelBase {
|
||||
private $_defaults = array(
|
||||
"max_batch_size" => 50, // the max batch size Mixpanel will accept is 50,
|
||||
"max_queue_size" => 1000, // the max num of items to hold in memory before flushing
|
||||
"debug" => false, // enable/disable debug mode
|
||||
"consumer" => "curl", // which consumer to use
|
||||
"host" => "api.mixpanel.com", // the host name for api calls
|
||||
"events_endpoint" => "/track", // host relative endpoint for events
|
||||
"people_endpoint" => "/engage", // host relative endpoint for people updates
|
||||
"groups_endpoint" => "/groups", // host relative endpoint for groups updates
|
||||
"use_ssl" => true, // use ssl when available
|
||||
"error_callback" => null // callback to use on consumption failures
|
||||
);
|
||||
protected $_options = array();
|
||||
public function __construct($options = array()) {
|
||||
$options = array_merge($this->_defaults, $options);
|
||||
$this->_options = $options;
|
||||
}
|
||||
protected function _log($msg) {
|
||||
$arr = debug_backtrace();
|
||||
$class = $arr[0]['class'];
|
||||
$line = $arr[0]['line'];
|
||||
error_log ( "[ $class - line $line ] : " . $msg );
|
||||
}
|
||||
protected function _debug() {
|
||||
return isset($this->_options["debug"]) && $this->_options["debug"] == true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php
|
||||
wp-content/plugins/mailpoet/vendor/mixpanel/mixpanel-php/lib/ConsumerStrategies/AbstractConsumer.php
Vendored
+30
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
require_once(dirname(__FILE__) . "/../Base/MixpanelBase.php");
|
||||
abstract class ConsumerStrategies_AbstractConsumer extends Base_MixpanelBase {
|
||||
function __construct($options = array()) {
|
||||
parent::__construct($options);
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Instantiated new Consumer");
|
||||
}
|
||||
}
|
||||
protected function _encode($params) {
|
||||
return base64_encode(json_encode($params));
|
||||
}
|
||||
protected function _handleError($code, $msg) {
|
||||
if (isset($this->_options['error_callback'])) {
|
||||
$handler = $this->_options['error_callback'];
|
||||
call_user_func($handler, $code, $msg);
|
||||
}
|
||||
if ($this->_debug()) {
|
||||
$arr = debug_backtrace();
|
||||
$class = get_class($arr[0]['object']);
|
||||
$line = $arr[0]['line'];
|
||||
error_log ( "[ $class - line $line ] : " . print_r($msg, true) );
|
||||
}
|
||||
}
|
||||
public function getNumThreads() {
|
||||
return 1;
|
||||
}
|
||||
abstract function persist($batch);
|
||||
}
|
||||
Vendored
+142
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
require_once(dirname(__FILE__) . "/AbstractConsumer.php");
|
||||
class ConsumerStrategies_CurlConsumer extends ConsumerStrategies_AbstractConsumer {
|
||||
protected $_host;
|
||||
protected $_endpoint;
|
||||
protected $_connect_timeout;
|
||||
protected $_timeout;
|
||||
protected $_protocol;
|
||||
protected $_fork = null;
|
||||
protected $_num_threads;
|
||||
function __construct($options) {
|
||||
parent::__construct($options);
|
||||
$this->_host = $options['host'];
|
||||
$this->_endpoint = $options['endpoint'];
|
||||
$this->_connect_timeout = isset($options['connect_timeout']) ? $options['connect_timeout'] : 5;
|
||||
$this->_timeout = isset($options['timeout']) ? $options['timeout'] : 30;
|
||||
$this->_protocol = isset($options['use_ssl']) && $options['use_ssl'] == true ? "https" : "http";
|
||||
$this->_fork = isset($options['fork']) ? ($options['fork'] == true) : false;
|
||||
$this->_num_threads = isset($options['num_threads']) ? max(1, intval($options['num_threads'])) : 1;
|
||||
// ensure the environment is workable for the given settings
|
||||
if ($this->_fork == true) {
|
||||
$exists = function_exists('exec');
|
||||
if (!$exists) {
|
||||
throw new Exception('The "exec" function must exist to use the cURL consumer in "fork" mode. Try setting fork = false or use another consumer.');
|
||||
}
|
||||
$disabled = explode(', ', ini_get('disable_functions'));
|
||||
$enabled = !in_array('exec', $disabled);
|
||||
if (!$enabled) {
|
||||
throw new Exception('The "exec" function must be enabled to use the cURL consumer in "fork" mode. Try setting fork = false or use another consumer.');
|
||||
}
|
||||
} else {
|
||||
if (!function_exists('curl_init')) {
|
||||
throw new Exception('The cURL PHP extension is required to use the cURL consumer with fork = false. Try setting fork = true or use another consumer.');
|
||||
}
|
||||
}
|
||||
}
|
||||
public function persist($batch) {
|
||||
if (count($batch) > 0) {
|
||||
$url = $this->_protocol . "://" . $this->_host . $this->_endpoint;
|
||||
if ($this->_fork) {
|
||||
$data = "data=" . $this->_encode($batch);
|
||||
return $this->_execute_forked($url, $data);
|
||||
} else {
|
||||
return $this->_execute($url, $batch);
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
protected function _execute($url, $batch) {
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Making blocking cURL call to $url");
|
||||
}
|
||||
$mh = curl_multi_init();
|
||||
$chs = array();
|
||||
$batch_size = ceil(count($batch) / $this->_num_threads);
|
||||
for ($i=0; $i<$this->_num_threads && !empty($batch); $i++) {
|
||||
$ch = curl_init();
|
||||
$chs[] = $ch;
|
||||
$data = "data=" . $this->_encode(array_splice($batch, 0, $batch_size));
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_connect_timeout);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
curl_multi_add_handle($mh,$ch);
|
||||
}
|
||||
$running = 0;
|
||||
do {
|
||||
curl_multi_exec($mh, $running);
|
||||
curl_multi_select($mh);
|
||||
} while ($running > 0);
|
||||
$info = curl_multi_info_read($mh);
|
||||
$error = false;
|
||||
foreach ($chs as $ch) {
|
||||
$response = curl_multi_getcontent($ch);
|
||||
if (false === $response) {
|
||||
$this->_handleError(curl_errno($ch), curl_error($ch));
|
||||
$error = true;
|
||||
}
|
||||
elseif ("1" != trim($response)) {
|
||||
$this->_handleError(0, $response);
|
||||
$error = true;
|
||||
}
|
||||
curl_multi_remove_handle($mh, $ch);
|
||||
}
|
||||
if (CURLE_OK != $info['result']) {
|
||||
$this->_handleError($info['result'], "cURL error with code=".$info['result']);
|
||||
$error = true;
|
||||
}
|
||||
curl_multi_close($mh);
|
||||
return !$error;
|
||||
}
|
||||
protected function _execute_forked($url, $data) {
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Making forked cURL call to $url");
|
||||
}
|
||||
$exec = 'curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d ' . $data . ' "' . $url . '"';
|
||||
if(!$this->_debug()) {
|
||||
$exec .= " >/dev/null 2>&1 &";
|
||||
}
|
||||
exec($exec, $output, $return_var);
|
||||
if ($return_var != 0) {
|
||||
$this->_handleError($return_var, $output);
|
||||
}
|
||||
return $return_var == 0;
|
||||
}
|
||||
public function getConnectTimeout()
|
||||
{
|
||||
return $this->_connect_timeout;
|
||||
}
|
||||
public function getEndpoint()
|
||||
{
|
||||
return $this->_endpoint;
|
||||
}
|
||||
public function getFork()
|
||||
{
|
||||
return $this->_fork;
|
||||
}
|
||||
public function getHost()
|
||||
{
|
||||
return $this->_host;
|
||||
}
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->_options;
|
||||
}
|
||||
public function getProtocol()
|
||||
{
|
||||
return $this->_protocol;
|
||||
}
|
||||
public function getTimeout()
|
||||
{
|
||||
return $this->_timeout;
|
||||
}
|
||||
public function getNumThreads() {
|
||||
return $this->_num_threads;
|
||||
}
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
require_once(dirname(__FILE__) . "/AbstractConsumer.php");
|
||||
class ConsumerStrategies_FileConsumer extends ConsumerStrategies_AbstractConsumer {
|
||||
private $_file;
|
||||
function __construct($options) {
|
||||
parent::__construct($options);
|
||||
// what file to write to?
|
||||
$this->_file = isset($options['file']) ? $options['file'] : dirname(__FILE__)."/../../messages.txt";
|
||||
}
|
||||
public function persist($batch) {
|
||||
if (count($batch) > 0) {
|
||||
return file_put_contents($this->_file, json_encode($batch)."\n", FILE_APPEND | LOCK_EX) !== false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+162
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
require_once(dirname(__FILE__) . "/AbstractConsumer.php");
|
||||
class ConsumerStrategies_SocketConsumer extends ConsumerStrategies_AbstractConsumer {
|
||||
private $_host;
|
||||
private $_endpoint;
|
||||
private $_connect_timeout;
|
||||
private $_protocol;
|
||||
private $_socket;
|
||||
private $_async;
|
||||
private $_port;
|
||||
public function __construct($options = array()) {
|
||||
parent::__construct($options);
|
||||
$this->_host = $options['host'];
|
||||
$this->_endpoint = $options['endpoint'];
|
||||
$this->_connect_timeout = isset($options['connect_timeout']) ? $options['connect_timeout'] : 5;
|
||||
$this->_async = isset($options['async']) && $options['async'] === false ? false : true;
|
||||
if (array_key_exists('use_ssl', $options) && $options['use_ssl'] == true) {
|
||||
$this->_protocol = "ssl";
|
||||
$this->_port = 443;
|
||||
} else {
|
||||
$this->_protocol = "tcp";
|
||||
$this->_port = 80;
|
||||
}
|
||||
}
|
||||
public function persist($batch) {
|
||||
$socket = $this->_getSocket();
|
||||
if (!is_resource($socket)) {
|
||||
return false;
|
||||
}
|
||||
$data = "data=".$this->_encode($batch);
|
||||
$body = "";
|
||||
$body.= "POST ".$this->_endpoint." HTTP/1.1\r\n";
|
||||
$body.= "Host: " . $this->_host . "\r\n";
|
||||
$body.= "Content-Type: application/x-www-form-urlencoded\r\n";
|
||||
$body.= "Accept: application/json\r\n";
|
||||
$body.= "Content-length: " . strlen($data) . "\r\n";
|
||||
$body.= "\r\n";
|
||||
$body.= $data;
|
||||
return $this->_write($socket, $body);
|
||||
}
|
||||
private function _getSocket() {
|
||||
if(is_resource($this->_socket)) {
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Using existing socket");
|
||||
}
|
||||
return $this->_socket;
|
||||
} else {
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Creating new socket at ".time());
|
||||
}
|
||||
return $this->_createSocket();
|
||||
}
|
||||
}
|
||||
private function _createSocket($retry = true) {
|
||||
try {
|
||||
$socket = pfsockopen($this->_protocol . "://" . $this->_host, $this->_port, $err_no, $err_msg, $this->_connect_timeout);
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Opening socket connection to " . $this->_protocol . "://" . $this->_host . ":" . $this->_port);
|
||||
}
|
||||
if ($err_no != 0) {
|
||||
$this->_handleError($err_no, $err_msg);
|
||||
return $retry == true ? $this->_createSocket(false) : false;
|
||||
} else {
|
||||
// cache the socket
|
||||
$this->_socket = $socket;
|
||||
return $socket;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->_handleError($e->getCode(), $e->getMessage());
|
||||
return $retry == true ? $this->_createSocket(false) : false;
|
||||
}
|
||||
}
|
||||
private function _destroySocket() {
|
||||
$socket = $this->_socket;
|
||||
$this->_socket = null;
|
||||
fclose($socket);
|
||||
}
|
||||
private function _write($socket, $data, $retry = true) {
|
||||
$bytes_sent = 0;
|
||||
$bytes_total = strlen($data);
|
||||
$socket_closed = false;
|
||||
$success = true;
|
||||
$max_bytes_per_write = 8192;
|
||||
// if we have no data to write just return true
|
||||
if ($bytes_total == 0) {
|
||||
return true;
|
||||
}
|
||||
// try to write the data
|
||||
while (!$socket_closed && $bytes_sent < $bytes_total) {
|
||||
try {
|
||||
$bytes = fwrite($socket, $data, $max_bytes_per_write);
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Socket wrote ".$bytes." bytes");
|
||||
}
|
||||
// if we actually wrote data, then remove the written portion from $data left to write
|
||||
if ($bytes > 0) {
|
||||
$data = substr($data, $max_bytes_per_write);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->_handleError($e->getCode(), $e->getMessage());
|
||||
$socket_closed = true;
|
||||
}
|
||||
if (isset($bytes) && $bytes) {
|
||||
$bytes_sent += $bytes;
|
||||
} else {
|
||||
$socket_closed = true;
|
||||
}
|
||||
}
|
||||
// create a new socket if the current one is closed and retry the message
|
||||
if ($socket_closed) {
|
||||
$this->_destroySocket();
|
||||
if ($retry) {
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Retrying socket write...");
|
||||
}
|
||||
$socket = $this->_getSocket();
|
||||
if ($socket) return $this->_write($socket, $data, false);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// only wait for the response in debug mode or if we explicitly want to be synchronous
|
||||
if ($this->_debug() || !$this->_async) {
|
||||
$res = $this->handleResponse(fread($socket, 2048));
|
||||
if ($res["status"] != "200") {
|
||||
$this->_handleError($res["status"], $res["body"]);
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
private function handleResponse($response) {
|
||||
$lines = explode("\n", $response);
|
||||
// extract headers
|
||||
$headers = array();
|
||||
foreach($lines as $line) {
|
||||
$kvsplit = explode(":", $line);
|
||||
if (count($kvsplit) == 2) {
|
||||
$header = $kvsplit[0];
|
||||
$value = $kvsplit[1];
|
||||
$headers[$header] = trim($value);
|
||||
}
|
||||
}
|
||||
// extract status
|
||||
$line_one_exploded = explode(" ", $lines[0]);
|
||||
$status = $line_one_exploded[1];
|
||||
// extract body
|
||||
$body = $lines[count($lines) - 1];
|
||||
// if the connection has been closed lets kill the socket
|
||||
if (isset($headers["Connection"]) and $headers['Connection'] == "close") {
|
||||
$this->_destroySocket();
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Server told us connection closed so lets destroy the socket so it'll reconnect on next call");
|
||||
}
|
||||
}
|
||||
$ret = array(
|
||||
"status" => $status,
|
||||
"body" => $body,
|
||||
);
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
<?php
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
require_once(dirname(__FILE__) . "/Base/MixpanelBase.php");
|
||||
require_once(dirname(__FILE__) . "/Producers/MixpanelPeople.php");
|
||||
require_once(dirname(__FILE__) . "/Producers/MixpanelEvents.php");
|
||||
require_once(dirname(__FILE__) . "/Producers/MixpanelGroups.php");
|
||||
class Mixpanel extends Base_MixpanelBase {
|
||||
public $people;
|
||||
private $_events;
|
||||
public $group;
|
||||
private static $_instances = array();
|
||||
public function __construct($token, $options = array()) {
|
||||
parent::__construct($options);
|
||||
$this->people = new Producers_MixpanelPeople($token, $options);
|
||||
$this->_events = new Producers_MixpanelEvents($token, $options);
|
||||
$this->group = new Producers_MixpanelGroups($token, $options);
|
||||
}
|
||||
public static function getInstance($token, $options = array()) {
|
||||
if(!isset(self::$_instances[$token])) {
|
||||
self::$_instances[$token] = new Mixpanel($token, $options);
|
||||
}
|
||||
return self::$_instances[$token];
|
||||
}
|
||||
public function enqueue($message = array()) {
|
||||
$this->_events->enqueue($message);
|
||||
}
|
||||
public function enqueueAll($messages = array()) {
|
||||
$this->_events->enqueueAll($messages);
|
||||
}
|
||||
public function flush($desired_batch_size = 50) {
|
||||
$this->_events->flush($desired_batch_size);
|
||||
}
|
||||
public function reset() {
|
||||
$this->_events->reset();
|
||||
}
|
||||
public function identify($user_id, $anon_id = null) {
|
||||
$this->_events->identify($user_id, $anon_id);
|
||||
}
|
||||
public function track($event, $properties = array()) {
|
||||
$this->_events->track($event, $properties);
|
||||
}
|
||||
public function register($property, $value) {
|
||||
$this->_events->register($property, $value);
|
||||
}
|
||||
public function registerAll($props_and_vals = array()) {
|
||||
$this->_events->registerAll($props_and_vals);
|
||||
}
|
||||
public function registerOnce($property, $value) {
|
||||
$this->_events->registerOnce($property, $value);
|
||||
}
|
||||
public function registerAllOnce($props_and_vals = array()) {
|
||||
$this->_events->registerAllOnce($props_and_vals);
|
||||
}
|
||||
public function unregister($property) {
|
||||
$this->_events->unregister($property);
|
||||
}
|
||||
public function unregisterAll($properties) {
|
||||
$this->_events->unregisterAll($properties);
|
||||
}
|
||||
public function getProperty($property)
|
||||
{
|
||||
return $this->_events->getProperty($property);
|
||||
}
|
||||
public function createAlias($distinct_id, $alias) {
|
||||
$this->_events->createAlias($distinct_id, $alias);
|
||||
}
|
||||
}
|
||||
Vendored
+114
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
require_once(dirname(__FILE__) . "/../Base/MixpanelBase.php");
|
||||
require_once(dirname(__FILE__) . "/../ConsumerStrategies/FileConsumer.php");
|
||||
require_once(dirname(__FILE__) . "/../ConsumerStrategies/CurlConsumer.php");
|
||||
require_once(dirname(__FILE__) . "/../ConsumerStrategies/SocketConsumer.php");
|
||||
if (!function_exists('json_encode')) {
|
||||
throw new Exception('The JSON PHP extension is required.');
|
||||
}
|
||||
abstract class Producers_MixpanelBaseProducer extends Base_MixpanelBase {
|
||||
protected $_token;
|
||||
private $_queue = array();
|
||||
private $_consumer = null;
|
||||
private $_consumers = array(
|
||||
"file" => "ConsumerStrategies_FileConsumer",
|
||||
"curl" => "ConsumerStrategies_CurlConsumer",
|
||||
"socket" => "ConsumerStrategies_SocketConsumer"
|
||||
);
|
||||
protected $_max_queue_size = 1000;
|
||||
public function __construct($token, $options = array()) {
|
||||
parent::__construct($options);
|
||||
// register any customer consumers
|
||||
if (isset($options["consumers"])) {
|
||||
$this->_consumers = array_merge($this->_consumers, $options['consumers']);
|
||||
}
|
||||
// set max queue size
|
||||
if (isset($options["max_queue_size"])) {
|
||||
$this->_max_queue_size = $options['max_queue_size'];
|
||||
}
|
||||
// associate token
|
||||
$this->_token = $token;
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Using token: ".$this->_token);
|
||||
}
|
||||
// instantiate the chosen consumer
|
||||
$this->_consumer = $this->_getConsumer();
|
||||
}
|
||||
public function __destruct() {
|
||||
$attempts = 0;
|
||||
$max_attempts = 10;
|
||||
$success = false;
|
||||
while (!$success && $attempts < $max_attempts) {
|
||||
if ($this->_debug()) {
|
||||
$this->_log("destruct flush attempt #".($attempts+1));
|
||||
}
|
||||
$success = $this->flush();
|
||||
$attempts++;
|
||||
}
|
||||
}
|
||||
public function flush($desired_batch_size = 50) {
|
||||
$queue_size = count($this->_queue);
|
||||
$succeeded = true;
|
||||
$num_threads = $this->_consumer->getNumThreads();
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Flush called - queue size: ".$queue_size);
|
||||
}
|
||||
while($queue_size > 0 && $succeeded) {
|
||||
$batch_size = min(array($queue_size, $desired_batch_size*$num_threads, $this->_options['max_batch_size']*$num_threads));
|
||||
$batch = array_splice($this->_queue, 0, $batch_size);
|
||||
$succeeded = $this->_persist($batch);
|
||||
if (!$succeeded) {
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Batch consumption failed!");
|
||||
}
|
||||
$this->_queue = array_merge($batch, $this->_queue);
|
||||
if ($this->_debug()) {
|
||||
$this->_log("added batch back to queue, queue size is now $queue_size");
|
||||
}
|
||||
}
|
||||
$queue_size = count($this->_queue);
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Batch of $batch_size consumed, queue size is now $queue_size");
|
||||
}
|
||||
}
|
||||
return $succeeded;
|
||||
}
|
||||
public function reset() {
|
||||
$this->_queue = array();
|
||||
}
|
||||
public function getQueue() {
|
||||
return $this->_queue;
|
||||
}
|
||||
public function getToken() {
|
||||
return $this->_token;
|
||||
}
|
||||
protected function _getConsumer() {
|
||||
$key = $this->_options['consumer'];
|
||||
$Strategy = $this->_consumers[$key];
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Using consumer: " . $key . " -> " . $Strategy);
|
||||
}
|
||||
$this->_options['endpoint'] = $this->_getEndpoint();
|
||||
return new $Strategy($this->_options);
|
||||
}
|
||||
public function enqueue($message = array()) {
|
||||
array_push($this->_queue, $message);
|
||||
// force a flush if we've reached our threshold
|
||||
if (count($this->_queue) > $this->_max_queue_size) {
|
||||
$this->flush();
|
||||
}
|
||||
if ($this->_debug()) {
|
||||
$this->_log("Queued message: ".json_encode($message));
|
||||
}
|
||||
}
|
||||
public function enqueueAll($messages = array()) {
|
||||
foreach($messages as $message) {
|
||||
$this->enqueue($message);
|
||||
}
|
||||
}
|
||||
protected function _persist($message) {
|
||||
return $this->_consumer->persist($message);
|
||||
}
|
||||
abstract function _getEndpoint();
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
require_once(dirname(__FILE__) . "/MixpanelBaseProducer.php");
|
||||
require_once(dirname(__FILE__) . "/MixpanelPeople.php");
|
||||
require_once(dirname(__FILE__) . "/../ConsumerStrategies/CurlConsumer.php");
|
||||
class Producers_MixpanelEvents extends Producers_MixpanelBaseProducer {
|
||||
private $_super_properties = array("mp_lib" => "php");
|
||||
public function track($event, $properties = array()) {
|
||||
// if no token is passed in, use current token
|
||||
if (!isset($properties["token"])) $properties['token'] = $this->_token;
|
||||
// if no time is passed in, use the current time
|
||||
if (!isset($properties["time"])) $properties['time'] = microtime(true);
|
||||
$params['event'] = $event;
|
||||
$params['properties'] = array_merge($this->_super_properties, $properties);
|
||||
$this->enqueue($params);
|
||||
}
|
||||
public function register($property, $value) {
|
||||
$this->_super_properties[$property] = $value;
|
||||
}
|
||||
public function registerAll($props_and_vals = array()) {
|
||||
foreach($props_and_vals as $property => $value) {
|
||||
$this->register($property, $value);
|
||||
}
|
||||
}
|
||||
public function registerOnce($property, $value) {
|
||||
if (!isset($this->_super_properties[$property])) {
|
||||
$this->register($property, $value);
|
||||
}
|
||||
}
|
||||
public function registerAllOnce($props_and_vals = array()) {
|
||||
foreach($props_and_vals as $property => $value) {
|
||||
if (!isset($this->_super_properties[$property])) {
|
||||
$this->register($property, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
public function unregister($property) {
|
||||
unset($this->_super_properties[$property]);
|
||||
}
|
||||
public function unregisterAll($properties) {
|
||||
foreach($properties as $property) {
|
||||
$this->unregister($property);
|
||||
}
|
||||
}
|
||||
public function getProperty($property) {
|
||||
return $this->_super_properties[$property];
|
||||
}
|
||||
public function identify($user_id, $anon_id = null) {
|
||||
$this->register("distinct_id", $user_id);
|
||||
$UUIDv4 = '/^(\$device:)?[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*-[a-zA-Z0-9]*$/i';
|
||||
if (!empty($anon_id)) {
|
||||
if (preg_match($UUIDv4, $anon_id) !== 1) {
|
||||
error_log("Running Identify method (identified_id: $user_id, anon_id: $anon_id) failed, anon_id not in UUID v4 format");
|
||||
} else {
|
||||
$this->track('$identify', array(
|
||||
'$identified_id' => $user_id,
|
||||
'$anon_id' => $anon_id
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
public function createAlias($distinct_id, $alias) {
|
||||
$msg = array(
|
||||
"event" => '$create_alias',
|
||||
"properties" => array("distinct_id" => $distinct_id, "alias" => $alias, "token" => $this->_token)
|
||||
);
|
||||
// Save the current fork/async options
|
||||
$old_fork = isset($this->_options['fork']) ? $this->_options['fork'] : false;
|
||||
$old_async = isset($this->_options['async']) ? $this->_options['async'] : false;
|
||||
// Override fork/async to make the new consumer synchronous
|
||||
$this->_options['fork'] = false;
|
||||
$this->_options['async'] = false;
|
||||
// The name is ambiguous, but this creates a new consumer with current $this->_options
|
||||
$consumer = $this->_getConsumer();
|
||||
$success = $consumer->persist(array($msg));
|
||||
// Restore the original fork/async settings
|
||||
$this->_options['fork'] = $old_fork;
|
||||
$this->_options['async'] = $old_async;
|
||||
if (!$success) {
|
||||
error_log("Creating Mixpanel Alias (distinct id: $distinct_id, alias: $alias) failed");
|
||||
throw new Exception("Tried to create an alias but the call was not successful");
|
||||
} else {
|
||||
return $msg;
|
||||
}
|
||||
}
|
||||
function _getEndpoint() {
|
||||
return $this->_options['events_endpoint'];
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
require_once(dirname(__FILE__) . "/MixpanelBaseProducer.php");
|
||||
class Producers_MixpanelGroups extends Producers_MixpanelBaseProducer {
|
||||
private function _constructPayload($group_key, $group_id, $operation, $value, $ignore_time = false) {
|
||||
$payload = array(
|
||||
'$token' => $this->_token,
|
||||
'$group_key' => $group_key,
|
||||
'$group_id' => $group_id,
|
||||
'$time' => microtime(true),
|
||||
$operation => $value
|
||||
);
|
||||
if ($ignore_time === true) $payload['$ignore_time'] = true;
|
||||
return $payload;
|
||||
}
|
||||
public function set($group_key, $group_id, $props, $ignore_time = false) {
|
||||
$payload = $this->_constructPayload($group_key, $group_id, '$set', $props, $ignore_time);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function setOnce($group_key, $group_id, $props, $ignore_time = false) {
|
||||
$payload = $this->_constructPayload($group_key, $group_id, '$set_once', $props, $ignore_time);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function remove($group_key, $group_id, $props, $ignore_time = false) {
|
||||
$payload = $this->_constructPayload($group_key, $group_id, '$remove', $props, $ignore_time);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function union($group_key, $group_id, $prop, $val, $ignore_time = false) {
|
||||
$payload = $this->_constructPayload($group_key, $group_id, '$union', array("$prop" => $val), $ignore_time);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function deleteGroup($group_key, $group_id, $ignore_time = false) {
|
||||
$payload = $this->_constructPayload($group_key, $group_id, '$delete', "", $ignore_time);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
function _getEndpoint() {
|
||||
return $this->_options['groups_endpoint'];
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
require_once(dirname(__FILE__) . "/MixpanelBaseProducer.php");
|
||||
class Producers_MixpanelPeople extends Producers_MixpanelBaseProducer {
|
||||
private function _constructPayload($distinct_id, $operation, $value, $ip = null, $ignore_time = false, $ignore_alias = false) {
|
||||
$payload = array(
|
||||
'$token' => $this->_token,
|
||||
'$distinct_id' => $distinct_id,
|
||||
'$time' => microtime(true),
|
||||
$operation => $value
|
||||
);
|
||||
if ($ip !== null) $payload['$ip'] = $ip;
|
||||
if ($ignore_time === true) $payload['$ignore_time'] = true;
|
||||
if ($ignore_alias === true) $payload['$ignore_alias'] = true;
|
||||
return $payload;
|
||||
}
|
||||
public function set($distinct_id, $props, $ip = null, $ignore_time = false, $ignore_alias = false) {
|
||||
$payload = $this->_constructPayload($distinct_id, '$set', $props, $ip, $ignore_time, $ignore_alias);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function setOnce($distinct_id, $props, $ip = null, $ignore_time = false, $ignore_alias = false) {
|
||||
$payload = $this->_constructPayload($distinct_id, '$set_once', $props, $ip, $ignore_time, $ignore_alias);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function remove($distinct_id, $props, $ip = null, $ignore_time = false, $ignore_alias = false) {
|
||||
$payload = $this->_constructPayload($distinct_id, '$unset', $props, $ip, $ignore_time, $ignore_alias);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function increment($distinct_id, $prop, $val, $ip = null, $ignore_time = false, $ignore_alias = false) {
|
||||
$payload = $this->_constructPayload($distinct_id, '$add', array("$prop" => $val), $ip, $ignore_time, $ignore_alias);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function append($distinct_id, $prop, $val, $ip = null, $ignore_time = false, $ignore_alias = false) {
|
||||
$operation = gettype($val) == "array" ? '$union' : '$append';
|
||||
$payload = $this->_constructPayload($distinct_id, $operation, array("$prop" => $val), $ip, $ignore_time, $ignore_alias);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function trackCharge($distinct_id, $amount, $timestamp = null, $ip = null, $ignore_time = false, $ignore_alias = false) {
|
||||
$timestamp = $timestamp == null ? time() : $timestamp;
|
||||
$date_iso = date("c", $timestamp);
|
||||
$transaction = array(
|
||||
'$time' => $date_iso,
|
||||
'$amount' => $amount
|
||||
);
|
||||
$val = array('$transactions' => $transaction);
|
||||
$payload = $this->_constructPayload($distinct_id, '$append', $val, $ip, $ignore_time, $ignore_alias);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function clearCharges($distinct_id, $ip = null, $ignore_time = false, $ignore_alias = false) {
|
||||
$payload = $this->_constructPayload($distinct_id, '$set', array('$transactions' => array()), $ip, $ignore_time, $ignore_alias);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
public function deleteUser($distinct_id, $ip = null, $ignore_time = false, $ignore_alias = false) {
|
||||
$payload = $this->_constructPayload($distinct_id, '$delete', "", $ip, $ignore_time, $ignore_alias);
|
||||
$this->enqueue($payload);
|
||||
}
|
||||
function _getEndpoint() {
|
||||
return $this->_options['people_endpoint'];
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
<?php
|
||||
@@ -0,0 +1 @@
|
||||
<?php
|
||||
Vendored
+70
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
class MixpanelBaseProducerTest extends PHPUnit_Framework_TestCase {
|
||||
protected $_instance = null;
|
||||
protected $_file = null;
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->_file = dirname(__FILE__)."/output-".time().".txt";
|
||||
$this->_instance = new _Producers_MixpanelBaseProducer("token", array("consumer" => "file", "debug" => true, "file" => $this->_file));
|
||||
}
|
||||
protected function tearDown() {
|
||||
parent::tearDown();
|
||||
$this->_instance->reset();
|
||||
$this->_instance = null;
|
||||
@unlink($this->_file);
|
||||
}
|
||||
public function testTokenMatch() {
|
||||
$this->assertEquals("token", $this->_instance->getToken());
|
||||
}
|
||||
public function testFlush() {
|
||||
$event1 = array("event" => "test", "properties" => array("prop1" => "val1"));
|
||||
$event2 = array("event" => "test2", "properties" => array("prop2" => "val2"));
|
||||
$this->_instance->enqueue($event1);
|
||||
$this->_instance->enqueue($event2);
|
||||
$this->_instance->flush(1);
|
||||
$contents = file_get_contents($this->_file);
|
||||
$this->assertEquals('[{"event":"test","properties":{"prop1":"val1"}}]'."\n".
|
||||
'[{"event":"test2","properties":{"prop2":"val2"}}]'."\n", $contents);
|
||||
}
|
||||
public function testReset() {
|
||||
$event1 = array("event" => "test", "properties" => array("prop1" => "val1"));
|
||||
$this->_instance->enqueue($event1);
|
||||
$this->_instance->reset();
|
||||
$this->assertEmpty($this->_instance->getQueue());
|
||||
}
|
||||
public function testEnqueue() {
|
||||
$this->_instance->reset();
|
||||
$event1 = array("event" => "test", "properties" => array("prop1" => "val1"));
|
||||
$this->_instance->enqueue($event1);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$this->assertCount(1, $queue);
|
||||
$this->assertEquals($event1, $queue[0]);
|
||||
}
|
||||
public function testEnqueueAll() {
|
||||
$this->_instance->reset();
|
||||
$event1 = array("event" => "test", "properties" => array("prop1" => "val1"));
|
||||
$event2 = array("event" => "test2", "properties" => array("prop1" => "val1"));
|
||||
$events = array($event1, $event2);
|
||||
$this->_instance->enqueueAll($events);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$this->assertCount(2, $queue);
|
||||
$this->assertEquals($event1, $queue[0]);
|
||||
$this->assertEquals($event2, $queue[1]);
|
||||
}
|
||||
public function testSetMaxQueueSize() {
|
||||
$this->_instance->enqueue(array("event" => "test"));
|
||||
$queue = $this->_instance->getQueue();
|
||||
$this->assertEquals(1, count($queue));
|
||||
$this->_instance->flush();
|
||||
$new_instance = new Producers_MixpanelEvents("token", array('max_queue_size' => 0));
|
||||
$new_instance->track("test");
|
||||
$queue = $new_instance->getQueue();
|
||||
$this->assertEquals(0, count($queue));
|
||||
}
|
||||
}
|
||||
// stub for tests
|
||||
class _Producers_MixpanelBaseProducer extends Producers_MixpanelBaseProducer {
|
||||
function _getEndpoint() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
class ConsumerStrategies_AbstractConsumerTest extends PHPUnit_Framework_TestCase {
|
||||
protected $_instance = null;
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_instance = new AbstractConsumer();
|
||||
}
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
$this->_instance = null;
|
||||
}
|
||||
public function test_encode() {
|
||||
$encoded = base64_encode(json_encode(array("1" => "one")));
|
||||
$this->assertEquals($encoded, $this->_instance->encode(array("1" => "one")));
|
||||
}
|
||||
}
|
||||
class AbstractConsumer extends ConsumerStrategies_AbstractConsumer {
|
||||
function persist($batch)
|
||||
{
|
||||
// TODO: Implement persist() method.
|
||||
}
|
||||
function encode($msg) {
|
||||
return $this->_encode($msg);
|
||||
}
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
class ConsumerStrategies_CurlConsumerTest extends PHPUnit_Framework_TestCase {
|
||||
public function testSettings() {
|
||||
$consumer = new CurlConsumer(array(
|
||||
"host" => "localhost",
|
||||
"endpoint" => "/endpoint",
|
||||
"timeout" => 2,
|
||||
"use_ssl" => false,
|
||||
"fork" => false
|
||||
));
|
||||
$this->assertEquals("localhost", $consumer->getHost());
|
||||
$this->assertEquals("/endpoint", $consumer->getEndpoint());
|
||||
$this->assertEquals(2, $consumer->getTimeout());
|
||||
$this->assertEquals("http", $consumer->getProtocol());
|
||||
$this->assertEquals(false, $consumer->getFork());
|
||||
}
|
||||
public function testBlocking() {
|
||||
$consumer = new CurlConsumer(array(
|
||||
"host" => "localhost",
|
||||
"endpoint" => "/endpoint",
|
||||
"timeout" => 2,
|
||||
"use_ssl" => true,
|
||||
"fork" => false
|
||||
));
|
||||
$consumer->persist(array("msg"));
|
||||
$this->assertEquals(1, $consumer->blockingCalls);
|
||||
}
|
||||
public function testForked() {
|
||||
$consumer = new CurlConsumer(array(
|
||||
"host" => "localhost",
|
||||
"endpoint" => "/endpoint",
|
||||
"timeout" => 2,
|
||||
"use_ssl" => true,
|
||||
"fork" => true
|
||||
));
|
||||
$consumer->persist(array("msg"));
|
||||
$this->assertEquals(1, $consumer->forkedCalls);
|
||||
}
|
||||
public function testExecuteCurlFailure() {
|
||||
$error_handler = new ErrorHandler();
|
||||
$consumer = new ConsumerStrategies_CurlConsumer(array(
|
||||
"host" => "some.domain.that.should.not.ever.exist.tld",
|
||||
"endpoint" => "/endpoint",
|
||||
"timeout" => 2,
|
||||
"use_ssl" => false,
|
||||
"fork" => false,
|
||||
"error_callback" => array($error_handler, 'handle_error')
|
||||
));
|
||||
$resp = $consumer->persist(array("msg"));
|
||||
$this->assertFalse($resp);
|
||||
$this->assertEquals($error_handler->last_code, CURLE_COULDNT_RESOLVE_HOST);
|
||||
}
|
||||
public function testOptions() {
|
||||
function callback() { }
|
||||
$consumer = new ConsumerStrategies_CurlConsumer(array(
|
||||
"host" => "localhost",
|
||||
"endpoint" => "/endpoint",
|
||||
"timeout" => 2,
|
||||
"connect_timeout" => 1,
|
||||
"use_ssl" => true,
|
||||
"fork" => false,
|
||||
"num_threads" => 5,
|
||||
"error_callback" => 'callback'
|
||||
));
|
||||
$this->assertEquals($consumer->getHost(), "localhost");
|
||||
$this->assertEquals($consumer->getEndpoint(), "/endpoint");
|
||||
$this->assertEquals($consumer->getTimeout(), 2);
|
||||
$this->assertEquals($consumer->getConnectTimeout(), 1);
|
||||
$this->assertEquals($consumer->getProtocol(), "https");
|
||||
$this->assertEquals($consumer->getNumThreads(), 5);
|
||||
}
|
||||
}
|
||||
class ErrorHandler {
|
||||
public $last_code = -1;
|
||||
public $last_msg = "";
|
||||
public function handle_error($code, $msg) {
|
||||
$this->last_code = $code;
|
||||
$this->last_msg = $msg;
|
||||
}
|
||||
}
|
||||
class CurlConsumer extends ConsumerStrategies_CurlConsumer {
|
||||
public $forkedCalls = 0;
|
||||
public $blockingCalls = 0;
|
||||
public function getEndpoint()
|
||||
{
|
||||
return $this->_endpoint;
|
||||
}
|
||||
public function getFork()
|
||||
{
|
||||
return $this->_fork;
|
||||
}
|
||||
public function getHost()
|
||||
{
|
||||
return $this->_host;
|
||||
}
|
||||
public function getProtocol()
|
||||
{
|
||||
return $this->_protocol;
|
||||
}
|
||||
public function getTimeout()
|
||||
{
|
||||
return $this->_timeout;
|
||||
}
|
||||
protected function _execute($url, $data)
|
||||
{
|
||||
$this->blockingCalls++;
|
||||
return parent::_execute($url, $data);
|
||||
}
|
||||
protected function _execute_forked($url, $data)
|
||||
{
|
||||
$this->forkedCalls++;
|
||||
return parent::_execute_forked($url, $data);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
class ConsumerStrategies_FileConsumerTest extends PHPUnit_Framework_TestCase {
|
||||
protected $_instance = null;
|
||||
protected $_file = null;
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_file = dirname(__FILE__)."/output-".time().".txt";
|
||||
$this->_instance = new ConsumerStrategies_FileConsumer(array("file" => $this->_file));
|
||||
}
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
$this->_instance = null;
|
||||
@unlink($this->_file);
|
||||
}
|
||||
public function testPersist() {
|
||||
$this->_instance->persist(array("msg"));
|
||||
$contents = file_get_contents($this->_file);
|
||||
$this->assertEquals('["msg"]'."\n", $contents);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
class ConsumerStrategies_SocketConsumerTest extends PHPUnit_Framework_TestCase {
|
||||
protected $_instance = null;
|
||||
protected $_file = null;
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_instance = new ConsumerStrategies_SocketConsumer(array(
|
||||
"host" => "localhost",
|
||||
"endpoint" => "/endpoint",
|
||||
"timeout" => 2,
|
||||
"use_ssl" => false
|
||||
));
|
||||
}
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
$this->_instance = null;
|
||||
}
|
||||
public function testPersist() {
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
<?php
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
class MixpanelTest extends PHPUnit_Framework_TestCase {
|
||||
protected $_instance = null;
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->_instance = Mixpanel::getInstance("token");
|
||||
}
|
||||
protected function tearDown() {
|
||||
parent::tearDown();
|
||||
$this->_instance->reset();
|
||||
$this->_instance = null;
|
||||
}
|
||||
public function testGetInstance() {
|
||||
$instance = Mixpanel::getInstance("token");
|
||||
$this->assertInstanceOf("Mixpanel", $instance);
|
||||
$this->assertEquals($this->_instance, $instance);
|
||||
$this->assertInstanceOf("Producers_MixpanelPeople", $this->_instance->people);
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
class MixpanelEventsProducerTest extends PHPUnit_Framework_TestCase {
|
||||
protected $_instance = null;
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_instance = new Producers_MixpanelEvents("token");
|
||||
}
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
$this->_instance->reset();
|
||||
$this->_instance = null;
|
||||
}
|
||||
public function testTrack() {
|
||||
$this->_instance->track("test_event", array("number" => 1));
|
||||
$queue = $this->_instance->getQueue();
|
||||
$this->assertEquals(1, count($queue));
|
||||
$this->assertEquals("test_event", $queue[0]['event']);
|
||||
$this->assertEquals(1, $queue[0]['properties']['number']);
|
||||
}
|
||||
public function testRegister() {
|
||||
$this->_instance->register("super_property", "super_value");
|
||||
$this->assertEquals("super_value", $this->_instance->getProperty("super_property"));
|
||||
}
|
||||
public function testRegisterAll() {
|
||||
$this->_instance->registerAll(array("prop1" => "val1", "prop2" => "val2"));
|
||||
$this->assertEquals("val1", $this->_instance->getProperty("prop1"));
|
||||
$this->assertEquals("val2", $this->_instance->getProperty("prop2"));
|
||||
}
|
||||
public function testRegisterOnce() {
|
||||
$this->_instance->registerOnce("prop3", "val3");
|
||||
$this->_instance->registerOnce("prop3", "val4");
|
||||
$this->assertEquals("val3", $this->_instance->getProperty("prop3"));
|
||||
}
|
||||
public function testRegisterAllOnce() {
|
||||
$this->_instance->registerAllOnce(array("prop5" => "val5", "prop6" => "val6"));
|
||||
$this->_instance->registerAllOnce(array("prop5" => "val6", "prop6" => "val7"));
|
||||
$this->assertEquals("val5", $this->_instance->getProperty("prop5"));
|
||||
$this->assertEquals("val6", $this->_instance->getProperty("prop6"));
|
||||
}
|
||||
public function unregister() {
|
||||
$this->_instance->register("prop7", "val7");
|
||||
$this->_instance->register("prop8", "val8");
|
||||
$this->assertEquals("val7", $this->_instance->getProperty("prop7"));
|
||||
$this->assertEquals("val8", $this->_instance->getProperty("prop8"));
|
||||
$this->_instance->unregister("prop7");
|
||||
$this->assertEquals(null, $this->_instance->getProperty("prop7"));
|
||||
$this->assertEquals("val8", $this->_instance->getProperty("prop8"));
|
||||
}
|
||||
public function unregisterAll() {
|
||||
$this->_instance->registerAll(array("prop9" => "val9", "prop10" => "val10"));
|
||||
$this->assertEquals("val9", $this->_instance->getProperty("prop9"));
|
||||
$this->assertEquals("val10", $this->_instance->getProperty("prop10"));
|
||||
$this->assertEquals("val11", $this->_instance->getProperty("prop11"));
|
||||
$this->_instance->unregisterAll(array("prop9", "prop10"));
|
||||
$this->assertEquals(null, $this->_instance->getProperty("prop9"));
|
||||
$this->assertEquals(null, $this->_instance->getProperty("prop10"));
|
||||
$this->assertEquals("val11", $this->_instance->getProperty("prop11"));
|
||||
}
|
||||
public function testCreateAlias() {
|
||||
$distinct_id = 1;
|
||||
$alias = 2;
|
||||
$msg = $this->_instance->createAlias($distinct_id, $alias);
|
||||
$this->assertEquals('$create_alias', $msg['event']);
|
||||
$this->assertEquals($distinct_id, $msg['properties']['distinct_id']);
|
||||
$this->assertEquals($alias, $msg['properties']['alias']);
|
||||
}
|
||||
public function testCreateAliasRespectsConsumerSetting() {
|
||||
$tmp_file = __DIR__ . '/test.tmp';
|
||||
$this->assertFileNotExists($tmp_file);
|
||||
$options = array('consumer' => 'file', 'file' => $tmp_file);
|
||||
$instance = new Producers_MixpanelEvents('token', $options);
|
||||
try {
|
||||
$instance->createAlias(1, 2);
|
||||
$this->assertStringEqualsFile($tmp_file, '[{"event":"$create_alias","properties":{"distinct_id":1,"alias":2,"token":"token"}}]' . PHP_EOL);
|
||||
} catch (Exception $e) {
|
||||
unlink($tmp_file);
|
||||
throw $e;
|
||||
}
|
||||
unlink($tmp_file);
|
||||
}
|
||||
public function testIdentifyInvalidAnonId() {
|
||||
$user_id = 1;
|
||||
$anon_id = 111;
|
||||
$this->_instance->identify($user_id, $anon_id);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$this->assertEquals(0, count($queue));
|
||||
}
|
||||
public function testIdentifyValidAnonId() {
|
||||
$user_id = 1;
|
||||
$anon_id = '2c93fdf3-4fbf-4fec-baaf-136ce87c13cc';
|
||||
$test = $this->_instance->identify($user_id, $anon_id);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$this->assertEquals(1, count($queue));
|
||||
$this->assertEquals('$identify', $queue[0]['event']);
|
||||
$this->assertEquals($user_id, $queue[0]['properties']['$identified_id']);
|
||||
$this->assertEquals($anon_id, $queue[0]['properties']['$anon_id']);
|
||||
}
|
||||
public function testIdentifyValidAnonIdLong() {
|
||||
$user_id = 1;
|
||||
$anon_id = '13bbf7943e584-0885c2531-5c793977-3e8000-13bbf7943e64cf';
|
||||
$test = $this->_instance->identify($user_id, $anon_id);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$this->assertEquals(1, count($queue));
|
||||
$this->assertEquals('$identify', $queue[0]['event']);
|
||||
$this->assertEquals($user_id, $queue[0]['properties']['$identified_id']);
|
||||
$this->assertEquals($anon_id, $queue[0]['properties']['$anon_id']);
|
||||
}
|
||||
public function testIdentifyValidAnonIdDevice() {
|
||||
$user_id = 1;
|
||||
$anon_id = '$device:13bbf7943e584-0885c2531-5c793977-3e8000-13bbf7943e64cf';
|
||||
$test = $this->_instance->identify($user_id, $anon_id);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$this->assertEquals(1, count($queue));
|
||||
$this->assertEquals('$identify', $queue[0]['event']);
|
||||
$this->assertEquals($user_id, $queue[0]['properties']['$identified_id']);
|
||||
$this->assertEquals($anon_id, $queue[0]['properties']['$anon_id']);
|
||||
}
|
||||
public function testIdentifyNoAnonId() {
|
||||
$user_id = 1;
|
||||
$test = $this->_instance->identify($user_id);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$this->assertEquals(0, count($queue));
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
class MixpanelGroupsProducerTest extends PHPUnit_Framework_TestCase {
|
||||
protected $_instance = null;
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_instance = new Producers_MixpanelGroups("token");
|
||||
}
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
$this->_instance->reset();
|
||||
$this->_instance = null;
|
||||
}
|
||||
public function testSet() {
|
||||
$this->_instance->set("company","Mixpanel", array("industry" => "tech"));
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals("company", $msg['$group_key']);
|
||||
$this->assertEquals("Mixpanel", $msg['$group_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertArrayNotHasKey('$ignore_time', $msg);
|
||||
$this->assertArrayHasKey('$set', $msg);
|
||||
$this->assertArrayHasKey("industry", $msg['$set']);
|
||||
$this->assertEquals("tech", $msg['$set']['industry']);
|
||||
}
|
||||
public function testSetIgnoreTime() {
|
||||
$this->_instance->set("company","Mixpanel", array("industry" => "Tech"), true);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals("company", $msg['$group_key']);
|
||||
$this->assertEquals("Mixpanel", $msg['$group_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals(true, $msg['$ignore_time']);
|
||||
$this->assertArrayHasKey('$set', $msg);
|
||||
$this->assertArrayHasKey("industry", $msg['$set']);
|
||||
$this->assertEquals("Tech", $msg['$set']['industry']);
|
||||
}
|
||||
public function testSetOnce() {
|
||||
$this->_instance->setOnce("company","Mixpanel", array("industry" => "Tech"), true);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals("company", $msg['$group_key']);
|
||||
$this->assertEquals("Mixpanel", $msg['$group_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertArrayHasKey('$set_once', $msg);
|
||||
$this->assertArrayHasKey("industry", $msg['$set_once']);
|
||||
$this->assertEquals("Tech", $msg['$set_once']['industry']);
|
||||
}
|
||||
public function testUnionSingle() {
|
||||
$this->_instance->union("company","Mixpanel", "actions", "Logged In");
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals("company", $msg['$group_key']);
|
||||
$this->assertEquals("Mixpanel", $msg['$group_id']);
|
||||
$this->assertArrayHasKey('$union', $msg);
|
||||
$this->assertArrayHasKey("actions", $msg['$union']);
|
||||
$this->assertEquals("Logged In", $msg['$union']['actions']);
|
||||
}
|
||||
public function testUnionMultiple() {
|
||||
$this->_instance->union("company","Mixpanel", "actions", array("Logged In", "Logged Out"));
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals("company", $msg['$group_key']);
|
||||
$this->assertEquals("Mixpanel", $msg['$group_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertArrayHasKey('$union', $msg);
|
||||
$this->assertArrayHasKey("actions", $msg['$union']);
|
||||
$this->assertEquals(array("Logged In", "Logged Out"), $msg['$union']['actions']);
|
||||
}
|
||||
public function testRemove() {
|
||||
$this->_instance->remove("company","Mixpanel", array("industry" => "tech"));
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals("company", $msg['$group_key']);
|
||||
$this->assertEquals("Mixpanel", $msg['$group_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertArrayNotHasKey('$ignore_time', $msg);
|
||||
$this->assertArrayHasKey('$unset', $msg);
|
||||
$this->assertArrayHasKey("industry", $msg['$unset']);
|
||||
$this->assertEquals("tech", $msg['$unset']['industry']);
|
||||
}
|
||||
public function testDeleteGroup() {
|
||||
$this->_instance->deleteGroup("company","Mixpanel");
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals("company", $msg['$group_key']);
|
||||
$this->assertEquals("Mixpanel", $msg['$group_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertArrayHasKey('$delete', $msg);
|
||||
$this->assertEquals("", $msg['$delete']);
|
||||
}
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) exit;
|
||||
class MixpanelPeopleProducerTest extends PHPUnit_Framework_TestCase {
|
||||
protected $_instance = null;
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_instance = new Producers_MixpanelPeople("token");
|
||||
}
|
||||
protected function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
$this->_instance->reset();
|
||||
$this->_instance = null;
|
||||
}
|
||||
public function testSet() {
|
||||
$this->_instance->set(12345, array("name" => "John"), "192.168.0.1");
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals(12345, $msg['$distinct_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals("192.168.0.1", $msg['$ip']);
|
||||
$this->assertArrayNotHasKey('$ignore_time', $msg);
|
||||
$this->assertArrayNotHasKey('$ignore_alias', $msg);
|
||||
$this->assertArrayHasKey('$set', $msg);
|
||||
$this->assertArrayHasKey("name", $msg['$set']);
|
||||
$this->assertEquals("John", $msg['$set']['name']);
|
||||
}
|
||||
public function testSetIgnoreTime() {
|
||||
$this->_instance->set(12345, array("name" => "John"), "192.168.0.1", true);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals(12345, $msg['$distinct_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals("192.168.0.1", $msg['$ip']);
|
||||
$this->assertEquals(true, $msg['$ignore_time']);
|
||||
$this->assertArrayHasKey('$set', $msg);
|
||||
$this->assertArrayHasKey("name", $msg['$set']);
|
||||
$this->assertEquals("John", $msg['$set']['name']);
|
||||
}
|
||||
public function testSetIgnoreAlias() {
|
||||
$this->_instance->set(12345, array("name" => "John"), "192.168.0.1", false, true);
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals(12345, $msg['$distinct_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals("192.168.0.1", $msg['$ip']);
|
||||
$this->assertArrayNotHasKey('$ignore_time', $msg);
|
||||
$this->assertEquals(true, $msg['$ignore_alias']);
|
||||
$this->assertArrayHasKey('$set', $msg);
|
||||
$this->assertArrayHasKey("name", $msg['$set']);
|
||||
$this->assertEquals("John", $msg['$set']['name']);
|
||||
}
|
||||
public function testSetOnce() {
|
||||
$this->_instance->setOnce(12345, array("name" => "John"), "192.168.0.1");
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals(12345, $msg['$distinct_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals("192.168.0.1", $msg['$ip']);
|
||||
$this->assertArrayHasKey('$set_once', $msg);
|
||||
$this->assertArrayHasKey("name", $msg['$set_once']);
|
||||
$this->assertEquals("John", $msg['$set_once']['name']);
|
||||
}
|
||||
public function testIncrement() {
|
||||
$this->_instance->increment(12345, "logins", 1, "192.168.0.1");
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals(12345, $msg['$distinct_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals("192.168.0.1", $msg['$ip']);
|
||||
$this->assertArrayHasKey('$add', $msg);
|
||||
$this->assertArrayHasKey("logins", $msg['$add']);
|
||||
$this->assertEquals(1, $msg['$add']['logins']);
|
||||
}
|
||||
public function testAppendSingle() {
|
||||
$this->_instance->append(12345, "actions", "Logged In", "192.168.0.1");
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals(12345, $msg['$distinct_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals("192.168.0.1", $msg['$ip']);
|
||||
$this->assertArrayHasKey('$append', $msg);
|
||||
$this->assertArrayHasKey("actions", $msg['$append']);
|
||||
$this->assertEquals("Logged In", $msg['$append']['actions']);
|
||||
}
|
||||
public function testAppendMultiple() {
|
||||
$this->_instance->append(12345, "actions", array("Logged In", "Logged Out"), "192.168.0.1");
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals(12345, $msg['$distinct_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals("192.168.0.1", $msg['$ip']);
|
||||
$this->assertArrayHasKey('$union', $msg);
|
||||
$this->assertArrayHasKey("actions", $msg['$union']);
|
||||
$this->assertEquals(array("Logged In", "Logged Out"), $msg['$union']['actions']);
|
||||
}
|
||||
public function testTrackCharge() {
|
||||
date_default_timezone_set("America/Los_Angeles");
|
||||
$time = time();
|
||||
$this->_instance->trackCharge(12345, "20.00", $time, "192.168.0.1");
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals(12345, $msg['$distinct_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals("192.168.0.1", $msg['$ip']);
|
||||
$this->assertArrayHasKey('$append', $msg);
|
||||
$this->assertArrayHasKey('$transactions', $msg['$append']);
|
||||
$this->assertArrayHasKey('$amount', $msg['$append']['$transactions']);
|
||||
$this->assertArrayHasKey('$time', $msg['$append']['$transactions']);
|
||||
$this->assertEquals("20.00", $msg['$append']['$transactions']['$amount']);
|
||||
$this->assertEquals(date("c", $time), $msg['$append']['$transactions']['$time']);
|
||||
}
|
||||
public function testClearCharges() {
|
||||
$this->_instance->clearCharges(12345,"192.168.0.1");
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals(12345, $msg['$distinct_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals("192.168.0.1", $msg['$ip']);
|
||||
$this->assertArrayHasKey('$set', $msg);
|
||||
$this->assertArrayHasKey('$transactions', $msg['$set']);
|
||||
$this->assertSameSize(array(), $msg['$set']['$transactions']);
|
||||
}
|
||||
public function testDeleteUser() {
|
||||
$this->_instance->deleteUser(12345,"192.168.0.1");
|
||||
$queue = $this->_instance->getQueue();
|
||||
$msg = $queue[count($queue)-1];
|
||||
$this->assertEquals(12345, $msg['$distinct_id']);
|
||||
$this->assertEquals("token", $msg['$token']);
|
||||
$this->assertEquals("192.168.0.1", $msg['$ip']);
|
||||
$this->assertArrayHasKey('$delete', $msg);
|
||||
$this->assertEquals("", $msg['$delete']);
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
<?php
|
||||
@@ -0,0 +1 @@
|
||||
<?php
|
||||
Reference in New Issue
Block a user