event.js 3.2 KB

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