event.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. import moment from 'moment'
  16. import App from '../base'
  17. import '../modules/localStorage'
  18. import { sendMB } from '../infrastructure/event-tracking'
  19. import { debugConsole } from '@/utils/debugging'
  20. const CACHE_KEY = 'mbEvents'
  21. // keep track of how many heartbeats we've sent so we can calculate how
  22. // long wait until the next one
  23. let heartbeatsSent = 0
  24. let nextHeartbeat = new Date()
  25. App.factory('eventTracking', function ($http, localStorage) {
  26. const _getEventCache = function () {
  27. let eventCache = localStorage(CACHE_KEY)
  28. // Initialize as an empy object if the event cache is still empty.
  29. if (eventCache == null) {
  30. eventCache = {}
  31. localStorage(CACHE_KEY, eventCache)
  32. }
  33. return eventCache
  34. }
  35. const _eventInCache = function (key) {
  36. const curCache = _getEventCache()
  37. return curCache[key] || false
  38. }
  39. const _addEventToCache = function (key) {
  40. const curCache = _getEventCache()
  41. curCache[key] = true
  42. return localStorage(CACHE_KEY, curCache)
  43. }
  44. const _sendEditingSessionHeartbeat = segmentation =>
  45. $http({
  46. url: `/editingSession/${window.project_id}`,
  47. method: 'PUT',
  48. data: { segmentation },
  49. headers: {
  50. 'X-CSRF-Token': window.csrfToken,
  51. },
  52. })
  53. return {
  54. send(category, action, label, value) {
  55. return ga('send', 'event', category, action, label, value)
  56. },
  57. sendGAOnce(category, action, label, value) {
  58. if (!_eventInCache(action)) {
  59. _addEventToCache(action)
  60. return this.send(category, action, label, value)
  61. }
  62. },
  63. editingSessionHeartbeat(segmentationCb = () => {}) {
  64. debugConsole.log('[Event] heartbeat trigger')
  65. // If the next heartbeat is in the future, stop
  66. if (nextHeartbeat > new Date()) return
  67. const segmentation = segmentationCb()
  68. debugConsole.log('[Event] send heartbeat request', segmentation)
  69. _sendEditingSessionHeartbeat(segmentation)
  70. heartbeatsSent++
  71. // send two first heartbeats at 0 and 30s then increase the backoff time
  72. // 1min per call until we reach 5 min
  73. const backoffSecs =
  74. heartbeatsSent <= 2
  75. ? 30
  76. : heartbeatsSent <= 6
  77. ? (heartbeatsSent - 2) * 60
  78. : 300
  79. nextHeartbeat = moment().add(backoffSecs, 'seconds').toDate()
  80. },
  81. sendMB,
  82. sendMBSampled(key, segmentation, rate = 0.01) {
  83. if (Math.random() < rate) {
  84. this.sendMB(key, segmentation)
  85. }
  86. },
  87. sendMBOnce(key, segmentation) {
  88. if (!_eventInCache(key)) {
  89. _addEventToCache(key)
  90. this.sendMB(key, segmentation)
  91. }
  92. },
  93. eventInCache(key) {
  94. return _eventInCache(key)
  95. },
  96. }
  97. })
  98. export default $('.navbar a').on('click', function (e) {
  99. const href = $(e.target).attr('href')
  100. if (href != null) {
  101. return ga('send', 'event', 'navigation', 'top menu bar', href)
  102. }
  103. })