2020-11-04 08:35:37 -05:00
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const Metrics = require('../../infrastructure/Metrics')
|
|
|
|
const Queues = require('../../infrastructure/Queues')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
function identifyUser(userId, oldUserId) {
|
|
|
|
if (isAnalyticsDisabled() || isSmokeTestUser(userId)) {
|
|
|
|
return
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-11-04 08:35:37 -05:00
|
|
|
Metrics.analyticsQueue.inc({ status: 'adding', event_type: 'identify' })
|
|
|
|
Queues.analytics.events
|
|
|
|
.add('identify', { userId, oldUserId })
|
|
|
|
.then(() => {
|
|
|
|
Metrics.analyticsQueue.inc({ status: 'added', event_type: 'identify' })
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
Metrics.analyticsQueue.inc({ status: 'error', event_type: 'identify' })
|
|
|
|
})
|
2019-06-21 05:57:43 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
function recordEvent(userId, event, segmentation) {
|
|
|
|
if (isAnalyticsDisabled() || isSmokeTestUser(userId)) {
|
|
|
|
return
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-11-04 08:35:37 -05:00
|
|
|
Metrics.analyticsQueue.inc({ status: 'adding', event_type: 'event' })
|
|
|
|
Queues.analytics.events
|
|
|
|
.add('event', { userId, event, segmentation })
|
|
|
|
.then(() => {
|
|
|
|
Metrics.analyticsQueue.inc({ status: 'added', event_type: 'event' })
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
Metrics.analyticsQueue.inc({ status: 'error', event_type: 'event' })
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
function updateEditingSession(userId, projectId, countryCode) {
|
|
|
|
if (isAnalyticsDisabled() || isSmokeTestUser(userId)) {
|
|
|
|
return
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-11-04 08:35:37 -05:00
|
|
|
Metrics.analyticsQueue.inc({
|
|
|
|
status: 'adding',
|
|
|
|
event_type: 'editing-session'
|
|
|
|
})
|
|
|
|
Queues.analytics.editingSessions
|
|
|
|
.add({ userId, projectId, countryCode })
|
|
|
|
.then(() => {
|
|
|
|
Metrics.analyticsQueue.inc({
|
|
|
|
status: 'added',
|
|
|
|
event_type: 'editing-session'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
Metrics.analyticsQueue.inc({
|
|
|
|
status: 'error',
|
|
|
|
event_type: 'editing-session'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
function isSmokeTestUser(userId) {
|
|
|
|
const smokeTestUserId = Settings.smokeTest && Settings.smokeTest.userId
|
|
|
|
return smokeTestUserId != null && userId.toString() === smokeTestUserId
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
function isAnalyticsDisabled() {
|
|
|
|
return !(Settings.analytics && Settings.analytics.enabled)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-11-04 08:35:37 -05:00
|
|
|
identifyUser,
|
|
|
|
recordEvent,
|
|
|
|
updateEditingSession
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|