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

标签 javascript php rest mixpanel

我一直在尝试找到正确的 API 来获取已完成“事件”的“用户列表”。

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

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

我发现“live”api 的工作接近我的要求,但它不支持日期范围。“live”api 请求示例,

$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
                ));

有人可以帮我吗?谢谢!

最佳答案

检查这里的脚本是否对您有帮助

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

关于javascript - Mixpanel - 根据跟踪的事件获取用户(distinct_id 或其他属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24578364/

相关文章:

javascript - 在文件上传问题上将图像添加到 HTML5 Canvas

javascript - Angular : factory and controller

php - Laravel 查询生成器 - 将列设置为 NULL

php - heroku 上的 Imagick - 这可能吗?

php - Laravel 在自定义 Blade 指令中使用数据库中的变量

c# - 第三方使用的 Azure 函数 REST API - 是否需要使用 ConfigureAwait(false)?

java - 服务器端使用承载 token 在 Java 中以编程方式验证 Keycloak 用户

javascript - 获取 Backbone 模型的属性

c# - ServiceStack REST 服务中的自定义异常处理

javascript - 提交后如何在handleSubmit方法中重新加载页面/路由器?