fix(analytics): allow tracking static pages events when the user is not authenticated

This commit is contained in:
Matteo Pagliazzi 2020-11-29 20:15:12 +01:00
parent 103b4cb8a8
commit 6b3a6eb59f
2 changed files with 2 additions and 11 deletions

View file

@ -1,19 +1,10 @@
import {
generateUser,
requester,
translate as t,
} from '../../../../helpers/api-integration/v3';
import { mockAnalyticsService as analytics } from '../../../../../website/server/libs/analyticsService';
describe('POST /analytics/track/:eventName', () => {
it('requires authentication', async () => {
await expect(requester().post('/analytics/track/event')).to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('missingAuthHeaders'),
});
});
it('calls res.analytics', async () => {
const user = await generateUser();
sandbox.spy(analytics, 'track');

View file

@ -19,7 +19,7 @@ api.trackEvent = {
method: 'POST',
url: '/analytics/track/:eventName',
// we authenticate these requests to make sure they actually came from a real user
middlewares: [authWithHeaders()],
middlewares: [authWithHeaders({ optional: true })],
async handler (req, res) {
// As of now only web can track events using this route
if (req.headers['x-client'] !== 'habitica-web') {
@ -30,7 +30,7 @@ api.trackEvent = {
const eventProperties = req.body;
res.analytics.track(req.params.eventName, {
uuid: user._id,
uuid: user ? user._id : null,
headers: req.headers,
category: 'behaviour',
gaLabel: 'local',