Skip to content

Commit b6c8eeb

Browse files
shiiinjijhuleatt
andauthored
useAnalytics missing in ReactFire v4 #429 (#431)
* add declaration of useAnalytics * add an example for Analytics * update docs/use.md for reactfire 4.0 and firebase 9.0 on Log Page Views to Google Analytics for Firebase with React Router * Update docs/use.md Co-authored-by: Jeff <[email protected]> * Update docs/use.md Co-authored-by: Jeff <[email protected]> * Update example/withoutSuspense/Analytics.tsx Co-authored-by: Jeff <[email protected]> * regenerate reference docs Co-authored-by: Jeff <[email protected]>
1 parent cc0c93e commit b6c8eeb

File tree

6 files changed

+142
-30
lines changed

6 files changed

+142
-30
lines changed

docs/reference/modules/index.md

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/modules/sdk.md

Lines changed: 76 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/use.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -424,32 +424,35 @@ function App() {
424424
## Log Page Views to Google Analytics for Firebase with React Router
425425

426426
```jsx
427-
import { useAnalytics } from 'reactfire';
427+
import { AnalyticsProvider, useAnalytics } from 'reactfire';
428428
import { Router, Route, Switch } from 'react-router';
429+
import { getAnalytics, logEvent } from 'firebase/analytics'
429430

430431
function MyPageViewLogger({ location }) {
431432
const analytics = useAnalytics();
432433

433434
// By passing `location.pathname` to the second argument of `useEffect`,
434435
// we only log on first render and when the `pathname` changes
435436
useEffect(() => {
436-
analytics.logEvent('page-view', { path_name: location.pathname });
437-
}, [location.pathname]);
437+
logEvent(analytics, 'page_view', { page_location: location.href });
438+
}, [location.href]);
438439

439440
return null;
440441
}
441442

442443
function App() {
443-
const analytics = useAnalytics();
444+
const app = useFirebaseApp()
444445

445446
return (
446-
<Router>
447-
<Switch>
448-
<Route exact path="/about" component={<AboutPage />} />
449-
<Route component={<NotFoundPage />} />
450-
</Switch>
451-
<MyPageViewLogger />
452-
</Router>
447+
<AnalyticsProvider sdk={getAnalytics(app)}>
448+
<Router>
449+
<Switch>
450+
<Route exact path="/about" component={<AboutPage />} />
451+
<Route component={<NotFoundPage />} />
452+
</Switch>
453+
<MyPageViewLogger />
454+
</Router>
455+
</AnalyticsProvider>
453456
);
454457
}
455458
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from 'react'
2+
import { AnalyticsProvider, useFirebaseApp, useAnalytics } from 'reactfire';
3+
import { getAnalytics, logEvent } from 'firebase/analytics'
4+
5+
6+
function MyPageViewLogger() {
7+
const analytics = useAnalytics()
8+
9+
React.useEffect(() => {
10+
logEvent(analytics, 'page_view', { page_location: location.href });
11+
}, [location.href])
12+
13+
return null
14+
}
15+
16+
export function Analytics() {
17+
const app = useFirebaseApp();
18+
return (
19+
<AnalyticsProvider sdk={getAnalytics(app)}>
20+
<MyPageViewLogger />
21+
</AnalyticsProvider>
22+
);
23+
}

example/withoutSuspense/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { AuthProvider, useFirebaseApp, useInitPerformance } from 'reactfire';
33
import { Card } from '../display/Card';
4+
import { Analytics } from './Analytics'
45
import { Auth } from './Auth';
56
import { Firestore } from './Firestore';
67
import { RealtimeDatabase } from './RealtimeDatabase';
@@ -39,6 +40,7 @@ export const App = () => {
3940
<Storage />
4041
</Card>
4142
</AuthProvider>
43+
<Analytics />
4244
</div>
4345
);
4446
};

0 commit comments

Comments
 (0)