Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: fix injection of analytics key at build time #4221

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions forge.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions lib/gui/app/modules/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions lib/gui/etcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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';
Expand Down Expand Up @@ -115,12 +114,15 @@ async function getCommandLineURL(argv: string[]): Promise<string | undefined> {
}
}

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) => {
Expand Down
11 changes: 10 additions & 1 deletion webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModuleOptions>['rules'] = [
// Add support for native node modules
Expand Down Expand Up @@ -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: {
Expand Down
Loading