feat: add initial analytics implementation

This commit is contained in:
Andrew Bastin
2021-07-05 12:45:10 -04:00
parent b1418c081c
commit 913b073ba4
2 changed files with 64 additions and 9 deletions

48
helpers/fb/analytics.ts Normal file
View File

@@ -0,0 +1,48 @@
import firebase from "firebase"
import { authEvents$ } from "./auth"
import { settings$ } from "~/newstore/settings"
let analytics: firebase.analytics.Analytics
type SettingsCustomDimensions = {
usesProxy: boolean
usesExtension: boolean
usesScrollInto: boolean
syncCollections: boolean
syncEnvironments: boolean
syncHistory: boolean
}
export function initAnalytics() {
analytics = firebase.app().analytics()
initLoginListeners()
initSettingsListeners()
}
function initLoginListeners() {
authEvents$.subscribe((ev) => {
if (ev.event === "login") {
analytics.setUserId(ev.user.uid)
analytics.logEvent("login", {
method: ev.user.providerData[0]?.providerId, // Assume the first provider is the login provider
})
} else if (ev.event === "logout") {
analytics.logEvent("logout")
}
})
}
function initSettingsListeners() {
settings$.subscribe((settings) => {
analytics.setUserProperties(<SettingsCustomDimensions>{
usesProxy: settings.PROXY_ENABLED,
usesExtension: settings.EXTENSIONS_ENABLED,
usesScrollInto: settings.SCROLL_INTO_ENABLED,
syncCollections: settings.syncCollections,
syncEnvironments: settings.syncEnvironments,
syncHistory: settings.syncHistory,
})
})
}