Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 2ecd91c

Browse files
NehaModinethip
authored andcommitted
Code to send Analytics Data to server (#14057)
* Code to send Analytics Data to server * Addressing the review comments * changes done to handle stage server and Prod Server * changes done to define stage URL and Prod URL * changes done to call analytics data server after health data server * as server is change from Stage to Production ,environment variable needs to be changed to production * Refactor code and cleanup * Add force flag for testing and handle dev/stage or production enviroment * Exposing command to explicitly test health data sending capability * Handle stage environment * Clean config json * Correcting the sequence of call
1 parent 453e927 commit 2ecd91c

5 files changed

Lines changed: 88 additions & 17 deletions

File tree

src/brackets.config.dev.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"healthDataServerURL" : "https://healthdev.brackets.io/healthDataLog"
2+
"healthDataServerURL" : "https://healthdev.brackets.io/healthDataLog",
3+
"analyticsDataServerURL" : "https://cc-api-data-stage.adobe.io/ingest",
4+
"serviceKey" : "brackets-service",
5+
"environment" : "stage"
36
}

src/brackets.config.dist.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"healthDataServerURL" : "https://health.brackets.io/healthDataLog"
2+
"healthDataServerURL" : "https://health.brackets.io/healthDataLog",
3+
"analyticsDataServerURL" : "https://cc-api-data.adobe.io/ingest",
4+
"serviceKey" : "brackets-service",
5+
"environment" : "production"
36
}

src/config.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
"extension_url": "https://s3.amazonaws.com/extend.brackets/{0}/{0}-{1}.zip",
2121
"linting.enabled_by_default": true,
2222
"build_timestamp": "",
23-
"healthDataServerURL": "https://healthdev.brackets.io/healthDataLog"
23+
"healthDataServerURL": "https://healthdev.brackets.io/healthDataLog",
24+
"analyticsDataServerURL": "https://cc-api-data-stage.adobe.io/ingest",
25+
"serviceKey": "brackets-service",
26+
"environment": "stage"
2427
},
2528
"name": "Brackets",
2629
"version": "1.12.0-0",
@@ -96,4 +99,4 @@
9699
"url": "https://github.com/adobe/brackets/blob/master/LICENSE"
97100
}
98101
]
99-
}
102+
}

src/extensions/default/HealthData/HealthDataManager.js

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
*
2222
*/
2323

