mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-04 07:55:33 +00:00
Send events to custom DB backend
This commit is contained in:
parent
abcfb2dd16
commit
93cd511211
5 changed files with 76 additions and 2 deletions
|
@ -0,0 +1,7 @@
|
|||
AnalyticsManager = require "./AnalyticsManager"
|
||||
|
||||
module.exports = AnalyticsController =
|
||||
recordEvent: (req, res, next) ->
|
||||
AnalyticsManager.recordEvent req.session?.user?._id, req.params.event, req.body, (error) ->
|
||||
return next(error) if error?
|
||||
res.send 204
|
|
@ -0,0 +1,45 @@
|
|||
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.STRING
|
||||
})
|
||||
|
||||
module.exports =
|
||||
recordEvent: (user_id, event, metadata = {}, callback = (error) ->) ->
|
||||
if typeof(metadata) != "string"
|
||||
metadata = JSON.stringify(metadata)
|
||||
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()
|
|
@ -38,6 +38,7 @@ ContactRouter = require("./Features/Contacts/ContactRouter")
|
|||
ReferencesController = require('./Features/References/ReferencesController')
|
||||
AuthorizationMiddlewear = require('./Features/Authorization/AuthorizationMiddlewear')
|
||||
BetaProgramController = require('./Features/BetaProgram/BetaProgramController')
|
||||
AnalyticsRouter = require('./Features/Analytics/AnalyticsRouter')
|
||||
|
||||
logger = require("logger-sharelatex")
|
||||
_ = require("underscore")
|
||||
|
@ -68,7 +69,8 @@ module.exports = class Router
|
|||
StaticPagesRouter.apply(webRouter, apiRouter)
|
||||
RealTimeProxyRouter.apply(webRouter, apiRouter)
|
||||
ContactRouter.apply(webRouter, apiRouter)
|
||||
|
||||
AnalyticsRouter.apply(webRouter, apiRouter)
|
||||
|
||||
Modules.applyRouter(webRouter, apiRouter)
|
||||
|
||||
|
||||
|
@ -280,4 +282,4 @@ module.exports = class Router
|
|||
metrics.inc("client-side-error")
|
||||
res.sendStatus(204)
|
||||
|
||||
webRouter.get '*', ErrorController.notFound
|
||||
webRouter.get '*', ErrorController.notFound
|
||||
|
|
|
@ -46,6 +46,8 @@
|
|||
"nodemailer-sendgrid-transport": "^0.2.0",
|
||||
"nodemailer-ses-transport": "^1.3.0",
|
||||
"optimist": "0.6.1",
|
||||
"pg": "^6.0.3",
|
||||
"pg-hstore": "^2.3.2",
|
||||
"redback": "0.4.0",
|
||||
"redis": "0.10.1",
|
||||
"redis-sharelatex": "0.0.9",
|
||||
|
@ -53,6 +55,7 @@
|
|||
"requests": "^0.1.7",
|
||||
"rimraf": "2.2.6",
|
||||
"sanitizer": "0.1.1",
|
||||
"sequelize": "^3.2.0",
|
||||
"settings-sharelatex": "git+https://github.com/sharelatex/settings-sharelatex.git#v1.0.0",
|
||||
"sixpack-client": "^1.0.0",
|
||||
"temp": "^0.8.3",
|
||||
|
|
|
@ -3,6 +3,11 @@ define [
|
|||
"modules/localStorage"
|
||||
], (App) ->
|
||||
CACHE_KEY = "countlyEvents"
|
||||
send = (category, action, attributes = {})->
|
||||
ga('send', 'event', category, action)
|
||||
event_name = "#{action}-#{category}"
|
||||
Intercom?("trackEvent", event_name, attributes)
|
||||
|
||||
|
||||
App.factory "event_tracking", (localStorage) ->
|
||||
_getEventCache = () ->
|
||||
|
@ -41,6 +46,18 @@ define [
|
|||
if ! _eventInCache(key)
|
||||
_addEventToCache(key)
|
||||
@sendCountly key, segmentation
|
||||
|
||||
send: (category, action, attributes = {})->
|
||||
event_name = "#{action}-#{category}"
|
||||
$.ajax {
|
||||
url: "/event/#{event_name}",
|
||||
method: "POST",
|
||||
data: attributes,
|
||||
dataType: "json",
|
||||
headers: {
|
||||
"X-CSRF-Token": window.csrfToken
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# App.directive "countlyTrack", () ->
|
||||
|
|
Loading…
Reference in a new issue