|
|
@@ -1,9 +1,8 @@
|
|
1
|
|
-import amplitude from 'amplitude-js';
|
|
2
|
|
-
|
|
3
|
1
|
import logger from '../logger';
|
|
4
|
2
|
|
|
5
|
3
|
import AbstractHandler from './AbstractHandler';
|
|
6
|
|
-import { fixDeviceID } from './amplitude';
|
|
|
4
|
+import { fixDeviceID } from './amplitude/fixDeviceID';
|
|
|
5
|
+import amplitude from './amplitude/lib';
|
|
7
|
6
|
|
|
8
|
7
|
/**
|
|
9
|
8
|
* Analytics handler for Amplitude.
|
|
|
@@ -19,41 +18,39 @@ export default class AmplitudeHandler extends AbstractHandler {
|
|
19
|
18
|
constructor(options) {
|
|
20
|
19
|
super(options);
|
|
21
|
20
|
|
|
22
|
|
- const { amplitudeAPPKey, host, user } = options;
|
|
|
21
|
+ const { amplitudeAPPKey, user } = options;
|
|
23
|
22
|
|
|
24
|
23
|
this._enabled = true;
|
|
25
|
|
- this._host = host; // Only used on React Native.
|
|
26
|
24
|
|
|
27
|
25
|
const onError = e => {
|
|
28
|
26
|
logger.error('Error initializing Amplitude', e);
|
|
29
|
27
|
this._enabled = false;
|
|
30
|
28
|
};
|
|
31
|
29
|
|
|
32
|
|
- const amplitudeOptions = {
|
|
33
|
|
- domain: navigator.product === 'ReactNative' ? host : undefined,
|
|
34
|
|
- includeReferrer: true,
|
|
35
|
|
- onError
|
|
36
|
|
- };
|
|
|
30
|
+ if (navigator.product === 'ReactNative') {
|
|
|
31
|
+ amplitude.getInstance().init(amplitudeAPPKey);
|
|
|
32
|
+ fixDeviceID(amplitude.getInstance()).then(() => {
|
|
|
33
|
+ amplitude.getInstance().getDeviceId()
|
|
|
34
|
+ .then(deviceId => {
|
|
|
35
|
+ this._deviceId = deviceId;
|
|
|
36
|
+ });
|
|
|
37
|
+ });
|
|
|
38
|
+ } else {
|
|
|
39
|
+ const amplitudeOptions = {
|
|
|
40
|
+ includeReferrer: true,
|
|
|
41
|
+ onError
|
|
|
42
|
+ };
|
|
37
|
43
|
|
|
38
|
|
- this._getInstance().init(amplitudeAPPKey, undefined, amplitudeOptions);
|
|
39
|
|
- fixDeviceID(this._getInstance());
|
|
|
44
|
+ amplitude.getInstance().init(amplitudeAPPKey, undefined, amplitudeOptions);
|
|
|
45
|
+ fixDeviceID(amplitude.getInstance());
|
|
|
46
|
+ }
|
|
40
|
47
|
|
|
41
|
48
|
if (user) {
|
|
42
|
|
- this._getInstance().setUserId(user);
|
|
|
49
|
+ this._userId = user;
|
|
|
50
|
+ amplitude.getInstance().setUserId(user);
|
|
43
|
51
|
}
|
|
44
|
52
|
}
|
|
45
|
53
|
|
|
46
|
|
- /**
|
|
47
|
|
- * Returns the AmplitudeClient instance.
|
|
48
|
|
- *
|
|
49
|
|
- * @returns {AmplitudeClient}
|
|
50
|
|
- */
|
|
51
|
|
- _getInstance() {
|
|
52
|
|
- const name = navigator.product === 'ReactNative' ? this._host : undefined;
|
|
53
|
|
-
|
|
54
|
|
- return amplitude.getInstance(name);
|
|
55
|
|
- }
|
|
56
|
|
-
|
|
57
|
54
|
/**
|
|
58
|
55
|
* Sets the Amplitude user properties.
|
|
59
|
56
|
*
|
|
|
@@ -62,7 +59,7 @@ export default class AmplitudeHandler extends AbstractHandler {
|
|
62
|
59
|
*/
|
|
63
|
60
|
setUserProperties(userProps) {
|
|
64
|
61
|
if (this._enabled) {
|
|
65
|
|
- this._getInstance().setUserProperties(userProps);
|
|
|
62
|
+ amplitude.getInstance().setUserProperties(userProps);
|
|
66
|
63
|
}
|
|
67
|
64
|
}
|
|
68
|
65
|
|
|
|
@@ -79,7 +76,7 @@ export default class AmplitudeHandler extends AbstractHandler {
|
|
79
|
76
|
return;
|
|
80
|
77
|
}
|
|
81
|
78
|
|
|
82
|
|
- this._getInstance().logEvent(this._extractName(event), event);
|
|
|
79
|
+ amplitude.getInstance().logEvent(this._extractName(event), event);
|
|
83
|
80
|
}
|
|
84
|
81
|
|
|
85
|
82
|
/**
|
|
|
@@ -88,10 +85,17 @@ export default class AmplitudeHandler extends AbstractHandler {
|
|
88
|
85
|
* @returns {Object}
|
|
89
|
86
|
*/
|
|
90
|
87
|
getIdentityProps() {
|
|
|
88
|
+ if (navigator.product === 'ReactNative') {
|
|
|
89
|
+ return {
|
|
|
90
|
+ deviceId: this._deviceId,
|
|
|
91
|
+ userId: this._userId
|
|
|
92
|
+ };
|
|
|
93
|
+ }
|
|
|
94
|
+
|
|
91
|
95
|
return {
|
|
92
|
|
- sessionId: this._getInstance().getSessionId(),
|
|
93
|
|
- deviceId: this._getInstance().options.deviceId,
|
|
94
|
|
- userId: this._getInstance().options.userId
|
|
|
96
|
+ sessionId: amplitude.getInstance().getSessionId(),
|
|
|
97
|
+ deviceId: amplitude.getInstance().options.deviceId,
|
|
|
98
|
+ userId: amplitude.getInstance().options.userId
|
|
95
|
99
|
};
|
|
96
|
100
|
}
|
|
97
|
101
|
}
|