Skip to content

Commit

Permalink
Merge pull request #120 from Floating-Dartists/feat/#119_change-url-o…
Browse files Browse the repository at this point in the history
…n-the-fly

Feat: Change url on the fly
  • Loading branch information
TesteurManiak authored Aug 19, 2023
2 parents 4c06ef6 + e9ce78a commit 2100d3d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 5 deletions.
28 changes: 26 additions & 2 deletions lib/src/matomo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class MatomoTracker {

late final PlatformInfo _platformInfo;

@visibleForTesting
MatomoDispatcher get dispatcher => _dispatcher;

late MatomoDispatcher _dispatcher;

static final instance = MatomoTracker._internal();
Expand All @@ -61,7 +64,28 @@ class MatomoTracker {
/// Should not be confused with the `url` tracking parameter
/// which is constructed by combining [contentBase] with a `path`
/// (e.g. in [trackPageViewWithName]).
late final String url;
///
/// You can use [setUrl] to change this value after initialization.
String get url {
if (_url case final url?) {
return url;
}
throw const UninitializedMatomoInstanceException();
}

String? _url;

/// Sets the url of the Matomo endpoint and updates the dispatcher.
///
/// Note that this will change the url used by the request that are still
/// in the queue.
void setUrl(String newUrl) {
_initializationCheck();

_url = newUrl;
_dispatcher = _dispatcher.copyWith(baseUrl: newUrl);
}

late final Session session;

Visitor get visitor => _visitor;
Expand Down Expand Up @@ -223,7 +247,7 @@ class MatomoTracker {
log.setLogging(level: verbosityLevel);

this.siteId = siteId;
this.url = url;
_url = url;
this.customHeaders = customHeaders;
_pingInterval = pingInterval;
_lock = sync.Lock();
Expand Down
16 changes: 16 additions & 0 deletions lib/src/matomo_dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,20 @@ class MatomoDispatcher {

return baseUri.replace(queryParameters: queryParameters);
}

MatomoDispatcher copyWith({
String? baseUrl,
String? tokenAuth,
String? userAgent,
Logger? log,
http.Client? httpClient,
}) {
return MatomoDispatcher(
baseUrl: baseUrl ?? baseUri.toString(),
tokenAuth: tokenAuth ?? this.tokenAuth,
userAgent: userAgent ?? this.userAgent,
log: log ?? this.log,
httpClient: httpClient ?? this.httpClient,
);
}
}
2 changes: 1 addition & 1 deletion test/ressources/mock/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const wantedContentMap = <String, String>{
const wantedContentMapFull = <String, String>{
'c_n': matomoContentName,
'c_p': matomoContentPiece,
'c_t': matomoContentTarget
'c_t': matomoContentTarget,
};

// PerformanceInfo
Expand Down
2 changes: 1 addition & 1 deletion test/src/dispatch_settings_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void main() {
// will filter out recent
DispatchSettings.whereUserId(_uid2),
// will filter out old
DispatchSettings.whereNotOlderThanADay
DispatchSettings.whereNotOlderThanADay,
],
);
final after = [old, recent]..retainWhere(chained);
Expand Down
40 changes: 40 additions & 0 deletions test/src/matomo_dispatcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,44 @@ void main() {
),
);
});

group('copyWith', () {
test('should copy with new url', () {
const newUrl = 'https://test.com';

final initialDispatcher = MatomoDispatcher(
baseUrl: matomoDispatcherBaseUrl,
userAgent: matomoTrackerUserAgent,
tokenAuth: matomoDispatcherToken,
httpClient: mockHttpClient,
log: mockLogger,
);

final newDispatcher = initialDispatcher.copyWith(baseUrl: newUrl);

expect(newDispatcher.baseUri.toString(), newUrl);
expect(newDispatcher.userAgent, matomoTrackerUserAgent);
expect(newDispatcher.tokenAuth, matomoDispatcherToken);
expect(newDispatcher.httpClient, mockHttpClient);
expect(newDispatcher.log, mockLogger);
});

test('should do nothing', () {
final initialDispatcher = MatomoDispatcher(
baseUrl: matomoDispatcherBaseUrl,
userAgent: matomoTrackerUserAgent,
tokenAuth: matomoDispatcherToken,
httpClient: mockHttpClient,
log: mockLogger,
);

final newDispatcher = initialDispatcher.copyWith();

expect(newDispatcher.baseUri.toString(), matomoDispatcherBaseUrl);
expect(newDispatcher.userAgent, matomoTrackerUserAgent);
expect(newDispatcher.tokenAuth, matomoDispatcherToken);
expect(newDispatcher.httpClient, mockHttpClient);
expect(newDispatcher.log, mockLogger);
});
});
}
29 changes: 29 additions & 0 deletions test/src/matomo_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:matomo_tracker/src/content.dart';
import 'package:matomo_tracker/src/event_info.dart';
import 'package:matomo_tracker/src/exceptions.dart';
import 'package:matomo_tracker/src/matomo.dart';
import 'package:mocktail/mocktail.dart';

Expand Down Expand Up @@ -474,4 +475,32 @@ void main() {
},
);
});

group('url getter', () {
test('should throw if not initialized', () {
expect(
() => MatomoTracker().url,
throwsA(isA<UninitializedMatomoInstanceException>()),
);
});
});

group('setUrl', () {
test('should throw if not initialized', () {
expect(
() => MatomoTracker().setUrl(''),
throwsA(isA<UninitializedMatomoInstanceException>()),
);
});

test('should set the new url and dispatcher', () async {
const newUrl = 'https://test.com';
final tracker = await getInitializedMatomoTracker();

tracker.setUrl(newUrl);

expect(tracker.url, newUrl);
expect(tracker.dispatcher.baseUri.toString(), newUrl);
});
});
}
2 changes: 1 addition & 1 deletion test/src/persistent_queue_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const _nonEmptyJsonElementCount = 2;
const _action = <String, String>{'action3': 'value3'};
const _actions = [
_action,
<String, String>{'action4': 'value4'}
<String, String>{'action4': 'value4'},
];

void main() {
Expand Down

0 comments on commit 2100d3d

Please sign in to comment.