Skip to content

Commit

Permalink
Merge pull request #133 from Floating-Dartists/132-contentbaseurl-not…
Browse files Browse the repository at this point in the history
…-working-with-hash-sign

132 contentbaseurl not working with hash sign
  • Loading branch information
TesteurManiak authored Jan 12, 2024
2 parents 9e91e75 + d2654c8 commit b9d5c3e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 24 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/', '/#/');
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ dev_dependencies:
flutter_test:
sdk: flutter
meta: ^1.9.1
mocktail: ^1.0.1
mocktail: ^1.0.2

flutter: null
47 changes: 35 additions & 12 deletions test/src/matomo_action_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import '../ressources/mock/data.dart';
import '../ressources/mock/mock.dart';

void main() {
MatomoAction getCompleteMatomoAction() {
MatomoAction getCompleteMatomoAction({
String path = matomoActionPath,
bool withCampaign = true,
}) {
return MatomoAction(
path: matomoActionPath,
path: path,
action: matomoActionName,
dimensions: matomoEventDimension,
discountAmount: matomoDiscountAmount,
Expand All @@ -24,16 +27,18 @@ void main() {
name: matomoEventName,
value: matomoEventValue,
),
campaign: Campaign(
name: matomoCampaignName,
keyword: matomoCampaignKeyword,
source: matomoCampaignSource,
medium: matomoCampaignMedium,
content: matomoCampaignContent,
id: matomoCampaignId,
group: matomoCampaignGroup,
placement: matomoCampaignPlacement,
),
campaign: withCampaign
? Campaign(
name: matomoCampaignName,
keyword: matomoCampaignKeyword,
source: matomoCampaignSource,
medium: matomoCampaignMedium,
content: matomoCampaignContent,
id: matomoCampaignId,
group: matomoCampaignGroup,
placement: matomoCampaignPlacement,
)
: null,
content: Content(
name: matomoContentName,
piece: matomoContentPiece,
Expand Down Expand Up @@ -247,6 +252,24 @@ void main() {
expect(mapEquals(wantedEvent, eventMap), isTrue);
});
});

test('Issue #132: bad url encoding', () {
final fixedDate = DateTime(2022).toUtc();
const contentBase = 'https://app.articlett.schule/#/';
const localPath = 'sherlock';
const expectedUrl = "https%3A%2F%2Fapp.articlett.schule%2F%23%2Fsherlock";

when(() => mockMatomoTracker.contentBase).thenReturn(contentBase);

withClock(Clock.fixed(fixedDate), () {
final matomoAction = getCompleteMatomoAction(
path: localPath,
withCampaign: false,
);
final eventMap = matomoAction.toMap(mockMatomoTracker);
expect(Uri.encodeQueryComponent(eventMap['url'] ?? ''), expectedUrl);
});
});
});

group('copyWith', () {
Expand Down

0 comments on commit b9d5c3e

Please sign in to comment.