diff --git a/react/features/analytics/handlers/AmplitudeHandler.ts b/react/features/analytics/handlers/AmplitudeHandler.ts index 65c489289d..30bd93538d 100644 --- a/react/features/analytics/handlers/AmplitudeHandler.ts +++ b/react/features/analytics/handlers/AmplitudeHandler.ts @@ -10,8 +10,6 @@ import amplitude from './amplitude/lib'; * Analytics handler for Amplitude. */ export default class AmplitudeHandler extends AbstractHandler { - _deviceId: string; - _userId: Object; /** * Creates new instance of the Amplitude analytics handler. @@ -44,13 +42,7 @@ export default class AmplitudeHandler extends AbstractHandler { if (navigator.product === 'ReactNative') { amplitude.init(amplitudeAPPKey); - fixDeviceID(amplitude).then(() => { - const deviceId = amplitude.getDeviceId(); - - if (deviceId) { - this._deviceId = deviceId; - } - }); + fixDeviceID(amplitude); } else { const amplitudeOptions: any = { includeReferrer: true, @@ -65,7 +57,6 @@ export default class AmplitudeHandler extends AbstractHandler { } if (user) { - this._userId = user; amplitude.setUserId(user); } } @@ -113,13 +104,6 @@ export default class AmplitudeHandler extends AbstractHandler { * @returns {Object} */ getIdentityProps() { - if (navigator.product === 'ReactNative') { - return { - deviceId: this._deviceId, - userId: this._userId - }; - } - return { sessionId: amplitude.getSessionId(), deviceId: amplitude.getDeviceId(), diff --git a/react/features/analytics/handlers/amplitude/fixDeviceID.native.ts b/react/features/analytics/handlers/amplitude/fixDeviceID.native.ts index ced0389365..c08cc6300b 100644 --- a/react/features/analytics/handlers/amplitude/fixDeviceID.native.ts +++ b/react/features/analytics/handlers/amplitude/fixDeviceID.native.ts @@ -17,7 +17,7 @@ export async function fixDeviceID(amplitude: Types.ReactNativeClient) { const current = await DefaultPreference.get('amplitudeDeviceId'); if (current) { - await amplitude.setDeviceId(current); + amplitude.setDeviceId(current); } else { const uid = await getUniqueId(); @@ -27,7 +27,7 @@ export async function fixDeviceID(amplitude: Types.ReactNativeClient) { return; } - await amplitude.setDeviceId(uid as string); + amplitude.setDeviceId(uid as string); await DefaultPreference.set('amplitudeDeviceId', uid as string); } } diff --git a/react/features/analytics/handlers/amplitude/fixDeviceID.web.ts b/react/features/analytics/handlers/amplitude/fixDeviceID.web.ts index cc4e5c190f..35dcb89eee 100644 --- a/react/features/analytics/handlers/amplitude/fixDeviceID.web.ts +++ b/react/features/analytics/handlers/amplitude/fixDeviceID.web.ts @@ -1,11 +1,37 @@ import { Types } from '@amplitude/analytics-browser'; +// @ts-ignore +import { jitsiLocalStorage } from '@jitsi/js-utils'; + +import logger from '../../logger'; + +/** + * Key used to store the device id in local storage. + */ +const DEVICE_ID_KEY = '__AMDID'; /** * Custom logic for setting the correct device id. * - * @param {Types.BrowserClient} _amplitude - The amplitude instance. + * @param {Types.BrowserClient} amplitude - The amplitude instance. * @returns {void} */ -export function fixDeviceID(_amplitude: Types.BrowserClient): Promise { - return new Promise(resolve => resolve(true)); +export function fixDeviceID(amplitude: Types.BrowserClient) { + const deviceId = jitsiLocalStorage.getItem(DEVICE_ID_KEY); + + if (deviceId) { + // Set the device id in Amplitude. + try { + amplitude.setDeviceId(JSON.parse(deviceId)); + } catch (error) { + logger.error('Failed to set device ID in Amplitude', error); + + return Promise.resolve(false); + } + } else { + const newDeviceId = amplitude.getDeviceId(); + + if (newDeviceId) { + jitsiLocalStorage.setItem(DEVICE_ID_KEY, JSON.stringify(newDeviceId)); + } + } }