Best Practice to send tracking data to server from app

It’s very common to track the user behaviour in an app and if you are not doing that in your app, you should definitely use some analytics tool to do that. You can not make improvements to your app unless you know what exactly is happening with your users.

If you are using some Standard analytics tool like Flurry or Google Analytics, don’t bother reading this post. But if you are tracking some data without the help of these tools, then the curious question arises when do i send the data from app to server.

If you are implementing some real time analytics, then you might have to send data immediately back to the server with a very minimum delay of lets say around 5-10 secs, anything more than that will not add the value to the term real time.

On the other hand, if the analytics are digested in the real time, then there is no need to hurry, Do i send the data every time the user switches from screen to other screen?, it is a good way but not optimum if you want less pings to your server, considering the data can be coming from a lot of users.

The objective is to reduce the server load and also to maintain no data loss that is being sent to server.

My approach would be to first store the data in the local database and at every screen exit start a timer to send the data to the server, the key is to find the optimum delay for the timer.

1. If the user exits another screen before the you send the data, cancel the existing timer and start a new one. This way we are sending the data back to the server only when there is a long interval between the user actions and being postponed if there isn’t much time difference between user actions.

2. If there isn’t any other screen exit then the first timer will send the data back to the server.

You can also implement an ACK mechanism to make sure there is no loss of data, and you wipe the data from the local database only if you receive an ACK. If your backend is reliable enough to process all the data received there is no need to wait for an ACK.

Happy Coding…


Android: Intent extras not received

If you have put extras to an intent and they are not received at the receiving activity, there could be 2 reasons..

1. The activity already exists in android backstack and the extras are not caught in onCreate() but they can be found in onNewIntent()

2. If the extras are passed with an activity of a PendingIntent, then as per the official documentation. So, to pass the extras correctly, either you need to make sure each of the intents are having differentiation in terms of action, data, type, class, and categories. Or cancel the current PendingIntent if exists in the system by using FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT.

Happy Coding..

Android: Never miss on GCM ID tracking

I have come across an excellent blog regarding GCM tracking and in the mobile world, push is the only gateway for you to reach the user after the installation.So, we can’t afford to mess it up by not following some easy methods which ensure successful tracking of GCM ids. Please visit the following link for the entire details …

Happy Coding…

LBS – How to find User’s Home and Work location

LBS (location based services) for mobiles can be improved by knowing user’s work and home location, for eg. you don’t want to disturb the user with a push notification while he is busy working. There can be many uses around knowing this sensitive data. As for android devices google is able to predict the same and providing you with alerts at the right time. Mobile OS doesn’t track this information in general and hence doesn’t have an API for the app to access this information.

So, the only way for the app to know about user’s location is by detecting the location at various intervals of time. Hypothetically lets say the app get’s the user location every hour, but the challenge is how do you find home and work location using this location data.

Method 1

Not considering extreme cases, we know that a typical user would definitely be in office from 10.00 AM to 5.00 PM, so during these period the app receives 6 locations according to the assumptions made above. It’s hard to find which of these 6 locations is the exact work location, so geometric centre would be right solution, but what if the user has gone out for lunch during the work for a couple of hours, then the geometric centre will give us the wrong work location. Right method would be to cluster the locations based on the maximum distance between 2 locations and pick the cluster with maximum locations to find the geometric center which would be the user’s work location.

I think estimating work location for 1 day will not be enough, you have to observe the user over a period of time to get the exact work location. 10 days is a good period to get a right estimate, which would mean we have to track the user’s location for 14 days(5 working days per week). Each day we get a rough estimate of user’s work location and by clustering these day locations obtained based on the maximum distance between 2 locations and picking the cluster with maximum locations to find the geometric center will give us the exact work location.

The app needs to keep track of the user’s last 14 days locations and calculate the work location with the data over past 2 weeks to keep the data accurate and fresh, this way if user starts working at new location, we can get the new work location in 7 days.

Finding home location uses the same algorithm except that the locations used would be from 11.00PM to 6.00AM, as user stays at home during midnight and the 14 days period handles the variations.


In this method, make clusters of the 24 locations based on the distance between locations. Pull out the top 2 clusters with maximum locations and identify the time periods. One of them must be work location and other is home location. Do the same over a period of 2 weeks to get a proper estimate of user’s home and work location. But to find which of these locations is home location, observe the locations over the weekend assuming user doesn’t go to work on these days :P.

Happy Coding…

New Torrent Alerter

