Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Mixpanel .track() with no dependencies
//a vanilla implementation of .track()
//by AK
function trackStuff(uuid, eventName, props, token) {
//the mixpanel data model
let spec = {
"event": eventName,
"properties": {
// these two properties are required!
"distinct_id": uuid,
"token": token
//iterate through props
if (props) {
for (prop in props) {
//append them to our spec[prop] = props[prop];
// base64 encode
let payload = btoa(JSON.stringify(spec));
//make a vanilla HTTP request as per:
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
console.log("data sent");
};"GET", "" + payload + "&ip=1", true);
// how we might use this
let someProps = {
"user type": "maverick",
"interests": ["foo", "bar", "baz", "qux"]
trackStuff("ak!", "App Open", someProps, "009c696fd3f040f5b32297ac50845210");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment