event.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = segmentation =>
  44. $http({
  45. url: `/editingSession/${window.project_id}`,
  46. method: 'PUT',
  47. data: { segmentation },
  48. headers: {
  49. 'X-CSRF-Token': window.csrfToken,
  50. },
  51. })
  52. return {
  53. send(category, action, label, value) {
  54. return ga('send', 'event', category, action, label, value)
  55. },
  56. sendGAOnce(category, action, label, value) {
  57. if (!_eventInCache(action)) {
  58. _addEventToCache(action)
  59. return this.send(category, action, label, value)
  60. }
  61. },
  62. editingSessionHeartbeat(segmentationCb = () => {}) {
  63. sl_console.log('[Event] heartbeat trigger')
  64. // If the next heartbeat is in the future, stop
  65. if (nextHeartbeat > new Date()) return
  66. const segmentation = segmentationCb()
  67. sl_console.log('[Event] send heartbeat request', segmentation)
  68. _sendEditingSessionHeartbeat(segmentation)
  69. heartbeatsSent++
  70. // send two first heartbeats at 0 and 30s then increase the backoff time
  71. // 1min per call until we reach 5 min
  72. const backoffSecs =
  73. heartbeatsSent <= 2
  74. ? 30
  75. : heartbeatsSent <= 6
  76. ? (heartbeatsSent - 2) * 60
  77. : 300
  78. nextHeartbeat = moment().add(backoffSecs, 'seconds').toDate()
  79. },
  80. sendMB,
  81. sendMBSampled(key, segmentation, rate = 0.01) {
  82. if (Math.random() < rate) {
  83. this.sendMB(key, segmentation)
  84. }
  85. },
  86. sendMBOnce(key, segmentation) {
  87. if (!_eventInCache(key)) {
  88. _addEventToCache(key)
  89. this.sendMB(key, segmentation)
  90. }
  91. },
  92. eventInCache(key) {
  93. return _eventInCache(key)
  94. },
  95. }
  96. })
  97. export default $('.navbar a').on('click', function (e) {
  98. const href = $(e.target).attr('href')
  99. if (href != null) {
  100. return ga('send', 'event', 'navigation', 'top menu bar', href)
  101. }
  102. })