mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-03 22:48:36 +08:00
106 lines
2.7 KiB
Markdown
106 lines
2.7 KiB
Markdown
---
|
|
title: Quick Start
|
|
description: Getting started with Analytics in React Native Firebase
|
|
---
|
|
|
|
# Analytics Quick Start
|
|
|
|
## Installation
|
|
|
|
Install this module with Yarn:
|
|
|
|
```bash
|
|
yarn add @react-native-firebase/analytics
|
|
```
|
|
|
|
> Integrating manually and not via React Native auto-linking? Check the setup instructions for <Anchor version group href="/android">Android</Anchor> & <Anchor version group href="/ios">iOS</Anchor>.
|
|
|
|
## Module usage
|
|
|
|
The Analytics package will automatically start tracking events such as when users clear app data, dismiss notifications and more.
|
|
To view the full list of automatic events, see <Anchor version group href="/reserved-events">Reserved Events</Anchor>.
|
|
|
|
The package also provides a JavaScript API to allow for logging custom events and metrics throughout your application.
|
|
|
|
Import the Cloud Functions package into your project:
|
|
|
|
```js
|
|
import analytics from '@react-native-firebase/analytics';
|
|
```
|
|
|
|
The package also provides access to the firebase instance:
|
|
|
|
```js
|
|
import { firebase } from '@react-native-firebase/analytics';
|
|
```
|
|
|
|
### Custom events
|
|
|
|
To log a custom event, use the `logEvent` method:
|
|
|
|
```js
|
|
import analytics from '@react-native-firebase/analytics';
|
|
|
|
async function onProductView() {
|
|
await analytics().logEvent('product_view', {
|
|
id: '123456789',
|
|
color: 'red',
|
|
via: 'ProductCatalog',
|
|
});
|
|
}
|
|
```
|
|
|
|
### Attaching user data
|
|
|
|
User data can be attached to analytical events via the `setUser*` methods:
|
|
|
|
```js
|
|
import analytics from '@react-native-firebase/analytics';
|
|
|
|
async function onSignIn(user) {
|
|
await Promise.all([
|
|
analytics().setUserId(user.uid),
|
|
analytics().setUserProperty('account_balance', user.balance),
|
|
]);
|
|
}
|
|
```
|
|
|
|
### Tracking screen names
|
|
|
|
Similar to Analytics on the web, it's important to understand the user journey within your application, for example
|
|
tracking drop off points during a e-commerce transaction flow. The Analytics package provides a method called
|
|
`setCurrentScreen` to help track this.
|
|
|
|
```js
|
|
import React, { useEffect } from 'react';
|
|
import { View } from 'react';
|
|
import analytics from '@react-native-firebase/analytics';
|
|
|
|
function BasketScreen() {
|
|
async function trackScreenView(screen) {
|
|
// Set & override the MainActivity screen name
|
|
await analytics().setCurrentScreen(screen, screen);
|
|
}
|
|
|
|
// Track a screen view once the component has mounted
|
|
useEffect(() => {
|
|
trackScreenView('BasketScreen');
|
|
}, []);
|
|
|
|
return <View />;
|
|
}
|
|
```
|
|
|
|
### Resetting analytics data
|
|
|
|
In some cases, resetting all analytics data is required on certain events such as signing out of the application.
|
|
To achieve this call the `resetAnalyticsData` method.
|
|
|
|
```js
|
|
import analytics from '@react-native-firebase/analytics';
|
|
|
|
async function onSignOut() {
|
|
await analytics().resetAnalyticsData();
|
|
}
|
|
```
|