fix(amplitude) sync device ID on web too

Note the use of jitsiLocalStorage since we also need to consider the
case when local storage is performed in the host page when in an
iframe.
This commit is contained in:
Saúl Ibarra Corretgé
2025-07-04 15:39:21 +02:00
committed by Saúl Ibarra Corretgé
parent d2ed9ffef6
commit ec1bfe73b3
3 changed files with 32 additions and 22 deletions

View File

@@ -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(),

View File

@@ -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);
}
}

View File

@@ -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<any> {
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));
}
}
}