Old School is The Best School?

Video is not related with the post :) Today i wanted to integrate Google Analytics to my Android application. I am using PhoneGap at this application. As you know, i can’t use Google Analytics SDK with PhoneGap because of MainActivity loads only index.html and then index.html controls everything (etc other pages)

By the way, i found a plugin for PhoneGap that uses Google Analytics. But it didn’t work for me. I tried so many thing. Almost tried every code/step that writes at Google Groups, Stackoverflow etc. I couldn’t figure out why it is not working.

Anyway, in my application i have a spinner(selectbox) that i want to track how many people selected which one. It’s really simple tracking activity. Then i wrote a simple JS code and PHP HTTP API. Let’s explain what i am doing:

If mobile device has not internet connection:
Insert track variables to database (Storage)
Elseif mobile device has internet connection:
Send current track variables to PHP Api
and send track variables in database to PHP Api.

It’s really simple? Let share code:

/*Open DB*/
var db = window.openDatabase("yourdbname", "dbversion", "DbName", 1000000);
/*Get device connection type*/
var connection = checkConnection();
var d = new Date(); /* You have to use time i think. d.getTime() will return milliseconds date */
var db = window.openDatabase("eskisehir", "1.0", "AndroidArge", 1000000);

if (connection != "false"){

    /*Sending tracking variables to api*/
    {post: "variables"}
    /*Check tracks and send them to api*/
    db.transaction(countTracks, errorCB);
    /*Let write track variables to db*/
    db.transaction(AddTrackToDB, errorCB, successCB);

function countTracks(tx){
    //Getting all old track 
    tx.executeSql('SELECT * FROM track', [], successTracks, errorCB);

function successTracks(tx,results){
    // If there is trackings at db
    if(results.rows.length > 0){
        var len = results.rows.length;
        for (var i=0; i<len; i++){
                {post: results.rows.item(i).data}
        //Let delete tracking
        tx.executeSql("DELETE FROM track WHERE id = "+results.rows.item(i).id);
function AddTrackToDB(tx) {
    var d = new Date();
    //Let create table if not exist. This is not good idea. But i did it :)
    tx.executeSql("CREATE TABLE IF NOT EXISTS track (id INTEGER PRIMARY KEY, data TEXT)");
    //Let insert variables to db
    tx.executeSql("INSERT INTO track (data) VALUES ('your tracking variables')");

function errorCB(tx, err) {
    //Of course you will not send alert to user :)
    alert("Error : "+err+"\n :(.");

function successCB() {
    //alert("successfully runned sql code!");
function checkConnection() {
    //I don't know if there is another way to get is connected. I found this code snipped in PhoneGap docs.
    var networkState = navigator.network.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.NONE]     = 'false';

    return states[networkState];

or here you can find gist: https://gist.github.com/3849990

I don’t know this is true way, but it’s really simple and quick way. Also i don’t need GA’s thousands detailed reports. I just want to track simple variable.

I will be happy if this simple solution be useful for you. And if someone do this a js plugin, i will be really really happy :)

You can ask anything with comment.

Leave a Reply

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

%d blogcu bunu beğendi: