Track my traffic -Implementing Traffic Analytics

24 / Feb / 2015 by kartikey 0 comments

This one is for a particular use case, so lets come straight out to the point. We start with the obvious first:

Problem Statement:

To track the traffic of a public page from your application.

User Story:

The current system needs to add a feature that would enable the end user to use your product to track the traffic on his application/web page. All that the application should seek from the user is the URL to the page/application he wants your traffic check on.

The Approach:

Because it’s virtually invisible to naked eye and can still be planted to play traffic God, hiding away from plain sight, the one-pixel image can be used to its full efficiency here. It’s minuscule size hinders it from affecting the page design too. Here’s how we approached the solution with the pixel image.

How to use the image pixel?

Prompt your users to embed an image tag code provided to them by you, and that’s all.

The image tag would contain a link to a controller-action in its src attribute.

<img src="url" name="tracker"/>

Now, with the image tag appended, whenever this concerned page is now accessed, the url in the image tag sends a request to your controller-action. This incoming request can now be treated as a hit count on the page to track down.

The response for each of such particular requests is now the one-pixel image, that when returned as a response gets rendered in the particular page from where the request comes and voila, you have your tracker ready for use. However, even though you have your tracker ready to use, the process does not simply end here.

As a general assumption, the kind of data this feature produces, becomes a huge chunk of information in no time and a further analytics on this data, is more or less always on the agenda. This huge chunk of never ending data, thus produced, can result in a bit of a problem for your average relational databases, in no time.

Welcome, non-relational databases.

Luckily, most of us modern day developers, are no stranger to the likes of non-relational databases, one of them being mongodb. As an added advantage to Grails developers, Grails with its mongodb plugin, provides seamless integration and a GORM support for mongodb.

How can mongo be used here? (A suggestive approach)

You can start by creating a domain (PageViewAnalytics or something semantically related to your use case) with a somewhat similar structure:

class PageTrafficAnalytics {

    static mapWith = "mongo"

    String pageId
    String readerId
    Date dateCreated

    static constraints = {
            pageId nullable: false
            readerId nullable: false

    static mapping = {
        collection "public_traffic_analytics"

Once done with the designing part of the analytics domain, storing the tracked page-views becomes somewhat a cake walk. For every request made by your image tag source, all you now have to do is make an instance of the class; with the pageId (page url), the readerId (user id) and the timestamp.

new PageTrafficAnalytics(pageUrl: pageUrl, readerId: readerId).save()

This suggested approach helps you keep your analytical data in the most granular form possible, i.e. a new entry for every hit stored with its particular timestamp. For attaining an even higher level of granularity, the Date object, defining the exact time of the request, can be further segregated and stored as Date, Month, Year, Weekday, Hour, Minute, Seconds and so on to the level that the application analytics demands, thus facilitating any graphical implementation of such data.


Leave a comment -