in packages/fxa-settings/src/lib/metrics.ts [168:236]
export async function init(enabled: boolean, flowQueryParams: QueryParams) {
setEnabled(enabled);
if (!initialized) {
// Initialize from the qs if we have the critical flow pieces
if (
flowQueryParams.deviceId &&
flowQueryParams.flowBeginTime &&
flowQueryParams.flowId
) {
flowEventData = { ...flowQueryParams };
} else {
// Or initialize as a fresh flow
const flowResponse = await fetch('/metrics-flow');
flowEventData = await flowResponse.json();
}
let uniqueUserIdFromLocalStorage;
try {
uniqueUserIdFromLocalStorage = JSON.parse(
localStorage.getItem('__fxa_storage.uniqueUserId')!
);
} catch (e) {}
flowEventData.uniqueUserId = flowQueryParams.uniqueUserId
? flowQueryParams.uniqueUserId
: uniqueUserIdFromLocalStorage;
// Make sure the default values are set. If default values are not set, metrics posts can fail.
// There are situations where arriving directly to /beta/settings means query params won't be
// provided, and we will use a set of fixed defaults.
if (!flowEventData.uniqueUserId) {
flowEventData.uniqueUserId = uuid();
try {
localStorage.setItem(
'__fxa_storage.uniqueUserId',
JSON.stringify(flowEventData.uniqueUserId)
);
} catch (e) {}
}
flowEventData.broker =
configurableProperties.broker ||
flowEventData.broker ||
flowEventDataDefaults.broker;
flowEventData.context =
configurableProperties.context ||
flowEventData.context ||
flowEventDataDefaults.broker;
flowEventData.service =
configurableProperties.service ||
flowEventData.service ||
flowEventDataDefaults.service;
flowEventData.isSampledUser =
configurableProperties.isSampledUser ||
flowEventData.isSampledUser ||
flowEventDataDefaults.isSampledUser;
flowEventData.deviceId =
configurableProperties.deviceId ||
flowEventData.deviceId ||
flowEventDataDefaults.deviceId;
initialized = true;
}
}