getObserver method
Returns a SnowplowObserver for automatically tracking PageViewEvent
and ScreenView
events from a navigator when the currently active
ModalRoute of the navigator changes.
ScreenView
events are tracked on all platforms. Optionally,
PageViewEvent
events may be tracked on Web if
TrackerConfiguration.webActivityTracking
is configured when creating
the tracker.
The nameExtractor
function is used to extract a name
from RouteSettings of the now active route and that name is used in
tracked ScreenView
or PageViewEvent
events.
The following operations will result in tracking a view event:
Navigator.pushNamed(context, '/contact/123');
Navigator.push<void>(context, MaterialPageRoute(
settings: RouteSettings(name: '/contact/123'),
builder: (_) => ContactDetail(123)));
Navigator.pushReplacement<void>(context, MaterialPageRoute(
settings: RouteSettings(name: '/contact/123'),
builder: (_) => ContactDetail(123)));
Navigator.pop(context);
If using MaterialApp, add the retrieved observer to
navigatorObservers
, e.g.:
MaterialApp(
navigatorObservers: [
tracker.getObserver()
],
...
);
If using the Router
API with the MaterialApp.router
constructor,
add the observer to the observers
of your Navigator instance, e.g.:
return Navigator(
observers: [tracker.getObserver()],
...
);
You can also trigger view event tracking within your ModalRoute by implementing RouteAware<ModalRoute<dynamic>> and subscribing it to SnowplowObserver. See the RouteObserver<ModalRoute<dynamic>> docs for an example.
Implementation
SnowplowObserver getObserver(
{ScreenNameExtractor nameExtractor = defaultNameExtractor}) {
return SnowplowObserver(tracker: this, nameExtractor: nameExtractor);
}