Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/renderer/src/extensions/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ function init(context: IExtensionContext): boolean {
);

analyticsLog("info", "Analytics started");

// For events that wait on the account being known, such as a login's source.
context.api.events.emit("analytics-started");
} catch (err) {
// there is no error handling anywhere invoking initializeAnalytics,
// the results aren't even adviced, so any unhandled exception here would
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const HealthCheckPage = ({ api, onRefresh, active, registerReset }: IHealthCheck
const dispatch = useDispatch();
// Logging in is a prerequisite for the Nexus-backed checks rather than a fix for an
// issue, so it goes through the same OAuth flow as the header's login button.
const requestLogin = useNexusLogin();
const requestLogin = useNexusLogin("health_check");
const [selected, setSelected] = useState<IListedEntry | null>(null);
const [selectedTab, setSelectedTab] = useState("active");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const LoginDialog = ({
}: ILoginDialogProps) => {
const { t } = useTranslation(["nexus_integration"]);
const dispatch = useDispatch();
const login = useNexusLogin();
const login = useNexusLogin("login_dialog");
const oauthPending = useSelector(oauthPendingSelector);
const isOpen = visible || oauthPending !== undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const ignoreDismiss = () => undefined;
export const LoginPrompt = ({ visible, onHide }: ILoginPromptProps) => {
const { t } = useTranslation(["nexus_integration"]);
const dispatch = useDispatch();
const login = useNexusLogin();
const login = useNexusLogin("startup_prompt");
const [dontShowAgain, setDontShowAgain] = useState(false);

const rememberChoice = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { EventEmitter } from "events";

import { beforeEach, describe, expect, it, vi } from "vitest";

import type { IExtensionApi } from "@/types/IExtensionContext";

const experiment = vi.hoisted(() => ({ cohort: "test" as "test" | "control" }));

vi.mock("../experiments/loginPromptExperiment", () => ({
loginPromptCohort: () => experiment.cohort,
}));

/** `consent` is the analytics setting, which tests flip to model consent given later. */
const makeApi = () => {
const events = new EventEmitter();
const tracked = vi.fn();
events.on("analytics-track-mixpanel-event", tracked);
const settings = { consent: true as boolean | undefined };
const getState = () => ({ settings: { analytics: { enabled: settings.consent } } });
return { api: { events, getState } as unknown as IExtensionApi, events, tracked, settings };
};

// The pending source is module state, so each test gets a fresh copy of the module.
const load = async () => {
vi.resetModules();
return import("./loginTracker.js");
};

describe("login tracking", () => {
beforeEach(() => {
vi.resetModules();
experiment.cohort = "test";
});

it("sends the login's source once analytics starts", async () => {
const { noteLoginCompleted, trackLoginsWhenAnalyticsStarts } = await load();
const { api, events, tracked } = makeApi();
trackLoginsWhenAnalyticsStarts(api);

noteLoginCompleted(api, "startup_prompt");
expect(tracked).not.toHaveBeenCalled();

events.emit("analytics-started");

expect(tracked).toHaveBeenCalledOnce();
expect(tracked).toHaveBeenCalledWith({
eventName: "app_login_completed",
properties: { source: "startup_prompt", login_prompt_cohort: "test" },
});
});

it("sends nothing when analytics starts without a login to report", async () => {
const { trackLoginsWhenAnalyticsStarts } = await load();
const { api, events, tracked } = makeApi();
trackLoginsWhenAnalyticsStarts(api);

events.emit("analytics-started");

expect(tracked).not.toHaveBeenCalled();
});

it("sends a login only once, however often analytics restarts", async () => {
const { noteLoginCompleted, trackLoginsWhenAnalyticsStarts } = await load();
const { api, events, tracked } = makeApi();
trackLoginsWhenAnalyticsStarts(api);

noteLoginCompleted(api, "header");
events.emit("analytics-started");
events.emit("analytics-started");

expect(tracked).toHaveBeenCalledOnce();
});

it("credits the latest login when an earlier one was never sent", async () => {
const { noteLoginCompleted, trackLoginsWhenAnalyticsStarts } = await load();
const { api, events, tracked } = makeApi();
trackLoginsWhenAnalyticsStarts(api);

noteLoginCompleted(api, "header");
noteLoginCompleted(api, "health_check");
events.emit("analytics-started");

expect(tracked).toHaveBeenCalledWith({
eventName: "app_login_completed",
properties: { source: "health_check", login_prompt_cohort: "test" },
});
});

it("doesn't send a login made before the user consented, even once they do", async () => {
const { noteLoginCompleted, trackLoginsWhenAnalyticsStarts } = await load();
const { api, events, tracked, settings } = makeApi();
trackLoginsWhenAnalyticsStarts(api);

settings.consent = undefined; // never asked: the consent prompt only follows a login
noteLoginCompleted(api, "startup_prompt");
settings.consent = true; // says yes at the prompt that follows
events.emit("analytics-started");

expect(tracked).not.toHaveBeenCalled();
});

it("drops an earlier held login when a later one is made without consent", async () => {
const { noteLoginCompleted, trackLoginsWhenAnalyticsStarts } = await load();
const { api, events, tracked, settings } = makeApi();
trackLoginsWhenAnalyticsStarts(api);

noteLoginCompleted(api, "header");
settings.consent = false;
noteLoginCompleted(api, "startup_prompt");
settings.consent = true;
events.emit("analytics-started");

expect(tracked).not.toHaveBeenCalled();
});

it("reports the cohort the login was made in, even if it reads differently later", async () => {
const { noteLoginCompleted, trackLoginsWhenAnalyticsStarts } = await load();
const { api, events, tracked } = makeApi();
trackLoginsWhenAnalyticsStarts(api);

experiment.cohort = "control";
noteLoginCompleted(api, "header");
experiment.cohort = "test";
events.emit("analytics-started");

expect(tracked).toHaveBeenCalledWith({
eventName: "app_login_completed",
properties: { source: "header", login_prompt_cohort: "control" },
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { MixpanelEvent } from "@/extensions/analytics/mixpanel/MixpanelEvents";
import { isAnalyticsEnabled } from "@/extensions/analytics/selectors";
import type { IExtensionApi } from "@/types/IExtensionContext";

import { type LoginPromptCohort, loginPromptCohort } from "../experiments/loginPromptExperiment";

/** Which button started the login; `login_dialog` is the dialog's own, opened by `ensureLoggedIn`. */
export type LoginSource = "startup_prompt" | "header" | "health_check" | "login_dialog";

type LoginCompletedProps = { source: LoginSource; login_prompt_cohort: LoginPromptCohort };

export const createLoginTracker = (api: IExtensionApi) => ({
trackLoginCompleted: (props: LoginCompletedProps) =>
api.events.emit("analytics-track-mixpanel-event", {
eventName: "app_login_completed",
properties: { ...props },
} satisfies MixpanelEvent),
});

// Analytics only starts once the account is known, after the login, so the login waits here.
let pending: LoginCompletedProps | undefined;

/**
* Holds a successful login to send once analytics starts. Only when consent was given before the
* login: agreeing afterwards doesn't cover it. A later login replaces or clears the held one.
*/
export const noteLoginCompleted = (api: IExtensionApi, source: LoginSource): void => {
pending = isAnalyticsEnabled(api.getState())
? { source, login_prompt_cohort: loginPromptCohort() }
: undefined;
};

/** Sends the held login, if any, when analytics starts. Call once at startup. */
export const trackLoginsWhenAnalyticsStarts = (api: IExtensionApi): void => {
const tracker = createLoginTracker(api);
api.events.on("analytics-started", () => {
if (pending !== undefined) {
tracker.trackLoginCompleted(pending);
pending = undefined;
}
});
};
2 changes: 2 additions & 0 deletions src/renderer/src/extensions/nexus_integration/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import {
} from "./constants";
import * as eh from "./eventHandlers";
import { LoginDialogForCohort } from "./experiments/LoginDialogForCohort";
import { trackLoginsWhenAnalyticsStarts } from "./hooks/loginTracker";
import {
adoptMembershipRead,
ensureFreshMembership,
Expand Down Expand Up @@ -1130,6 +1131,7 @@ function once(api: IExtensionApi, callbacks: Array<(nexus: NexusT) => void>) {
api.onAsync("get-trending-mods", eh.onGetTrendingMods(api, nexus));
api.onAsync("send-metric", eh.sendMetric(api, nexus));
trackMembershipReads(api);
trackLoginsWhenAnalyticsStarts(api);
nxmProtocol.start();
api.events.on("refresh-user-info", eh.onRefreshUserInfo(nexus, api));
api.events.on("endorse-mod", eh.onEndorseMod(api, nexus));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { setUserAPIKey } from "../actions/account";
import { setLoginId, setOauthPending } from "../actions/session";
import { NEXUS_BASE_URL } from "../constants";
import { OAUTH_URL } from "../constants";
import { noteLoginCompleted } from "../hooks/loginTracker";
import type { IValidateKeyData } from "../types/IValidateKeyData";
import { getPageURL } from "../util/sso";

Expand Down Expand Up @@ -398,7 +399,9 @@ class LoginDialog extends ComponentEx<IProps, ILoginDialogState> {
this.nextState.requested = true;
this.context.api.events.emit("request-nexus-login", (err: Error) => {
this.nextState.requested = false;
if (err !== null && !(err instanceof UserCanceled)) {
if (err === null) {
noteLoginCompleted(this.context.api, "login_dialog");
} else if (!(err instanceof UserCanceled)) {
this.context.api.showErrorNotification("Failed to get access key", err, {
id: "failed-get-nexus-key",
allowReport: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const PremiumIndicator: FC<React.PropsWithChildren<unknown>> = () => {
const showAd = useSelector(shouldShowPremiumAd);
const premium = useSelector(isPremiumSelector);

const handleLogin = useNexusLogin();
const handleLogin = useNexusLogin("header");

const handleGoPremium = useCallback(() => {
window.api.shell.openUrl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import { useDispatch } from "react-redux";
import { setDialogVisible } from "@/actions";
import { useExtensionContext } from "@/ExtensionProvider";
import { fixLoginPromptCohort } from "@/extensions/nexus_integration/experiments/loginPromptExperiment";
import {
type LoginSource,
noteLoginCompleted,
} from "@/extensions/nexus_integration/hooks/loginTracker";

import { UserCanceled } from "../../../../util/CustomErrors";

/**
* Starts the Nexus Mods login flow. The login dialog opens on its own once the login is waiting on
* the website, so it goes straight to the progress view. Cancelling is a choice rather than a
* fault, so only a real failure is reported.
* fault, so only a real failure is reported. `source` says which button started it, for analytics.
*/
export const useNexusLogin = (): (() => void) => {
export const useNexusLogin = (source: LoginSource): (() => void) => {
const dispatch = useDispatch();
const extensions = useExtensionContext();
const api = extensions.getApi();
Expand All @@ -24,12 +28,14 @@ export const useNexusLogin = (): (() => void) => {
}

api.events.emit("request-nexus-login", (err: Error) => {
if (err != null && !(err instanceof UserCanceled)) {
if (err == null) {
noteLoginCompleted(api, source);
} else if (!(err instanceof UserCanceled)) {
api.showErrorNotification?.("Login Failed", err, {
id: "failed-get-nexus-key",
allowReport: false,
});
}
});
}, [api, dispatch]);
}, [api, dispatch, source]);
};