ruby, Ruby on Rails

A formal introduction to gaevents

This post serves a general introduction to gaevents, a Ruby gem to send user events to google analytics.

You can find the gem here:

and the code:

What is gaevents?
In a nutshell, gaevents is a Ruby gem that enables sending user events to Google Analytics in batches from a Ruby/Rails application.

What unique problem does it solve?
Traditionally, Google analytics defines events as:

Events are user interactions with content that can be tracked independently from a web page or a screen load.

However, in a complex application, users might trigger delayed events which are equally important for the analytics.

Delayed Events:

To understand delayed events, let’s take the example of Netflix. Netflix allows users to create subscriptions which are then billed monthly or annually depending on the plan. When the user has first created the subscription, they interacted with the application, i.e. navigated a few pages, clicked buttons and submitted forms. These interactions fit perfectly with GA’s definition of “events” and are therefore easy to track. However, depending on your requirement, you might also want to track all subsequent charges that were made on the user’s card. These charges are automatically generated by the system and are not dependent on the user interaction. Such events are referred here as delayed events.

Depending on your application there can be many such system generated events which are equally important for the analytics but don’t fit the textbook definition of events. Gaevents provides a clean way to track these events. The gem also supports batch events update out-of-box!

How does it work?
You can use gaevents to send out the events as and when they occur, however here, we will focus on sending batch updates with the help of redis.

In your application, whenever an event is triggered, capture it in a redis queue. Something like:

