feat(Amplitude): Set device id from cookie. (#4997)
This commit is contained in:
parent
a53d284bbe
commit
1cde7e63c7
|
@ -28,6 +28,7 @@ export default class AmplitudeHandler extends AbstractHandler {
|
||||||
};
|
};
|
||||||
|
|
||||||
amplitude.getInstance(this._amplitudeOptions).init(amplitudeAPPKey, undefined, { includeReferrer: true });
|
amplitude.getInstance(this._amplitudeOptions).init(amplitudeAPPKey, undefined, { includeReferrer: true });
|
||||||
|
amplitude.fixDeviceID(this._amplitudeOptions);
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
amplitude.getInstance(this._amplitudeOptions).setUserId(user);
|
amplitude.getInstance(this._amplitudeOptions).setUserId(user);
|
||||||
|
|
|
@ -111,5 +111,12 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Currently not implemented.
|
||||||
|
*
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
fixDeviceID() { } // eslint-disable-line no-empty-function
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,38 @@
|
||||||
import amplitude from 'amplitude-js';
|
import amplitude from 'amplitude-js';
|
||||||
|
|
||||||
export default {
|
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 = {}) {
|
getInstance(options = {}) {
|
||||||
return amplitude.getInstance(options.instanceName);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue