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

81 lines
1.9 KiB
CoffeeScript
Raw Normal View History

settings = require "settings-sharelatex"
2016-08-10 11:42:56 -04:00
logger = require "logger-sharelatex"
_ = require "underscore"
2018-02-12 10:16:21 -05:00
request = require "requestretry"
Errors = require '../Errors/Errors'
2016-08-10 11:42:56 -04:00
makeRequest = (opts, callback)->
if settings.apis?.analytics?.url?
urlPath = opts.url
opts.url = "#{settings.apis.analytics.url}#{urlPath}"
2018-02-12 10:16:21 -05:00
request opts, callback
else
callback(new Errors.ServiceNotConfiguredError('Analytics service not configured'))
2016-08-10 11:42:56 -04:00
module.exports =
2017-03-22 12:01:26 -04:00
identifyUser: (user_id, old_user_id, callback = (error)->)->
opts =
body:
old_user_id:old_user_id
json:true
method:"POST"
timeout:1000
2017-03-22 11:50:49 -04:00
url: "/user/#{user_id}/identify"
makeRequest opts, callback
recordEvent: (user_id, event, segmentation = {}, callback = (error) ->) ->
2016-11-28 14:19:00 -05:00
if user_id+"" == settings.smokeTest?.userId+""
return callback()
opts =
body:
event:event
segmentation:segmentation
json:true
method:"POST"
timeout:1000
url: "/user/#{user_id}/event"
2018-02-12 10:16:21 -05:00
maxAttempts: 20
retryDelay: 5000
if settings.overleaf?
opts.qs = {fromV2: 1}
makeRequest opts, callback
2018-01-22 11:20:08 -05:00
updateEditingSession: (userId, projectId, segmentation = {}, callback = (error) ->) ->
if userId+"" == settings.smokeTest?.userId+""
return callback()
opts =
body:
segmentation: segmentation
json: true
method: "PUT"
timeout: 1000
2018-01-23 07:47:51 -05:00
url: "/editingSession"
qs:
userId: userId
projectId: projectId
2018-02-12 10:16:21 -05:00
maxAttempts: 20
retryDelay: 5000
if settings.overleaf?
opts.qs.fromV2 = 1
makeRequest opts, callback
getLastOccurrence: (user_id, event, callback = (error) ->) ->
opts =
body:
event:event
json:true
method:"POST"
timeout:1000
url: "/user/#{user_id}/event/last_occurrence"
makeRequest opts, (err, response, body)->
2018-01-22 10:00:56 -05:00
if err?
console.log response, opts
logger.err {user_id, err}, "error getting last occurance of event"
return callback err
else
return callback null, body