mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-03 00:54:41 -05:00
94a46c3c40
[web] event-tracking: implement sprinkle using the ng attributes GitOrigin-RevId: 5195d827b590ee32e86ba2ffe365ad7cab65750b
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
import sessionStorage from '../infrastructure/session-storage'
|
|
|
|
const CACHE_KEY = 'mbEvents'
|
|
|
|
function alreadySent(key) {
|
|
const eventCache = sessionStorage.getItem(CACHE_KEY) || {}
|
|
return !!eventCache[key]
|
|
}
|
|
function markAsSent(key) {
|
|
const eventCache = sessionStorage.getItem(CACHE_KEY) || {}
|
|
eventCache[key] = true
|
|
sessionStorage.setItem(CACHE_KEY, eventCache)
|
|
}
|
|
|
|
export function send(category, action, label, value) {
|
|
if (typeof window.ga === 'function') {
|
|
window.ga('send', 'event', category, action, label, value)
|
|
}
|
|
}
|
|
|
|
export function sendOnce(category, action, label, value) {
|
|
if (alreadySent(category)) return
|
|
if (typeof window.ga !== 'function') return
|
|
|
|
window.ga('send', 'event', category, action, label, value)
|
|
markAsSent(category)
|
|
}
|
|
|
|
export function sendMB(key, segmentation = {}) {
|
|
sendBeacon(key, segmentation)
|
|
}
|
|
|
|
export function sendMBOnce(key, segmentation = {}) {
|
|
if (alreadySent(key)) return
|
|
sendMB(key, segmentation)
|
|
markAsSent()
|
|
}
|
|
|
|
export function sendMBSampled(key, body = {}, rate = 0.01) {
|
|
if (Math.random() < rate) {
|
|
sendMB(key, body)
|
|
}
|
|
}
|
|
|
|
function sendBeacon(key, data) {
|
|
if (!navigator || !navigator.sendBeacon) return
|
|
|
|
data._csrf = window.csrfToken
|
|
const blob = new Blob([JSON.stringify(data)], {
|
|
type: 'application/json; charset=UTF-8',
|
|
})
|
|
navigator.sendBeacon(`/event/${key}`, blob)
|
|
}
|