forked from segmentio/analytics_flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
properties.dart
69 lines (64 loc) · 2.58 KB
/
properties.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import 'package:analytics/map_transform.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:json_annotation/json_annotation.dart';
const Map<String, PropertyMapper> mappings = {
"total": PropertyMapper("value"),
"products": PropertyMapper("items", fromJson: itemsFromJson),
"name": PropertyMapper("itemName"),
"product_id": PropertyMapper("itemId"),
"productId": PropertyMapper("itemId"),
"query": PropertyMapper("searchTerm"),
"share_via": PropertyMapper("method"),
"category": PropertyMapper("contentType")
};
dynamic itemsFromJson(dynamic items) {
if (items is List<dynamic>) {
return items.map((item) {
final mappedObject = mapProperties(item, mappings);
return AnalyticsEventItemJson(mappedObject);
}).toList();
} else {
return items;
}
}
String sanitizeEventName(String eventName) {
return eventName.replaceAll(RegExp(r'[^a-zA-Z0-9]'), '_');
}
Map<String, Object?> castParameterType(Map<String, Object?> properties) {
return properties.map((key, value) {
if (value is String || value is num || value == null) {
return MapEntry(key, value);
} else {
return MapEntry(key, value.toString());
}
});
}
@JsonSerializable()
class AnalyticsEventItemJson extends AnalyticsEventItem {
AnalyticsEventItemJson(Map<String, Object?> json)
: super(
affiliation: json['affiliation'].toString(),
currency: json['currency'].toString(),
coupon: json['coupon'].toString(),
creativeName: json['creativeName'].toString(),
creativeSlot: json['creativeSlot'].toString(),
discount: num.tryParse(json['discount'].toString()),
index: int.tryParse(json['index'].toString()),
itemBrand: json['itemBrand'].toString(),
itemCategory: json['itemCategory'].toString(),
itemCategory2: json['itemCategory2'].toString(),
itemCategory3: json['itemCategory3'].toString(),
itemCategory4: json['itemCategory4'].toString(),
itemCategory5: json['itemCategory5'].toString(),
itemId: json['itemId'].toString(),
itemListId: json['itemListId'].toString(),
itemListName: json['itemListName'].toString(),
itemName: json['itemName'].toString(),
itemVariant: json['itemVariant'].toString(),
locationId: json['locationId'].toString(),
price: num.tryParse(json['price'].toString()),
promotionId: json['promotionId'].toString(),
promotionName: json['promotionName'].toString(),
quantity: int.tryParse(json['quantity'].toString()),
);
}