manifold/web/lib/firebase/tracking.ts

44 lines
1001 B
TypeScript
Raw Normal View History

2022-04-19 05:16:08 +00:00
import { doc, collection, setDoc } from 'firebase/firestore'
import { db } from './init'
import { ClickEvent, LatencyEvent, View } from 'common/tracking'
2022-04-19 05:16:08 +00:00
export async function trackView(userId: string, contractId: string) {
const ref = doc(collection(db, 'private-users', userId, 'views'))
2022-04-19 05:16:08 +00:00
const view: View = {
contractId,
timestamp: Date.now(),
}
return await setDoc(ref, view)
}
export async function trackClick(userId: string, contractId: string) {
const ref = doc(collection(db, 'private-users', userId, 'events'))
2022-04-19 05:16:08 +00:00
const clickEvent: ClickEvent = {
type: 'click',
contractId,
timestamp: Date.now(),
}
return await setDoc(ref, clickEvent)
}
export async function trackLatency(
userId: string,
type: 'feed' | 'portfolio',
latency: number
) {
const ref = doc(collection(db, 'private-users', userId, 'latency'))
const latencyEvent: LatencyEvent = {
type,
latency,
timestamp: Date.now(),
}
return await setDoc(ref, latencyEvent)
}