It’s time consuming to go to and search for a tv series new episode, or some new movie. It becomes even harder if you follow a lot of series and dying to wait for a new movie torrent to come up. For eg. I don’t know when “Hunger games 720p” comes up on torrents, i can’t keep checking for the torrent everyday or i may forget to watch the movie unless i see the name somewhere probably on IMDB.

So, this calls for a torrent alerter for a string, I want to get an alert on my email, when a new torrent comes up for a tv series I am interested or a movie, or a song, or anything. So the idea is that the tool should take in a list of strings for the torrents i am interested and should alert me when something relevant comes up.

I searched for something like this over the internet but couldn’t find a service/tool, which does what i need. I guess in theory it’s hard to provide such service, for free at least. Imagine millions of users subscribing to millions of unique torrents, it takes a lot of infrastructure to handle this.

So I decided to write my own alerter. It was easy to achieve this.. the steps involved a briefed as follows.

1. Understand how search works on and see if it has an API to get the results. We can use to get a normal html response or to get a rss feed. query being the torrent you are interested in , could be the name of a movie or documentary or tv series.

2. Now, i only need latest torrents which were added may be 12 hours old or may be less than that.So getting the results by time sorted is the way to go, so i found that, does what i need, instead of search we use searchA.

3. BeatuifulSoup comes to the rescue to parse the html response given by the above query, Now it’s up to me to extract the torrents that were added with in the time limit. I had to find the time the torrent was uploaded and compare it to the current time and check if it’s relatively fresh for me.

5. BeatifulSoup helps you to extract name, link, time of the torrents listed.

6. After finding the torrents i need, I use SES to send a mail to my email address with the torrents i need.

7. I put the whole process in the scheduler to check for updates regularly.

So, I don’t have to search for torrents anymore, they drop in my mail every day. Bye bye searching, the current world is all about notifications … 😀

Happy Coding..

Android: Center aligning a TextView with drawableLeft

Instead of using a TextView and ImageView, we can use drawables of TextView to reduce the no.of views used.

So, the problem at hand is, how do you align a TextView text and it’s left drawable to the center horizontally in a given width.

Fail Method: 

Put the textview width to “match_parent” and use “android:gravity” as center, the expected result is that both drawable and text should be center aligned horizontally, but to my surprise, i see only text horizontally aligned and the drawable is aligned to the textview left.

Working Method But Not Efficient:

Take a horizontal LinearLayout, put ImageView and TextView(no drawableLeft since we are using ImageView) in it with each of the view’s width set to “wrap_content”, and use “android:layout_gravity” to center on the LinearLayout. This works but we are using extra ImageView and LinearLayout.

Best Method:

Set TextView’s width to “wrap_content”, use drawableLeft,  and set “layout_gravity” to center for the TextView.

Happy Coding…

Android: How to find if a user has uninstalled the application

In the apps world, uninstall is a very common phenomenon, especially in Android, where there are no checks before an app gets published.

So how do you find if a particular user has uninstalled your application, a quick answer is GCM. Lets look at what happens when you send a push to a device where your application has been uninstalled.. (picked up from GCM docs)

  1. The end user uninstalls the application.
  2. Your server sends a message to GCM server.
  3. The GCM server sends the message to the device.
  4. The GCM client receives the message and queries Package Manager about whether there are broadcast receivers configured to receive it, which returns false.
  5. The GCM client informs the GCM server that the application was uninstalled.
  6. The GCM server marks the registration ID for deletion.
  7. The 3rd-party server sends a message to GCM.
  8. The GCM returns a NotRegistered error message to your server.
  9. Your server should delete the registration ID.

So from the above steps, it’s easy to understand that if your server gets “NotRegistered” error message, then the app has been uninstalled on the user’s device. So all you need to do is, send a silent push to your application, to check if it exists on user’s device.

Though from my personal experience, GCM server doesn’t return the error message even the app has been uninstalled, at least for the first few days of uninstallation.My guess is that GCM server takes some time to update the information of app uninstallation.

A more accurate approach will be to receive an ack from the application for the push message, So, if you receive an ack then the app still exists, if you don’t then the app doesn’t exist on the user’s device. But, there is a catch here, what if the app exists on the user’s device but the message is not delivered because the user isn’t connected with internet. How long will you wait to receive the ack, here is where your conscience comes to the rescue, you have to decide the time you will wait for the ack and decide if the app exists on user’s device. If your app is dependent on internet for the app usage and you don’t receive an ack with in 3-4 days, it’s safe to assume the user is useless to you whether he has the app or not, you can safely put him under uninstall category.

Identifying the uninstallation details can help you find some insights about the app if you can map user’s information with device information.

Happy Coding…