Skip to content

Commit

Permalink
fix(web): #173 修复用户行为分析不启用时使用会导致报错的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Jun 1, 2021
1 parent c24d0f7 commit 94e7dba
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/web/utils/analytics-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import { isAvailableString } from '@shared/utils/string-helper';
const token = _get(Config, 'posthog.token');
const instance = _get(Config, 'posthog.instance');

/**
* 分析系统是否启用
*/
let enabled = false;

/**
* 初始化统计分析系统
*/
Expand All @@ -25,6 +30,8 @@ export function initAnalytics() {
version: config.version,
platform: config.platform,
});

enabled = true;
}
}

Expand All @@ -33,6 +40,10 @@ export function initAnalytics() {
* @param info 用户信息
*/
export function setAnalyticsUser(info: UserInfo) {
if (!enabled) {
return;
}

posthog.identify(info.uuid, {
name: getUserName(info),
username: info.username,
Expand All @@ -45,12 +56,20 @@ export function setAnalyticsUser(info: UserInfo) {
* @param properties 相关属性
*/
export function trackEvent(name: string, properties?: posthog.Properties) {
if (!enabled) {
return;
}

posthog.capture(name, properties);
}

/**
* 发送单页应用Url变更事件
*/
export function trackUrlChangeEvent() {
if (!enabled) {
return;
}

posthog.capture('$pageview');
}

0 comments on commit 94e7dba

Please sign in to comment.