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

132 contentbaseurl not working with hash sign #133

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
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
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