Created
April 19, 2017 16:49
-
-
Save jordanh/35bd9d7a5f7c8a7d6c8be1777f79b062 to your computer and use it in GitHub Desktop.
zapier-mixpanel-jql-run-javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mixpanelApiUrl = 'https://mixpanel.com/api/2.0/jql'; | |
var mixpanelApiSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
var toDate = new Date(input.toDatePretty); | |
var fromDate = new Date(toDate); | |
// calculate 30 days ago: | |
fromDate.setDate(toDate.getDate() - 30); | |
var toDateISO = toDate.toISOString().slice(0,10); | |
var fromDateISO = fromDate.toISOString().slice(0,10); | |
// Your JQL here: | |
var params = { | |
script: ` | |
var FROM_DATE = '${fromDateISO}'; | |
var TO_DATE = '${toDateISO}'; | |
function main() { | |
return Events({ | |
from_date: FROM_DATE, | |
to_date: TO_DATE, | |
event_selectors: [{ | |
event: 'Loaded a Page', | |
selector: '(defined (properties["id"])) and ("|" in properties["id"])' | |
}] | |
}) | |
.groupByUser(function (count, events) { | |
return 1; | |
}) | |
.reduce(mixpanel.reducer.count()) | |
} | |
` | |
}; | |
var basicAuth = new Buffer(mixpanelApiSecret).toString('base64'); | |
const formData = Object.keys(params).map((key) => { | |
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); | |
}).join('&'); | |
fetch(mixpanelApiUrl, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', | |
'Authorization': 'Basic ' + basicAuth, | |
'Accept': 'application/json', | |
}, | |
body: formData | |
}).then(function(response) { | |
if(response.ok) { | |
return response.json(); | |
} else { | |
throw "Bad Response"; | |
} | |
}).then(function(json) { | |
callback(null, { | |
value: json[0], | |
dateString: toDateISO | |
}); | |
}).catch(callback); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment