You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Amplitude.web.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import amplitude from 'amplitude-js';
  2. export default {
  3. /**
  4. * Returns the AmplitudeClient instance.
  5. *
  6. * @param {Object} options - Optional parameters.
  7. * @property {string} options.instanceName - The name of the AmplitudeClient instance.
  8. * @returns {AmplitudeClient}
  9. */
  10. getInstance(options = {}) {
  11. return amplitude.getInstance(options.instanceName);
  12. },
  13. /**
  14. * Sets the device id to the value of __AMDID cookie or sets the __AMDID cookie value to the current device id in
  15. * case the __AMDID cookie is not set.
  16. *
  17. * @param {*} options - Optional parameters.
  18. * @property {string} options.instanceName - The name of the AmplitudeClient instance.
  19. * @property {string} options.host - The host from the original URL.
  20. * @returns {void}
  21. */
  22. fixDeviceID(options) {
  23. const deviceId = document.cookie.replace(/(?:(?:^|.*;\s*)__AMDID\s*=\s*([^;]*).*$)|^.*$/, '$1');
  24. const instance = this.getInstance(options);
  25. if (deviceId === '') {
  26. const { host = '' } = options;
  27. document.cookie
  28. = `__AMDID=${instance.options.deviceId};max-age=630720000${
  29. host === '' ? '' : `;domain=.${host}`}`; // max-age=10 years
  30. } else {
  31. instance.setDeviceId(deviceId);
  32. }
  33. }
  34. };