Skip to content

Commit 179d3d4

Browse files
fix: event posthog
1 parent 6d4c33b commit 179d3d4

File tree

8 files changed

+119
-79
lines changed

8 files changed

+119
-79
lines changed

package.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
"test": "jest",
2121
"test:e2e": "playwright test --workers=1",
2222
"compile": "cross-env 'NODE_OPTIONS=\"--max-old-space-size=4096\"' electron-webpack",
23-
"dist:win": "electron-builder --x64 --ia32 --win portable msi nsis",
24-
"dist:mac": "electron-builder --mac --x64 --arm64",
25-
"dist:mac-local": "CSC_IDENTITY_AUTO_DISCOVERY=false electron-builder --mac dmg",
26-
"dist:linux": "electron-builder --linux",
23+
"dist:win": "cross-env ELECTRON_FORCE_IS_PACKAGED=true electron-builder --x64 --ia32 --win portable msi nsis",
24+
"dist:mac": "cross-env ELECTRON_FORCE_IS_PACKAGED=true electron-builder --mac --x64 --arm64",
25+
"dist:mac-local": "cross-env ELECTRON_FORCE_IS_PACKAGED=true CSC_IDENTITY_AUTO_DISCOVERY=false electron-builder --mac dmg",
26+
"dist:linux": "cross-env ELECTRON_FORCE_IS_PACKAGED=true electron-builder --linux",
2727
"lint": "eslint src/",
2828
"lint:fix": "eslint --fix src/",
2929
"lint:test": "eslint -c tests/.eslintrc.json 'tests/**/*.ts*'",
@@ -96,7 +96,6 @@
9696
"jszip"
9797
]
9898
},
99-
"//build.extraResources": "Static should be set again because it is overwritten",
10099
"build": {
101100
"generateUpdatesFilesForAllChannels": true,
102101
"appId": "fr.gouv.social.fabrique.archifiltre.docs",
@@ -223,8 +222,8 @@
223222
"csv-stringify": "^5.6.4",
224223
"deep-equal": "2.0.5",
225224
"dotenv": "^16.0.0",
226-
"electron": "^18.3.7",
227-
"electron-builder": "^23.0.3",
225+
"electron": "^18.3.15",
226+
"electron-builder": "^23.6.0",
228227
"electron-webpack": "^2.8.2",
229228
"electron-webpack-ts": "^4.0.1",
230229
"eslint": "^7",
@@ -246,7 +245,7 @@
246245
"rmfr": "2.0.0",
247246
"sass": "^1.49.9",
248247
"sass-loader": "10",
249-
"semantic-release": "^19.0.3",
248+
"semantic-release": "19.0.5",
250249
"stdio-mock": "^1.1.0",
251250
"ts-jest": "27.1.4",
252251
"tsconfig-paths": "^4.1.0",

src/common/modules/tracker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function initTracking(): Promise<void> {
1313
await getTrackerProvider().init();
1414
if (IS_MAIN) {
1515
ipcMain.on("tracking.toggle", (_, enable) => {
16-
toggleTracking(enableTracking === enable);
16+
toggleTracking((enableTracking = enable as boolean));
1717
});
1818
}
1919
}
@@ -26,7 +26,7 @@ export function toggleTracking(enable = !enableTracking): void {
2626
}
2727
}
2828

29-
const provider: TrackerProvider | null = null;
29+
let provider: TrackerProvider | null = null;
3030
function findProvider(name?: ProviderType): TrackerProvider {
3131
const appId = getConfig("appId");
3232
const disabled = !enableTracking;
@@ -50,5 +50,6 @@ export function getTrackerProvider(): TrackerProvider {
5050
return provider;
5151
}
5252

53-
return findProvider(process.env.TRACKER_PROVIDER as ProviderType);
53+
provider = findProvider(process.env.TRACKER_PROVIDER as ProviderType);
54+
return provider;
5455
}

src/common/tracker/provider/PostHogProvider.ts

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
import { version } from "@common/utils/package";
13
import type { Integration } from "@sentry/types";
4+
import { totalmem } from "os";
25
import type FrontPostHog from "posthog-js";
36
import type NodeJsPostHog from "posthog-node";
47

