Files
habitica/website/server/controllers/top-level/analytics.js
Matteo Pagliazzi 48dbe547c0 Analytics: track generic events through the server (#12735)
* Loggly-only user support events (#12676)

* feat(analytics): Loggly-only user support events

* fix(analytics): clean up more Unknowns

* wip: allow tracking events from the server

* analytics: allow tracking generic events on the server

* remove console.logs

* remove console.log (client)

* add integration test

Co-authored-by: Sabe Jones <sabrecat@gmail.com>
2020-10-28 22:39:19 +01:00

48 lines
1.3 KiB
JavaScript

import {
NotAuthorized,
} from '../../libs/errors';
import {
authWithHeaders,
} from '../../middlewares/auth';
const api = {};
/**
* @apiIgnore Analytics are considered part of the private API
* @api {post} /analytics/track/:eventName Track a generic analytics event
* @apiName AnalyticsTrack
* @apiGroup Analytics
*
* @apiSuccess {Object} data An empty object
* */
api.trackEvent = {
method: 'POST',
url: '/analytics/track/:eventName',
// we authenticate these requests to make sure they actually came from a real user
middlewares: [authWithHeaders()],
async handler (req, res) {
// As of now only web can track events using this route
if (req.headers['x-client'] !== 'habitica-web') {
throw new NotAuthorized('Only habitica.com is allowed to track analytics events.');
}
const { user } = res.locals;
const eventProperties = req.body;
res.analytics.track(req.params.eventName, {
uuid: user._id,
headers: req.headers,
category: 'behaviour',
gaLabel: 'local',
// hitType: 'event', sent from the client
...eventProperties,
});
// not using res.respond
// because we don't want to send back notifications and other user-related data
res.status(200).send({});
},
};
export default api;