diff --git a/test/api/v3/integration/user/POST-user_sleep.test.js b/test/api/v3/integration/user/POST-user_sleep.test.js index 0e9773150e..292b77e9db 100644 --- a/test/api/v3/integration/user/POST-user_sleep.test.js +++ b/test/api/v3/integration/user/POST-user_sleep.test.js @@ -1,6 +1,7 @@ import { generateUser, } from '../../../../helpers/api-integration/v3'; +import { mockAnalyticsService as analytics } from '../../../../../website/server/libs/analyticsService'; describe('POST /user/sleep', () => { let user; @@ -22,4 +23,15 @@ describe('POST /user/sleep', () => { await user.sync(); expect(user.preferences.sleep).to.be.false; }); + + it('sends sleep status to analytics service', async () => { + sandbox.spy(analytics, 'track'); + + await user.post('/user/sleep'); + await user.sync(); + expect(analytics.track).to.be.calledOnce; + expect(analytics.track).to.be.calledWith('sleep', sandbox.match.has('status', user.preferences.sleep)); + + sandbox.restore(); + }); }); diff --git a/website/common/script/ops/sleep.js b/website/common/script/ops/sleep.js index c84994c294..e80f766b0a 100644 --- a/website/common/script/ops/sleep.js +++ b/website/common/script/ops/sleep.js @@ -1,5 +1,14 @@ -module.exports = function sleep (user) { +module.exports = function sleep (user, req = {}, analytics) { user.preferences.sleep = !user.preferences.sleep; + if (analytics) { + analytics.track('sleep', { + uuid: user._id, + status: user.preferences.sleep, + category: 'behavior', + headers: req.headers, + }); + } + return [user.preferences.sleep]; }; diff --git a/website/server/controllers/api-v3/user.js b/website/server/controllers/api-v3/user.js index 0f7d1b1017..8b460e486b 100644 --- a/website/server/controllers/api-v3/user.js +++ b/website/server/controllers/api-v3/user.js @@ -729,7 +729,7 @@ api.sleep = { url: '/user/sleep', async handler (req, res) { let user = res.locals.user; - let sleepRes = common.ops.sleep(user); + let sleepRes = common.ops.sleep(user, req, res.analytics); await user.save(); res.respond(200, ...sleepRes); }, @@ -787,7 +787,6 @@ api.buy = { let quantity = 1; if (req.body.quantity) quantity = req.body.quantity; req.quantity = quantity; - buyRes = common.ops.buy(user, req, res.analytics); await user.save();