Skip to content

Commit 87372ac

Browse files
thomas-stockxcollinjackson
authored andcommitted
[firebase_analytics] Added Navigator.pushReplacement screen tracking (flutter#1428)
* Track Navigator.pushReplacement screen changes Currently only standard push and pops were automatically logged to Firebase. However, this resulted in missing screen_view events for apps with more complex navigation. * Added test for pushReplacement
1 parent 1ba00aa commit 87372ac

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ Mike Diarmid <[email protected]>
2727
Invertase <[email protected]>
2828
Elliot Hesp <[email protected]>
2929
Katarina Sheremet <[email protected]>
30+
Thomas Stockx <[email protected]>

packages/firebase_analytics/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.1.1
2+
3+
* Added screen_view tracking of Navigator.pushReplacement
4+
15
## 2.1.0
26

37
* Add Login event support

packages/firebase_analytics/lib/observer.dart

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ String defaultNameExtractor(RouteSettings settings) => settings.name;
3232
/// settings: RouteSettings(name: '/contact/123',
3333
/// builder: ContactDetail(123)))),
3434
///
35+
/// Navigator.pushReplacement(context, MaterialPageRoute(
36+
/// settings: RouteSettings(name: '/contact/123',
37+
/// builder: ContactDetail(123)))),
38+
///
3539
/// Navigator.pop(context);
3640
/// ```
3741
///
@@ -94,6 +98,14 @@ class FirebaseAnalyticsObserver extends RouteObserver<PageRoute<dynamic>> {
9498
}
9599
}
96100

101+
@override
102+
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
103+
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
104+
if (newRoute is PageRoute) {
105+
_sendScreenView(newRoute);
106+
}
107+
}
108+
97109
@override
98110
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
99111
super.didPop(route, previousRoute);

packages/firebase_analytics/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Google Analytics for Firebase, an app measuremen
33
solution that provides insight on app usage and user engagement on Android and iOS.
44
author: Flutter Team <[email protected]>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_analytics
6-
version: 2.1.0
6+
version: 2.1.1
77

88
flutter:
99
plugin:

packages/firebase_analytics/test/observer_test.dart

+10
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ void main() {
5757
verify(analytics.setCurrentScreen(screenName: 'route')).called(1);
5858
});
5959

60+
test('setCurrentScreen on route pushReplacement', () {
61+
final PageRoute<dynamic> route = MockPageRoute();
62+
final PageRoute<dynamic> previousRoute = MockPageRoute();
63+
when(route.settings).thenReturn(const RouteSettings(name: 'route'));
64+
65+
observer.didReplace(newRoute: route, oldRoute: previousRoute);
66+
67+
verify(analytics.setCurrentScreen(screenName: 'route')).called(1);
68+
});
69+
6070
test('uses nameExtractor', () {
6171
observer = FirebaseAnalyticsObserver(
6272
analytics: analytics,

0 commit comments

Comments
 (0)