Skip to content

Commit be36ebc

Browse files
committed
ref
1 parent 910a9e2 commit be36ebc

File tree

3 files changed

+96
-72
lines changed

3 files changed

+96
-72
lines changed

lib/posthog_flutter_web.dart

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flutter_web_plugins/flutter_web_plugins.dart';
99
import 'src/posthog_config.dart';
1010
import 'src/posthog_flutter_platform_interface.dart';
1111
import 'src/posthog_flutter_web_handler.dart';
12+
import 'src/utils/capture_utils.dart';
1213

1314
/// A web implementation of the PosthogFlutterPlatform of the PosthogFlutter plugin.
1415
class PosthogFlutterWeb extends PosthogFlutterPlatformInterface {
@@ -92,44 +93,19 @@ class PosthogFlutterWeb extends PosthogFlutterPlatformInterface {
9293
Map<String, Object>? userProperties,
9394
Map<String, Object>? userPropertiesSetOnce,
9495
}) async {
95-
// Create a mutable copy of properties to extract $set and $set_once
96-
final propertiesCopy =
97-
properties != null ? Map<String, Object>.from(properties) : null;
98-
99-
// Extract $set and $set_once from properties for backward compatibility
100-
Map<String, Object>? legacyUserProperties;
101-
Map<String, Object>? legacyUserPropertiesSetOnce;
102-
103-
if (propertiesCopy != null) {
104-
if (propertiesCopy['\$set'] is Map) {
105-
legacyUserProperties =
106-
Map<String, Object>.from(propertiesCopy['\$set'] as Map);
107-
propertiesCopy.remove('\$set');
108-
}
109-
if (propertiesCopy['\$set_once'] is Map) {
110-
legacyUserPropertiesSetOnce =
111-
Map<String, Object>.from(propertiesCopy['\$set_once'] as Map);
112-
propertiesCopy.remove('\$set_once');
113-
}
114-
}
115-
116-
// Merge legacy properties with new parameters (new parameters take precedence)
117-
final mergedUserProperties = <String, Object>{
118-
...?legacyUserProperties,
119-
...?userProperties,
120-
};
121-
final mergedUserPropertiesSetOnce = <String, Object>{
122-
...?legacyUserPropertiesSetOnce,
123-
...?userPropertiesSetOnce,
124-
};
96+
final extracted = CaptureUtils.extractUserProperties(
97+
properties: properties,
98+
userProperties: userProperties,
99+
userPropertiesSetOnce: userPropertiesSetOnce,
100+
);
125101

126102
return handleWebMethodCall(MethodCall('capture', {
127103
'eventName': eventName,
128-
if (propertiesCopy != null) 'properties': propertiesCopy,
129-
if (mergedUserProperties.isNotEmpty)
130-
'userProperties': mergedUserProperties,
131-
if (mergedUserPropertiesSetOnce.isNotEmpty)
132-
'userPropertiesSetOnce': mergedUserPropertiesSetOnce,
104+
if (extracted.properties != null) 'properties': extracted.properties,
105+
if (extracted.userProperties != null)
106+
'userProperties': extracted.userProperties,
107+
if (extracted.userPropertiesSetOnce != null)
108+
'userPropertiesSetOnce': extracted.userPropertiesSetOnce,
133109
}));
134110
}
135111

lib/src/posthog_flutter_io.dart

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:posthog_flutter/src/util/logging.dart';
1010
import 'surveys/models/posthog_display_survey.dart' as models;
1111
import 'surveys/models/survey_callbacks.dart';
1212
import 'error_tracking/dart_exception_processor.dart';
13+
import 'utils/capture_utils.dart';
1314
import 'utils/property_normalizer.dart';
1415

1516
import 'posthog_config.dart';
@@ -183,46 +184,21 @@ class PosthogFlutterIO extends PosthogFlutterPlatformInterface {
183184
}
184185

