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