Skip to content

Commit

Permalink
Merge pull request #1720 from cb1kenobi/timob-7983
Browse files Browse the repository at this point in the history
[TIMOB-7983] Cleaned up analytics to support the new analtyics backend
  • Loading branch information
nebrius committed Mar 17, 2012
2 parents 1dd4ac9 + 047bb20 commit cf6f03d
Showing 1 changed file with 30 additions and 43 deletions.
73 changes: 30 additions & 43 deletions mobileweb/titanium/Ti/_/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ define(["Ti/_", "Ti/_/dom", "Ti/_/lang", "Ti/App", "Ti/Platform"], function(_, d

var global = window,
sessionId = sessionStorage.getItem("ti:sessionId"),
is = require.is,
cfg = require.config,
analyticsEnabled = App.analytics,
analyticsStorageName = "ti:analyticsEvents",
analyticsEventSeq = 0,
analyticsLastSent = null,
analyticsUrl = "https://api.appcelerator.net/p/v2/mobile-web-track";
analyticsUrl = "https://api.appcelerator.net/p/v2/mobile-web-track",
pending = {};

sessionId || sessionStorage.setItem("ti:sessionId", sessionId = _.uuid());

Expand All @@ -20,6 +22,21 @@ define(["Ti/_", "Ti/_/dom", "Ti/_/lang", "Ti/App", "Ti/Platform"], function(_, d
localStorage.setItem(analyticsStorageName, JSON.stringify(data));
}

function onSuccess(response) {
if (is(response.data, "Object") && response.data.success) {
var ids = pending[response.data.callback],
keepers = [];
if (ids) {
getStorage().forEach(function(evt) {
~ids.indexOf(evt.id) || keepers.push(evt);
});
setStorage(keepers);
}
}
}

require.on(global, "message", onSuccess);

return _.analytics = {

add: function(type, event, data, isUrgent) {
Expand All @@ -41,28 +58,26 @@ define(["Ti/_", "Ti/_/dom", "Ti/_/lang", "Ti/App", "Ti/Platform"], function(_, d
ts: now.toISOString().replace('Z', (tz < 0 ? '-' : '+') + (atz < 100 ? "00" : (atz < 1000 ? "0" : "")) + atz),
data: data
});

setStorage(storage);
this.send(isUrgent);
}
},

send: function(isUrgent) {
if (analyticsEnabled) {
var i,
evt,
storage = getStorage(),
var rand = Math.floor(Math.random() * 1e6),
now = (new Date()).getTime(),
jsonStrs = [],
ids = [];
ids = [],
jsonStrs = [];

if (storage === null || (!isUrgent && analyticsLastSent !== null && now - analyticsLastSent < 300000 /* 5 minutes */)) {
if (!isUrgent && analyticsLastSent !== null && now - analyticsLastSent < 60000 /* 1 minute */) {
return;
}

analyticsLastSent = now;

for (i = 0; i < storage.length; i++) {
evt = storage[i];
getStorage().forEach(function(evt) {
ids.push(evt.id);
jsonStrs.push(JSON.stringify({
id: evt.id,
Expand All @@ -76,40 +91,18 @@ define(["Ti/_", "Ti/_/dom", "Ti/_/lang", "Ti/App", "Ti/Platform"], function(_, d
deploytype: cfg.deployType,
sid: sessionId,
ts: evt.ts,
data: /(Array|Object)/.test(require.is(evt.data)) ? JSON.stringify(evt.data) : evt.data
data: /(Array|Object)/.test(is(evt.data)) ? JSON.stringify(evt.data) : evt.data
}));
}
});

function onSuccess() {
// remove sent events on successful sent
var j, k, found,
storage = getStorage(),
id,
evs = [];

for (j = 0; j < storage.length; j++) {
id = storage[j].id;
found = 0;
for (k = 0; k < ids.length; k++) {
if (id == ids[k]) {
found = 1;
ids.splice(k, 1);
break;
}
}
found || evs.push(ev);
}
pending[rand] = ids;

setStorage(evs);
}

if (require.has("ti-analytics-use-xhr")) {
if (require.has("analytics-use-xhr")) {
var xhr = new XmlHttpRequest;
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
try {
var response = eval('(' + xhr.responseText + ')');
response && response.success && onSuccess();
onSuccess({ data: eval('(' + xhr.responseText + ')') });
} catch (e) {}
}
};
Expand All @@ -118,9 +111,7 @@ define(["Ti/_", "Ti/_/dom", "Ti/_/lang", "Ti/App", "Ti/Platform"], function(_, d
xhr.send(lang.urlEncode({ content: jsonStrs }));
} else {
var body = document.body,
rand = Math.floor(Math.random() * 1e6),
iframeName = "analytics" + rand,
callback = "mobileweb_jsonp" + rand,
iframe = dom.create("iframe", {
id: iframeName,
name: iframeName,
Expand All @@ -129,7 +120,7 @@ define(["Ti/_", "Ti/_/dom", "Ti/_/lang", "Ti/App", "Ti/Platform"], function(_, d
}
}, body),
form = dom.create("form", {
action: analyticsUrl + "?callback=" + callback,
action: analyticsUrl + "?callback=" + rand + "&output=html",
method: "POST",
style: {
display: "none"
Expand All @@ -143,10 +134,6 @@ define(["Ti/_", "Ti/_/dom", "Ti/_/lang", "Ti/App", "Ti/Platform"], function(_, d
value: "[" + jsonStrs.join(",") + "]"
}, form);

global[callback] = function(response) {
response && response.success && onSuccess();
};

// need to delay attaching of iframe events so they aren't prematurely called
setTimeout(function() {
function onIframeLoaded() {
Expand Down

0 comments on commit cf6f03d

Please sign in to comment.