Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended the newVisit capabilities #105

Merged
merged 1 commit into from
Jun 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 87 additions & 11 deletions lib/src/matomo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ class MatomoTracker {
///
/// The [newVisit] parameter is used to mark this initialization the start
/// of a new visit. If set to `false` it is left to Matomo to decide if this
/// is a new visit or not.
/// is a new visit or not. In practice, this will be used as the `newVisit`
/// parameter in the first `track...` method call but only if the `newVisit`
/// parameter in that method call is left to `null`.
///
/// The [visitorId] should have a length of 16 characters otherwise an
/// [ArgumentError] will be thrown. This parameter corresponds with the
Expand Down Expand Up @@ -429,13 +431,19 @@ class MatomoTracker {
/// - `campaign`: The campaign that lead to this page view.
///
/// For remarks on [dimensions] see [trackDimensions].
///
/// The [newVisit] parameter can be used to make this action the begin
/// of a new visit. If it's left to `null` and this is the first `track...`
/// call after [MatomoTracker.initialize], the `newVisit` from there will
/// be used.
void trackScreen(
BuildContext context, {
String? pvId,
String? path,
Campaign? campaign,
Map<String, String>? dimensions,
PerformanceInfo? performanceInfo,
bool? newVisit,
}) {
final actionName = context.widget.toStringShort();

Expand All @@ -446,6 +454,7 @@ class MatomoTracker {
campaign: campaign,
dimensions: dimensions,
performanceInfo: performanceInfo,
newVisit: _inferNewVisit(newVisit),
);
}

Expand All @@ -465,13 +474,19 @@ class MatomoTracker {
/// - `campaign`: The campaign that lead to this page view.
///
/// For remarks on [dimensions] see [trackDimensions].
///
/// The [newVisit] parameter can be used to make this action the begin
/// of a new visit. If it's left to `null` and this is the first `track...`
/// call after [MatomoTracker.initialize], the `newVisit` from there will
/// be used.
void trackScreenWithName({
required String actionName,
String? pvId,
String? path,
Campaign? campaign,
Map<String, String>? dimensions,
PerformanceInfo? performanceInfo,
bool? newVisit,
}) {
_initializationCheck();

Expand All @@ -491,6 +506,7 @@ class MatomoTracker {
dimensions: dimensions,
screenId: pvId ?? randomAlphaNumeric(6),
performanceInfo: performanceInfo,
newVisit: _inferNewVisit(newVisit),
);
_lastPageView = lastPageView;
return _track(lastPageView);
Expand All @@ -501,10 +517,16 @@ class MatomoTracker {
/// The [goalId] corresponds with `idgoal` and [revenue] with `revenue`.
///
/// For remarks on [dimensions] see [trackDimensions].
///
/// The [newVisit] parameter can be used to make this action the begin
/// of a new visit. If it's left to `null` and this is the first `track...`
/// call after [MatomoTracker.initialize], the `newVisit` from there will
/// be used.
void trackGoal(
int goalId, {
double? revenue,
Map<String, String>? dimensions,
bool? newVisit,
}) {
_initializationCheck();

Expand All @@ -514,6 +536,7 @@ class MatomoTracker {
goalId: goalId,
revenue: revenue,
dimensions: dimensions,
newVisit: _inferNewVisit(newVisit),
),
);
}
Expand All @@ -526,17 +549,24 @@ class MatomoTracker {
/// take precedance over [attachLastPvId].
///
/// For remarks on [dimensions] see [trackDimensions].
///
/// The [newVisit] parameter can be used to make this action the begin
/// of a new visit. If it's left to `null` and this is the first `track...`
/// call after [MatomoTracker.initialize], the `newVisit` from there will
/// be used.
void trackEvent({
required EventInfo eventInfo,
String? pvId,
Map<String, String>? dimensions,
bool? newVisit,
}) {
validateDimension(dimensions);
return _track(
MatomoAction(
eventInfo: eventInfo,
screenId: _inferPvId(pvId),
dimensions: dimensions,
newVisit: _inferNewVisit(newVisit),
),
);
}
Expand All @@ -551,13 +581,20 @@ class MatomoTracker {
///
/// Also note that counting starts at 1 and NOT at 0 as opposed to what is stated
/// in the [Tracking HTTP API](https://developer.matomo.org/api-reference/tracking-api) documentation.
///
/// The [newVisit] parameter can be used to make this action the begin
/// of a new visit. If it's left to `null` and this is the first `track...`
/// call after [MatomoTracker.initialize], the `newVisit` from there will
/// be used.
void trackDimensions(
Map<String, String> dimensions,
) {
Map<String, String> dimensions, {
bool? newVisit,
}) {
validateDimension(dimensions);
return _track(
MatomoAction(
dimensions: dimensions,
newVisit: _inferNewVisit(newVisit),
),
);
}
Expand All @@ -568,11 +605,17 @@ class MatomoTracker {
/// [searchCount] with `search_count`.
///
/// For remarks on [dimensions] see [trackDimensions].
///
/// The [newVisit] parameter can be used to make this action the begin
/// of a new visit. If it's left to `null` and this is the first `track...`
/// call after [MatomoTracker.initialize], the `newVisit` from there will
/// be used.
void trackSearch({
required String searchKeyword,
String? searchCategory,
int? searchCount,
Map<String, String>? dimensions,
bool? newVisit,
}) {
validateDimension(dimensions);
return _track(
Expand All @@ -581,20 +624,27 @@ class MatomoTracker {
searchCategory: searchCategory,
searchCount: searchCount,
dimensions: dimensions,
newVisit: _inferNewVisit(newVisit),
),
);
}

/// Tracks a cart update.
///
/// For remarks on [dimensions] see [trackDimensions].
///
/// The [newVisit] parameter can be used to make this action the begin
/// of a new visit. If it's left to `null` and this is the first `track...`
/// call after [MatomoTracker.initialize], the `newVisit` from there will
/// be used.
void trackCartUpdate(
List<TrackingOrderItem>? trackingOrderItems,
num? subTotal,
num? taxAmount,
num? shippingCost,
num? discountAmount, {
Map<String, String>? dimensions,
bool? newVisit,
}) {
_initializationCheck();

Expand All @@ -608,13 +658,19 @@ class MatomoTracker {
shippingCost: shippingCost,
discountAmount: discountAmount,
dimensions: dimensions,
newVisit: _inferNewVisit(newVisit),
),
);
}

/// Tracks an order.
///
/// For remarks on [dimensions] see [trackDimensions].
///
/// The [newVisit] parameter can be used to make this action the begin
/// of a new visit. If it's left to `null` and this is the first `track...`
/// call after [MatomoTracker.initialize], the `newVisit` from there will
/// be used.
void trackOrder(
String? orderId,
List<TrackingOrderItem>? trackingOrderItems,
Expand All @@ -624,6 +680,7 @@ class MatomoTracker {
num? shippingCost,
num? discountAmount, {
Map<String, String>? dimensions,
bool? newVisit,
}) {
_initializationCheck();

Expand All @@ -639,16 +696,23 @@ class MatomoTracker {
shippingCost: shippingCost,
discountAmount: discountAmount,
dimensions: dimensions,
newVisit: _inferNewVisit(newVisit),
),
);
}

/// Tracks the click on an outgoing link.
///
/// For remarks on [dimensions] see [trackDimensions].
///
/// The [newVisit] parameter can be used to make this action the begin
/// of a new visit. If it's left to `null` and this is the first `track...`
/// call after [MatomoTracker.initialize], the `newVisit` from there will
/// be used.
void trackOutlink(
String? link, {
Map<String, String>? dimensions,
bool? newVisit,
}) {
_initializationCheck();

Expand All @@ -657,6 +721,7 @@ class MatomoTracker {
MatomoAction(
link: link,
dimensions: dimensions,
newVisit: _inferNewVisit(newVisit),
),
);
}
Expand All @@ -672,16 +737,23 @@ class MatomoTracker {
/// take precedance over [attachLastPvId].
///
/// For remarks on [dimensions] see [trackDimensions].
///
/// The [newVisit] parameter can be used to make this action the begin
/// of a new visit. If it's left to `null` and this is the first `track...`
/// call after [MatomoTracker.initialize], the `newVisit` from there will
/// be used.
void trackContentImpression({
required Content content,
String? pvId,
Map<String, String>? dimensions,
bool? newVisit,
}) {
return _track(
MatomoAction(
content: content,
screenId: _inferPvId(pvId),
dimensions: dimensions,
newVisit: _inferNewVisit(newVisit),
),
);
}
Expand All @@ -700,6 +772,10 @@ class MatomoTracker {
/// will take precedance over [attachLastPvId].
///
/// For remarks on [dimensions] see [trackDimensions].
///
/// Note that this method is missing a `newVisit` parameter on purpose since
/// it doesn't make sense to have an interaction without an impression first,
/// and then the impression would mark the new visit, not the interaction.
void trackContentInteraction({
required Content content,
required String interaction,
Expand All @@ -716,21 +792,15 @@ class MatomoTracker {
);
}

void _track(MatomoAction action) {
var ac = action;
if (_newVisit) {
ac = ac.copyWith(newVisit: true);
_newVisit = false;
}
queue.add(ac.toMap(this));
}
void _track(MatomoAction action) => queue.add(action.toMap(this));

void _ping() {
final lastPageView = _lastPageView;
if (lastPageView != null) {
_track(
lastPageView.copyWith(
ping: true,
newVisit: false,
),
);
}
Expand Down Expand Up @@ -808,4 +878,10 @@ class MatomoTracker {

String? _inferPvId(String? pvId) =>
pvId ?? (attachLastPvId ? _lastPageView?.screenId : null);

bool _inferNewVisit(bool? localNewVisit) {
final globalNewVisit = _newVisit;
_newVisit = false;
return localNewVisit ?? globalNewVisit;
}
}