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
|
|
|
|
2021-03-31 05:24:39 -04:00
|
|
|
const analyticsEventsQueue = Queues.getAnalyticsEventsQueue()
|
|
|
|
const analyticsEditingSessionsQueue = Queues.getAnalyticsEditingSessionsQueue()
|
2021-05-19 04:29:39 -04:00
|
|
|
const analyticsUserPropertiesQueue = Queues.getAnalyticsUserPropertiesQueue()
|
2021-03-31 05:24:39 -04:00
|
|
|
|
2020-11-04 08:35:37 -05:00
|
|
|
function identifyUser(userId, oldUserId) {
|
2021-06-24 05:34:26 -04:00
|
|
|
if (!userId || !oldUserId) {
|
|
|
|
return
|
|
|
|
}
|
2020-11-04 08:35:37 -05:00
|
|
|
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' })
|
2021-03-31 05:24:39 -04:00
|
|
|
analyticsEventsQueue
|
2020-11-04 08:35:37 -05:00
|
|
|
.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) {
|
2021-06-24 05:34:26 -04:00
|
|
|
if (!userId) {
|
|
|
|
return
|
|
|
|
}
|
2020-11-04 08:35:37 -05:00
|
|
|
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' })
|
2021-03-31 05:24:39 -04:00
|
|
|
analyticsEventsQueue
|
2020-11-04 08:35:37 -05:00
|
|
|
.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) {
|
2021-06-24 05:34:26 -04:00
|
|
|
if (!userId) {
|
|
|
|
return
|
|
|
|
}
|
2020-11-04 08:35:37 -05:00
|
|
|
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',
|
2021-04-27 03:52:58 -04:00
|
|
|
event_type: 'editing-session',
|
2020-11-04 08:35:37 -05:00
|
|
|
})
|
2021-03-31 05:24:39 -04:00
|
|
|
analyticsEditingSessionsQueue
|
2020-11-04 08:35:37 -05:00
|
|
|
.add({ userId, projectId, countryCode })
|
|
|
|
.then(() => {
|
|
|
|
Metrics.analyticsQueue.inc({
|
|
|
|
status: 'added',
|
2021-04-27 03:52:58 -04:00
|
|
|
event_type: 'editing-session',
|
2020-11-04 08:35:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
Metrics.analyticsQueue.inc({
|
|
|
|
status: 'error',
|
2021-04-27 03:52:58 -04:00
|
|
|
event_type: 'editing-session',
|
2020-11-04 08:35:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-19 04:29:39 -04:00
|
|
|
function setUserProperty(userId, propertyName, propertyValue) {
|
2021-06-24 05:34:26 -04:00
|
|
|
if (!userId) {
|
|
|
|
return
|
|
|
|
}
|
2021-05-19 04:29:39 -04:00
|
|
|
if (isAnalyticsDisabled() || isSmokeTestUser(userId)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Metrics.analyticsQueue.inc({
|
|
|
|
status: 'adding',
|
|
|
|
event_type: 'user-property',
|
|
|
|
})
|
|
|
|
analyticsUserPropertiesQueue
|
|
|
|
.add({ userId, propertyName, propertyValue })
|
|
|
|
.then(() => {
|
|
|
|
Metrics.analyticsQueue.inc({
|
|
|
|
status: 'added',
|
|
|
|
event_type: 'user-property',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
Metrics.analyticsQueue.inc({
|
|
|
|
status: 'error',
|
|
|
|
event_type: 'user-property',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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,
|
2021-04-27 03:52:58 -04:00
|
|
|
updateEditingSession,
|
2021-05-19 04:29:39 -04:00
|
|
|
setUserProperty,
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|