analytics.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {
  2. Analytics,
  3. getAnalytics,
  4. logEvent,
  5. setAnalyticsCollectionEnabled,
  6. setUserId,
  7. setUserProperties,
  8. } from "firebase/analytics"
  9. import { authEvents$ } from "./auth"
  10. import {
  11. HoppAccentColor,
  12. HoppBgColor,
  13. settings$,
  14. settingsStore,
  15. } from "~/newstore/settings"
  16. let analytics: Analytics | null = null
  17. type SettingsCustomDimensions = {
  18. usesProxy: boolean
  19. usesExtension: boolean
  20. syncCollections: boolean
  21. syncEnvironments: boolean
  22. syncHistory: boolean
  23. usesBg: HoppBgColor
  24. usesAccent: HoppAccentColor
  25. usesTelemetry: boolean
  26. }
  27. type HoppRequestEvent =
  28. | {
  29. platform: "rest" | "graphql-query" | "graphql-schema"
  30. strategy: "normal" | "proxy" | "extension"
  31. }
  32. | { platform: "wss" | "sse" | "socketio" | "mqtt" }
  33. export function initAnalytics() {
  34. analytics = getAnalytics()
  35. initLoginListeners()
  36. initSettingsListeners()
  37. }
  38. function initLoginListeners() {
  39. authEvents$.subscribe((ev) => {
  40. if (ev.event === "login") {
  41. if (settingsStore.value.TELEMETRY_ENABLED && analytics) {
  42. setUserId(analytics, ev.user.uid)
  43. logEvent(analytics, "login", {
  44. method: ev.user.providerData[0]?.providerId, // Assume the first provider is the login provider
  45. })
  46. }
  47. } else if (ev.event === "logout") {
  48. if (settingsStore.value.TELEMETRY_ENABLED && analytics) {
  49. logEvent(analytics, "logout")
  50. }
  51. }
  52. })
  53. }
  54. function initSettingsListeners() {
  55. // Keep track of the telemetry status
  56. let telemetryStatus = settingsStore.value.TELEMETRY_ENABLED
  57. settings$.subscribe((settings) => {
  58. const conf: SettingsCustomDimensions = {
  59. usesProxy: settings.PROXY_ENABLED,
  60. usesExtension: settings.EXTENSIONS_ENABLED,
  61. syncCollections: settings.syncCollections,
  62. syncEnvironments: settings.syncEnvironments,
  63. syncHistory: settings.syncHistory,
  64. usesAccent: settings.THEME_COLOR,
  65. usesBg: settings.BG_COLOR,
  66. usesTelemetry: settings.TELEMETRY_ENABLED,
  67. }
  68. // User toggled telemetry mode to off or to on
  69. if (
  70. ((telemetryStatus && !settings.TELEMETRY_ENABLED) ||
  71. settings.TELEMETRY_ENABLED) &&
  72. analytics
  73. ) {
  74. setUserProperties(analytics, conf)
  75. }
  76. telemetryStatus = settings.TELEMETRY_ENABLED
  77. if (analytics) setAnalyticsCollectionEnabled(analytics, telemetryStatus)
  78. })
  79. if (analytics) setAnalyticsCollectionEnabled(analytics, telemetryStatus)
  80. }
  81. export function logHoppRequestRunToAnalytics(ev: HoppRequestEvent) {
  82. if (settingsStore.value.TELEMETRY_ENABLED && analytics) {
  83. logEvent(analytics, "hopp-request", ev)
  84. }
  85. }
  86. export function logPageView(pagePath: string) {
  87. if (settingsStore.value.TELEMETRY_ENABLED && analytics) {
  88. logEvent(analytics, "page_view", {
  89. page_path: pagePath,
  90. })
  91. }
  92. }