浏览代码

feat(Amplitude): Set device id from cookie. (#4997)

master
Hristo Terezov 5 年前
父节点
当前提交
1cde7e63c7
没有帐户链接到提交者的电子邮件

+ 1
- 0
react/features/analytics/handlers/AmplitudeHandler.js 查看文件

28
         };
28
         };
29
 
29
 
30
         amplitude.getInstance(this._amplitudeOptions).init(amplitudeAPPKey, undefined, { includeReferrer: true });
30
         amplitude.getInstance(this._amplitudeOptions).init(amplitudeAPPKey, undefined, { includeReferrer: true });
31
+        amplitude.fixDeviceID(this._amplitudeOptions);
31
 
32
 
32
         if (user) {
33
         if (user) {
33
             amplitude.getInstance(this._amplitudeOptions).setUserId(user);
34
             amplitude.getInstance(this._amplitudeOptions).setUserId(user);

+ 8
- 1
react/features/analytics/handlers/amplitude/Amplitude.native.js 查看文件

111
         }
111
         }
112
 
112
 
113
         return instance;
113
         return instance;
114
-    }
114
+    },
115
+
116
+    /**
117
+     * Currently not implemented.
118
+     *
119
+     * @returns {void}
120
+     */
121
+    fixDeviceID() { } // eslint-disable-line no-empty-function
115
 };
122
 };

+ 31
- 0
react/features/analytics/handlers/amplitude/Amplitude.web.js 查看文件

1
 import amplitude from 'amplitude-js';
1
 import amplitude from 'amplitude-js';
2
 
2
 
3
 export default {
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
     getInstance(options = {}) {
11
     getInstance(options = {}) {
5
         return amplitude.getInstance(options.instanceName);
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
 };

正在加载...
取消
保存