UmengStats.ets
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { preInit, InternalPlugin, setLogEnabled, init, onEventObject, onProfileSignOff,
onProfileSignIn } from '@umeng/analytics';
import { common } from '@kit.AbilityKit';
import { AccountManagerUtils } from '../utils/AccountManagerUtils';
import { UserDataLocal } from '../utils/UserDataLocal';
import { EmitterUtils } from '../utils/EmitterUtils';
import { EmitterEventId } from '../utils/EmitterEventId';
import { Logger } from '../utils/Logger';
export class UmengStats {
private static _inited = false
// 启动时调用
static preInit(context: common.AbilityStageContext) {
// 需在 AppScope/resources/rawfile/umconfig.json 配置key和channel
preInit({
context: context.getApplicationContext(),
plugins: [new InternalPlugin()],
enableLog: Logger.isDebug
});
}
// 在用户同意隐私政策后再调用此方法
static initAfterAgreeProtocol() {
if (UmengStats._inited) {
return
}
init();
UmengStats._inited = true
UmengStats.onLogin()
// UmengStats.event("testHarmony", {"key1": "value1"})
EmitterUtils.receiveEvent(EmitterEventId.LOGIN_SUCCESS, () => {
UmengStats.onLogin()
})
EmitterUtils.receiveEvent(EmitterEventId.FORCE_USER_LOGIN_OUT, () => {
UmengStats.onLogout()
})
}
static onLogin() {
AccountManagerUtils.isLogin().then((login) => {
if (!login) { return }
let userId = UserDataLocal.getUserId()
if (userId.length > 0) {
onProfileSignIn("USER", userId)
}
})
}
static onLogout() {
onProfileSignOff()
}
// 属性key,128位以内的非空字符串,value为256位以内的数值或字符串
static event(eventId: string, param?: Record<string, string | number>) {
if (!UmengStats._inited) {
return
}
onEventObject(eventId, param);
}
}