Skip to content

Commit dd3e7a5

Browse files
authored
Added a call to provide user properties during init (#71)
* dding a call to provide user properties during init * Removing filtering for user properties during init * User details filtering implemented in iOS * Config change in calling sequence * Fixing iOS user details issue during init. * Reverting SERVER_URL and APP_KEY values
1 parent 1b2bf86 commit dd3e7a5

File tree

7 files changed

+104
-41
lines changed

7 files changed

+104
-41
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
## 22.02.0
2+
* Fixed notification trampoline restrictions in Android 12 using reverse activity trampolining implementation.
3+
* Adding a call to provide user properties during initialization.
24
* Updated underlying android SDK version to 22.02.1
35
* Updated underlying iOS SDK version to 22.06.0
46

android/src/main/java/ly/count/dart/countly_flutter/CountlyFlutterPlugin.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,11 @@ private void populateConfig(JSONObject _config) throws JSONException {
11491149
Map<String, Object> customCrashSegment = toMap(_config.getJSONObject("customCrashSegment"));
11501150
this.config.setCustomCrashSegment(customCrashSegment);
11511151
}
1152+
if (_config.has("providedUserProperties")) {
1153+
Map<String, Object> providedUserProperties = toMap(_config.getJSONObject("providedUserProperties"));
1154+
this.config.setUserProperties(providedUserProperties);
1155+
}
1156+
11521157
if (_config.has("consents")) {
11531158
String[] consents = toStringArray(_config.getJSONArray("consents"));
11541159
this.config.setConsentEnabled(consents);

example/lib/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ class _MyAppState extends State<MyApp> {
4444
Countly.isInitialized().then((bool isInitialized) {
4545
if (!isInitialized) {
4646
Countly.pushTokenType(Countly.messagingMode['TEST']!); // Set messaging mode for push notifications
47+
4748
var crashSegment = {'Key': 'Value'};
49+
var userProperties = {'customProperty': 'custom Value', 'username': 'USER_NAME', 'email': 'USER_EMAIL'};
50+
4851
Map<String, String> attributionValues = {};
4952
if (Platform.isIOS) {
5053
attributionValues[AttributionKey.IDFA] = 'IDFA';
@@ -73,6 +76,7 @@ class _MyAppState extends State<MyApp> {
7376
])
7477
..setLocation(country_code: 'TR', city: 'Istanbul', ipAddress: '41.0082,28.9784', gpsCoordinates: '10.2.33.12') // Set user location.
7578
..setCustomCrashSegment(crashSegment)
79+
..setUserProperties(userProperties)
7680
..recordIndirectAttribution(attributionValues)
7781
..recordDirectAttribution('countly', campaignData)
7882
..setRemoteConfigAutomaticDownload(true, (error) {

ios/Classes/CountlyFlutterPlugin.m

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ + (CountlyFeedbackWidget *)createWithDictionary:(NSDictionary *)dictionary;
2626

2727
NSArray<CountlyFeedbackWidget*>* feedbackWidgetList = nil;
2828

29+
NSString* const NAME_KEY = @"name";
30+
NSString* const USERNAME_KEY = @"username";
31+
NSString* const EMAIL_KEY = @"email";
32+
NSString* const ORG_KEY = @"organization";
33+
NSString* const PHONE_KEY = @"phone";
34+
NSString* const PICTURE_KEY = @"picture";
35+
NSString* const PICTURE_PATH_KEY = @"picturePath";
36+
NSString* const GENDER_KEY = @"gender";
37+
NSString* const BYEAR_KEY = @"byear";
38+
NSString* const CUSTOM_KEY = @"custom";
39+
2940
@implementation CountlyFlutterPlugin
3041

3142
CountlyConfig* config = nil;
@@ -141,39 +152,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
141152
dispatch_async(dispatch_get_main_queue(), ^ {
142153
NSDictionary* userData = [command objectAtIndex:0];
143154

144-
NSString* name = userData[@"name"];
145-
NSString* username = userData[@"username"];
146-
NSString* email = userData[@"email"];
147-
NSString* organization = userData[@"organization"];
148-
NSString* phone = userData[@"phone"];
149-
NSString* picture = userData[@"picture"];
150-
NSString* gender = userData[@"gender"];
151-
NSString* byear = userData[@"byear"];
152-
153-
if(name) {
154-
Countly.user.name = name;
155-
}
156-
if(username) {
157-
Countly.user.username = username;
158-
}
159-
if(email) {
160-
Countly.user.email = email;
161-
}
162-
if(organization) {
163-
Countly.user.organization = organization;
164-
}
165-
if(phone) {
166-
Countly.user.phone = phone;
167-
}
168-
if(picture) {
169-
Countly.user.pictureURL = picture;
170-
}
171-
if(gender) {
172-
Countly.user.gender = gender;
173-
}
174-
if(byear) {
175-
Countly.user.birthYear = @([byear integerValue]);
176-
}
155+
[self setUserData:userData];
177156

178157
[Countly.user save];
179158
result(@"setuserdata!");
@@ -962,6 +941,51 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
962941
}
963942
}
964943

944+
-(NSDictionary*) removePredefinedUserProperties:(NSDictionary * __nullable) userData {
945+
NSMutableDictionary* userProperties = [userData mutableCopy];
946+
NSArray* nameFields = [[NSArray alloc] initWithObjects:NAME_KEY, USERNAME_KEY, EMAIL_KEY, ORG_KEY, PHONE_KEY, PICTURE_KEY, PICTURE_PATH_KEY, GENDER_KEY, BYEAR_KEY, nil];
947+
948+
for (NSString* nameField in nameFields) {
949+
[userProperties removeObjectForKey:nameField];
950+
}
951+
return userProperties;
952+
}
953+
954+
-(void) setUserData:(NSDictionary * __nullable) userData {
955+
NSString* name = userData[NAME_KEY];
956+
NSString* username = userData[USERNAME_KEY];
957+
NSString* email = userData[EMAIL_KEY];
958+
NSString* organization = userData[ORG_KEY];
959+
NSString* phone = userData[PHONE_KEY];
960+
NSString* picture = userData[PICTURE_KEY];
961+
NSString* gender = userData[GENDER_KEY];
962+
NSString* byear = userData[BYEAR_KEY];
963+
964+
if(name) {
965+
Countly.user.name = name;
966+
}
967+
if(username) {
968+
Countly.user.username = username;
969+
}
970+
if(email) {
971+
Countly.user.email = email;
972+
}
973+
if(organization) {
974+
Countly.user.organization = organization;
975+
}
976+
if(phone) {
977+
Countly.user.phone = phone;
978+
}
979+
if(picture) {
980+
Countly.user.pictureURL = picture;
981+
}
982+
if(gender) {
983+
Countly.user.gender = gender;
984+
}
985+
if(byear) {
986+
Countly.user.birthYear = @([byear integerValue]);
987+
}
988+
}
965989
-(void) feedbackWidgetDataCallback:(NSDictionary * __nullable) widgetData error:(NSString * __nullable )error{
966990
NSMutableDictionary *feedbackWidgetData = [[NSMutableDictionary alloc] init];
967991
if(widgetData) {
@@ -971,7 +995,7 @@ -(void) feedbackWidgetDataCallback:(NSDictionary * __nullable) widgetData error:
971995
feedbackWidgetData[@"error"] = error;
972996
}
973997
[_channel invokeMethod:@"feedbackWidgetDataCallback" arguments: feedbackWidgetData];
974-
}
998+
}
975999

9761000
- (CountlyFeedbackWidget*)getFeedbackWidget:(NSString*)widgetId
9771001
{
@@ -1066,6 +1090,14 @@ - (void)populateConfig:(NSDictionary*)_config
10661090
if(crashSegmentation) {
10671091
config.crashSegmentation = crashSegmentation;
10681092
}
1093+
1094+
NSDictionary* providedUserProperties = _config[@"providedUserProperties"];
1095+
if(providedUserProperties) {
1096+
[self setUserData:providedUserProperties];
1097+
NSDictionary* customeProperties = [self removePredefinedUserProperties:providedUserProperties];
1098+
Countly.user.custom = customeProperties;
1099+
}
1100+
10691101
NSArray* consents = _config[@"consents"];
10701102
if(consents) {
10711103
config.consents = consents;

ios/Classes/CountlyiOS/CountlyConsentManager.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ - (void)giveConsentForFeatures:(NSArray *)features
7979
//NOTE: Otherwise, if location consent is given after sessions consent, begin_session request will be sent with an empty string as location.
8080
if ([features containsObject:CLYConsentLocation] && !self.consentForLocation)
8181
self.consentForLocation = YES;
82+
83+
if ([features containsObject:CLYConsentUserDetails] && !self.consentForUserDetails)
84+
self.consentForUserDetails = YES;
8285

8386
if ([features containsObject:CLYConsentSessions] && !self.consentForSessions)
8487
self.consentForSessions = YES;
8588

8689
if ([features containsObject:CLYConsentEvents] && !self.consentForEvents)
8790
self.consentForEvents = YES;
8891

89-
if ([features containsObject:CLYConsentUserDetails] && !self.consentForUserDetails)
90-
self.consentForUserDetails = YES;
91-
9292
if ([features containsObject:CLYConsentCrashReporting] && !self.consentForCrashReporting)
9393
self.consentForCrashReporting = YES;
9494

@@ -260,6 +260,7 @@ - (void)setConsentForUserDetails:(BOOL)consentForUserDetails
260260
if (consentForUserDetails)
261261
{
262262
CLY_LOG_D(@"Consent for UserDetails is given.");
263+
[Countly.user save];
263264
}
264265
else
265266
{

lib/countly_config.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class CountlyConfig {
2929
Map<String, String>? _iaAttributionValues;
3030
Map<String, dynamic>? _customCrashSegment;
3131
bool? _enableRemoteConfigAutomaticDownload;
32+
Map<String, dynamic>? _providedUserProperties;
3233

3334
CountlyConfig(this._serverURL, this._appKey);
3435

@@ -85,6 +86,8 @@ class CountlyConfig {
8586

8687
bool? get enableUnhandledCrashReporting => _enableUnhandledCrashReporting;
8788

89+
Map<String, dynamic>? get providedUserProperties => _providedUserProperties;
90+
8891
bool? get enableRemoteConfigAutomaticDownload => _enableRemoteConfigAutomaticDownload;
8992

9093
/// URL of the Countly server to submit data to.
@@ -240,4 +243,10 @@ class CountlyConfig {
240243
_iaAttributionValues = attributionValues;
241244
return this;
242245
}
246+
247+
/// Used to provide user properties that would be sent as soon as possible
248+
CountlyConfig setUserProperties(Map<String, dynamic> userProperties) {
249+
_providedUserProperties = userProperties;
250+
return this;
251+
}
243252
}

lib/countly_flutter.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ class Countly {
309309
int optionsCount = options.length;
310310
log('Calling "setUserData" with options Count:[$optionsCount]');
311311
List<dynamic> args = [];
312+
Map<String, String> userData = _getUserData(options);
313+
args.add(userData);
314+
final String? result = await _channel.invokeMethod('setuserdata', <String, dynamic>{'data': json.encode(args)});
315+
return result;
316+
}
317+
318+
static Map<String, String> _getUserData(Map<String, dynamic> options)
319+
{
312320
Map<String, String> userData = {};
313321
if (options.containsKey('name')) {
314322
userData['name'] = options['name'].toString();
@@ -337,11 +345,8 @@ class Countly {
337345
if (options.containsKey('byear')) {
338346
userData['byear'] = options['byear'].toString();
339347
}
340-
args.add(userData);
341-
final String? result = await _channel.invokeMethod('setuserdata', <String, dynamic>{'data': json.encode(args)});
342-
return result;
348+
return userData;
343349
}
344-
345350
/// This method will ask for permission, enables push notification and send push token to countly server.
346351
/// Should be call after Countly init
347352
static Future<String?> askForNotificationPermission() async {
@@ -1800,6 +1805,11 @@ class Countly {
18001805
if (config.customCrashSegment != null) {
18011806
countlyConfig['customCrashSegment'] = config.customCrashSegment;
18021807
}
1808+
1809+
if (config.providedUserProperties != null) {
1810+
countlyConfig['providedUserProperties'] = config.providedUserProperties;
1811+
}
1812+
18031813
if (config.consents != null) {
18041814
countlyConfig['consents'] = config.consents;
18051815
}

0 commit comments

Comments
 (0)