const paciuAnalyticsCounter = async (options = {}, configs = {}) => { const localStorageKey = 'paciu-analytics-counter'; const access_token = 'fb66edeb-52ca-470f-91c3-3b0001b05bdf'; const counter_token = '29132304-2496-49d1-8cf8-7ec716dd391b'; const { delay, v } = configs; const data = JSON.parse(localStorage.getItem(localStorageKey) || '[]'); const target = `${location.pathname.replace(/\/$/, '')}${location.search}`; const find = data.find((item) => item.target === target); const now = Date.now(); const ttl = now + (1000 * 60 * 3); let can = false; let unique = false; if (find) { if (now >= (find?.ttl || now)) { can = true; find.ttl = ttl; } } else { data.push({ target, ttl }); can = true; unique = true; } localStorage.setItem(localStorageKey, JSON.stringify(data)); const send = async () => { await fetch(`https://analytics.paciu.tech/api/v${v}/analytics/counters/${counter_token}/history`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${access_token}`, }, body: JSON.stringify({ user_agent: navigator.userAgent, url: location.href, type: 'page-view', unique, ...options, }) }); }; if (can) { setTimeout(() => send(), delay); } };