AnalyticsController.coffee 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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. countryCode = null
  10. if userId?
  11. GeoIpLookup.getDetails req.ip, (err, geoDetails) ->
  12. if geoDetails?.country_code? and geoDetails.country_code != ""
  13. countryCode = geoDetails.country_code
  14. AnalyticsManager.updateEditingSession userId, projectId, countryCode, (error) ->
  15. respondWith(error, res, next)
  16. else
  17. res.send 204
  18. recordEvent: (req, res, next) ->
  19. user_id = AuthenticationController.getLoggedInUserId(req) or req.sessionID
  20. AnalyticsManager.recordEvent user_id, req.params.event, req.body, (error) ->
  21. respondWith(error, res, next)
  22. respondWith = (error, res, next) ->
  23. if error instanceof Errors.ServiceNotConfiguredError
  24. # ignore, no-op
  25. res.send(204)
  26. else if error?
  27. next(error)
  28. else
  29. res.send 204