// Ruby on Rails Analytics · Rails · Developer Tools

Rails: Non blocking analytics

3 min read · Ruby on Rails
Summary 3 min read

Adding analytics calls inline in controllers makes users wait on third-party APIs and slows the whole app. The fix is to move every analytics call into a background job, and, once you send data to several services, wrap them behind a single adapter, which is what B&G's open-source bg-common gem does.

Scenario: You’ve been working on a web based product which started getting some traction. You want to understand your users’ behavior on your app, and whether the user experience is smooth and going the way you’ve assumed, or if it’s got bumps that you can fix. Naturally, you decide to track every action and event that is occurring. You integrate with something like Google Analytics or Keen.io to track user actions as well as sync some meta data about your user base to better understand what’s going on.

Problem: After deploying the changes that add the tracking, you start receiving complaints that the app is slower, and in some cases, users are experiencing weird behaviors and an increased rate of errors. What happened?

Diagnosis: You’ve added your analytics code inline in your controllers, so after each action a user takes, they have to wait for the request to your own servers to finish as well as every request to each analytics platform you are using. What has happened is that you’ve introduced bottlenecks into your code that is acting as user blocking logic. Ask yourself this: Does the user need to wait for the analytics calls to finish before they can continue using your app? The answer is no. From the user’s perspective, they don’t care at all about what’s happening in the background and that you are making an extra call to a third party service for analytics. In fact, this is also true even if you’re not using a third party service and tracking the actions yourself. The analytics logic is secondary to the user experience. If something fails along the way in terms of collecting data for analytics, the user should not be blocked from using your app.

Solution: The fix is easy. Move all analytics related code to background jobs. Let’s add the following job file to the app (in app/jobs/analytics/keen_event_job.rb):

If you read the Keen ruby gem README, you’ll find instructions on how to do the same thing but using EventMachine and replacing publish with publish_async and that solution is good if Keen is the only analytics platform you’re using. I find that most projects I work on use multiple analytics platforms like Google Analytics and Keen, but also use other platforms that you’d want to send data to for better customer service like Intercom.io.

At this point, if you’re using multiple services that you’d want to send data to, you’ll find yourself repeating a bunch of code everywhere in your app. That’s when it becomes worth while to write a wrapper that handles calling all of the different services for you. Using the adaptor pattern, we can write a class that has a single interface and is responsible for dispatching the requests to each analytics service.

That sounds a bit difficult and a lot of work. Especially if you are working on multiple apps which all need to implement some sort of analytics. That is why we’ve created a gem that does that for you. The gem is still in its early phases, and will probably change a lot in the near future in order to develop a consistent API and make it modular so it can accept new integrations easily. But for now, it does the job. You can find the gem on github, it’s called BG::Common because it includes a bunch of common things that you’d want to implement in any Rails app. It is likely that we will break it up soon and make the analytics portion of it its own separate gem.

Bear & Giraffe Advisory

We partner with founders and tech leads to build sustainable Rails architecture, untangle monoliths, and scale pragmatic systems.

Schedule a Strategy Call
ESC

Type to search 38 posts