I tried to get data from my Mixpanel project using Apps Script's UrlFetchApp. It involves sending a JQL script within the body of the HTTP POST request. Here's the JQL script:
var jqlScript = 'function main() { return Events(params).filter(function(event){return event.name == \"purchase\";}).reduce(mixpanel.reducer.count());}';
var params = '{\"from_date\":\"' + getDateString(aWeekAgo) + '\",\"to_date\":\"' + getDateString(yesterday) + '\"}';
var payload = {
script: jqlScript,
params: params
};
var options = {
'method': 'post',
'headers': {
'Authorization' : 'Basic #############' // my API secret
},
'payload': payload
};
var data = UrlFetchApp.fetch('https://mixpanel.com/api/2.0/jql/', options);
But I keep getting this error in the Execution transcript:
"Request failed for https://mixpanel.com/api/2.0/jql/ returned code 400. Truncated server response: {"request": "/api/2.0/jql/", "error": "Error validating script: Uncaught exception SyntaxError: Unexpected end of input\nfunction main() { return E... (use muteHttpExceptions option to examine full response) (line 22, file "Code")"
I double-checked the script and made sure there's no missing '}' or things like that, as other questions on this site suggested. I'm stuck here!
I assume there's something wrong with App Script's escaping, because I don't know why this works, but I found a functional solution:
If you remove all semicolons from your jql/javascript string, it will work.