Created
October 7, 2012 23:45
-
-
Save shibby/3849990 to your computer and use it in GitHub Desktop.
PhoneGap Old School Analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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("yourapiaddress", | |
{post: "variables"} | |
); | |
/*Check tracks and send them to api*/ | |
db.transaction(countTracks, errorCB); | |
}else{ | |
/*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("yourapiaddress", | |
{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]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment