Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An Example of Mixpanel inside a SPA
//an example of Mixpanel implemented in a frameworkless SPA
//by AK
const app = {
init: function() {
console.log("app init!");
this.authUser().then((user) => {
//identify the user to Mixpanel
//make a user profiles
uuid: user.uuid,
role: user.userType
this.getAppData().then(() => {
//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
"current theme":,
"# of foos": app.DOM.foos.length,
"something about bar": [[0],[1],[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
You can’t perform that action at this time.