redis.set("analytical_events", [
   {:event => “event_1”, :user => user1},
   {:event => “event_2”, :user => user2},
   {:event => “event_3”, :user => user3}

You can create a worker job (using DelayedJob or Resque) which will look for these events at a given interval of time:

events = JSON.parse(redis.get("analytical_events"))

Create GAEvents::Event for the collected events:

gaevents = []
events.each { |event|
   gaevents <<{
          :cid => event[:user].cid,
          :t => event[:user].tid,
          :ec => "gauser",
          :ea => event[:event],
          :uid => event[:user].id,
          :tid => event[:user].tid

Please refer Measurement Protocol Parameter Reference for the list of all parameters and their accepted values.

Once all the events are collected you can send them in one shot:

res = GAEvents.track(gaevents)

gaevents is focused on solving one unique problem and aims at solving it well.

Please feel free to share your thoughts in the comments below. Do check out the github repository. Any contribution or suggestion is welcome.



The Strange Ruby Splat

End of Line

This article has been republished on Monkey and Crow.

As of ruby 1.9, you can do some pretty odd things with array destructuring and splatting. Putting the star before an object invokes the splat operator, which has a variety of effects. First we?ll start with some very useful examples, then we will poke around the dark corners of ruby?s arrays and the splat operator.

Method Definitions

You can use a splat in a method definition to gather up any remaining arguments:

In the example above, what will get the first argument, then *people will capture however many other arguments you pass into say. A real world example of this can be found in the definition of Delegator#method_missing. A common ruby idiom is to pass a hash in as the last argument to a method. Rails defines an array helper Array#extract_options! to make this idiom easier to…

View original post 328 more words

Immutable JS
immutablejs, Javascript, redux

ImmutableJS Examples For Humans

As I already talked about earlier, we at BlogVault, are redoing our dashboard. We ditched BackboneJS and Underscore templates for flux architecture (Redux) and ReactJS. Redux attempts to make state mutations predictable by imposing certain restrictions on how and when updates can happen. These restrictions are reflected in the three principles of Redux:

  1. Single source of truth
  2.  State is read-only
  3. Changes are made with pure functions

Using ImmutableJS ensures the state is not accidentally mutated, hence strictly following above principles.

Developed and maintained by Facebook, ImmutableJS has a strong community and project itself has 15308 stars and 807 forks on GitHub, at the time of writing. However ImmutableJS currently has one pain point – plain JS examples! If you go through docs, you can find most API examples are in typescript. Understanding or translating this to JS can be difficult for most novice coders. I faced similar issues while getting started. While going through endless SO and GitHub threads I realised I was not alone. In fact I remember seeing a few angry comments, which I do not support, as it goes against the spirit of community. Following this philosophy I decided to note down examples for all APIs for my future reference. This might also help others with similar difficulty.

Most of these examples are basic and should be enough to get started. I have not described any API as its already beautifully done in ImmutableJS docs.

Let me know in comments if I missed anything or made any errors.


How I reduced my blogspot page load time by half

Page load time is serious business. Okay may not be for my old PhotoShop blog which I no longer maintain, but for your main-source-of-income site it is! In 2012 Amazon analyzed its data and calculated that a page load slowdown of just one second could cost it $1.6 billion in sales each year. That’s massive, in fact let me help you visualize that amount – Facebook bought Instagram for $1 billion same year. You can do your math now!

Anyway coming back to reality, I have this old blog that I used to maintain in my freshman year. It was terribly built over blogspot. I copy pasted some theme, bundled a bunch of widgets and marketed extensively among my peer which lead to some good traffic. That improved my google rank especially for one particular post on Godfather style poster designing.

Fast forward today, I accidentally visited my old blog and realized it was still getting consistent traffic. That was great! However first thing I noticed about my own blog was – how shitty it was! I checked my browser console only to confirm my blog was bleeding with javascript errors. So much so, even posts date were displayed as “undefined”.

Next thing I noticed was the painful load time. I felt like running out of patience with my own blog owing its terrible load speed. My blog had one share widget that would take forever to load. Then there were links that were not pointing to anywhere. Sadly I had no one to blame and I didn’t feel very proud of myself.

I would have left it on its state as I have no intentions of contributing there, however there is something every emotional about your first blog. Also the developer in me could not tolerate such mess of code and hence I decided to get my hands dirty cleaning it.

Say no to JavaScript errors

Here is how my browser console looked initially on page load:

That’s ugly. There are a lot of stupid errors that can easily be solved. As clearly visible from above screenshot my theme was trying to load some amazon affiliate link. An adware? maybe! after all I copy pasted theme code from some untrusted source. First thing was to remove any code I cannot understand . This was a time taking task but result was worth it. It took care of all javascript errors and cleaned my blog from all adwares and unwanted backlinks.

Widgets should not slow you down

I noticed my share widget would just take forever to load. An ideal solution would have been to fix it or replace it with a better light-weight share widget, however I went brutal here and chopped it off completely from my site. One less thing to worry about!

Bare minimum navigation

As mentioned above my blog had nav menus pointing no where. That was bad design, confusing and hence completely unacceptable. Fix – remove them or replace them with more suited menus.

Here’s how it looked initially:


“Post RSS” and “Comments RSS” are of least importance and surely does not deserve a place in top menu. “Edit”? Now seriously, how did it even get there? It was plane stupid. I removed all these three menus and replaced them with “About me” which link to About page of this blog.

UI glitches are disgrace

My posts were not able to show post date correctly, instead it would print undefined:


I looked into the theme code, the issue here was, the original author made an assumption about the date format returned upon query. Obviously his assumption was wrong and hence his clever attempt of parsing the result resulted “undefined” . If years of coding has taught me one thing, its say no to assumptions. I simplified the code and completely removed the parsing part to display simple date.

Say yes to page-breaks

All these changes did very little to improve my page load time since my home page was still fetching a lot of data. This made me realize my posts had no page breaks.  Just for sake of clarifying: A page break is a marker in an electronic document that tells the document interpreter that the content which follows is part of a newpage. Or in simpler words enabling “read more…” option. This reduced my homepage size to a great extent as now posts only have a feature image and all other images go on their individual page and hence are not fetched in home page.


That was a lot of work on a blog you don’t even maintain. But at the end I was rewarded with amazing result. Lets see:

Pingdom test result before my changes:


Pingdom test after my changes:


As claimed before, page load time reduced by half! And obviously you can see improvements in other areas too. Yoo…hoo!! 🙂

Javascript, react, redux

My Experience with React and Redux

How it started

We, at BlogVault, decided to completely redo our web application UI, which is currently powered by Backbone.js with Underscore templates. Exploring options, we tried using MarionetteJs on top of Backbone.js for our specialized migration dashboard that powers migration for our partner web-hosts like wpengine, savvii, cloudways etc. While MarionetteJs provided a decent improvement over our Backbone.js code with its common-practice architectural solutions, the code still looked hacky at times.

In Backbone.js responsive views listen to models and collections and re-render once underlying data is updated. However Backbone.js being so minimal (which is often credited as a plus point), it leaves most design completely on developers. This is where the problem starts, if you are not a disciplined coder there are enough ways to screw up. For example – manipulating DOM directly upon success callback. I have seen enough people posting such code on various forums. This may not be a big issue at beginning, but as the application keeps growing and you keep adding new features, you realize your views are not in perfect sync with your models and collections anymore. A quick fix would be manually manipulating DOM to update UI, which as insane as it sounds, is not at all a rare practice, such code snippets are posted all over the internet in abundance. This was just one example, there are many other. This leads to spaghetti data-flow, abrupt DOM manipulations, and at times massive memory leaks. This was one of the big reason we wanted to move away from Backbone.js.

Next we considered AngularJs, however dropped it almost immediately after we learned that Angular 2.0 (which at that time was not released and currently is in beta) won’t be backward compatible.

Finally we decided to try Facebook’s newly released view library React and application architecture Flux. Rewriting everything in React and Flux had its own risks. React was brand new, meaning considerably smaller community, lesser plugins and most terrifying – fewer StackOverflow posts! Also most of the code is in ECMA6, and to add further,  Flux is a non-MVC (or MV*?) architecture. However experimenting with few examples we decided to give it a try.


What is React and Redux?

The official site for React defines it as “a javascript library for building user interfaces”. And Redux is officially defined as “a predictable state container for JavaScript apps”. Lets look at them in detail.

Redux can easily be distinguished with most of the object-oriented MV* architectures, as:

  • the complete state of your application is maintained inside a single object called store.
  • at any given time the App’s state (store) is immutable, and
  • data flows strictly in one direction.

That’s a lot to digest if you are new to this. However the more we dive into the real world problems, more it makes sense.


How they work?

Data in a Flux architecture flows in single direction:

Action > Dispatcher > Store > View


Action suggests something just happened. Dispatcher is called and it sends the action to the store. Store modifies itself (without mutating existing structure) in response to action. Finally, as per the state of store, view gets updated.

Redux is an extremely light implementation of Flux architecture. It implements the above Flux data-flow in following sequence:

Action > Dispatch > Reducer > Store


In Redux implementation, an action is a plain object describing what happened. A function store.dispatch(action) is called to pass action to store. By this time, Redux knows something happened and depending on action, it has the result for what happened (suppose as action triggered a server request and got response). Now, in reducer we specify how the store’s state should change in response to that action. Once the reducer modifies the state, React picks up the new store and re-renders view automatically.


Now coming back to React. As it should be clear by now, React is the view part of our implementation. If I have to explain it in the vaguest possible way, I can say its comprises of two parts:

  • a template written in jsx.
  • a mechanism to maintain its own internal state in memory.

At the end React processes these two parts and churns out good-old-plain HTML that your browser understands.

Given the capability of maintaining states, it makes React extremely powerful and a delight to write UI on. Think of this – you need to display a message based on some event. Generally you will do DOM manipulation in event handler. However in React all you need to do is set a state in event handler and as per your template, React will automatically re-render that particular part. That’s powerful, and clean!

In fact I recently read somewhere someone comparing DOM manipulation with goto statements. Its as bad! There is no definitive source of truth and hence tracking down the source of manipulation is walking into hell-hole.



React and Redux together are fairly powerful and provides robust and efficient solution for building a single page application. In Redux, since the data-flow is restricted to one direction, scope for screwing up is significantly reduced. React has beautiful in-built data-binding which responds to state changes flawlessly. And lastly React provided various components lifecycle callbacks which helps write elegant easy-to-maintain code.

It was difficult learning curve attributed to – relatively fewer resources, smaller (yet massively growing) community, ECMA6 based javascript (which was completely new to me), new concepts like virtual DOM etc. However once I got the basics clear, everything seem to fall in line. 🙂

linux, Mariadb, mysql, Ubuntu

How I screwed MySQL install with MariaDB and recovered

Broken packages are mess! I recently installed MariaDB to give it a try. After testing, I decided to uninstall it for now and maybe come back later. I was careless while removing MariaDB and ended up messing MySQL configs (MariaDB runs on top of MySQL). Things were only to get worse from here. A high priority task pops up that requires me to access MySQL. Unable to access it, I decide to completely remove MySQL and do a fresh install – basically a hard reset. If only things were that simple in real world. By this time my MariaDB was half removed, MySQL configs were a mess and I was now about to remove MySQL completely. Here’s what happened:

Alright, maybe I did not uninstall MariaDB correctly. Lets try removing that once more:

Same error! Ok, I clearly screwed MySQL. I should try installing it back. Lets see how that goes:

Unmet dependencies? Ok! I know what to do in such cases:

Nothing! That’s scary. There’s no way forward (installing) and there’s no way backward (uninstalling). I am basically stuck!

Maybe autoclean is a good idea? Sadly no difference.

I noticed both MySQL and MariaDB remove commands were exiting because of ‘mysql-server-5.6’, so fixing it was my best bet. I also noticed the error raised while removing mysql-server was from file `/var/lib/dpkg/info/mysql-server-5.6.postrm` as evident in following line:

/var/lib/dpkg/info/mysql-server-5.6.postrm: line 53: /usr/share/mysql-common/configure-symlinks: No such file or directory


So what if I comment the line that’s raising this error? Risky, but desperate times – desperate measures. After creating a backup of the file, I edited it to remove the line that raised the error and retried. Here’s what happened :

Finally! Editing the file worked and I was able to remove both packages. Next I fresh installed mysql-server and it worked perfectly. What a relief! 🙂


Recover your iPod playlist within seconds

I had to leave for Goa the other day and I was packing my bare essentials when I came across my age old forgotten iPod. Finding it out of blue was a bliss and I decided to take it along. I put it on charge and it was ready to rock.

2015-08-30I never realized what wonder old playlists can do. Listening to those old tracks I felt as if I was transported back to those good old carefree college days. After returning from Goa I decided to export those tracks to my computer, however there was an issue. We all know for some reason Apple renames music files and rearrange them in random folders. This makes it impossible to figure out which file corresponds which track.

Here take a look:

ipod files list

These are all random filenames. However to my relief these files were not encrypted and their ID3 tags were still intact. That meant all I needed to do was rename these files and I was good to go.

However given the size of my playlist (6.5 GB) there was no option to do this manually. So I wrote a little script that could help me out.


My script goes through these files, collecting their ID3 tags, maintaining a hash and finally copying these files over user provided destination to respective folders based on album name and the file itself is renamed on track title. Running the script restored back my playlist within 105 seconds. Here’s the benchmark:

0.160000 7.470000 7.630000 (105.228952)

And this is how my playlist looks now on my file system:

Selection_007 Selection_008

Here is the source code:

Feel free to star, fork, follow or contribute on GitHub. 🙂