diff --git a/lib/src/matomo_action.dart b/lib/src/matomo_action.dart index 3c34639..5a23b67 100644 --- a/lib/src/matomo_action.dart +++ b/lib/src/matomo_action.dart @@ -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; @@ -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/', '/#/'); +} diff --git a/test/src/matomo_action_test.dart b/test/src/matomo_action_test.dart index d5925b3..e6fa98d 100644 --- a/test/src/matomo_action_test.dart +++ b/test/src/matomo_action_test.dart @@ -267,8 +267,7 @@ void main() { withCampaign: false, ); final eventMap = matomoAction.toMap(mockMatomoTracker); - - expect(eventMap['url'], expectedUrl); + expect(Uri.encodeQueryComponent(eventMap['url'] ?? ''), expectedUrl); }); }); });