diff --git a/backend/models/analytics.js b/backend/models/analytics.js index 6d8d0d788..36543b9ef 100644 --- a/backend/models/analytics.js +++ b/backend/models/analytics.js @@ -4,7 +4,7 @@ const Schema = mongoose.Schema; const analyticsSchema = new Schema( { event: { type: String, required: true }, - data: { type: Schema.Types.Mixed, default: {} }, + data: { type: Schema.Types.Mixed }, }, { timestamps: true, diff --git a/backend/mongo-todo.md b/backend/mongo-todo.md index 943adde7a..a8782fff1 100644 --- a/backend/mongo-todo.md +++ b/backend/mongo-todo.md @@ -8,6 +8,11 @@ - Personal bests items should not be arrays - should be objects that are set on new pb - Move user data to localstorage instead of cookies +- Result is duplicated in analytics + + - Should results be moved to a seperate collection + + - Does the entire test need to be in the analytics collection? - make sure refresh token won't expire - make refresh token expire after session if don't remeber me is set? diff --git a/backend/server.js b/backend/server.js index 308a9e225..423e04bce 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1186,51 +1186,66 @@ app.post("/api/checkLeaderboards", (req, res) => { // ANALYTICS API -function newAnalyticsEvent() {} +function newAnalyticsEvent(event, data) { + let newEvent = { + event: event, + }; + if (data) newEvent.data = data; + const newEventObj = new Analytics(newEvent); + newEventObj.save(); +} app.post("/api/analytics/usedCommandLine", (req, res) => { //save command used from command line to analytics - const command = req.body.command; + newAnalyticsEvent("usedCommandLine", { command: req.body.command }); res.sendStatus(200); }); app.post("/api/analytics/changedLanguage", (req, res) => { //save what a user changed their language to - const language = req.body.language; + newAnalyticsEvent("changedLanguage", { language: req.body.language }); res.sendStatus(200); }); app.post("/api/analytics/changedTheme", (req, res) => { //save what a user changed their theme to - const theme = req.body.theme; + newAnalyticsEvent("changedTheme", { theme: req.body.theme }); res.sendStatus(200); }); app.post("/api/analytics/testStarted", (req, res) => { //log that a test was started + newAnalyticsEvent("testStarted"); res.sendStatus(200); }); app.post("/api/analytics/testStartedNoLogin", (req, res) => { //log that a test was started without login + newAnalyticsEvent("testStartedNoLogin"); res.sendStatus(200); }); app.post("/api/analytics/testCompleted", (req, res) => { //log that a test was completed - const completedEvent = req.body.completedEvent; + newAnalyticsEvent("testCompleted", { + completedEvent: req.body.completedEvent, + }); res.sendStatus(200); }); app.post("/api/analytics/testCompletedNoLogin", (req, res) => { //log that a test was completed and user was not logged in - const completedEvent = req.body.completedEvent; + newAnalyticsEvent("testCompletedNoLogin", { + completedEvent: req.body.completedEvent, + }); res.sendStatus(200); }); app.post("/api/analytics/testCompletedInvalid", (req, res) => { //log that a test was completed and is invalid - const completedEvent = req.body.completedEvent; + newAnalyticsEvent("testCompletedInvalid", { + completedEvent: req.body.completedEvent, + }); res.sendStatus(200); });