Skip to content

Commit

Permalink
Rename userId to experimentationId
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Helmer <rhelmer@mozilla.com>
  • Loading branch information
Vinnl and rhelmer committed Apr 30, 2024
1 parent 6eeebb0 commit ed04f71
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const DashboardWrapper = (props: DashboardWrapperProps) => {
monthly: 42.42,
}}
isNewUser={true}
telemetryId="arbitrary-telemetry-id"
experimentationId="arbitrary-experimentation-id"
elapsedTimeInDaysSinceInitialScan={
props.elapsedTimeInDaysSinceInitialScan
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export type Props = {
fxaSettingsUrl: string;
scanCount: number;
isNewUser: boolean;
telemetryId: string;
experimentationId: string;
elapsedTimeInDaysSinceInitialScan?: number;
totalNumberOfPerformedScans?: number;
};
Expand All @@ -82,7 +82,7 @@ export type TabData = {

export const View = (props: Props) => {
const l10n = useL10n();
const recordTelemetry = useTelemetry(props.telemetryId);
const recordTelemetry = useTelemetry(props.experimentationId);
const countryCode = useContext(CountryCodeContext);

const adjustedScanResults = props.userScanData.results.map((scanResult) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { getEnabledFeatureFlags } from "../../../../../../../db/tables/featureFl
import { getAttributionsFromCookiesOrDb } from "../../../../../../functions/server/attributions";
import { checkSession } from "../../../../../../functions/server/checkSession";
import { isPrePlusUser } from "../../../../../../functions/server/isPrePlusUser";
import { getUserId } from "../../../../../../functions/server/getExperimentationId";
import { getExperimentationId } from "../../../../../../functions/server/getExperimentationId";
import { getElapsedTimeInDaysSinceInitialScan } from "../../../../../../functions/server/getElapsedTimeInDaysSinceInitialScan";
import { getExperiments } from "../../../../../../functions/server/getExperiments";
import { getLocale } from "../../../../../../functions/universal/getLocale";
Expand Down Expand Up @@ -100,7 +100,7 @@ export default async function DashboardPage() {
email: session.user.email,
});

const experimentationId = getUserId(session.user);
const experimentationId = getExperimentationId(session.user);
const experimentData = await getExperiments({
experimentationId: experimentationId,
countryCode: countryCode,
Expand Down Expand Up @@ -133,7 +133,7 @@ export default async function DashboardPage() {
totalNumberOfPerformedScans={profileStats?.total}
isNewUser={isNewUser}
elapsedTimeInDaysSinceInitialScan={elapsedTimeInDaysSinceInitialScan}
telemetryId={experimentationId}
experimentationId={experimentationId}
experimentData={experimentData}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getSha1 } from "../../../../../../../utils/fxa";
import { getAttributionsFromCookiesOrDb } from "../../../../../../functions/server/attributions";
import { getEnabledFeatureFlags } from "../../../../../../../db/tables/featureFlags";
import { getLatestOnerepScan } from "../../../../../../../db/tables/onerep_scans";
import { getUserId } from "../../../../../../functions/server/getExperimentationId";
import { getExperimentationId } from "../../../../../../functions/server/getExperimentationId";
import { getExperiments } from "../../../../../../functions/server/getExperiments";
import { getLocale } from "../../../../../../functions/universal/getLocale";
import { getCountryCode } from "../../../../../../functions/server/getCountryCode";
Expand Down Expand Up @@ -60,7 +60,7 @@ export default async function SettingsPage() {

const headersList = headers();
const countryCode = getCountryCode(headersList);
const experimentationId = getUserId(session.user);
const experimentationId = getExperimentationId(session.user);
const experimentData = await getExperiments({
experimentationId: experimentationId,
countryCode: countryCode,
Expand Down
4 changes: 2 additions & 2 deletions src/app/(proper_react)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ReactAriaI18nProvider } from "../../contextProviders/react-aria";
import { CountryCodeProvider } from "../../contextProviders/country-code";
import { getCountryCode } from "../functions/server/getCountryCode";
import { PageLoadEvent } from "../components/client/PageLoadEvent";
import { getUserId } from "../functions/server/getExperimentationId";
import { getExperimentationId } from "../functions/server/getExperimentationId";
import { getEnabledFeatureFlags } from "../../db/tables/featureFlags";

export default async function Layout({ children }: { children: ReactNode }) {
Expand All @@ -30,7 +30,7 @@ export default async function Layout({ children }: { children: ReactNode }) {
<CountryCodeProvider countryCode={countryCode}>
{children}
<PageLoadEvent
userId={getUserId(session?.user ?? null)}
experimentationId={getExperimentationId(session?.user ?? null)}
enabledFlags={enabledFlags}
/>
</CountryCodeProvider>
Expand Down
11 changes: 7 additions & 4 deletions src/app/components/client/PageLoadEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useTelemetry } from "../../hooks/useTelemetry";
import { GleanMetricMap } from "../../../telemetry/generated/_map";

export type Props = {
userId: string;
experimentationId: string;
enabledFlags: FeatureFlagName[];
};

Expand All @@ -25,10 +25,13 @@ export const PageLoadEvent = (props: Props) => {
]);
const pathname = usePathname();

const recordTelemetry = useTelemetry(props.userId);
const recordTelemetry = useTelemetry(props.experimentationId);

if (props.userId.startsWith("guest") && !cookies.experimentationId) {
setCookie("experimentationId", props.userId);
if (
props.experimentationId.startsWith("guest") &&
!cookies.experimentationId
) {
setCookie("experimentationId", props.experimentationId);
}

// On first load of the page, record a page view.
Expand Down
14 changes: 8 additions & 6 deletions src/app/functions/server/getExperimentationId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export type ExperimentationId = UUID | `guest-${UUID}`;
* @param user
* @returns v5 UUID, possibly with `guest-` prefix.
*/
export function getUserId(user: Session["user"] | null): ExperimentationId {
export function getExperimentationId(
user: Session["user"] | null,
): ExperimentationId {
const accountId = user?.subscriber?.id;
let userId: null | ExperimentationId;
let experimentationId: null | ExperimentationId;

if (accountId && typeof accountId === "string") {
// If the user is logged in, use the Subscriber ID.
Expand All @@ -29,19 +31,19 @@ export function getUserId(user: Session["user"] | null): ExperimentationId {
"NIMBUS_UUID_NAMESPACE not set, cannot create experimentationId",
);
}
userId = uuidv5(accountId, namespace) as UUID;
experimentationId = uuidv5(accountId, namespace) as UUID;
} else {
// if the user is not logged in, use a cookie with a randomly-generated Nimbus user ID.
// TODO: could we use client ID for this? There's no supported way to get it from GleanJS.
const cookie = cookies().get("experimentationId");
if (cookie) {
userId = cookie.value as ExperimentationId;
experimentationId = cookie.value as ExperimentationId;
} else {
// TODO Cookies can only be set in server action or route handler
// @see https://nextjs.org/docs/app/api-reference/functions/cookies#cookiessetname-value-options
// This is set client-side in <PageLoadEvent>.
userId = `guest-${randomUUID()}`;
experimentationId = `guest-${randomUUID()}`;
}
}
return userId;
return experimentationId;
}

0 comments on commit ed04f71

Please sign in to comment.