overleaf/services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee

43 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2016-08-10 15:42:56 +00:00
Settings = require "settings-sharelatex"
logger = require "logger-sharelatex"
_ = require "underscore"
if !Settings.analytics?.postgres?
module.exports =
recordEvent: (user_id, event, segmentation, callback = () ->) ->
logger.log {user_id, event, segmentation}, "no event tracking configured, logging event"
2016-08-10 15:42:56 +00:00
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,
2016-08-11 09:18:58 +00:00
segmentation: Sequelize.JSONB
2016-08-10 15:42:56 +00:00
})
module.exports =
recordEvent: (user_id, event, segmentation = {}, callback = (error) ->) ->
2016-08-10 15:42:56 +00:00
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, segmentation })
2016-08-10 15:42:56 +00:00
.then(
(result) -> callback(),
(error) ->
logger.err {err: error, user_id, event, segmentation}, "error recording analytics event"
2016-08-10 15:42:56 +00:00
callback(error)
)
sync: () -> sequelize.sync()