Skip to content

Commit 902a5f6

Browse files
feat(cli_tools): Enable IP geotracking in analytics events (#93)
To enable IP-based geotracking of the client origin, MixPanel requires the URI query parameter `ip` to be set to 1. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added option to disable IP tracking in analytics configuration for enhanced privacy control <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent ed5c5f2 commit 902a5f6

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

packages/cli_tools/lib/src/analytics/analytics.dart

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,44 @@ class MixPanelAnalytics implements Analytics {
1919
static const _defaultTimeout = Duration(seconds: 2);
2020

2121
final String _uniqueUserId;
22-
final String _endpoint;
2322
final String _projectToken;
2423
final String _version;
24+
25+
final Uri _endpoint;
2526
final Duration _timeout;
2627

2728
MixPanelAnalytics({
2829
required final String uniqueUserId,
2930
required final String projectToken,
3031
required final String version,
31-
final String endpoint = _defaultEndpoint,
32+
final String? endpoint,
3233
final Duration timeout = _defaultTimeout,
34+
final bool disableIpTracking = false,
3335
}) : _uniqueUserId = uniqueUserId,
3436
_projectToken = projectToken,
3537
_version = version,
36-
_endpoint = endpoint,
38+
_endpoint = _buildEndpoint(
39+
endpoint ?? _defaultEndpoint,
40+
disableIpTracking,
41+
),
3742
_timeout = timeout;
3843

44+
static Uri _buildEndpoint(
45+
final String baseEndpoint,
46+
final bool disableIpTracking,
47+
) {
48+
final uri = Uri.parse(baseEndpoint);
49+
final ipValue = disableIpTracking ? '0' : '1';
50+
51+
final updatedUri = uri.replace(
52+
queryParameters: {
53+
...uri.queryParameters,
54+
'ip': ipValue,
55+
},
56+
);
57+
return updatedUri;
58+
}
59+
3960
@override
4061
void cleanUp() {}
4162

@@ -71,7 +92,7 @@ class MixPanelAnalytics implements Analytics {
7192
Future<void> _quietPost(final String payload) async {
7293
try {
7394
await http.post(
74-
Uri.parse(_endpoint),
95+
_endpoint,
7596
body: 'data=$payload',
7697
headers: {
7798
'Accept': 'text/plain',

0 commit comments

Comments
 (0)