24-
/*global define, $, brackets,navigator, console, appshell */
24+
/*global define, $, brackets, console, appshell */
2525
define(function (require, exports, module) {
2626
"use strict";
27-
2827
var AppInit = brackets.getModule("utils/AppInit"),
28+
CommandManager = brackets.getModule("command/CommandManager"),
2929
HealthLogger = brackets.getModule("utils/HealthLogger"),
3030
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
3131
UrlParams = brackets.getModule("utils/UrlParams").UrlParams,
@@ -59,7 +59,6 @@ define(function (require, exports, module) {
5959
oneTimeHealthData.bracketsLanguage = brackets.getLocale();
6060
oneTimeHealthData.bracketsVersion = brackets.metadata.version;
6161
$.extend(oneTimeHealthData, HealthLogger.getAggregatedHealthData());
62-
6362
HealthDataUtils.getUserInstalledExtensions()
6463
.done(function (userInstalledExtensions) {
6564
oneTimeHealthData.installedExtensions = userInstalledExtensions;
@@ -121,17 +120,42 @@ define(function (require, exports, module) {
121120

122121
PreferencesManager.setViewState("UUID", oneTimeHealthData.uuid);
123122
PreferencesManager.setViewState("OlderUUID", oneTimeHealthData.olderuuid);
124-
125123
return result.resolve(oneTimeHealthData);
126124
}
127125
}
128126
});
129127

130128
});
131-
132129
return result.promise();
133130
}
134131

132+
// Get Analytics data
133+
function getAnalyticsData() {
134+
var userUuid = PreferencesManager.getViewState("UUID"),
135+
olderUuid = PreferencesManager.getViewState("OlderUUID");
136+
137+
return {
138+
project: brackets.config.serviceKey,
139+
environment: brackets.config.environment,
140+
time: new Date().toISOString(),
141+
ingesttype: "dunamis",
142+
data: {
143+
"event.guid": uuid.v4(),
144+
"event.user_guid": olderUuid || userUuid,
145+
"event.dts_end": new Date().toISOString(),
146+
"event.category": "pingData",
147+
"event.subcategory": "",
148+
"event.type": "",
149+
"event.subtype": "",
150+
"event.user_agent": window.navigator.userAgent || "",
151+
"event.language": brackets.app.language,
152+
"source.name": brackets.metadata.version,
153+
"source.platform": brackets.platform,
154+
"source.version": brackets.metadata.version
155+
}
156+
};
157+
}
158+
135159
/**
136160
* Send data to the server
137161
*/
@@ -165,13 +189,39 @@ define(function (require, exports, module) {
165189
return result.promise();
166190
}
167191

192+
// Send Analytics data to Server
193+
function sendAnalyticsDataToServer() {
194+
var result = new $.Deferred();
195+
196+
var analyticsData = getAnalyticsData();
197+
$.ajax({
198+
url: brackets.config.analyticsDataServerURL,
199+
type: "POST",
200+
data: JSON.stringify({events: [analyticsData]}),
201+
headers: {
202+
"Content-Type": "application/json",
203+
"x-api-key": brackets.config.serviceKey
204+
}
205+
})
206+
.done(function () {
207+
result.resolve();
208+
})
209+
.fail(function (jqXHR, status, errorThrown) {
210+
console.error("Error in sending Adobe Analytics Data. Response : " + jqXHR.responseText + ". Status : " + status + ". Error : " + errorThrown);
211+
result.reject();
212+
});
213+
214+
return result.promise();
215+
}
216+
168217
/*
169218
* Check if the Health Data is to be sent to the server. If the user has enabled tracking, Health Data will be sent once every 24 hours.
170219
* Send Health Data to the server if the period is more than 24 hours.
171220
* We are sending the data as soon as the user launches brackets. The data will be sent to the server only after the notification dialog
172221
* for opt-out/in is closed.
222+
@param forceSend Flag for sending analytics data for testing purpose
173223
*/
174-
function checkHealthDataSend() {
224+
function checkHealthDataSend(forceSend) {
175225
var result = new $.Deferred(),
176226
isHDTracking = prefs.get("healthDataTracking"),
177227
nextTimeToSend,
@@ -191,12 +241,11 @@ define(function (require, exports, module) {
191241
// don't return yet though - still want to set the timeout below
192242
}
193243

194-
if (currentTime >= nextTimeToSend) {
195-
// Bump up nextHealthDataSendTime now to avoid any chance of sending data again before 24 hours, e.g. if the server request fails
196-
// or the code below crashes
244+
if (currentTime >= nextTimeToSend || forceSend) {
245+
// Bump up nextHealthDataSendTime at the begining of chaining to avoid any chance of sending data again before 24 hours, // e.g. if the server request fails or the code below crashes
197246
PreferencesManager.setViewState("nextHealthDataSendTime", currentTime + ONE_DAY);
198-
199-
sendHealthDataToServer()
247+
sendHealthDataToServer().always(function() {
248+
sendAnalyticsDataToServer()
200249
.done(function () {
201250
// We have already sent the health data, so can clear all health data
202251
// Logged till now
@@ -209,7 +258,7 @@ define(function (require, exports, module) {
209258
.always(function () {
210259
timeoutVar = setTimeout(checkHealthDataSend, ONE_DAY);
211260
});
212-
261+
});
213262
} else {
214263
timeoutVar = setTimeout(checkHealthDataSend, nextTimeToSend - currentTime);
215264
result.reject();
@@ -221,6 +270,15 @@ define(function (require, exports, module) {
221270
return result.promise();
222271
}
223272

273+
// Expose a command to test data sending capability, but limit it to dev environment only
274+
CommandManager.register("Sends health data and Analytics data for testing purpose", "sendHealthData", function() {
275+
if (brackets.config.environment === "stage") {
276+
return checkHealthDataSend(true);
277+
} else {
278+
return $.Deferred().reject().promise();
279+
}
280+
});
281+
224282
prefs.on("change", "healthDataTracking", function () {
225283
checkHealthDataSend();
226284
});
@@ -238,5 +296,6 @@ define(function (require, exports, module) {
238296
});
239297

240298
exports.getHealthData = getHealthData;
299+
exports.getAnalyticsData = getAnalyticsData;
241300
exports.checkHealthDataSend = checkHealthDataSend;
242301
});

src/extensions/default/HealthData/HealthDataPreview.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ define(function (require, exports, module) {
4545
var result = new $.Deferred();
4646

4747
HealthDataManager.getHealthData().done(function (healthDataObject) {
48-
var content = JSON.stringify(healthDataObject, null, 4);
48+
var combinedHealthAnalyticsData = HealthDataManager.getAnalyticsData("pingData", "", "", ""),
49+
content;
50+
combinedHealthAnalyticsData = [healthDataObject, combinedHealthAnalyticsData ];
51+
content = JSON.stringify(combinedHealthAnalyticsData, null, 4);
4952
content = _.escape(content);
5053
content = content.replace(/ /g, " ");
5154
content = content.replace(/(?:\r\n|\r|\n)/g, "<br />");

0 commit comments

Comments
 (0)