Mixpanel - 根据跟踪的事件获取用户(distinct_id或其他属性)

时间:2014-07-04 16:45:36

标签: javascript php rest mixpanel

我一直在努力找到正确的api来获取已经完成'事件'的“用户列表”。

例如,在mixpanel中跟踪了一个名为“创建的个人资料”的事件,我想获得在日期范围之间完成此事件的用户列表,

我正在使用他们提供的这个API https://mixpanel.com/docs/api-documentation/data-export-api#libs-php

我发现'live'api正在接近我的要求,但它不支持日期范围..样本'live'api req,

$api_key = 'XXX';
$api_secret = 'XXX';
$this->load->library('mixpanel_export');
$this->mixpanel_export->init($api_key, $api_secret);

$data = $this->mixpanel_export->request(array('live'), array(
                    'event' => 'Received Rating',
                    'on' => '5 in properties["level"]',
                    'start_time' => 0
                ));
有人可以帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

检查此处的脚本是否可以帮助您

https://gist.github.com/dawalama/34155428ff8f53e4d508

<?php

// Based on https://mixpanel.com/docs/api-documentation/exporting-raw-data-you-inserted-into-mixpanel
class MixpanelExport {

    private $api_url = 'https://data.mixpanel.com/api/2.0/export';
    private $api_key;
    private $api_secret;
    public $debug;

    public function __construct($api_key, $api_secret) {
        $this->api_key = $api_key;
        $this->api_secret = $api_secret;
        $this->debug = false;
    }

    public function export($params) {

        if (empty($params)) {
            return false;
        }

        if (!isset($params['api_key'])) {
            $params['api_key'] = $this->api_key;
        }

        if (!isset($params['expire'])) {
            $current_utc_time = time() - date('Z');
            $params['expire'] = $current_utc_time + 100000; // Default 10 minutes
        }

        $sig = $this->signature($params);
        $request_url = $this->api_url . '?sig=' . $sig . '&'. http_build_query($params);

        if ($this->debug) {
            echo "\nURI: $request_url\n";
        }

        $curl_handle = curl_init();
        curl_setopt($curl_handle, CURLOPT_URL, $request_url);
        curl_setopt($curl_handle, CURLOPT_HEADER, 0);
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl_handle);
        curl_close($curl_handle);
        return $data;
    }

    private function signature($params) {
        /**
         * Algorithm to generate signature
         *
         * args = all query parameters going to be sent out with the request 
         * (e.g. api_key, unit, interval, expire, format, etc.) excluding sig.
         *
         * args_sorted = sort_args_alphabetically_by_key(args)
         *
         * args_concat = join(args_sorted) 
         *
         * # Output: api_key=ed0b8ff6cc3fbb37a521b40019915f18&event=["pages"]
         * #         expire=1248499222&format=json&interval=24&unit=hour
         *
         * sig = md5(args_concat + api_secret)
         **/
        ksort($params);
        $param_string = '';
        foreach ($params as $key=>$value) {
            $param_string .= $key . '=' . $value;
        }
        return md5($param_string . $this->api_secret);
    }
}

/** Example usage
 *
 * $api_key = "<your api key>";
 * $api_secret = "<your secret key>";
 * $params = [
 * 'from_date'=>'2014-01-01',
 * 'to_date'=>'2014-02-01',
 * 'event'=>["event-x"]
 * ];
 *
 * $exporter = new MixpanelExport($api_key, $api_secret);
 * echo $exporter->export($params);
 ** 

您必须使用此处描述的端点https://mixpanel.com/docs/api-documentation/exporting-raw-data-you-inserted-into-mixpanel