我无法连接到 mixpanel。
我尝试过使用正确的 api_key 和 api_secret,如下所示:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js" />
</script>
<script type="text/javascript" src="faulty-labs-md5.js" />
</script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
$("#btnTest").click(function() {
var api_key = 'BigSecret';
var api_secret = 'BigSecret2';
var expire = new Date('2012', '12', '24').getTime() / 1000 + 3600;
var from_date = $("#date1").val();
var to_date = $("#date2").val();
var sig = faultylabs.MD5("api_key=" + api_key + "expire=" + expire + "from_date=" + from_date + "to_date=" + to_date + api_secret);
//var path = 'https://data.mixpanel.com/api/2.0/export?api_key=' + api_key + "&expire=" + expire + "&from_date=" + from_date + "&to_date=" + to_date;
var path = 'https://data.mixpanel.com/api/2.0/export?api_key=' + api_key + "&expire=" + expire + "&from_date=" + from_date;
path = path + "&sig=" + sig.toLowerCase();
$.jsonp({
type: 'GET',
url: path,
async: false,
callback: to_date, // sneaky bogus shenanigans
callbackParameter: 'to_date', // more of same
contentType: "application/json",
dataType: 'jsonp',
cache: true,
success: function(json) {
alert(json);
},
error: function(e) {
console.log(e.message);
}
});
});
});
</script>
<input type="text" id="date1" value="2012-10-29" />
<input type="text" id="date2" value="2012-10-29" />
<button onclick="return false" id="btnTest">Test</button>
</body>
</html>
正如您所看到的,我尝试将此 API 与 JSONP 一起使用,但我迷失了方向。有人了解 mixpanel 和 JSONP 吗?
先感谢您。
编辑:我添加了该页面的新版本。
这是我意识到的一些事情。我能够让它与我尝试过的所有其他端点一起工作,但“导出”除外。我很想相信这是原始数据导出端点特有的东西。通过阅读他们的docs https://mixpanel.com/docs/api-documentation/exporting-raw-data-you-inserted-into-mixpanel,原始数据端点是唯一不属于主 API 的端点,因此需要不同的基本 URI”https://data.mixpanel.com/api/2.0 https://data.mixpanel.com/api/2.0”。所有其他端点“事件”、“分段”等均使用“https://mixpanel.com/api/2.0 https://mixpanel.com/api/2.0“。我已经在下面写下了我所经历的步骤。我正在使用jquery.MD5 https://github.com/gabrieleromanato/jQuery-MD5/blob/master/jquery.md5.js用于 md5 实现的 lib
$(function() {
$("#test_request").click(function() {
var api_secret, arg_keys, args, args_concat, end_point, key,
mixpanel_base_uri, sig, sorted_keys, _i, _len;
args = {};
end_point = "export";
api_secret = "BIG_SECRET2";
args.api_key = "BIG_SECRET";
args.from_date = $("input[name=from_date]").val();
args.to_date = $("input[name=to_date]").val();
//using Math.floor to round to integer as api expects integer
args.expire = Math.floor(new Date().getTime() / 1000 + 3600);
arg_keys = Object.keys(args);
sorted_keys = arg_keys.sort();
args_concat = "";
//concatenating key value pairs
for (_i = 0, _len = sorted_keys.length; _i < _len; _i++) {
key = sorted_keys[_i];
args_concat = "" + args_concat + key + "=" + args[key];
}
sig = $.md5(args_concat + api_secret);
//merge signature property with args hash
$.extend(args, {
sig: sig
});
//export endpoint isn't part of the main api and data.mixpanel instead of just mixpanel.com
DATA_URI = "https://data.mixpanel.com/api/2.0"
DEFAULT_URI = "https://mixpanel.com/api/2.0"
BASE_URI = end_point === "export" ? DATA_URI : DEFAULT_URI;
$.getJSON("" + BASE_URI + "/" + end_point + "?callback=?", args, function(result) {
alert("result is" + JSON.stringify(result));
});
});
});
我还为所有其他端点提供了一个可行的解决方案。链接在这里http://jsfiddle.net/Dantheta/CmKQN/ http://jsfiddle.net/Dantheta/CmKQN/
希望你觉得它有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)