Skip to content

Commit

Permalink
Using RouteAware logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EPNW-Eric committed May 11, 2023
1 parent b8342de commit 201e7e4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 42 deletions.
20 changes: 0 additions & 20 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,3 @@ class OtherPage extends StatelessWidget {
);
}
}

class OtherPage extends StatelessWidget {
const OtherPage({super.key});

@override
Widget build(BuildContext context) {
return TraceableWidget(
path: '/otherpage',
actionName: 'Other Page',
child: Scaffold(
appBar: AppBar(
title: const Text('Other Page'),
),
body: const Center(
child: Text('Welcome to the other page!'),
),
),
);
}
}
7 changes: 7 additions & 0 deletions lib/src/traceable_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TraceableWidget extends StatefulWidget {
required this.child,
this.actionName,
this.pvId,
this.updatePvIdAfterPop = true,
this.path,
this.dimensions,
this.campaign,
Expand All @@ -21,6 +22,9 @@ class TraceableWidget extends StatefulWidget {
/// {@macro traceableClientMixin.pvId}
final String? pvId;

/// {@macro traceableClientMixin.updatePvIdAfterPop}
final bool updatePvIdAfterPop;

/// {@macro traceableClientMixin.path}
final String? path;

Expand Down Expand Up @@ -52,6 +56,9 @@ class _TraceableWidgetState extends State<TraceableWidget>
@override
String get pvId => widget.pvId ?? super.pvId;

@override
bool get updatePvIdAfterPop => widget.updatePvIdAfterPop;

@override
String? get path => widget.path;

Expand Down
43 changes: 21 additions & 22 deletions lib/src/traceable_widget_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@ mixin TraceableClientMixin<T extends StatefulWidget> on State<T>
String get actionName => 'Created widget ${widget.toStringShort()}';

/// {@template traceableClientMixin.pvId}
/// A 6 character unique ID.
/// A 6 character unique page view ID.
///
/// Each unique ID represents one page view.
///
/// The default implementation will generate one on widget creation.
/// {@endtemplate}
@protected
String get pvId => _pvId;
String _pvId = randomAlphaNumeric(6);

/// {@template traceableClientMixin.updatePvIdAfterPop}
/// Used to control if a [Navigator.pop] back to this page is considered
/// a new page view.
///
/// If you do not consider this a new page view in your apps logic,
/// you should return `false` to tell the widget to keep the old [pvId].
/// If you overworte the [pvId] getter, this has no effect.
/// {@endtemplate}
@protected
bool get updatePvIdAfterPop => true;

/// {@template traceableClientMixin.path}
/// Path to the widget. (e.g. `'/home'`).
///
Expand All @@ -44,7 +57,7 @@ mixin TraceableClientMixin<T extends StatefulWidget> on State<T>
///
/// If Custom Dimension ID is 2 use `dimension2=dimensionValue` to send a
/// value for this dimension.
///
///
/// For additional remarks see [MatomoTracker.trackDimensions].
/// {@endtemplate}
@protected
Expand All @@ -61,7 +74,7 @@ mixin TraceableClientMixin<T extends StatefulWidget> on State<T>
@override
void initState() {
super.initState();
_startTracking();
_track();
}

@override
Expand All @@ -81,7 +94,10 @@ mixin TraceableClientMixin<T extends StatefulWidget> on State<T>

@override
void didPopNext() {
// TODO(EPNW-Eric): Add call to onReentry here
if (updatePvIdAfterPop) {
_pvId = randomAlphaNumeric(6);
}
_track();
}

@override
Expand All @@ -90,7 +106,7 @@ mixin TraceableClientMixin<T extends StatefulWidget> on State<T>
@override
void didPushNext() {}

void _startTracking() {
void _track() {
tracker.trackScreenWithName(
actionName: actionName,
pvId: pvId,
Expand All @@ -99,21 +115,4 @@ mixin TraceableClientMixin<T extends StatefulWidget> on State<T>
dimensions: dimensions,
);
}

/// Should be called if a [Navigator.pop]s back to this page.
///
/// This will than trigger an action to tell Matomo that the
/// app is back on this page.
///
/// If you do not consider this a new page view in your apps logic,
/// you should set [updatePvId] to `false` to tell the widget to keep
/// the old [pvId]. If you overworte the [pvId] getter, this has no
/// effect.
@protected
void onReentry({bool updatePvId = true}) {
if (updatePvId) {
_pvId = randomAlphaNumeric(6);
}
_startTracking();
}
}

0 comments on commit 201e7e4

Please sign in to comment.