185186
try {
186-
// Create a mutable copy of properties to extract $set and $set_once
187-
final propertiesCopy =
188-
properties != null ? Map<String, Object>.from(properties) : null;
189-
190-
// Extract $set and $set_once from properties for backward compatibility
191-
Map<String, Object>? legacyUserProperties;
192-
Map<String, Object>? legacyUserPropertiesSetOnce;
193-
194-
if (propertiesCopy != null) {
195-
if (propertiesCopy['\$set'] is Map) {
196-
legacyUserProperties =
197-
Map<String, Object>.from(propertiesCopy['\$set'] as Map);
198-
propertiesCopy.remove('\$set');
199-
}
200-
if (propertiesCopy['\$set_once'] is Map) {
201-
legacyUserPropertiesSetOnce =
202-
Map<String, Object>.from(propertiesCopy['\$set_once'] as Map);
203-
propertiesCopy.remove('\$set_once');
204-
}
205-
}
206-
207-
// Merge legacy properties with new parameters (new parameters take precedence)
208-
final mergedUserProperties = <String, Object>{
209-
...?legacyUserProperties,
210-
...?userProperties,
211-
};
212-
final mergedUserPropertiesSetOnce = <String, Object>{
213-
...?legacyUserPropertiesSetOnce,
214-
...?userPropertiesSetOnce,
215-
};
216-
217-
final normalizedProperties = propertiesCopy != null
218-
? PropertyNormalizer.normalize(propertiesCopy)
187+
final extracted = CaptureUtils.extractUserProperties(
188+
properties: properties,
189+
userProperties: userProperties,
190+
userPropertiesSetOnce: userPropertiesSetOnce,
191+
);
192+
193+
final normalizedProperties = extracted.properties != null
194+
? PropertyNormalizer.normalize(extracted.properties!)
219195
: null;
220-
final normalizedUserProperties = mergedUserProperties.isNotEmpty
221-
? PropertyNormalizer.normalize(mergedUserProperties)
196+
final normalizedUserProperties = extracted.userProperties != null
197+
? PropertyNormalizer.normalize(extracted.userProperties!)
222198
: null;
223199
final normalizedUserPropertiesSetOnce =
224-
mergedUserPropertiesSetOnce.isNotEmpty
225-
? PropertyNormalizer.normalize(mergedUserPropertiesSetOnce)
200+
extracted.userPropertiesSetOnce != null
201+
? PropertyNormalizer.normalize(extracted.userPropertiesSetOnce!)
226202
: null;
227203

228204
await _methodChannel.invokeMethod('capture', {

lib/src/utils/capture_utils.dart

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/// Result of extracting user properties from capture properties.
2+
class ExtractedCaptureProperties {
3+
/// The properties map with $set and $set_once removed.
4+
final Map<String, Object>? properties;
5+
6+
/// Merged user properties from $set in properties and userProperties parameter.
7+
final Map<String, Object>? userProperties;
8+
9+
/// Merged user properties from $set_once in properties and userPropertiesSetOnce parameter.
10+
final Map<String, Object>? userPropertiesSetOnce;
11+
12+
ExtractedCaptureProperties({
13+
this.properties,
14+
this.userProperties,
15+
this.userPropertiesSetOnce,
16+
});
17+
}
18+
19+
/// Utility class for capture-related operations.
20+
class CaptureUtils {
21+
/// Extracts $set and $set_once from properties for backward compatibility,
22+
/// merges them with the new userProperties and userPropertiesSetOnce parameters,
23+
/// and returns the cleaned properties along with the merged user properties.
24+
///
25+
/// New parameters (userProperties, userPropertiesSetOnce) take precedence
26+
/// over legacy $set and $set_once in properties.
27+
static ExtractedCaptureProperties extractUserProperties({
28+
Map<String, Object>? properties,
29+
Map<String, Object>? userProperties,
30+
Map<String, Object>? userPropertiesSetOnce,
31+
}) {
32+
// Create a mutable copy of properties to extract $set and $set_once
33+
final propertiesCopy =
34+
properties != null ? Map<String, Object>.from(properties) : null;
35+
36+
// Extract $set and $set_once from properties for backward compatibility
37+
Map<String, Object>? legacyUserProperties;
38+
Map<String, Object>? legacyUserPropertiesSetOnce;
39+
40+
if (propertiesCopy != null) {
41+
if (propertiesCopy['\$set'] is Map) {
42+
legacyUserProperties =
43+
Map<String, Object>.from(propertiesCopy['\$set'] as Map);
44+
propertiesCopy.remove('\$set');
45+
}
46+
if (propertiesCopy['\$set_once'] is Map) {
47+
legacyUserPropertiesSetOnce =
48+
Map<String, Object>.from(propertiesCopy['\$set_once'] as Map);
49+
propertiesCopy.remove('\$set_once');
50+
}
51+
}
52+
53+
// Merge legacy properties with new parameters (new parameters take precedence)
54+
final mergedUserProperties = <String, Object>{
55+
...?legacyUserProperties,
56+
...?userProperties,
57+
};
58+
final mergedUserPropertiesSetOnce = <String, Object>{
59+
...?legacyUserPropertiesSetOnce,
60+
...?userPropertiesSetOnce,
61+
};
62+
63+
return ExtractedCaptureProperties(
64+
properties: propertiesCopy,
65+
userProperties:
66+
mergedUserProperties.isNotEmpty ? mergedUserProperties : null,
67+
userPropertiesSetOnce: mergedUserPropertiesSetOnce.isNotEmpty
68+
? mergedUserPropertiesSetOnce
69+
: null,
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)