event.js 3.7 KB

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