From 3b8279c714806cbb20827365e3b741b3876505e9 Mon Sep 17 00:00:00 2001 From: "weakmap@gmail.com" Date: Tue, 17 Dec 2024 13:07:25 +0100 Subject: [PATCH] adding fredy version --- lib/services/tracking/Tracker.js | 109 ++++++++++++++++++------------- package.json | 1 + yarn.lock | 12 ++++ 3 files changed, 75 insertions(+), 47 deletions(-) diff --git a/lib/services/tracking/Tracker.js b/lib/services/tracking/Tracker.js index 4eed74e..b9f0515 100644 --- a/lib/services/tracking/Tracker.js +++ b/lib/services/tracking/Tracker.js @@ -1,75 +1,90 @@ import Mixpanel from 'mixpanel'; -import { getJobs } from '../storage/jobStorage.js'; -import { getUniqueId } from './uniqueId.js'; -import { config, inDevMode } from '../../utils.js'; +import {getJobs} from '../storage/jobStorage.js'; +import {getUniqueId} from './uniqueId.js'; +import {config, inDevMode} from '../../utils.js'; import os from 'os'; +import {readFileSync} from 'fs'; +import {packageUp} from 'package-up'; const mixpanelTracker = Mixpanel.init('718670ef1c58c0208256c1e408a3d75e'); - const distinct_id = getUniqueId() || 'N/A'; +const version = await getPackageVersion(); export const track = function () { - //only send tracking information if the user allowed to do so. - if (config.analyticsEnabled && !inDevMode()) { - const activeProvider = new Set(); - const activeAdapter = new Set(); + //only send tracking information if the user allowed to do so. + if (config.analyticsEnabled && !inDevMode()) { + const activeProvider = new Set(); + const activeAdapter = new Set(); - const jobs = getJobs(); + const jobs = getJobs(); - if (jobs != null && jobs.length > 0) { - jobs.forEach((job) => { - job.provider.forEach((provider) => { - activeProvider.add(provider.id); - }); - job.notificationAdapter.forEach((adapter) => { - activeAdapter.add(adapter.id); - }); - }); + if (jobs != null && jobs.length > 0) { + jobs.forEach((job) => { + job.provider.forEach((provider) => { + activeProvider.add(provider.id); + }); + job.notificationAdapter.forEach((adapter) => { + activeAdapter.add(adapter.id); + }); + }); - mixpanelTracker.track( - 'fredy_tracking', - enrichTrackingObject({ - adapter: Array.from(activeAdapter), - provider: Array.from(activeProvider), - }), - ); + mixpanelTracker.track( + 'fredy_tracking', + enrichTrackingObject({ + adapter: Array.from(activeAdapter), + provider: Array.from(activeProvider), + }), + ); + } } - } }; /** * Note, this will only be used when Fredy runs in demo mode */ export function trackDemoJobCreated(jobData) { - if (config.analyticsEnabled && !inDevMode() && config.demoMode) { - mixpanelTracker.track('demoJobCreated', enrichTrackingObject(jobData)); - } + if (config.analyticsEnabled && !inDevMode() && config.demoMode) { + mixpanelTracker.track('demoJobCreated', enrichTrackingObject(jobData)); + } } /** * Note, this will only be used when Fredy runs in demo mode */ export function trackDemoAccessed() { - if (config.analyticsEnabled && !inDevMode() && config.demoMode) { - mixpanelTracker.track('demoAccessed', enrichTrackingObject({})); - } + if (config.analyticsEnabled && !inDevMode() && config.demoMode) { + mixpanelTracker.track('demoAccessed', enrichTrackingObject({})); + } } function enrichTrackingObject(trackingObject) { - const operating_system = os.platform(); - const os_version = os.release(); - const arch = process.arch; - const language = process.env.LANG || 'en'; - const nodeVersion = process.version || 'N/A'; + const operating_system = os.platform(); + const os_version = os.release(); + const arch = process.arch; + const language = process.env.LANG || 'en'; + const nodeVersion = process.version || 'N/A'; - return { - ...trackingObject, - isDemo: config.demoMode, - operating_system, - os_version, - arch, - nodeVersion, - language, - distinct_id, - }; + return { + ...trackingObject, + isDemo: config.demoMode, + operating_system, + os_version, + arch, + nodeVersion, + language, + distinct_id, + fredy_version: version + }; +} + +async function getPackageVersion() { + try { + const packagePath = await packageUp(); + const packageJson = readFileSync(packagePath, 'utf8'); + const json = JSON.parse(packageJson); + return json.version; + } catch (error) { + console.error('Error reading version from package.json', error); + } + return 'N/A'; } diff --git a/package.json b/package.json index a6b9717..5da95d5 100755 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "nanoid": "5.0.9", "node-fetch": "3.3.2", "node-mailjet": "6.0.6", + "package-up": "^5.0.0", "puppeteer": "^23.10.4", "puppeteer-extra": "^3.3.6", "puppeteer-extra-plugin-stealth": "^2.11.2", diff --git a/yarn.lock b/yarn.lock index a835350..6b051e8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3545,6 +3545,11 @@ filter-obj@^5.1.0: resolved "https://registry.npmjs.org/filter-obj/-/filter-obj-5.1.0.tgz" integrity sha512-qWeTREPoT7I0bifpPUXtxkZJ1XJzxWtfoWWkdVGqa+eCr3SHW/Ocp89o8vLvbUuQnadybJpjOKu4V+RwO6sGng== +find-up-simple@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/find-up-simple/-/find-up-simple-1.0.0.tgz#21d035fde9fdbd56c8f4d2f63f32fd93a1cfc368" + integrity sha512-q7Us7kcjj2VMePAa02hDAF6d+MzsdsAWEwYyOpwUtlerRBkOEPBCRZrAV4XfcSN8fHAgaD0hP7miwoay6DCprw== + find-up@^5.0.0: version "5.0.0" resolved "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz" @@ -5958,6 +5963,13 @@ pac-resolver@^7.0.1: degenerator "^5.0.0" netmask "^2.0.2" +package-up@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/package-up/-/package-up-5.0.0.tgz#3facda2aa9248c0b68b3dddfa4c58d22aa3faea2" + integrity sha512-MQEgDUvXCa3sGvqHg3pzHO8e9gqTCMPVrWUko3vPQGntwegmFo52mZb2abIVTjFnUcW0BcPz0D93jV5Cas1DWA== + dependencies: + find-up-simple "^1.0.0" + parent-module@^1.0.0: version "1.0.1" resolved "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz"