PageAnalytics
PageAnalytics is a Flutter plugin that helps you track page views and time spent on each page in your application. It provides callbacks for page start and stop events, allowing you to integrate with your analytics system easily.
Features
- Track page views automatically
- Measure time spent on each page
- Handle app lifecycle changes (pause/resume)
- Option to exclude specific pages from tracking
- Easy integration with custom analytics systems
Installation
Add this to your package's pubspec.yaml file:
dependencies:
page_analytics: ^1.0.0
Then run:
$ flutter pub get
Usage
- Import the package in your Dart code:
import 'package:page_analytics/page_analytics.dart';
- Create an instance of
PageAnalytics:
final analytics = PageAnalytics(
onPageStart: (pageName, pageData) {
// Handle page start event
print('Page started: $pageName');
},
onPageStop: (pageName, timeSpent, pageData) {
// Handle page stop event
print('Page stopped: $pageName, Time spent: $timeSpent seconds');
},
restrictedScreens: ['/login', '/splash'], // Optional: pages to exclude from tracking
);
- Integrate with your app's navigation:
class MyApp extends StatelessWidget {
final PageAnalytics analytics;
MyApp({required this.analytics});
@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateRoute: (settings) {
// Let PageAnalytics handle the route for analytics
analytics.onGenerateRoute(settings);
// Your actual route generation logic here
// ...
},
// ... other MaterialApp properties
);
}
}
- Don't forget to dispose of the analytics object when it's no longer needed:
@override
void dispose() {
analytics.dispose();
super.dispose();
}
Example
Here's a more complete example of how to use PageAnalytics in a Flutter app:
import 'package:flutter/material.dart';
import 'package:page_analytics/page_analytics.dart';
void main() {
final analytics = PageAnalytics(
onPageStart: (pageName, pageData) {
print('Page started: $pageName');
print('Page data: $pageData');
},
onPageStop: (pageName, timeSpent, pageData) {
print('Page stopped: $pageName');
print('Time spent: $timeSpent seconds');
print('Page data: $pageData');
},
);
runApp(MyApp(analytics: analytics));
}
class MyApp extends StatelessWidget {
final PageAnalytics analytics;
MyApp({required this.analytics});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'PageAnalytics Demo',
onGenerateRoute: (settings) {
analytics.onGenerateRoute(settings);
// Your actual route generation logic
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => HomePage());
case '/details':
return MaterialPageRoute(builder: (_) => DetailsPage());
default:
return null;
}
},
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home')),
body: Center(
child: ElevatedButton(
child: Text('Go to Details'),
onPressed: () {
Navigator.pushNamed(context, '/details', arguments: {'id': '123'});
},
),
),
);
}
}
class DetailsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Details')),
body: Center(child: Text('Details Page')),
);
}
}
In this example, the PageAnalytics instance will track navigation between the HomePage and DetailsPage, providing start and stop events for each page view.
Notes
- Make sure to handle the disposal of the
PageAnalyticsinstance when it's no longer needed to avoid memory leaks. - The
restrictedScreensparameter allows you to specify pages that should not be tracked. - The plugin automatically handles app lifecycle changes, tracking when the app is paused or resumed.
