Skip to content

Commit

Permalink
fix: fixed parsing of Uri
Browse files Browse the repository at this point in the history
  • Loading branch information
TesteurManiak committed Jan 11, 2024
1 parent 10ff174 commit d2654c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
39 changes: 28 additions & 11 deletions lib/src/matomo_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,23 @@ class MatomoAction {
final camp = campaign;
final campKeyword = camp?.keyword;
final localPath = path;
final uri = Uri.parse(
localPath != null
? '${tracker.contentBase}${localPath.prefixWithSlash()}'
: tracker.contentBase,
);
final url = uri.replace(
queryParameters: {
if (camp != null) ...camp.toMap(),
...uri.queryParameters,
},
).toString();

final baseUrl = (localPath != null
? '${tracker.contentBase.removeTrailingSlash()}${localPath.prefixWithSlash()}'
: tracker.contentBase)
.replaceHashtags();
final uri = Uri.parse(baseUrl);
final url = uri
.replace(
queryParameters: camp != null || uri.queryParameters.isNotEmpty
? {
if (camp != null) ...camp.toMap(),
...uri.queryParameters,
}
: null,
)
.toString()
.restoreHashtags();
final idgoal = goalId;
final aRevenue = revenue;
final event = eventInfo;
Expand Down Expand Up @@ -272,3 +278,14 @@ class MatomoAction {
};
}
}

extension on String {
String removeTrailingSlash() {
if (endsWith('/')) return substring(0, length - 1);
return this;
}

static const _placeholder = 'HASHTAG_PLACEHOLDER';
String replaceHashtags() => replaceAll('/#/', '/$_placeholder/');
String restoreHashtags() => replaceAll('/$_placeholder/', '/#/');
}
3 changes: 1 addition & 2 deletions test/src/matomo_action_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ void main() {
withCampaign: false,
);
final eventMap = matomoAction.toMap(mockMatomoTracker);

expect(eventMap['url'], expectedUrl);
expect(Uri.encodeQueryComponent(eventMap['url'] ?? ''), expectedUrl);
});
});
});
Expand Down

0 comments on commit d2654c8

Please sign in to comment.