eventTracking.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. // For sending event data to metabase and google analytics
  13. // ---
  14. // by default,
  15. // event not sent to MB.
  16. // for MB, add event-tracking-mb='true'
  17. // by default, event sent to MB via sendMB
  18. // event not sent to GA.
  19. // for GA, add event-tracking-ga attribute, where the value is the GA category
  20. // Either GA or MB can use the attribute event-tracking-send-once='true' to
  21. // send event just once
  22. // MB will use the key and GA will use the action to determine if the event
  23. // has been sent
  24. // event-tracking-trigger attribute is required to send event
  25. /* eslint-disable
  26. camelcase,
  27. max-len,
  28. */
  29. // TODO: This file was created by bulk-decaffeinate.
  30. // Fix any style issues and re-enable lint.
  31. /*
  32. * decaffeinate suggestions:
  33. * DS102: Remove unnecessary code created because of implicit returns
  34. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  35. */
  36. // For sending event data to metabase and google analytics
  37. // ---
  38. // by default,
  39. // event not sent to MB.
  40. // for MB, add event-tracking-mb='true'
  41. // by default, event sent to MB via sendMB
  42. // event not sent to GA.
  43. // for GA, add event-tracking-ga attribute, where the value is the GA category
  44. // Either GA or MB can use the attribute event-tracking-send-once='true' to
  45. // send event just once
  46. // MB will use the key and GA will use the action to determine if the event
  47. // has been sent
  48. // event-tracking-trigger attribute is required to send event
  49. import App from '../base'
  50. const isInViewport = function(element) {
  51. const elTop = element.offset().top
  52. const elBtm = elTop + element.outerHeight()
  53. const viewportTop = $(window).scrollTop()
  54. const viewportBtm = viewportTop + $(window).height()
  55. return elBtm > viewportTop && elTop < viewportBtm
  56. }
  57. export default App.directive('eventTracking', eventTracking => ({
  58. scope: {
  59. eventTracking: '@',
  60. eventSegmentation: '=?'
  61. },
  62. link(scope, element, attrs) {
  63. const sendGA = attrs.eventTrackingGa || false
  64. const sendMB = attrs.eventTrackingMb || false
  65. const sendMBFunction = attrs.eventTrackingSendOnce ? 'sendMBOnce' : 'sendMB'
  66. const sendGAFunction = attrs.eventTrackingSendOnce ? 'sendGAOnce' : 'send'
  67. const segmentation = scope.eventSegmentation || {}
  68. segmentation.page = window.location.pathname
  69. const sendEvent = function(scrollEvent) {
  70. /*
  71. @param {boolean} scrollEvent Use to unbind scroll event
  72. */
  73. if (sendMB) {
  74. eventTracking[sendMBFunction](scope.eventTracking, segmentation)
  75. }
  76. if (sendGA) {
  77. eventTracking[sendGAFunction](
  78. attrs.eventTrackingGa,
  79. attrs.eventTrackingAction || scope.eventTracking,
  80. attrs.eventTrackingLabel || ''
  81. )
  82. }
  83. if (scrollEvent) {
  84. return $(window).unbind('resize scroll')
  85. }
  86. }
  87. if (attrs.eventTrackingTrigger === 'load') {
  88. return sendEvent()
  89. } else if (attrs.eventTrackingTrigger === 'click') {
  90. return element.on('click', e => sendEvent())
  91. } else if (attrs.eventTrackingTrigger === 'hover') {
  92. let timer = null
  93. let timeoutAmt = 500
  94. if (attrs.eventHoverAmt) {
  95. timeoutAmt = parseInt(attrs.eventHoverAmt, 10)
  96. }
  97. return element
  98. .on('mouseenter', function() {
  99. timer = setTimeout(() => sendEvent(), timeoutAmt)
  100. })
  101. .on('mouseleave', () => clearTimeout(timer))
  102. } else if (
  103. attrs.eventTrackingTrigger === 'scroll' &&
  104. !eventTracking.eventInCache(scope.eventTracking)
  105. ) {
  106. $(window).on(
  107. 'resize scroll',
  108. _.throttle(() => {
  109. if (isInViewport(element)) {
  110. sendEvent(true)
  111. }
  112. }, 500)
  113. )
  114. }
  115. }
  116. }))