Grails Integration Mixpanel for track user activity

with No Comments

I have integrated mixpanel in two of my projects in grails so I wanted to share some thoughts on how Mixpanel might be integrated with grails.

First of all What is it Mixpanel?!

Mixpanel is a business analytics service and company, that provide to you collecting user activity by simple API and after analyze it by JQL.

Theme of this post will be only collections user activity, i.e. create user based on your datas in mixpanel and send “events” binded to current user.

let’s start.

First of all you need to register in mixpanel and receive your application token.

mixpanel token

I didn’t find grails mixpanel plugin, so we will use java library.

Let’s implement creating user(this code update profile too):

Firstly we should denied execute this code in test environment, that data from integration test doesn’t come to Mixpanel.

After that I prefer to send data in new thread. You can skip it if you want.

Than we see:
new MessageBuilder(MIXPANEL_TOKEN)
Here it is token that we received in your application.

In props there are few fields that started from “$”, it’s predefined values.

You can read more in mixpanel docs. Just you need to know that “$name” it’s name that you will see in user list. ‘$distinct_id’ should be unique to this parameter we will be map events.

Also you add all fields that you need, just putting it to JSON, as you see in example

Let’s start implement sending events:

Firstly update service

Main thing in next row:

delivery.addMessage(messageBuilder.event(user?.id?.toString(), eventType, props))
first param: it’s ‘$distinct_id’ that we specified during creation user, next eventType that will used in your dashboard and custom props.

That all only a little part of Mixpanel functionality, hope it will helpful for starting using this powerful service.

Leave a Reply