I'm not able to connect to mixpanel.
I have tried with a correct api_key and api_secret, like this:
<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>
As you can see, I try to use this API with JSONP, but I'm lost in the woods. Is there anybody who has knowledge about mixpanel and JSONP?
Thank you in advance.
EDIT: I've added the new version of the page.
from the documentation:
https://mixpanel.com/docs/api-documentation/data-export-api#libs-js
Assuming you calculate the signature correctly, the below example will work:
I've tried many things but I was not successful in sending a request to mixpanel with jsonp, because it generated new parameters. Nobody showed me a working solution, so I guess this problem is unsolvable. If somebody shows me a solution I will gladly remove this answer and accept his/her answer.
After squinting at this PHP, I'm pretty sure you need to do an md5 hash of your api signature.
Import an md5 library, like this one
And then do something like this:
I'm trying to accomplish the same thing you are, but unfortunately I still haven't got it working.
Edit - This is rough. The API will fail if ANY parameter is not hashed with the sig. It will also fail if any parameter is included which is not part of the API. JsonP adds a "callback" and a "_" timestamp parameter so it can do what it does, and this breaks the call. You can hack around this by excluding the timestamp with
cache: true
and using a special jsonp plugin which allows you to rename thecallback
parameter. Here I have renamed it toto_date
and named the callback function "2012-10-29". It's crazy, and it still doesn't work because the reply is not valid json. It's a series of newline-delimited json objects which can not be evaluated, and I'm still stuck on that part. Here's what I've got so far:Ultimately I threw up my hands and wrote a C# program to hit the API and spit out a CSV for me.
As Tyler Mentioned, you need to MD5 hash the signature before appending it to the request URL.
In addition, if what you pasted is your code, you have a bug:
path = path + + "&sig=" + sig;
should be:
path = path + "&sig=" + sig
The /export endpoint appears as though it does not support JSONP callbacks. If you calculate your signature and call the URL without a callback and without a cache buster (provided by default with an $.ajax call), you'll get results.
Here a few things I realized. I was able to get it working with all other end points I tried but "export". I'm tempted to believe it's something peculiar to just the raw data export endpoint. Reading through their docs, the raw data end point is the only end point that isn't part of the main API and therefore requires a different base URI "https://data.mixpanel.com/api/2.0". All other endpoints "events", "segmentation" etc. use "https://mixpanel.com/api/2.0". I've put down the steps I went through below. I'm using jquery.MD5 lib for the md5 implementation
I've also put up a working solution for all other endpoints. Here is the link http://jsfiddle.net/Dantheta/CmKQN/
Hope you find it useful.