-
Notifications
You must be signed in to change notification settings - Fork 46
/
RNSegmentIOAnalytics.m
81 lines (69 loc) · 2.42 KB
/
RNSegmentIOAnalytics.m
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
70
71
72
73
74
75
76
77
78
79
80
81
//
// RNSegmentIOAnalytics
//
// Created by Tal Kain <[email protected]>.
// Copyright (c) 2015 Fire Place Inc. All rights reserved.
//
#import "RNSegmentIOAnalytics.h"
#import "RCTConvert.h"
#import "Analytics.h"
#import <Foundation/Foundation.h>
@implementation RNSegmentIOAnalytics
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(setup:(NSString*)configKey :(NSUInteger)flushAt :(BOOL)shouldUseLocationServices)
{
SEGAnalyticsConfiguration *configuration = [SEGAnalyticsConfiguration configurationWithWriteKey:configKey];
configuration.flushAt = flushAt;
configuration.shouldUseLocationServices = shouldUseLocationServices;
[SEGAnalytics setupWithConfiguration:configuration];
}
/*
https://segment.com/docs/libraries/ios/#identify
*/
RCT_EXPORT_METHOD(identifyUser:(NSString*)userId traits:(NSDictionary *)traits) {
[[SEGAnalytics sharedAnalytics] identify:userId traits:[self convertToStringDictionary:traits]];
}
/*
https://segment.com/docs/libraries/ios/#track
*/
RCT_EXPORT_METHOD(track:(NSString*)trackText properties:(NSDictionary *)properties) {
[[SEGAnalytics sharedAnalytics] track:trackText
properties:[self convertToStringDictionary:properties]];
}
/*
https://segment.com/docs/libraries/ios/#screen
*/
RCT_EXPORT_METHOD(screen:(NSString*)screenName properties:(NSDictionary *)properties) {
[[SEGAnalytics sharedAnalytics] screen:screenName properties:[self convertToStringDictionary:properties]];
}
/*
https://segment.com/docs/libraries/ios/#flushing
*/
RCT_EXPORT_METHOD(flush) {
[[SEGAnalytics sharedAnalytics] flush];
}
/*
https://segment.com/docs/libraries/ios/#reset
*/
RCT_EXPORT_METHOD(reset) {
[[SEGAnalytics sharedAnalytics] reset];
}
/*
https://segment.com/docs/libraries/ios/#logging
*/
RCT_EXPORT_METHOD(debug: (BOOL)isEnabled) {
[SEGAnalytics debug:isEnabled];
}
-(NSMutableDictionary*) convertToStringDictionary: (NSDictionary *)properties {
/*
According to React Native's documentation:
For maps, it is the developer's responsibility to check the value types individually by manually calling RCTConvert helper methods.
*/
NSMutableDictionary *stringDictionary = [[NSMutableDictionary alloc] init];
for (NSString* key in [properties allKeys]) {
id value = [RCTConvert NSString:[properties objectForKey:key]];
[stringDictionary setObject:value forKey:[RCTConvert NSString:key]];
}
return stringDictionary;
}
@end