Obvious Ideas

From the desk of a Software Developer

Archive for December, 2009

Using Mixpanel API In Google App Engine Applications (Python)

View Comments

We started using MixPanel to get realtime statistics on user engagement in our application. We’ve been using MixPanel before for funnel tracking, but you can’t really appreciate their service until you start using their event tracking. It is truly realtime (as advertised) – the second the user performs an action on your application, you see it on your dashboard.

Besides the realtime reporting what I really like about MixPanel is the fact that they allow easily reporting from your backend and not only from frontend/Javascript (as opposed to Google Analytics).

Within their documentation they have code samples in many languages, including Python. But their Python sample requires ability to start new processes on the server you’re running – something not possible on App Engine. Therefore I changed it to use URLFetch in RPC mode (so that calls to Mixpanel won’t block the call):

It could be re-factored more, like allowing setting the project token outside the function call, but that I will leave for you to do :) (and if you do, please share with us)

Arik

Written by Arik

December 14th, 2009 at 7:21 pm

App Engine: Mapping Entities Using Deferred Tasks

View Comments

I recently started using deferred tasks in my App Engine application. I’m using them mainly for two things:

  1. splitting user requests into two pieces – one that being done immediately and one that being done in the background, to speed up the response time.
  2. mapping (iterating over) all my entities to calculate various statistics.

I won’t provide here details on how to use deferred tasks, because this is described in detail in a great article by Nick Johnson, which I highly recommend reading. One thing I do want to share from my experience, is my implementation of the Mapper base class:

This implementation is taken from Nick’s article mentioned above, but I made some changes to it. The first change is giving subclasses the ability to set the property to order the entities by. I needed this, because I found out that when your entities have custom key names ordering by key breaks for some reason.

The other change is re-factoring the way the next batch starts. It seems to me that in the sample provided in the article there’s an error in the indentation, resulting in the code not doing batching properly and can even break in some situations. In my version every task run maps only one batch (or less). This is sub optimal in terms of # of tasks it takes to map all your entities, but as long as you don’t have really a lot of entities this is not such an issue.

I really would love to hear others’ comments no my changes and about how you use deferred tasks or map your entities.

Arik

Written by Arik

December 7th, 2009 at 8:57 pm