2021-10-27 15:23:28 +05:30
|
|
|
import { delay } from 'lodash';
|
|
|
|
import api from '~/api';
|
2022-07-16 23:28:13 +05:30
|
|
|
import { DEFER_DURATION, TRACKING_CAP_KEY, TRACKING_CAP_LENGTH } from '../constants';
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
function shouldDispatchEvent() {
|
|
|
|
const timestamp = parseInt(localStorage.getItem(TRACKING_CAP_KEY), 10);
|
|
|
|
|
|
|
|
if (Number.isNaN(timestamp)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return timestamp + TRACKING_CAP_LENGTH < Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function dispatchRedisHllUserEvent(event, deferDuration = 0) {
|
2021-10-27 15:23:28 +05:30
|
|
|
delay(() => api.trackRedisHllUserEvent(event), deferDuration);
|
|
|
|
}
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
export function queueRedisHllEvents(events, { verifyCap = false } = {}) {
|
|
|
|
if (verifyCap) {
|
|
|
|
if (!shouldDispatchEvent()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem(TRACKING_CAP_KEY, Date.now());
|
|
|
|
}
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
events.forEach((event, index) => {
|
2022-07-16 23:28:13 +05:30
|
|
|
dispatchRedisHllUserEvent(event, DEFER_DURATION * (index + 1));
|
2021-10-27 15:23:28 +05:30
|
|
|
});
|
|
|
|
}
|