Skip to content

Commit 5a0a1f4

Browse files
authored
feat: forward screen custom attributes (#31)
* feat: forward screen custom attributes
1 parent e1e552a commit 5a0a1f4

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

mParticle-Google-Analytics-Firebase/MPKitFirebaseAnalytics.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ - (MPKitExecStatus *)logScreen:(MPEvent *)event {
162162
return [self execStatus:MPKitReturnCodeFail];
163163
}
164164

165-
NSString *standardizedFirebaseEventName = [self standardizeNameOrKey:event.name forEvent:YES];
166-
[FIRAnalytics logEventWithName:kFIREventScreenView parameters:@{kFIRParameterScreenName: standardizedFirebaseEventName}];
165+
NSMutableDictionary *screenParameters = [self getParametersForScreen:event];
166+
[FIRAnalytics logEventWithName:kFIREventScreenView parameters:screenParameters];
167167

168168
return [self execStatus:MPKitReturnCodeSuccess];
169169
}
@@ -235,7 +235,7 @@ - (id)standardizeValue:(id)value forEvent:(BOOL)forEvent {
235235
return standardizedValue;
236236
}
237237

238-
- (NSDictionary<NSString *, id> *)standardizeValues:(NSDictionary<NSString *, id> *)values forEvent:(BOOL)forEvent {
238+
- (NSMutableDictionary<NSString *, id> *)standardizeValues:(NSDictionary<NSString *, id> *)values forEvent:(BOOL)forEvent {
239239
NSMutableDictionary<NSString *, id> *standardizedValue = [[NSMutableDictionary alloc] init];
240240

241241
for (NSString *key in values.allKeys) {
@@ -246,6 +246,13 @@ - (id)standardizeValue:(id)value forEvent:(BOOL)forEvent {
246246
return standardizedValue;
247247
}
248248

249+
- (NSMutableDictionary<NSString *, id> *)getParametersForScreen:(MPEvent *)screenEvent {
250+
NSMutableDictionary *standardizedScreenParameters = [self standardizeValues:screenEvent.customAttributes forEvent:YES];
251+
NSString *standardizedFirebaseEventName = [self standardizeNameOrKey:screenEvent.name forEvent:YES];
252+
standardizedScreenParameters[kFIRParameterScreenName] = standardizedFirebaseEventName;
253+
return standardizedScreenParameters;
254+
}
255+
249256
- (MPKitExecStatus *)onLoginComplete:(FilteredMParticleUser *)user request:(FilteredMPIdentityApiRequest *)request {
250257
if (forwardRequestsServerSide) {
251258
return [self execStatus:MPKitReturnCodeUnavailable];

mParticle-Google-Analytics-FirebaseTests/MPKitFirebaseAnalyticsTests.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ @interface MPKitFirebaseAnalytics()
1111
- (NSString *)standardizeNameOrKey:(NSString *)nameOrKey forEvent:(BOOL)forEvent;
1212
- (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent parameters:(NSDictionary<NSString *, id> *)parameters;
1313
- (NSDictionary<NSString *, id> *)getParameterForCommerceEvent:(MPCommerceEvent *)commerceEvent;
14+
- (NSMutableDictionary<NSString *, id> *)getParametersForScreen:(MPEvent *)screenEvent;
1415
@end
1516

1617
@interface mParticle_Firebase_AnalyticsTests : XCTestCase
@@ -148,4 +149,28 @@ - (void)testCommerceEventCheckoutOptions {
148149
XCTAssertEqualObjects(kFIREventAddShippingInfo, eventName);
149150
}
150151

152+
- (void)testScreenNameAttributes {
153+
MPKitFirebaseAnalytics *exampleKit = [[MPKitFirebaseAnalytics alloc] init];
154+
[exampleKit didFinishLaunchingWithConfiguration:@{}];
155+
156+
MPEvent *event = [[MPEvent alloc] initWithName:@"testScreenName" type:MPEventTypeOther];
157+
event.customAttributes = @{@"testScreenAttribute":@"value"};
158+
MPKitExecStatus *execStatus = [exampleKit logScreen:event];
159+
160+
XCTAssertTrue(execStatus.success);
161+
162+
NSMutableDictionary<NSString *, id> *screenParameters = [exampleKit getParametersForScreen:event];
163+
164+
// Even though we only pass one custom attribute, the parameters should include the standardized screen name, so the total expected count is two
165+
XCTAssertEqual(screenParameters.count, 2);
166+
167+
NSString *standardizedScreenName = [exampleKit standardizeNameOrKey:event.name forEvent:YES];
168+
NSString *screenNameParameter = screenParameters[kFIRParameterScreenName];
169+
170+
// Test screen name parameter is not Nil and exists in the screen parameters dictionary
171+
XCTAssertNotNil(screenNameParameter);
172+
// Test screen name parameter value is correct
173+
XCTAssertEqualObjects(screenNameParameter, standardizedScreenName);
174+
}
175+
151176
@end

0 commit comments

Comments
 (0)