event-tracking.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import sessionStorage from './session-storage'
  2. import getMeta from '@/utils/meta'
  3. type Segmentation = Record<
  4. string,
  5. string | number | boolean | undefined | unknown | any // TODO: RecurlyError
  6. >
  7. const CACHE_KEY = 'mbEvents'
  8. function alreadySent(key: string) {
  9. const eventCache = sessionStorage.getItem(CACHE_KEY) || {}
  10. return !!eventCache[key]
  11. }
  12. function markAsSent(key: string) {
  13. const eventCache = sessionStorage.getItem(CACHE_KEY) || {}
  14. eventCache[key] = true
  15. sessionStorage.setItem(CACHE_KEY, eventCache)
  16. }
  17. export function send(
  18. category: string,
  19. action: string,
  20. label?: string,
  21. value?: string
  22. ) {
  23. if (typeof window.ga === 'function') {
  24. window.ga('send', 'event', category, action, label, value)
  25. }
  26. }
  27. export function sendOnce(
  28. category: string,
  29. action: string,
  30. label: string,
  31. value: string
  32. ) {
  33. if (alreadySent(category)) return
  34. if (typeof window.ga !== 'function') return
  35. window.ga('send', 'event', category, action, label, value)
  36. markAsSent(category)
  37. }
  38. export function sendMB(key: string, segmentation: Segmentation = {}) {
  39. if (!segmentation.page) {
  40. segmentation.page = window.location.pathname
  41. }
  42. sendBeacon(key, segmentation)
  43. if (typeof window.gtag !== 'function') return
  44. if (['paywall-click', 'paywall-prompt', 'plans-page-click'].includes(key)) {
  45. window.gtag('event', key, segmentation)
  46. }
  47. }
  48. export function sendMBOnce(key: string, segmentation: Segmentation = {}) {
  49. if (alreadySent(key)) return
  50. sendMB(key, segmentation)
  51. markAsSent(key)
  52. }
  53. export function sendMBSampled(
  54. key: string,
  55. segmentation: Segmentation = {},
  56. rate = 0.01
  57. ) {
  58. if (Math.random() < rate) {
  59. sendMB(key, segmentation)
  60. }
  61. }
  62. const sentOncePerPageLoad = new Set()
  63. export function sendMBOncePerPageLoad(
  64. key: string,
  65. segmentation: Segmentation = {}
  66. ) {
  67. if (sentOncePerPageLoad.has(key)) return
  68. sendMB(key, segmentation)
  69. sentOncePerPageLoad.add(key)
  70. }
  71. // Use breakpoint @screen-xs-max from less:
  72. // @screen-xs-max: (@screen-sm-min - 1);
  73. // @screen-sm-min: @screen-sm;
  74. // @screen-sm: 768px;
  75. export const isSmallDevice = window.screen.width < 768
  76. function sendBeacon(key: string, data: Segmentation) {
  77. if (!navigator || !navigator.sendBeacon) return
  78. if (!getMeta('ol-ExposedSettings').isOverleaf) return
  79. data._csrf = getMeta('ol-csrfToken')
  80. const blob = new Blob([JSON.stringify(data)], {
  81. type: 'application/json; charset=UTF-8',
  82. })
  83. try {
  84. navigator.sendBeacon(`/event/${key}`, blob)
  85. } catch (error) {
  86. // Ignored. There's a range of browser for which `navigator.sendBeacon` is available but
  87. // will throw an error if it's called with an unacceptable mime-typed Blob as the data.
  88. }
  89. }