Last active
May 9, 2018 23:35
-
-
Save tgoldenberg/3e32e4988a08081d62ce to your computer and use it in GitHub Desktop.
Segment Analytics for React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AppDelegate.m | |
#import <Analytics/SEGAnalytics.h> | |
[SEGAnalytics setupWithConfiguration:[SEGAnalyticsConfiguration configurationWithWriteKey:@"YOUR_ANALYTICS_KEY_HERE"]]; | |
# AnalyticsHelper.h | |
#import "RCTViewManager.h" | |
#import "RCTBridgeModule.h" | |
@interface AnalyticsHelper : NSObject <RCTBridgeModule> | |
@end | |
# AnalyticsHelper.m | |
#import "AnalyticsHelper.h" | |
#import <Analytics/SEGAnalytics.h> | |
@implementation AnalyticsHelper | |
RCT_EXPORT_MODULE(); | |
- (NSDictionary *)constantsToExport { | |
return @{ | |
@"greeting": @"Segment Analytics package"}; | |
} | |
RCT_EXPORT_METHOD(squareMe:(int)number:(RCTResponseSenderBlock)callback) { | |
callback(@[[NSNull null], [NSNumber numberWithInt:(number*number)]]); | |
} | |
RCT_EXPORT_METHOD(identify: (NSString*)userId) { | |
// [SEGAnalytics debug:YES]; | |
[[SEGAnalytics sharedAnalytics] identify:userId ]; | |
} | |
RCT_EXPORT_METHOD(open: (NSString*)userId) { | |
// [SEGAnalytics debug:YES]; | |
[[SEGAnalytics sharedAnalytics] track:@"Opens" | |
properties:@{ @"user": userId, @"type": @"open app" }]; | |
} | |
RCT_EXPORT_METHOD(shareVerse: (NSString*)userId) { | |
// [SEGAnalytics debug:YES]; | |
[[SEGAnalytics sharedAnalytics] track:@"Shares" | |
properties:@{ @"user": userId, @"type": @"share verse" }]; | |
} | |
RCT_EXPORT_METHOD(readChapter: (NSString*)userId) { | |
// [SEGAnalytics debug:YES]; | |
[[SEGAnalytics sharedAnalytics] track:@"Chapter Reads" | |
properties:@{ @"user": userId, @"type": @"read chapter" }]; | |
} | |
RCT_EXPORT_METHOD(readCommentary: (NSString*)userId) { | |
// [SEGAnalytics debug:YES]; | |
[[SEGAnalytics sharedAnalytics] track:@"Commentary Reads" | |
properties:@{ @"user": userId, @"type": @"read commentary" }]; | |
} | |
RCT_EXPORT_METHOD(giveFeedback: (NSString*)userId) { | |
// [SEGAnalytics debug:YES]; | |
[[SEGAnalytics sharedAnalytics] track:@"Feedbacks" | |
properties:@{ @"user": userId, @"type": @"give feedback" }]; | |
} | |
@end |
This is great!
I don't understand complete implementation though. Do you just add this and Segment's iOS lib, and the methods are exposed to JS via the bridge?
I've found analytics to be a huge pain point in React Native, so any help getting it running is welcome.
Will this also work in Android?
@Lucaska Were you able to figure out proper implementation with this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! Been looking for a good RN analytics solution for a while 🙌