mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
improve tracking
This commit is contained in:
17
lib/services/tracking/Tracker-Cron.js
Normal file
17
lib/services/tracking/Tracker-Cron.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import cron from 'node-cron';
|
||||
import { config, inDevMode } from '../../utils.js';
|
||||
import { trackMainEvent } from './Tracker.js';
|
||||
|
||||
async function runTask() {
|
||||
//make sure to only send tracking events if the user gave us the green light and we are not in dev mode
|
||||
if (config.analyticsEnabled && !inDevMode()) {
|
||||
await trackMainEvent();
|
||||
}
|
||||
}
|
||||
|
||||
export async function initTrackerCron() {
|
||||
//run directly on start
|
||||
await runTask();
|
||||
// then every 6 hours
|
||||
cron.schedule('0 */6 * * *', runTask);
|
||||
}
|
||||
@@ -10,11 +10,7 @@ const deviceId = getUniqueId() || 'N/A';
|
||||
const version = await getPackageVersion();
|
||||
const FREDY_TRACKING_URL = 'https://fredy.orange-coding.net/tracking';
|
||||
|
||||
let cached = null;
|
||||
let lastSent = 0;
|
||||
const SIX_HOURS = 6 * 3_600_000;
|
||||
|
||||
export const track = async () => {
|
||||
export const trackMainEvent = async () => {
|
||||
try {
|
||||
if (config.analyticsEnabled && !inDevMode()) {
|
||||
const activeProvider = new Set();
|
||||
@@ -33,19 +29,11 @@ export const track = async () => {
|
||||
provider: Array.from(activeProvider),
|
||||
});
|
||||
|
||||
const stringify = JSON.stringify(trackingObj);
|
||||
const now = Date.now();
|
||||
|
||||
// send if changed OR six hours passed since last send
|
||||
if (stringify !== cached || now - lastSent >= SIX_HOURS) {
|
||||
await fetch(`${FREDY_TRACKING_URL}/main`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: stringify,
|
||||
});
|
||||
cached = stringify;
|
||||
lastSent = now;
|
||||
}
|
||||
await fetch(`${FREDY_TRACKING_URL}/main`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(trackingObj),
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user