AnalyticsController.coffee 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. AnalyticsManager = require "./AnalyticsManager"
  2. Errors = require "../Errors/Errors"
  3. AuthenticationController = require("../Authentication/AuthenticationController")
  4. GeoIpLookup = require '../../infrastructure/GeoIpLookup'
  5. module.exports = AnalyticsController =
  6. updateEditingSession: (req, res, next) ->
  7. userId = AuthenticationController.getLoggedInUserId(req)
  8. projectId = req.params.projectId
  9. if userId?
  10. GeoIpLookup.getDetails req.ip, (err, geoDetails) ->
  11. countryCode = geoDetails?.country_code || null
  12. AnalyticsManager.updateEditingSession userId, projectId, countryCode, (error) ->
  13. respondWith(error, res, next)
  14. else
  15. res.send 204
  16. recordEvent: (req, res, next) ->
  17. user_id = AuthenticationController.getLoggedInUserId(req) or req.sessionID
  18. AnalyticsManager.recordEvent user_id, req.params.event, req.body, (error) ->
  19. respondWith(error, res, next)
  20. respondWith = (error, res, next) ->
  21. if error instanceof Errors.ServiceNotConfiguredError
  22. # ignore, no-op
  23. res.send(204)
  24. else if error?
  25. next(error)
  26. else
  27. res.send 204