eventTracking.coffee 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # For sending event data to metabase and google analytics
  2. # ---
  3. # by default,
  4. # event not sent to MB.
  5. # for MB, add event-tracking-mb='true'
  6. # by default, event sent to MB via sendMB
  7. # this can be changed to use sendMBOnce via event-tracking-send-once='true' attribute
  8. # event not sent to GA.
  9. # for GA, add event-tracking-ga attribute, where the value is the GA category
  10. # event-tracking-trigger attribute is required to send event
  11. define [
  12. 'base'
  13. ], (App) ->
  14. App.directive 'eventTracking', ['event_tracking', (event_tracking) ->
  15. return {
  16. scope: {
  17. eventTracking: '@',
  18. eventSegmentation: '=?'
  19. }
  20. link: (scope, element, attrs) ->
  21. sendGA = attrs.eventTrackingGa || false
  22. sendMB = attrs.eventTrackingMb || false
  23. sendMBFunction = if attrs.eventTrackingSendOnce then 'sendMBOnce' else 'sendMB'
  24. segmentation = scope.eventSegmentation || {}
  25. segmentation.page = window.location.pathname
  26. sendEvent = () ->
  27. if sendMB
  28. event_tracking[sendMBFunction] scope.eventTracking, segmentation
  29. if sendGA
  30. event_tracking.send attrs.eventTrackingGa, attrs.eventTrackingAction || scope.eventTracking, attrs.eventTrackingLabel || ''
  31. if attrs.eventTrackingTrigger == 'load'
  32. sendEvent()
  33. else if attrs.eventTrackingTrigger == 'click'
  34. element.on 'click', (e) ->
  35. sendEvent()
  36. }
  37. ]