eventTracking.js 3.5 KB

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