From 1cde7e63c7de2bfcdebb0ccaa37c380618e7fff2 Mon Sep 17 00:00:00 2001 From: Hristo Terezov Date: Thu, 23 Jan 2020 18:36:31 +0000 Subject: [PATCH] feat(Amplitude): Set device id from cookie. (#4997) --- .../analytics/handlers/AmplitudeHandler.js | 1 + .../handlers/amplitude/Amplitude.native.js | 9 +++++- .../handlers/amplitude/Amplitude.web.js | 31 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/react/features/analytics/handlers/AmplitudeHandler.js b/react/features/analytics/handlers/AmplitudeHandler.js index 8ac061a66..fcc198512 100644 --- a/react/features/analytics/handlers/AmplitudeHandler.js +++ b/react/features/analytics/handlers/AmplitudeHandler.js @@ -28,6 +28,7 @@ export default class AmplitudeHandler extends AbstractHandler { }; amplitude.getInstance(this._amplitudeOptions).init(amplitudeAPPKey, undefined, { includeReferrer: true }); + amplitude.fixDeviceID(this._amplitudeOptions); if (user) { amplitude.getInstance(this._amplitudeOptions).setUserId(user); diff --git a/react/features/analytics/handlers/amplitude/Amplitude.native.js b/react/features/analytics/handlers/amplitude/Amplitude.native.js index 56dbd2ced..b19c5f6ae 100644 --- a/react/features/analytics/handlers/amplitude/Amplitude.native.js +++ b/react/features/analytics/handlers/amplitude/Amplitude.native.js @@ -111,5 +111,12 @@ export default { } return instance; - } + }, + + /** + * Currently not implemented. + * + * @returns {void} + */ + fixDeviceID() { } // eslint-disable-line no-empty-function }; diff --git a/react/features/analytics/handlers/amplitude/Amplitude.web.js b/react/features/analytics/handlers/amplitude/Amplitude.web.js index 449c9d3d2..b2b280cca 100644 --- a/react/features/analytics/handlers/amplitude/Amplitude.web.js +++ b/react/features/analytics/handlers/amplitude/Amplitude.web.js @@ -1,7 +1,38 @@ import amplitude from 'amplitude-js'; export default { + /** + * Returns the AmplitudeClient instance. + * + * @param {Object} options - Optional parameters. + * @property {string} options.instanceName - The name of the AmplitudeClient instance. + * @returns {AmplitudeClient} + */ getInstance(options = {}) { return amplitude.getInstance(options.instanceName); + }, + + /** + * Sets the device id to the value of __AMDID cookie or sets the __AMDID cookie value to the current device id in + * case the __AMDID cookie is not set. + * + * @param {*} options - Optional parameters. + * @property {string} options.instanceName - The name of the AmplitudeClient instance. + * @property {string} options.host - The host from the original URL. + * @returns {void} + */ + fixDeviceID(options) { + const deviceId = document.cookie.replace(/(?:(?:^|.*;\s*)__AMDID\s*=\s*([^;]*).*$)|^.*$/, '$1'); + const instance = this.getInstance(options); + + if (deviceId === '') { + const { host = '' } = options; + + document.cookie + = `__AMDID=${instance.options.deviceId};max-age=630720000${ + host === '' ? '' : `;domain=.${host}`}`; // max-age=10 years + } else { + instance.setDeviceId(deviceId); + } } };