event.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if (!(nextHeartbeat <= new Date())) {
  63. return
  64. }
  65. _sendEditingSessionHeartbeat()
  66. heartbeatsSent++
  67. // send two first heartbeats at 0 and 30s then increase the backoff time
  68. // 1min per call until we reach 5 min
  69. const backoffSecs =
  70. heartbeatsSent <= 2
  71. ? 30
  72. : heartbeatsSent <= 6
  73. ? (heartbeatsSent - 2) * 60
  74. : 300
  75. return (nextHeartbeat = moment().add(backoffSecs, 'seconds').toDate())
  76. },
  77. sendMB,
  78. sendMBSampled(key, segmentation, rate = 0.01) {
  79. if (Math.random() < rate) {
  80. this.sendMB(key, segmentation)
  81. }
  82. },
  83. sendMBOnce(key, segmentation) {
  84. if (!_eventInCache(key)) {
  85. _addEventToCache(key)
  86. this.sendMB(key, segmentation)
  87. }
  88. },
  89. eventInCache(key) {
  90. return _eventInCache(key)
  91. },
  92. }
  93. })
  94. export default $('.navbar a').on('click', function (e) {
  95. const href = $(e.target).attr('href')
  96. if (href != null) {
  97. return ga('send', 'event', 'navigation', 'top menu bar', href)
  98. }
  99. })