AnalyticsManager.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const Settings = require('@overleaf/settings')
  2. const Metrics = require('../../infrastructure/Metrics')
  3. const Queues = require('../../infrastructure/Queues')
  4. const analyticsEventsQueue = Queues.getAnalyticsEventsQueue()
  5. const analyticsEditingSessionsQueue = Queues.getAnalyticsEditingSessionsQueue()
  6. const analyticsUserPropertiesQueue = Queues.getAnalyticsUserPropertiesQueue()
  7. function identifyUser(userId, oldUserId) {
  8. if (!userId || !oldUserId) {
  9. return
  10. }
  11. if (isAnalyticsDisabled() || isSmokeTestUser(userId)) {
  12. return
  13. }
  14. Metrics.analyticsQueue.inc({ status: 'adding', event_type: 'identify' })
  15. analyticsEventsQueue
  16. .add('identify', { userId, oldUserId })
  17. .then(() => {
  18. Metrics.analyticsQueue.inc({ status: 'added', event_type: 'identify' })
  19. })
  20. .catch(() => {
  21. Metrics.analyticsQueue.inc({ status: 'error', event_type: 'identify' })
  22. })
  23. }
  24. function recordEvent(userId, event, segmentation) {
  25. if (!userId) {
  26. return
  27. }
  28. if (isAnalyticsDisabled() || isSmokeTestUser(userId)) {
  29. return
  30. }
  31. Metrics.analyticsQueue.inc({ status: 'adding', event_type: 'event' })
  32. analyticsEventsQueue
  33. .add('event', { userId, event, segmentation })
  34. .then(() => {
  35. Metrics.analyticsQueue.inc({ status: 'added', event_type: 'event' })
  36. })
  37. .catch(() => {
  38. Metrics.analyticsQueue.inc({ status: 'error', event_type: 'event' })
  39. })
  40. }
  41. function updateEditingSession(userId, projectId, countryCode) {
  42. if (!userId) {
  43. return
  44. }
  45. if (isAnalyticsDisabled() || isSmokeTestUser(userId)) {
  46. return
  47. }
  48. Metrics.analyticsQueue.inc({
  49. status: 'adding',
  50. event_type: 'editing-session',
  51. })
  52. analyticsEditingSessionsQueue
  53. .add({ userId, projectId, countryCode })
  54. .then(() => {
  55. Metrics.analyticsQueue.inc({
  56. status: 'added',
  57. event_type: 'editing-session',
  58. })
  59. })
  60. .catch(() => {
  61. Metrics.analyticsQueue.inc({
  62. status: 'error',
  63. event_type: 'editing-session',
  64. })
  65. })
  66. }
  67. function setUserProperty(userId, propertyName, propertyValue) {
  68. if (!userId) {
  69. return
  70. }
  71. if (isAnalyticsDisabled() || isSmokeTestUser(userId)) {
  72. return
  73. }
  74. if (propertyValue === undefined) {
  75. throw new Error(
  76. 'propertyValue cannot be undefined, use null to unset a property'
  77. )
  78. }
  79. Metrics.analyticsQueue.inc({
  80. status: 'adding',
  81. event_type: 'user-property',
  82. })
  83. analyticsUserPropertiesQueue
  84. .add({ userId, propertyName, propertyValue })
  85. .then(() => {
  86. Metrics.analyticsQueue.inc({
  87. status: 'added',
  88. event_type: 'user-property',
  89. })
  90. })
  91. .catch(() => {
  92. Metrics.analyticsQueue.inc({
  93. status: 'error',
  94. event_type: 'user-property',
  95. })
  96. })
  97. }
  98. function isSmokeTestUser(userId) {
  99. const smokeTestUserId = Settings.smokeTest && Settings.smokeTest.userId
  100. return smokeTestUserId != null && userId.toString() === smokeTestUserId
  101. }
  102. function isAnalyticsDisabled() {
  103. return !(Settings.analytics && Settings.analytics.enabled)
  104. }
  105. module.exports = {
  106. identifyUser,
  107. recordEvent,
  108. updateEditingSession,
  109. setUserProperty,
  110. }