瀏覽代碼

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

master
Hristo Terezov 5 年之前
父節點
當前提交
1cde7e63c7
No account linked to committer's email address

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

@@ -28,6 +28,7 @@ export default class AmplitudeHandler extends AbstractHandler {
28 28
         };
29 29
 
30 30
         amplitude.getInstance(this._amplitudeOptions).init(amplitudeAPPKey, undefined, { includeReferrer: true });
31
+        amplitude.fixDeviceID(this._amplitudeOptions);
31 32
 
32 33
         if (user) {
33 34
             amplitude.getInstance(this._amplitudeOptions).setUserId(user);

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

@@ -111,5 +111,12 @@ export default {
111 111
         }
112 112
 
113 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,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
 };

Loading…
取消
儲存