@@ -10,18 +13,26 @@ import { TrackerProvider } from "./TrackerProvider";
1013
const TRACKER_FAKE_HOST = (process.env.TRACKER_FAKE_HREF ?? "").split("//")[1]!;
1114

1215
const DEFAULT_$SET = {
13-
// eslint-disable-next-line @typescript-eslint/naming-convention
1416
$current_url: process.env.TRACKER_FAKE_HREF,
1517
$host: TRACKER_FAKE_HOST,
1618
$pathname: "",
1719
};
1820
const DEFAULT_$SET_ONCE = {
19-
// eslint-disable-next-line @typescript-eslint/naming-convention
2021
$initial_current_url: process.env.TRACKER_FAKE_HREF,
21-
// eslint-disable-next-line @typescript-eslint/naming-convention
2222
$initial_pathname: "",
2323
$pathname: "",
2424
};
25+
26+
function getCommonProperties() {
27+
return {
28+
arch: process.arch,
29+
date: new Date(),
30+
os: process.platform,
31+
ram: totalmem() / 1024 / 1024 / 1024,
32+
version,
33+
};
34+
}
35+
2536
export class PostHogProvider extends TrackerProvider<
2637
typeof FrontPostHog,
2738
NodeJsPostHog
@@ -51,30 +62,30 @@ export class PostHogProvider extends TrackerProvider<
5162
this.tracker.capture({
5263
distinctId: this.appId,
5364
event: "$identify",
65+
properties: getCommonProperties(),
5466
});
5567
this.inited = true;
56-
} else {
57-
return new Promise<void>(
58-
(resolve) =>
59-
void import("posthog-js").then(({ default: frontPostHog }) => {
60-
this.hijackPostHog(frontPostHog);
61-
frontPostHog.init(process.env.TRACKER_POSTHOG_API_KEY, {
62-
/* eslint-disable @typescript-eslint/naming-convention */
63-
api_host: process.env.TRACKER_POSTHOG_URL,
64-
autocapture: false,
65-
capture_pageview: false,
66-
disable_session_recording: true,
67-
loaded: (posthog) => {
68-
this.tracker = posthog;
69-
this.tracker.identify(this.appId);
70-
this.inited = true;
71-
resolve();
72-
},
73-
/* eslint-enable @typescript-eslint/naming-convention */
74-
});
75-
})
76-
);
68+
return;
7769
}
70+
71+
return new Promise<void>(
72+
(resolve) =>
73+
void import("posthog-js").then(({ default: frontPostHog }) => {
74+
this.hijackPostHog(frontPostHog);
75+
frontPostHog.init(process.env.TRACKER_POSTHOG_API_KEY, {
76+
api_host: process.env.TRACKER_POSTHOG_URL,
77+
autocapture: false,
78+
capture_pageview: false,
79+
disable_session_recording: true,
80+
loaded: (posthog) => {
81+
this.tracker = posthog;
82+
this.tracker.identify(this.appId, getCommonProperties());
83+
this.inited = true;
84+
resolve();
85+
},
86+
});
87+
})
88+
);
7889
}
7990

