Skip to content

Commit ae00559

Browse files
altaywtfBrandonStalnaker
authored andcommitted
Implement getSession method
* Add and export getSession method on iOS * Add and export getSession method on Android * Add getSession method to the JS bridge * Update sample/android to display sessionID * Update iOS bridge to use session UUID * Update sample/iOS to display sessionID
1 parent 85ea571 commit ae00559

File tree

5 files changed

+73
-45
lines changed

5 files changed

+73
-45
lines changed

android/src/main/java/com/mparticle/react/MParticleModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ public void aliasUsers(final ReadableMap readableMap, final Callback completion)
294294
}
295295
}
296296

297+
public void getSession(Callback completion) {
298+
String sessionID = MParticle.getInstance().getCurrentSession().getSessionUUID();
299+
completion.invoke(sessionID);
300+
}
301+
297302
@ReactMethod
298303
public void getUserIdentities(final String userId, Callback completion) {
299304
MParticleUser selectedUser = MParticle.getInstance().Identity().getUser(parseMpid(userId));

ios/RNMParticle/RNMParticle.m

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ + (void)load {
6262
RCT_EXPORT_METHOD(removeGDPRConsentStateWithPurpose:(NSString *)purpose)
6363
{
6464
MParticleUser *user = [MParticle sharedInstance].identity.currentUser;
65-
65+
6666
MPConsentState *consentState = user.consentState ? user.consentState : [[MPConsentState alloc] init];
6767
[consentState removeGDPRConsentStateWithPurpose:purpose];
6868
user.consentState = consentState;
@@ -146,18 +146,18 @@ + (void)load {
146146
if (error) {
147147
reactError = [[NSMutableDictionary alloc] initWithCapacity:4];
148148
MPIdentityHTTPErrorResponse *response = error.userInfo[mParticleIdentityErrorKey];
149-
149+
150150
if ([response isKindOfClass:[NSString class]]) {
151151
[reactError setObject:response forKey:@"message"];
152152
} else {
153153
if ([NSNumber numberWithLong:response.httpCode] != nil) {
154154
[reactError setObject:[NSNumber numberWithLong:response.httpCode] forKey:@"httpCode"];
155155
}
156-
156+
157157
if ([NSNumber numberWithInt:response.code] != nil) {
158158
[reactError setObject:[NSNumber numberWithInt:response.code] forKey:@"responseCode"];
159159
}
160-
160+
161161
if (response.message != nil) {
162162
[reactError setObject:response.message forKey:@"message"];
163163
}
@@ -180,18 +180,18 @@ + (void)load {
180180
if (error) {
181181
reactError = [[NSMutableDictionary alloc] initWithCapacity:4];
182182
MPIdentityHTTPErrorResponse *response = error.userInfo[mParticleIdentityErrorKey];
183-
183+
184184
if ([response isKindOfClass:[NSString class]]) {
185185
[reactError setObject:response forKey:@"message"];
186186
} else {
187187
if ([NSNumber numberWithLong:response.httpCode] != nil) {
188188
[reactError setObject:[NSNumber numberWithLong:response.httpCode] forKey:@"httpCode"];
189189
}
190-
190+
191191
if ([NSNumber numberWithInt:response.code] != nil) {
192192
[reactError setObject:[NSNumber numberWithInt:response.code] forKey:@"responseCode"];
193193
}
194-
194+
195195
if (response.message != nil) {
196196
[reactError setObject:response.message forKey:@"message"];
197197
}
@@ -200,7 +200,7 @@ + (void)load {
200200
completion(@[reactError, apiResult.user.userId.stringValue, apiResult.previousUser.userId.stringValue]);
201201
} else {
202202
completion(@[reactError, @0]);
203-
}
203+
}
204204
} else {
205205
completion(@[[NSNull null], apiResult.user.userId.stringValue, apiResult.previousUser.userId.stringValue]);
206206
}
@@ -214,18 +214,18 @@ + (void)load {
214214
if (error) {
215215
reactError = [[NSMutableDictionary alloc] initWithCapacity:4];
216216
MPIdentityHTTPErrorResponse *response = error.userInfo[mParticleIdentityErrorKey];
217-
217+
218218
if ([response isKindOfClass:[NSString class]]) {
219219
[reactError setObject:response forKey:@"message"];
220220
} else {
221221
if ([NSNumber numberWithLong:response.httpCode] != nil) {
222222
[reactError setObject:[NSNumber numberWithLong:response.httpCode] forKey:@"httpCode"];
223223
}
224-
224+
225225
if ([NSNumber numberWithInt:response.code] != nil) {
226226
[reactError setObject:[NSNumber numberWithInt:response.code] forKey:@"responseCode"];
227227
}
228-
228+
229229
if (response.message != nil) {
230230
[reactError setObject:response.message forKey:@"message"];
231231
}
@@ -248,18 +248,18 @@ + (void)load {
248248
if (error) {
249249
reactError = [[NSMutableDictionary alloc] initWithCapacity:4];
250250
MPIdentityHTTPErrorResponse *response = error.userInfo[mParticleIdentityErrorKey];
251-
251+
252252
if ([response isKindOfClass:[NSString class]]) {
253253
[reactError setObject:response forKey:@"message"];
254254
} else {
255255
if ([NSNumber numberWithLong:response.httpCode] != nil) {
256256
[reactError setObject:[NSNumber numberWithLong:response.httpCode] forKey:@"httpCode"];
257257
}
258-
258+
259259
if ([NSNumber numberWithInt:response.code] != nil) {
260260
[reactError setObject:[NSNumber numberWithInt:response.code] forKey:@"responseCode"];
261261
}
262-
262+
263263
if (response.message != nil) {
264264
[reactError setObject:response.message forKey:@"message"];
265265
}
@@ -307,6 +307,12 @@ + (void)load {
307307
completion(@[[NSNull null], [selectedUser lastSeen]]);
308308
}
309309

310+
RCT_EXPORT_METHOD(getSession:(RCTResponseSenderBlock)completion)
311+
{
312+
NSString *sessionID = [MParticle sharedInstance].currentSession.UUID;
313+
completion(@[sessionID]);
314+
}
315+
310316
@end
311317

312318
typedef NS_ENUM(NSUInteger, MPReactCommerceEventAction) {
@@ -345,9 +351,9 @@ + (MPCommerceEvent *)MPCommerceEvent:(id)json {
345351
BOOL isProductAction = json[@"productActionType"] != nil;
346352
BOOL isPromotion = json[@"promotionActionType"] != nil;
347353
BOOL isImpression = json[@"impressions"] != nil;
348-
354+
349355
NSAssert(isProductAction || isPromotion || isImpression, @"Invalid commerce event");
350-
356+
351357
MPCommerceEvent *commerceEvent = nil;
352358
if (isProductAction) {
353359
MPCommerceEventAction action = [RCTConvert MPCommerceEventAction:json[@"productActionType"]];
@@ -360,7 +366,7 @@ + (MPCommerceEvent *)MPCommerceEvent:(id)json {
360366
else {
361367
commerceEvent = [[MPCommerceEvent alloc] initWithImpressionName:nil product:nil];
362368
}
363-
369+
364370
commerceEvent.checkoutOptions = json[@"checkoutOptions"];
365371
commerceEvent.currency = json[@"currency"];
366372
commerceEvent.productListName = json[@"productActionListName"];
@@ -370,15 +376,15 @@ + (MPCommerceEvent *)MPCommerceEvent:(id)json {
370376
commerceEvent.action = [RCTConvert MPCommerceEventAction:json[@"productActionType"]];
371377
commerceEvent.checkoutStep = [json[@"checkoutStep"] intValue];
372378
commerceEvent.nonInteractive = [json[@"nonInteractive"] boolValue];
373-
379+
374380
NSMutableArray *products = [NSMutableArray array];
375381
NSArray *jsonProducts = json[@"products"];
376382
[jsonProducts enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
377383
MPProduct *product = [RCTConvert MPProduct:obj];
378384
[products addObject:product];
379385
}];
380386
[commerceEvent addProducts:products];
381-
387+
382388
NSArray *jsonImpressions = json[@"impressions"];
383389
[jsonImpressions enumerateObjectsUsingBlock:^(NSDictionary *jsonImpression, NSUInteger idx, BOOL * _Nonnull stop) {
384390
NSString *listName = jsonImpression[@"impressionListName"];
@@ -388,7 +394,7 @@ + (MPCommerceEvent *)MPCommerceEvent:(id)json {
388394
[commerceEvent addImpression:product listName:listName];
389395
}];
390396
}];
391-
397+
392398
return commerceEvent;
393399
}
394400

@@ -400,7 +406,7 @@ + (MPPromotionContainer *)MPPromotionContainer:(id)json {
400406
MPPromotion *promotion = [RCTConvert MPPromotion:obj];
401407
[promotionContainer addPromotion:promotion];
402408
}];
403-
409+
404410
return promotionContainer;
405411
}
406412

@@ -450,43 +456,43 @@ + (MPCommerceEventAction)MPCommerceEventAction:(NSNumber *)json {
450456
case MPReactCommerceEventActionAddToCart:
451457
action = MPCommerceEventActionAddToCart;
452458
break;
453-
459+
454460
case MPReactCommerceEventActionRemoveFromCart:
455461
action = MPCommerceEventActionRemoveFromCart;
456462
break;
457-
463+
458464
case MPReactCommerceEventActionCheckout:
459465
action = MPCommerceEventActionCheckout;
460466
break;
461-
467+
462468
case MPReactCommerceEventActionCheckoutOptions:
463469
action = MPCommerceEventActionCheckoutOptions;
464470
break;
465-
471+
466472
case MPReactCommerceEventActionClick:
467473
action = MPCommerceEventActionClick;
468474
break;
469-
475+
470476
case MPReactCommerceEventActionViewDetail:
471477
action = MPCommerceEventActionViewDetail;
472478
break;
473-
479+
474480
case MPReactCommerceEventActionPurchase:
475481
action = MPCommerceEventActionPurchase;
476482
break;
477-
483+
478484
case MPReactCommerceEventActionRefund:
479485
action = MPCommerceEventActionRefund;
480486
break;
481-
487+
482488
case MPReactCommerceEventActionAddToWishList:
483489
action = MPCommerceEventActionAddToWishList;
484490
break;
485-
491+
486492
case MPReactCommerceEventActionRemoveFromWishlist:
487493
action = MPCommerceEventActionRemoveFromWishlist;
488494
break;
489-
495+
490496
default:
491497
action = MPCommerceEventActionAddToCart;
492498
NSAssert(NO, @"Invalid commerce event action");
@@ -515,7 +521,7 @@ + (MPIdentityApiResult *)MPIdentityApiResult:(id)json {
515521
MPIdentityApiResult *result = [[MPIdentityApiResult alloc] init];
516522
id obj = json[@"user"];
517523
result.user = [RCTConvert MParticleUser:obj];
518-
524+
519525
return result;
520526
}
521527

@@ -540,43 +546,40 @@ + (MPAliasRequest *)MPAliasRequest:(id)json {
540546
+ (MParticleUser *)MParticleUser:(id)json {
541547
MParticleUser *user = [[MParticleUser alloc] init];
542548
user.userId = json[@"userId"];
543-
549+
544550
return user;
545551
}
546552

547553
+ (MPEvent *)MPEvent:(id)json {
548554
MPEvent *event = [[MPEvent alloc] init];
549-
555+
550556
event.category = json[@"category"];
551557
event.duration = json[@"duration"];
552558
event.endTime = json[@"endTime"];
553559
event.info = json[@"info"];
554560
event.name = json[@"name"];
555561
event.startTime = json[@"startTime"];
556562
event.type = [json[@"type"] intValue];
557-
563+
558564
NSDictionary *jsonFlags = json[@"customFlags"];
559565
for (NSString *key in jsonFlags) {
560566
NSString *value = jsonFlags[key];
561567
[event addCustomFlag:value withKey:key];
562568
}
563-
569+
564570
return event;
565571
}
566572

567573
+ (MPGDPRConsent *)MPGDPRConsent:(id)json {
568574
MPGDPRConsent *mpConsent = [[MPGDPRConsent alloc] init];
569-
575+
570576
mpConsent.consented = [RCTConvert BOOL:json[@"consented"]];
571577
mpConsent.document = json[@"document"];
572578
mpConsent.timestamp = [RCTConvert NSDate:json[@"timestamp"]];
573579
mpConsent.location = json[@"location"];
574580
mpConsent.hardwareId = json[@"hardwareId"];
575-
581+
576582
return mpConsent;
577583
}
578584

579585
@end
580-
581-
582-

js/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ const logPushRegistration = (registrationField1, registrationField2) => {
108108
NativeModules.MParticle.logPushRegistration(registrationField1, registrationField2)
109109
}
110110

111+
const getSession = (completion) => {
112+
NativeModules.MParticle.getSession(completion)
113+
}
114+
111115
// ******** Identity ********
112116
class User {
113117
constructor (userId) {
@@ -572,7 +576,8 @@ const MParticle = {
572576
removeGDPRConsentStateWithPurpose,
573577
isKitActive,
574578
getAttributions,
575-
logPushRegistration
579+
logPushRegistration,
580+
getSession
576581
}
577582

578583
export default MParticle

sample/index.android.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default class MParticleSample extends Component {
2121
this.state = {isShowingText: true,
2222
optedOut: true,
2323
attributionResults: "{value: no attributionResults}",
24+
session: '',
2425
isKitActive: true};
2526

2627
this._toggleOptOut = this._toggleOptOut.bind(this)
@@ -111,6 +112,10 @@ export default class MParticleSample extends Component {
111112
}, 5000);
112113
}
113114

115+
componentDidMount() {
116+
MParticle.getSession(session => this.setState({ session }))
117+
}
118+
114119
_toggleOptOut() {
115120
MParticle.getOptOut((optedOut) => {
116121
MParticle.setOptOut(!optedOut)
@@ -157,9 +162,12 @@ export default class MParticleSample extends Component {
157162
<Text style={styles.welcome}>
158163
Opted Out = {optedOut}
159164
</Text>
160-
<Button
165+
<Button
161166
onPress={() => {this._toggleOptOut()}}
162167
title={'Opt ' + optAction}/>
168+
<Text>
169+
Session = {JSON.stringify(this.state.session)}
170+
</Text>
163171
<Text>
164172
Attributes = {JSON.stringify(this.state.attributionResults)}
165173
</Text>
@@ -183,7 +191,6 @@ export default class MParticleSample extends Component {
183191
<Button
184192
onPress={() => {this._incrementAttribute()}}
185193
title="Increment Attribute"/>
186-
187194
</View>
188195
);
189196
}

sample/index.ios.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default class MParticleSample extends Component {
2020
super(props);
2121
this.state = {isShowingText: true,
2222
optedOut: true,
23+
session: '',
2324
attributionResults: "{value: no attributionResults}",
2425
isKitActive: true};
2526

@@ -111,6 +112,10 @@ export default class MParticleSample extends Component {
111112
}, 5000);
112113
}
113114

115+
componentDidMount() {
116+
MParticle.getSession(session => this.setState({ session }))
117+
}
118+
114119
_toggleOptOut() {
115120
MParticle.getOptOut((optedOut) => {
116121
MParticle.setOptOut(!optedOut)
@@ -157,9 +162,12 @@ export default class MParticleSample extends Component {
157162
<Text style={styles.welcome}>
158163
Opted Out = {optedOut}
159164
</Text>
160-
<Button
165+
<Button
161166
onPress={() => {this._toggleOptOut()}}
162167
title={'Opt ' + optAction}/>
168+
<Text>
169+
Session = {JSON.stringify(this.state.session)}
170+
</Text>
163171
<Text>
164172
Attributes = {JSON.stringify(this.state.attributionResults)}
165173
</Text>

0 commit comments

Comments
 (0)