Skip to content

Commit bf904af

Browse files
committed
Merge pull request #319 from segmentio/fix/localytics
Custom Dimensions for Localytics
2 parents dfdcb0e + fe7e1fa commit bf904af

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

Analytics/Integrations/Localytics/SEGLocalyticsIntegration.m

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
@implementation SEGLocalyticsIntegration
1010

11-
+ (BOOL) validateEmail:(NSString *)candidate {
11+
+ (BOOL)validateEmail:(NSString *)candidate {
1212
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
13-
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
13+
NSPredicate *emailTest =
14+
[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
1415

1516
return [emailTest evaluateWithObject:candidate];
1617
}
@@ -30,33 +31,45 @@ - (id)init {
3031
return self;
3132
}
3233

33-
- (void)start
34-
{
34+
- (void)start {
3535
NSString *appKey = [self.settings objectForKey:@"appKey"];
3636

3737
[Localytics integrate:appKey];
3838

39-
NSNumber *sessionTimeoutInterval = [self.settings objectForKey:@"sessionTimeoutInterval"];
40-
if (sessionTimeoutInterval != nil && [sessionTimeoutInterval floatValue] > 0) {
39+
NSNumber *sessionTimeoutInterval =
40+
[self.settings objectForKey:@"sessionTimeoutInterval"];
41+
if (sessionTimeoutInterval != nil &&
42+
[sessionTimeoutInterval floatValue] > 0) {
4143
[Localytics setSessionTimeoutInterval:[sessionTimeoutInterval floatValue]];
4244
}
4345

4446
SEGLog(@"LocalyticsIntegration initialized.");
4547
}
4648

49+
- (void)setCustomDimensions:(NSDictionary *)dictionary {
50+
NSDictionary *customDimensions = self.settings[@"dimensions"];
51+
52+
for (NSString *key in dictionary) {
53+
if ([customDimensions objectForKey:key] != nil) {
54+
NSString *dimension = [customDimensions objectForKey:key];
55+
[Localytics setValue:[dictionary objectForKey:key]
56+
forCustomDimension:[dimension integerValue]];
57+
}
58+
}
59+
}
4760

4861
#pragma mark - Settings
4962

50-
- (void)validate
51-
{
63+
- (void)validate {
5264
BOOL hasAppKey = [self.settings objectForKey:@"appKey"] != nil;
5365
self.valid = hasAppKey;
5466
}
5567

5668
#pragma mark - Analytics API
5769

58-
- (void)identify:(NSString *)userId traits:(NSDictionary *)traits options:(NSDictionary *)options
59-
{
70+
- (void)identify:(NSString *)userId
71+
traits:(NSDictionary *)traits
72+
options:(NSDictionary *)options {
6073
if (userId) {
6174
[Localytics setCustomerId:userId];
6275
}
@@ -77,44 +90,56 @@ - (void)identify:(NSString *)userId traits:(NSDictionary *)traits options:(NSDic
7790
[Localytics setValue:name forIdentifier:@"customer_name"];
7891
}
7992

93+
[self setCustomDimensions:traits];
94+
8095
// Other traits. Iterate over all the traits and set them.
8196
for (NSString *key in traits) {
82-
NSString* traitValue = [NSString stringWithFormat:@"%@", [traits objectForKey:key]];
83-
[Localytics setValue:traitValue forIdentifier:key];
97+
NSString *traitValue =
98+
[NSString stringWithFormat:@"%@", [traits objectForKey:key]];
99+
[Localytics setValue:traitValue
100+
forProfileAttribute:key
101+
withScope:LLProfileScopeApplication];
84102
}
85103
}
86104

87-
- (void)track:(NSString *)event properties:(NSDictionary *)properties options:(NSDictionary *)options
88-
{
89-
// TODO add support for dimensions
105+
- (void)track:(NSString *)event
106+
properties:(NSDictionary *)properties
107+
options:(NSDictionary *)options {
90108
// TODO add support for value
91109

92110
// Backgrounded? Restart the session to add this event.
93-
BOOL isBackgrounded = [[UIApplication sharedApplication] applicationState] != UIApplicationStateActive;
111+
BOOL isBackgrounded = [[UIApplication sharedApplication] applicationState] !=
112+
UIApplicationStateActive;
94113
if (isBackgrounded) {
95114
[Localytics openSession];
96115
}
97116

98117
NSNumber *revenue = [SEGAnalyticsIntegration extractRevenue:properties];
99118
if (revenue) {
100-
[Localytics tagEvent:event attributes:properties customerValueIncrease:revenue];
119+
[Localytics tagEvent:event
120+
attributes:properties
121+
customerValueIncrease:revenue];
101122
} else {
102123
[Localytics tagEvent:event attributes:properties];
103124
}
104125

126+
[self setCustomDimensions:properties];
127+
105128
// Backgrounded? Close the session again after the event.
106129
if (isBackgrounded) {
107130
[Localytics closeSession];
108131
}
109132
}
110133

111-
- (void)screen:(NSString *)screenTitle properties:(NSDictionary *)properties options:(NSDictionary *)options
112-
{
134+
- (void)screen:(NSString *)screenTitle
135+
properties:(NSDictionary *)properties
136+
options:(NSDictionary *)options {
113137
// For enterprise only...
114138
[Localytics tagScreen:screenTitle];
115139
}
116140

117-
- (void)registerForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken options:(NSDictionary *)options {
141+
- (void)registerForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
142+
options:(NSDictionary *)options {
118143
[Localytics setPushToken:deviceToken];
119144
}
120145

@@ -124,33 +149,29 @@ - (void)flush {
124149

125150
#pragma mark - Callbacks for app state changes
126151

127-
- (void)applicationDidEnterBackground
128-
{
152+
- (void)applicationDidEnterBackground {
153+
[Localytics dismissCurrentInAppMessage];
129154
[Localytics closeSession];
130155
[Localytics upload];
131156
}
132157

133-
- (void)applicationWillEnterForeground
134-
{
158+
- (void)applicationWillEnterForeground {
135159
[Localytics openSession];
136160
[Localytics upload];
137161
}
138162

139-
- (void)applicationWillTerminate
140-
{
163+
- (void)applicationWillTerminate {
141164
[Localytics closeSession];
142165
[Localytics upload];
143166
}
144-
- (void)applicationWillResignActive
145-
{
167+
- (void)applicationWillResignActive {
168+
[Localytics dismissCurrentInAppMessage];
146169
[Localytics closeSession];
147170
[Localytics upload];
148171
}
149-
- (void)applicationDidBecomeActive
150-
{
172+
- (void)applicationDidBecomeActive {
151173
[Localytics openSession];
152174
[Localytics upload];
153175
}
154176

155-
156177
@end

0 commit comments

Comments
 (0)