event.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. const CACHE_KEY = 'mbEvents'
  20. // keep track of how many heartbeats we've sent so we can calculate how
  21. // long wait until the next one
  22. let heartbeatsSent = 0
  23. let nextHeartbeat = new Date()
  24. App.factory('eventTracking', function ($http, localStorage) {
  25. const _getEventCache = function () {
  26. let eventCache = localStorage(CACHE_KEY)
  27. // Initialize as an empy object if the event cache is still empty.
  28. if (eventCache == null) {
  29. eventCache = {}
  30. localStorage(CACHE_KEY, eventCache)
  31. }
  32. return eventCache
  33. }
  34. const _eventInCache = function (key) {
  35. const curCache = _getEventCache()
  36. return curCache[key] || false
  37. }
  38. const _addEventToCache = function (key) {
  39. const curCache = _getEventCache()
  40. curCache[key] = true
  41. return localStorage(CACHE_KEY, curCache)
  42. }
  43. const _sendEditingSessionHeartbeat = () =>
  44. $http({
  45. url: `/editingSession/${window.project_id}`,
  46. method: 'PUT',
  47. headers: {
  48. 'X-CSRF-Token': window.csrfToken,
  49. },
  50. })
  51. return {
  52. send(category, action, label, value) {
  53. return ga('send', 'event', category, action, label, value)
  54. },
  55. sendGAOnce(category, action, label, value) {
  56. if (!_eventInCache(action)) {
  57. _addEventToCache(action)
  58. return this.send(category, action, label, value)
  59. }
  60. },
  61. editingSessionHeartbeat() {
  62. sl_console.log('[Event] heartbeat trigger')
  63. if (!(nextHeartbeat <= new Date())) {
  64. return
  65. }
  66. sl_console.log('[Event] send heartbeat request')
  67. _sendEditingSessionHeartbeat()
  68. heartbeatsSent++
  69. // send two first heartbeats at 0 and 30s then increase the backoff time
  70. // 1min per call until we reach 5 min
  71. const backoffSecs =
  72. heartbeatsSent <= 2
  73. ? 30
  74. : heartbeatsSent <= 6
  75. ? (heartbeatsSent - 2) * 60
  76. : 300
  77. return (nextHeartbeat = moment().add(backoffSecs, 'seconds').toDate())
  78. },
  79. sendMB,
  80. sendMBSampled(key, segmentation, rate = 0.01) {
  81. if (Math.random() < rate) {
  82. this.sendMB(key, segmentation)
  83. }
  84. },
  85. sendMBOnce(key, segmentation) {
  86. if (!_eventInCache(key)) {
  87. _addEventToCache(key)
  88. this.sendMB(key, segmentation)
  89. }
  90. },
  91. eventInCache(key) {
  92. return _eventInCache(key)
  93. },
  94. }
  95. })
  96. export default $('.navbar a').on('click', function (e) {
  97. const href = $(e.target).attr('href')
  98. if (href != null) {
  99. return ga('send', 'event', 'navigation', 'top menu bar', href)
  100. }
  101. })