diff --git a/forge.config.ts b/forge.config.ts index f45cd36786..e5263bed8e 100644 --- a/forge.config.ts +++ b/forge.config.ts @@ -136,25 +136,6 @@ const config: ForgeConfig = { new sidecar.SidecarPlugin(), ], hooks: { - readPackageJson: async (_config, packageJson) => { - packageJson.analytics = {}; - - if (process.env.SENTRY_TOKEN) { - packageJson.analytics.sentry = { - token: process.env.SENTRY_TOKEN, - }; - } - - if (process.env.AMPLITUDE_TOKEN) { - packageJson.analytics.amplitude = { - token: 'balena-etcher', - }; - } - - // packageJson.packageType = 'dmg' | 'AppImage' | 'rpm' | 'deb' | 'zip' | 'nsis' | 'portable' - - return packageJson; - }, postPackage: async (_forgeConfig, options) => { if (options.platform === 'linux') { // symlink the etcher binary from balena-etcher to balenaEtcher to ensure compatibility with the wdio suite and the old name diff --git a/lib/gui/app/modules/analytics.ts b/lib/gui/app/modules/analytics.ts index 52743b0fd1..264a8c4b34 100644 --- a/lib/gui/app/modules/analytics.ts +++ b/lib/gui/app/modules/analytics.ts @@ -14,13 +14,13 @@ * limitations under the License. */ -import * as _ from 'lodash'; +import { findLastIndex, once } from 'lodash'; import type { Client } from 'analytics-client'; import { createClient, createNoopClient } from 'analytics-client'; import * as SentryRenderer from '@sentry/electron/renderer'; import * as settings from '../models/settings'; import { store } from '../models/store'; -import * as packageJSON from '../../../../package.json'; +import { version } from '../../../../package.json'; type AnalyticsPayload = _.Dictionary; @@ -73,7 +73,7 @@ export const anonymizePath = (input: string) => { const segments = mainPart.split(sep); // Moving from the end, find the first marker and cut the path from there. - const startCutIndex = _.findLastIndex(segments, (segment) => + const startCutIndex = findLastIndex(segments, (segment) => etcherSegmentMarkers.includes(segment), ); return ( @@ -119,21 +119,19 @@ let analyticsClient: Client; /** * @summary Init analytics configurations */ -export const initAnalytics = _.once(() => { +export const initAnalytics = once(() => { const dsn = - settings.getSync('analyticsSentryToken') || - _.get(packageJSON, ['analytics', 'sentry', 'token']); + settings.getSync('analyticsSentryToken') || process.env.SENTRY_TOKEN; SentryRenderer.init({ dsn, beforeSend: anonymizeSentryData }); const projectName = - settings.getSync('analyticsAmplitudeToken') || - _.get(packageJSON, ['analytics', 'amplitude', 'token']); + settings.getSync('analyticsAmplitudeToken') || process.env.AMPLITUDE_TOKEN; const clientConfig = { projectName, endpoint: 'data.balena-cloud.com', componentName: 'etcher', - componentVersion: packageJSON.version, + componentVersion: version, }; analyticsClient = projectName ? createClient(clientConfig) diff --git a/lib/gui/etcher.ts b/lib/gui/etcher.ts index f4206d60e9..6c5dc7b505 100644 --- a/lib/gui/etcher.ts +++ b/lib/gui/etcher.ts @@ -27,7 +27,7 @@ import { promises as fs } from 'fs'; import { platform } from 'os'; import * as path from 'path'; import * as semver from 'semver'; -import * as lodash from 'lodash'; +import { once } from 'lodash'; import './app/i18n'; @@ -37,7 +37,6 @@ import * as settings from './app/models/settings'; import { buildWindowMenu } from './menu'; import * as i18n from 'i18next'; import * as SentryMain from '@sentry/electron/main'; -import * as packageJSON from '../../package.json'; import { anonymizeSentryData } from './app/modules/analytics'; import { delay } from '../shared/utils'; @@ -115,12 +114,15 @@ async function getCommandLineURL(argv: string[]): Promise { } } -const initSentryMain = lodash.once(() => { +const initSentryMain = once(() => { const dsn = - settings.getSync('analyticsSentryToken') || - lodash.get(packageJSON, ['analytics', 'sentry', 'token']); + settings.getSync('analyticsSentryToken') || process.env.SENTRY_TOKEN; - SentryMain.init({ dsn, beforeSend: anonymizeSentryData }); + SentryMain.init({ + dsn, + beforeSend: anonymizeSentryData, + }); + console.log(SentryMain.getCurrentScope()); }); const sourceSelectorReady = new Promise((resolve) => { diff --git a/webpack.config.ts b/webpack.config.ts index 745b4b6c11..d12308466d 100644 --- a/webpack.config.ts +++ b/webpack.config.ts @@ -17,7 +17,7 @@ import type { Configuration, ModuleOptions } from 'webpack'; import { resolve } from 'path'; -import { BannerPlugin, IgnorePlugin } from 'webpack'; +import { BannerPlugin, IgnorePlugin, DefinePlugin } from 'webpack'; const rules: Required['rules'] = [ // Add support for native node modules @@ -79,6 +79,15 @@ export const rendererConfig: Configuration = { banner: '__REACT_DEVTOOLS_GLOBAL_HOOK__ = { isDisabled: true };', raw: true, }), + // Inject the analytics key into the code + new DefinePlugin({ + 'process.env.SENTRY_TOKEN': JSON.stringify( + process.env.SENTRY_TOKEN || '', + ), + 'process.env.AMPLITUDE_TOKEN': JSON.stringify( + process.env.AMPLITUDE_TOKEN || '', + ), + }), ], resolve: {