michaelcarter / mixpanel-data-export-js

Mixpanel data export js
91 stars 38 forks source link

/export missing? #3

Closed philjen closed 10 years ago

philjen commented 10 years ago

Was the /export endpoint left out intentionally? I'm poking around and can't seem to find it. Thanks!

michaelcarter commented 10 years ago

Sorry for the very late reply. It's a new endpoint, so it's been snuck on since the last update to this library! Unfortunately it also seems to work a little differently from the others so it's not completely trivial to add it to my library. From the mixpanel site:

Note: The specific end point for /export differs from that of our main API. This means all the same API authentication is the same. You can also utilize the same client libraries but you will, for the time being, have to change the end point to data.mixpanel.com instead of just mixpanel.com. Most libraries will automatically assume it gets a JSON string back and attempt to decode it. This specific API does not provide valid JSON in aggregate but does per row so be aware if you receive a decoding error."

Specifically the whole "not returning JSON" thing is a bit of a pain for me, I'll endeavour to add support for this endpoint this week though, as I have some free time.

hansbrough commented 10 years ago

yes - the invalid json is a pain. There is no enclosing braces and no comma's between the event objects.

loicknuchel commented 10 years ago

Hi !!!

I juste played with this endpoint but it seems to be quite tricky to get this on browser...

Firstly, I wrote a function to transform the returned data in JS object :

function buildResult(strValue){
  if(strValue){
    // add ',' at the end of all lines
    var step1 = strValue.replace(new RegExp('\n', 'g'), ',');
    // put data in an array
    var step2 = '['+step1+']';
    // remove last ',' to get a correct array
    var step3 = step2.replace(',]', ']');
    // parse json and return JS object
    return JSON.parse(step3);
  }
}

But now I'm struggling with jsonp in browser... If I add a callback to the url, I get a 400 (Bad Request) error from mixpanel and if not, as the loaded code is not a valid JS file, I get Uncaught SyntaxError: Unexpected token :

My Angular code :

var mixpanel = new MixpanelExport({
  api_key: credentials.api_key,
  api_secret: credentials.api_secret,
  api_stub: 'http://data.mixpanel.com/api/2.0/'
});
var url = mixpanel._buildRequestURL(['export'], params);
$http.jsonp(url+'calback=?', {
  transformResponse: function(data, headers){
    return buildResult(data);
  }
}).then(function(result){
  return result.data;
});

Any hint to get this working ?

I also tried with the get method but it results with a 400 (Bad Request) :

var mixpanel = new MixpanelExport({
  api_key: credentials.api_key,
  api_secret: credentials.api_secret,
  api_stub: 'http://data.mixpanel.com/api/2.0/'
});
mixpanel.get(['export'], params, function(data){
  console.log('data', data);
});

Any idea why ?

Thanks for helping !

michaelcarter commented 10 years ago

Hi all. I finally got around to looking at this (several months on, eek!) I've managed to get the export endpoint working for node.js stuff and have included that in the master branch (thanks @loicknuchel for your function there).

I've had no luck with the jsonp callback clientside though, so I'm going to email the guys at Mixpanel and see what's up. Thanks for your patience.

loicknuchel commented 10 years ago

Thanks for your work ! I hope Mixpanel guys will respond and solve this problem quickly (I don't have backend server and this issue is quite blocking for me to analyse specifically my events...).

michaelcarter commented 10 years ago

Ok, Mixpanel got back to me. Basically the export endpoint doesn't support jsonp, this was a deliberate move as it's not designed to be used in the browser. That said, if you pull down a copy of this repository and remove the error thrown from the source code for the export endpoint (line 21 of mixpanel_data_export.js), then you should be able to have it running in-browser by disabling the same origin policy, see this stackoverflow thread for more info: http://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome

I don't know if that helps at all... sorry @loicknuchel! If you're just doing some local analysis them perhaps write a quick node script for this?

loicknuchel commented 10 years ago

Thanks for your helpful response.

I will probably create a proxy server to transform data in json...