event-tracking.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import sessionStorage from './session-storage'
  2. import getMeta from '@/utils/meta'
  3. export 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. if (getMeta('ol-customerIoEnabled')) {
  43. segmentation['customerio-integration'] = 'enabled'
  44. }
  45. sendBeacon(key, segmentation)
  46. if (typeof window.gtag !== 'function') return
  47. if (['paywall-click', 'paywall-prompt', 'plans-page-click'].includes(key)) {
  48. window.gtag('event', key, segmentation)
  49. }
  50. }
  51. export function sendMBOnce(key: string, segmentation: Segmentation = {}) {
  52. if (alreadySent(key)) return
  53. sendMB(key, segmentation)
  54. markAsSent(key)
  55. }
  56. export function sendMBSampled(
  57. key: string,
  58. segmentation: Segmentation = {},
  59. rate = 0.01
  60. ) {
  61. if (Math.random() < rate) {
  62. sendMB(key, segmentation)
  63. }
  64. }
  65. const sentOncePerPageLoad = new Set()
  66. export function sendMBOncePerPageLoad(
  67. key: string,
  68. segmentation: Segmentation = {}
  69. ) {
  70. if (sentOncePerPageLoad.has(key)) return
  71. sendMB(key, segmentation)
  72. sentOncePerPageLoad.add(key)
  73. }
  74. // Use breakpoint @screen-xs-max from less:
  75. // @screen-xs-max: (@screen-sm-min - 1);
  76. // @screen-sm-min: @screen-sm;
  77. // @screen-sm: 768px;
  78. export const isSmallDevice = window.screen.width < 768
  79. function sendBeacon(key: string, data: Segmentation) {
  80. if (!navigator || !navigator.sendBeacon) return
  81. if (!getMeta('ol-ExposedSettings').isOverleaf) return
  82. data._csrf = getMeta('ol-csrfToken')
  83. const blob = new Blob([JSON.stringify(data)], {
  84. type: 'application/json; charset=UTF-8',
  85. })
  86. try {
  87. navigator.sendBeacon(`/event/${key}`, blob)
  88. } catch (error) {
  89. // Ignored. There's a range of browser for which `navigator.sendBeacon` is available but
  90. // will throw an error if it's called with an unacceptable mime-typed Blob as the data.
  91. }
  92. }