Javascript react native上每个用户级别的应用程序使用时间

Javascript react native上每个用户级别的应用程序使用时间,javascript,react-native,google-analytics,Javascript,React Native,Google Analytics,我正在开发一个react原生应用程序,我想找到一种方法,以用户为基础获取我们应用程序的应用程序使用时间 使用react native的AppState检查应用程序是否处于活动状态或处于后台 import React, {Component} from 'react'; import {AppState, Text} from 'react-native'; class AppStateExample extends Component { state = { appState: A

我正在开发一个react原生应用程序,我想找到一种方法,以用户为基础获取我们应用程序的应用程序使用时间

使用react native的AppState检查应用程序是否处于活动状态或处于后台

import React, {Component} from 'react';
import {AppState, Text} from 'react-native';

class AppStateExample extends Component {
  state = {
    appState: AppState.currentState,
  };

  componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);
  }

  componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppStateChange);
  }

  _handleAppStateChange = (nextAppState) => {
    if (
      this.state.appState.match(/inactive|background/) &&
      nextAppState === 'active'
    ) {

      console.log('App has come to the foreground!. save this time');
    }
    console.log('App sent to the background!. save this time. ');
    /// difference between both the times is the time spent by user on app
    this.setState({appState: nextAppState});
  };

  render() {
    return <Text>Current state is: {this.state.appState}</Text>;
  }
}
import React,{Component}来自'React';
从“react native”导入{AppState,Text};
类AppStateExample扩展组件{
状态={
appState:appState.currentState,
};
componentDidMount(){
AppState.addEventListener('change',this.\u handleAppStateChange);
}
组件将卸载(){
AppState.removeEventListener('change',this.\u handleAppStateChange);
}
_HandLeapStateChange=(nextAppState)=>{
如果(
this.state.appState.match(/inactive | background/)&&
nextAppState===“活动”
) {
console.log('应用程序已到达前台!.save this time');
}
console.log('应用程序已发送到后台!.save this time');
///这两个时间的差异是用户在应用程序上花费的时间
this.setState({appState:nextapstate});
};
render(){
返回的当前状态为:{this.state.appState};
}
}

您到底尝试了什么?这里没有任何关于平台、语言或您尝试做什么的信息