overleaf/services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee
2016-08-10 17:17:59 +01:00

43 lines
No EOL
1.2 KiB
CoffeeScript

Settings = require "settings-sharelatex"
logger = require "logger-sharelatex"
_ = require "underscore"
if !Settings.analytics?.postgres?
module.exports =
recordEvent: (user_id, event, metadata, callback = () ->) ->
logger.log {user_id, event, metadata}, "no event tracking configured, logging event"
callback()
else
Sequelize = require "sequelize"
options = _.extend {logging:false}, Settings.analytics.postgres
sequelize = new Sequelize(
Settings.analytics.postgres.database,
Settings.analytics.postgres.username,
Settings.analytics.postgres.password,
options
)
Event = sequelize.define("Event", {
user_id: Sequelize.STRING,
event: Sequelize.STRING,
metadata: Sequelize.JSON
})
module.exports =
recordEvent: (user_id, event, metadata = {}, callback = (error) ->) ->
if user_id? and typeof(user_id) != "string"
user_id = user_id.toString()
if user_id == Settings.smokeTest?.userId
# Don't record smoke tests analytics
return callback()
Event
.create({ user_id, event, metadata })
.then(
(result) -> callback(),
(error) ->
logger.err {err: error, user_id, event, metadata}, "error recording analytics event"
callback(error)
)
sync: () -> sequelize.sync()