8091
public async uninit(): Promise<void> {
@@ -101,7 +112,13 @@ export class PostHogProvider extends TrackerProvider<
101112

102113
public track<TEvent extends TrackEvent>(...args: TrackArgs<TEvent>): void {
103114
const [event, properties] = args;
104-
if (!this.tracker || this.disabled) return;
115+
if (!this.tracker || this.disabled) {
116+
disabled: this.disabled,
117+
tracker: this.tracker,
118+
});
119+
return;
120+
}
121+
105122
if (this.isMain(this.tracker)) {
106123
this.tracker.capture({
107124
distinctId: this.appId,
@@ -111,12 +128,15 @@ export class PostHogProvider extends TrackerProvider<
111128
} else {
112129
this.tracker.capture(event, properties);
113130
}
131+
console.log(`Event ${event} tracked successfully`);
114132
}
115133

116134
public enable(): void {
117135
if (!this.tracker) return;
118136
if (!this.isMain(this.tracker)) {
119137
this.tracker.opt_in_capturing();
138+
const optInProperties = getCommonProperties();
139+
this.tracker.capture("$opt_in", optInProperties);
120140
}
121141
super.enable();
122142
}
@@ -139,7 +159,6 @@ export class PostHogProvider extends TrackerProvider<
139159
const originalCaptureFn = posthog.capture.bind(posthog);
140160
const hijack = {
141161
$set: DEFAULT_$SET,
142-
// eslint-disable-next-line @typescript-eslint/naming-convention
143162
$set_once: DEFAULT_$SET_ONCE,
144163
...DEFAULT_$SET,
145164
};

src/common/tracker/type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type TrackErrorId = string & { readonly _trackErrorId?: never };
1313
* Tracking Plan props representation.
1414
*/
1515
export interface TrackCoreEventProps {
16-
"App Closed": { date: Date };
16+
"App Closed": { date: Date; version: string };
1717
"App First Opened": {
1818
arch: string;
1919
date: Date;

src/main/index.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,18 @@ const askBeforeLeaving = () => {
6464
}
6565
event.preventDefault();
6666
const language = getLanguage();
67-
let title = "";
68-
let message = "";
69-
let detail = "";
70-
let no = "";
71-
let yes = "";
67+
let title = "Bye bye!";
68+
let message = "Are you sure you want to leave?";
69+
let detail = "All data that has not been saved will be permanently lost!";
70+
let no = "No";
71+
let yes = "Yes";
7272
if (language === "fr") {
7373
title = "Bye bye !";
7474
message = "Êtes-vous sûr•e de vouloir quitter ?";
7575
detail =
7676
"Toutes les données qui n'ont pas été sauvegardées seront perdues définitivement !";
7777
no = "Non";
7878
yes = "Oui";
79-
} else {
80-
title = "Bye bye!";
81-
message = "Are you sure you want to leave?";
82-
detail = "All data that has not been saved will be permanently lost!";
83-
no = "No";
84-
yes = "Yes";
8579
}
8680
const options = {
8781
buttons: [no, yes],
@@ -174,8 +168,6 @@ void app.whenReady().then(() => {
174168
// initialization and is ready to create browser windows.
175169
// Some APIs can only be used after this event occurs.
176170
app.on("ready", async () => {
177-
// -- init all "modules"
178-
// TODO: do real modules
179171
await initNewUserConfig();
180172
await initTracking();
181173
loadHash();
@@ -221,7 +213,7 @@ app.on("activate", async () => {
221213

222214
app.on("will-quit", async (event) => {
223215
event.preventDefault();
224-
getTrackerProvider().track("App Closed", { date: new Date() });
216+
getTrackerProvider().track("App Closed", { date: new Date(), version });
225217
await sleep(1000);
226218
process.exit();
227219
});

webpack.main.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ require("dotenv").config();
66

77
module.exports =
88
/** @param {import("webpack").Configuration} config */ function (config) {
9+
const isProd = config.mode === "production";
10+
911
const workers = glob
1012
.sync("./src/main/**/*.worker.ts")
1113
.map((filePath) => {
@@ -17,7 +19,7 @@ module.exports =
1719
return acc;
1820
}, {});
1921

20-
if (config.mode === "production") {
22+
if (isProd) {
2123
for (const plugin of config.plugins) {
2224
if (plugin instanceof webpack.BannerPlugin) {
2325
plugin.options.exclude = /(preload|\.worker)\.js$/i;

webpack.renderer.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ require("dotenv").config();
77

88
module.exports =
99
/** @param {import("webpack").Configuration} config */ function (config) {
10+
const isProd = config.mode === "production";
11+
1012
const workers = glob
1113
.sync("./src/renderer/**/*.fork.ts")
1214
.map((filePath) => {
@@ -46,7 +48,7 @@ module.exports =
4648
// }
4749
// });
4850

49-
if (config.mode === "production") {
51+
if (isProd) {
5052
if (!config.plugins?.length) {
5153
config.plugins = [];
5254
}

0 commit comments

Comments
 (0)