Last active
July 19, 2020 14:06
-
-
Save ak--47/eb9e12d968d287cc0e5f4f30e5f490c1 to your computer and use it in GitHub Desktop.
An Example of Mixpanel inside a SPA
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
//an example of Mixpanel implemented in a frameworkless SPA | |
//docs: https://developer.mixpanel.com/docs/javascript | |
//by AK | |
const app = { | |
init: function() { | |
console.log("app init!"); | |
this.cacheDOM(); | |
this.bindEvents(); | |
this.authUser().then((user) => { | |
//identify the user to Mixpanel | |
mixpanel.identify(user.uuid); | |
//make a user profiles | |
mixpanel.people.set({ | |
$name: user.name, | |
uuid: user.uuid, | |
$email: user.email, | |
role: user.userType | |
}); | |
}); | |
this.getAppData().then(() => { | |
this.render(); | |
//tell mixpanel the page loaded | |
mixpanel.track("app loaded"); | |
//pass the app state to mixpanel | |
//these params are available to every subsquent .track() call | |
//they can be updated whenever we need | |
mixpanel.register({ | |
"current theme": app.storage.theme, | |
"# of foos": app.DOM.foos.length, | |
"something about bar": [app.DOM.bar[0], app.DOM.bar[1], app.DOM.bar[1]], | |
"dont forget qux" : getQuxState() | |
}); | |
//tell mixpanel to start a session; increment total # of sessions | |
mixpanel.track("Session Start"); | |
mixpanel.people.increment("# of sessions"); | |
//time the session, tell mixpanel the duration | |
mixpanel.time_event("Session End"); | |
window.onbeforeunload = function() { | |
mixpanel.track("Session End"); | |
}; | |
}); | |
} | |
//presumably a really great app would follow! | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment