diff --git a/src/extensions/default/HealthData/HealthDataManager.js b/src/extensions/default/HealthData/HealthDataManager.js index a452a4e5471..e6a72b5a181 100644 --- a/src/extensions/default/HealthData/HealthDataManager.js +++ b/src/extensions/default/HealthData/HealthDataManager.js @@ -308,22 +308,16 @@ define(function (require, exports, module) { isHDTracking = prefs.get("healthDataTracking"), isEventDataAlreadySent; - var options = { - location: { - scope: "default" - } - }; - if (isHDTracking) { - isEventDataAlreadySent = PreferencesManager.getViewState(Eventparams.eventName); - PreferencesManager.setViewState(Eventparams.eventName, 1, options); + isEventDataAlreadySent = HealthLogger.analyticsEventMap.get(Eventparams.eventName); + HealthLogger.analyticsEventMap.set(Eventparams.eventName, true); if (!isEventDataAlreadySent || forceSend) { sendAnalyticsDataToServer(Eventparams) .done(function () { - PreferencesManager.setViewState(Eventparams.eventName, 1, options); + HealthLogger.analyticsEventMap.set(Eventparams.eventName, true); result.resolve(); }).fail(function () { - PreferencesManager.setViewState(Eventparams.eventName, 0, options); + HealthLogger.analyticsEventMap.set(Eventparams.eventName, false); result.reject(); }); } else { @@ -336,6 +330,17 @@ define(function (require, exports, module) { return result.promise(); } + /** + * This function is auto called after 24 hours to empty the map + * Map is used to make sure that we send an event only once per 24 hours + **/ + + function emptyAnalyticsMap() { + HealthLogger.analyticsEventMap.clear(); + setTimeout(emptyAnalyticsMap, ONE_DAY); + } + setTimeout(emptyAnalyticsMap, ONE_DAY); + // Expose a command to test data sending capability, but limit it to dev environment only CommandManager.register("Sends health data and Analytics data for testing purpose", "sendHealthData", function() { if (brackets.config.environment === "stage") { diff --git a/src/utils/HealthLogger.js b/src/utils/HealthLogger.js index 3fd5e3d7e74..1ac31aa182a 100644 --- a/src/utils/HealthLogger.js +++ b/src/utils/HealthLogger.js @@ -24,6 +24,7 @@ /** * Utilities functions related to Health Data logging */ +/*global Map*/ define(function (require, exports, module) { "use strict"; @@ -36,7 +37,8 @@ define(function (require, exports, module) { EventDispatcher = require("utils/EventDispatcher"), HEALTH_DATA_STATE_KEY = "HealthData.Logs", - logHealthData = true; + logHealthData = true, + analyticsEventMap = new Map(); var commonStrings = { USAGE: "usage", FILE_OPEN: "fileOpen", @@ -319,9 +321,10 @@ define(function (require, exports, module) { * needs to be logged- should be a js var compatible string */ function sendAnalyticsData(eventName, eventCategory, eventSubCategory, eventType, eventSubType) { - var isEventDataAlreadySent = PreferencesManager.getViewState(eventName), + var isEventDataAlreadySent = analyticsEventMap.get(eventName), isHDTracking = PreferencesManager.getExtensionPrefs("healthData").get("healthDataTracking"), eventParams = {}; + if (isHDTracking && !isEventDataAlreadySent && eventName && eventCategory) { eventParams = { eventName: eventName, @@ -363,4 +366,5 @@ define(function (require, exports, module) { // A new search context on search bar up-Gives an idea of number of times user did a discrete search exports.SEARCH_NEW = "searchNew"; exports.commonStrings = commonStrings; + exports.analyticsEventMap = analyticsEventMap; });