improve tracking

This commit is contained in:
orangecoding
2025-09-13 17:06:18 +02:00
parent 251de1e42d
commit d66dc2cd93
5 changed files with 32 additions and 21 deletions

View File

@@ -6,9 +6,9 @@ import * as jobStorage from './lib/services/storage/jobStorage.js';
import FredyRuntime from './lib/FredyRuntime.js';
import { duringWorkingHoursOrNotSet } from './lib/utils.js';
import './lib/api/api.js';
import { track } from './lib/services/tracking/Tracker.js';
import { handleDemoUser } from './lib/services/storage/userStorage.js';
import { cleanupDemoAtMidnight } from './lib/services/demoCleanup.js';
import { initTrackerCron } from './lib/services/tracking/Tracker-Cron.js';
//if db folder does not exist, ensure to create it before loading anything else
if (!fs.existsSync('./db')) {
fs.mkdirSync('./db');
@@ -29,13 +29,13 @@ const fetchedProvider = await Promise.all(
);
handleDemoUser();
await initTrackerCron();
setInterval(
(function exec() {
const isDuringWorkingHoursOrNotSet = duringWorkingHoursOrNotSet(config, Date.now());
if (!config.demoMode) {
if (isDuringWorkingHoursOrNotSet) {
track();
config.lastRun = Date.now();
jobStorage
.getJobs()

View 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);
}

View File

@@ -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) {

View File

@@ -1,6 +1,6 @@
{
"name": "fredy",
"version": "11.6.2",
"version": "11.6.3",
"description": "[F]ind [R]eal [E]states [d]amn eas[y].",
"scripts": {
"prepare": "husky",
@@ -71,6 +71,7 @@
"lowdb": "7.0.1",
"markdown": "^0.5.0",
"nanoid": "5.1.5",
"node-cron": "^4.2.1",
"node-fetch": "3.3.2",
"node-mailjet": "6.0.9",
"p-throttle": "^8.0.0",

View File

@@ -5521,6 +5521,11 @@ node-abi@^3.3.0:
dependencies:
semver "^7.3.5"
node-cron@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/node-cron/-/node-cron-4.2.1.tgz#6979be4aee4702f06322d21220df8de252c8e265"
integrity sha512-lgimEHPE/QDgFlywTd8yTR61ptugX3Qer29efeyWw2rv259HtGBNn1vZVmp8lB9uo9wC0t/AT4iGqXxia+CJFg==
node-domexception@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"