|
@@ -2,31 +2,38 @@ import sessionStorage from '../infrastructure/session-storage'
|
|
|
|
|
|
|
|
const CACHE_KEY = 'mbEvents'
|
|
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) {
|
|
export function send(category, action, label, value) {
|
|
|
if (typeof window.ga === 'function') {
|
|
if (typeof window.ga === 'function') {
|
|
|
window.ga('send', 'event', category, action, label, value)
|
|
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 = {}) {
|
|
export function sendMB(key, segmentation = {}) {
|
|
|
sendBeacon(key, segmentation)
|
|
sendBeacon(key, segmentation)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export function sendMBOnce(key, segmentation = {}) {
|
|
export function sendMBOnce(key, segmentation = {}) {
|
|
|
- let eventCache = sessionStorage.getItem(CACHE_KEY)
|
|
|
|
|
-
|
|
|
|
|
- // Initialize as an empy object if the event cache is still empty.
|
|
|
|
|
- if (eventCache == null) {
|
|
|
|
|
- eventCache = {}
|
|
|
|
|
- sessionStorage.setItem(CACHE_KEY, eventCache)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const isEventInCache = eventCache[key] || false
|
|
|
|
|
- if (!isEventInCache) {
|
|
|
|
|
- eventCache[key] = true
|
|
|
|
|
- sessionStorage.setItem(CACHE_KEY, eventCache)
|
|
|
|
|
- sendMB(key, segmentation)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (alreadySent(key)) return
|
|
|
|
|
+ sendMB(key, segmentation)
|
|
|
|
|
+ markAsSent()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export function sendMBSampled(key, body = {}, rate = 0.01) {
|
|
export function sendMBSampled(key, body = {}, rate = 0.01) {
|