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:
https://rubygems.org/gems/gaevents

and the code:
https://github.com/singhshivam/gaevents/

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}
]).to_json

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 << GAEvents::Event.new({
          :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.

Cheers!

Advertisements
Standard
Ruby on Rails

FTPS (TLS/SSL) In Ruby

FTPS is an extension to the commonly used FTP that adds support for the Transport Layer Security (TLS) and the Secure Sockets Layer (SSL) cryptographic protocols.

While FTPS using clients like FileZilla, we generally prefix our host with ‘ftpes://’:
ftpes://ftp.example.com

Net::SSH is the most popular ruby library for ssh and ftp purpose. It however is not so helpful when it comes to FTPS. We will use Net::FTPFXP RubyGem instead.

Installing Net::FTPFXP is simple. Run:
gem install ftpfxp

Now to establish the connection, do the following:
require 'net/ftptls'
ftp = Net::FTPTLS.new()
ftp.passive = true
ftp.connect('host.com', port_number)
ftp.login('Username', 'Password')
ftp.list
ftp.close

Port number by default is 21.

Standard