Skip to content

Instantly share code, notes, and snippets.

@tgoldenberg
Last active May 9, 2018 23:35
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgoldenberg/3e32e4988a08081d62ce to your computer and use it in GitHub Desktop.
Save tgoldenberg/3e32e4988a08081d62ce to your computer and use it in GitHub Desktop.
Segment Analytics for React Native
# 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
@spencerrwise
Copy link

Thanks for this! Been looking for a good RN analytics solution for a while 🙌

@lallmon
Copy link

lallmon commented May 31, 2016

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?

@ArronJLinton
Copy link

@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