FIRMessagingAnalyticsTest.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import <FirebaseAnalyticsInterop/FIRInteropEventNames.h>
  19. #import <FirebaseAnalyticsInterop/FIRInteropParameterNames.h>
  20. #import "Firebase/Messaging/FIRMessagingAnalytics.h"
  21. // Analytics tracking is iOS only feature.
  22. #if TARGET_OS_IOS
  23. static NSString *const kFIRParameterLabel = @"label";
  24. static NSString *const kReengagementSource = @"Firebase";
  25. static NSString *const kReengagementMedium = @"notification";
  26. static NSString *const kFIREventOriginFCM = @"fcm";
  27. static const NSTimeInterval kAsyncTestTimout = 0.5;
  28. typedef void (^FakeAnalyticsLogEventHandler)(NSString *origin,
  29. NSString *name,
  30. NSDictionary *parameters);
  31. typedef void (^FakeAnalyticsUserPropertyHandler)(NSString *origin, NSString *name, id value);
  32. @interface FakeAnalytics : NSObject <FIRAnalyticsInterop>
  33. - (instancetype)initWithEventHandler:(FakeAnalyticsLogEventHandler)eventHandler;
  34. - (instancetype)initWithUserPropertyHandler:(FakeAnalyticsUserPropertyHandler)userPropertyHandler;
  35. @end
  36. @implementation FakeAnalytics
  37. static FakeAnalyticsLogEventHandler _eventHandler;
  38. static FakeAnalyticsLogEventHandler _userPropertyHandler;
  39. - (instancetype)initWithEventHandler:(FakeAnalyticsLogEventHandler)eventHandler {
  40. self = [super init];
  41. if (self) {
  42. _eventHandler = eventHandler;
  43. }
  44. return self;
  45. }
  46. - (instancetype)initWithUserPropertyHandler:(FakeAnalyticsUserPropertyHandler)userPropertyHandler {
  47. self = [super init];
  48. if (self) {
  49. _userPropertyHandler = userPropertyHandler;
  50. }
  51. return self;
  52. }
  53. - (void)logEventWithOrigin:(nonnull NSString *)origin
  54. name:(nonnull NSString *)name
  55. parameters:(nullable NSDictionary<NSString *, id> *)parameters {
  56. if (_eventHandler) {
  57. _eventHandler(origin, name, parameters);
  58. }
  59. }
  60. - (void)setUserPropertyWithOrigin:(nonnull NSString *)origin
  61. name:(nonnull NSString *)name
  62. value:(nonnull id)value {
  63. if (_userPropertyHandler) {
  64. _userPropertyHandler(origin, name, value);
  65. }
  66. }
  67. // Stubs
  68. - (void)clearConditionalUserProperty:(nonnull NSString *)userPropertyName
  69. clearEventName:(nonnull NSString *)clearEventName
  70. clearEventParameters:(nonnull NSDictionary *)clearEventParameters {
  71. }
  72. - (NSInteger)maxUserProperties:(nonnull NSString *)origin {
  73. return -1;
  74. }
  75. - (void)checkLastNotificationForOrigin:(nonnull NSString *)origin
  76. queue:(nonnull dispatch_queue_t)queue
  77. callback:(nonnull void (^)(NSString *_Nullable))
  78. currentLastNotificationProperty {
  79. }
  80. - (void)registerAnalyticsListener:(nonnull id<FIRAnalyticsInteropListener>)listener
  81. withOrigin:(nonnull NSString *)origin {
  82. }
  83. - (void)unregisterAnalyticsListenerWithOrigin:(nonnull NSString *)origin {
  84. }
  85. - (void)clearConditionalUserProperty:(nonnull NSString *)userPropertyName
  86. forOrigin:(nonnull NSString *)origin
  87. clearEventName:(nonnull NSString *)clearEventName
  88. clearEventParameters:
  89. (nonnull NSDictionary<NSString *, NSString *> *)clearEventParameters {
  90. }
  91. - (nonnull NSArray<NSDictionary<NSString *, NSString *> *> *)
  92. conditionalUserProperties:(nonnull NSString *)origin
  93. propertyNamePrefix:(nonnull NSString *)propertyNamePrefix {
  94. return nil;
  95. }
  96. - (void)setConditionalUserProperty:(nonnull NSDictionary<NSString *, id> *)conditionalUserProperty {
  97. }
  98. - (void)getUserPropertiesWithCallback:(nonnull FIRAInteropUserPropertiesCallback)callback {
  99. }
  100. @end
  101. @interface FIRMessagingAnalytics (ExposedForTest)
  102. + (BOOL)canLogNotification:(NSDictionary *)notification;
  103. + (NSMutableDictionary *)paramsForEvent:(NSString *)event
  104. withNotification:(NSDictionary *)notification;
  105. + (void)logAnalyticsEventWithOrigin:(NSString *)origin
  106. name:(NSString *)name
  107. parameters:(NSDictionary *)params
  108. toAnalytics:(id<FIRAnalyticsInterop> _Nullable)analytics;
  109. + (void)logUserPropertyForConversionTracking:(NSDictionary *)notification
  110. toAnalytics:(id<FIRAnalyticsInterop> _Nullable)analytics;
  111. + (void)logOpenNotification:(NSDictionary *)notification
  112. toAnalytics:(id<FIRAnalyticsInterop> _Nullable)analytics;
  113. + (void)logForegroundNotification:(NSDictionary *)notification
  114. toAnalytics:(id<FIRAnalyticsInterop> _Nullable)analytics;
  115. + (void)logEvent:(NSString *)event
  116. withNotification:(NSDictionary *)notification
  117. toAnalytics:(id<FIRAnalyticsInterop> _Nullable)analytics;
  118. ;
  119. @end
  120. @interface FIRMessagingAnalyticsTest : XCTestCase
  121. @property(nonatomic, readwrite, strong) id logClassMock;
  122. @end
  123. @implementation FIRMessagingAnalyticsTest
  124. - (void)setUp {
  125. [super setUp];
  126. self.logClassMock = OCMClassMock([FIRMessagingAnalytics class]);
  127. }
  128. - (void)tearDown {
  129. _eventHandler = nil;
  130. _userPropertyHandler = nil;
  131. [self.logClassMock stopMocking];
  132. [super tearDown];
  133. }
  134. - (void)testCanLogNotification {
  135. NSDictionary *notification = @{};
  136. XCTAssertFalse([FIRMessagingAnalytics canLogNotification:notification]);
  137. notification = @{@"google.c.a.e" : @1};
  138. XCTAssertFalse([FIRMessagingAnalytics canLogNotification:notification]);
  139. notification = @{@"aps" : @{@"alert" : @"to check the reporting format"}};
  140. XCTAssertFalse([FIRMessagingAnalytics canLogNotification:notification]);
  141. notification = @{@"aps" : @{@"alert" : @"to check the reporting format"}, @"google.c.a.e" : @"0"};
  142. XCTAssertFalse([FIRMessagingAnalytics canLogNotification:notification]);
  143. notification = @{
  144. @"aps" : @{@"alert" : @"to check the reporting format"},
  145. @"gcm.message_id" : @"0:1522880049414338%944841cd944841cd",
  146. @"gcm.n.e" : @"1",
  147. @"google.c.a.c_id" : @"575315420755741863",
  148. @"google.c.a.e" : @"1",
  149. @"google.c.a.ts" : @"1522880044",
  150. @"google.c.a.udt" : @"0"
  151. };
  152. XCTAssertTrue([FIRMessagingAnalytics canLogNotification:notification]);
  153. notification = @{
  154. @"aps" : @{@"content-available" : @"1"},
  155. @"gcm.message_id" : @"0:1522880049414338%944841cd944841cd",
  156. @"google.c.a.e" : @"1",
  157. @"google.c.a.ts" : @"1522880044",
  158. @"google.c.a.udt" : @"0"
  159. };
  160. XCTAssertTrue([FIRMessagingAnalytics canLogNotification:notification]);
  161. }
  162. - (void)testEmptyNotification {
  163. NSMutableDictionary *params = [FIRMessagingAnalytics paramsForEvent:@"" withNotification:@{}];
  164. XCTAssertNil(params);
  165. }
  166. - (void)testNoParamsIfAnalyticsIsNotEnabled {
  167. NSDictionary *notification = @{
  168. @"aps" : @{@"alert" : @"to check the reporting format"},
  169. @"gcm.message_id" : @"0:1522880049414338%944841cd944841cd",
  170. @"gcm.n.e" : @"1",
  171. @"google.c.a.c_id" : @"575315420755741863",
  172. @"google.c.a.ts" : @"1522880044",
  173. @"google.c.a.udt" : @"0"
  174. };
  175. NSMutableDictionary *params = [FIRMessagingAnalytics paramsForEvent:@""
  176. withNotification:notification];
  177. XCTAssertNil(params);
  178. }
  179. - (void)testNoParamsIfEmpty {
  180. NSDictionary *notification = @{
  181. @"google.c.a.e" : @"1",
  182. };
  183. NSMutableDictionary *params = [FIRMessagingAnalytics paramsForEvent:@""
  184. withNotification:notification];
  185. XCTAssertNotNil(params);
  186. XCTestExpectation *expectation = [self expectationWithDescription:@"completion"];
  187. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  188. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  189. XCTAssertEqualObjects(origin, kFIREventOriginFCM);
  190. XCTAssertEqualObjects(name, @"_cmp");
  191. XCTAssertEqual([parameters count], 0);
  192. [expectation fulfill];
  193. }];
  194. [FIRMessagingAnalytics logEvent:kFIRIEventFirebaseCampaign
  195. withNotification:notification
  196. toAnalytics:analytics];
  197. [self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
  198. }
  199. - (void)testParamForEventAndNotification {
  200. NSDictionary *notification = @{
  201. @"aps" : @{@"alert" : @"to check the reporting format"},
  202. @"gcm.message_id" : @"0:1522880049414338%944841cd944841cd",
  203. @"gcm.n.e" : @"1",
  204. @"google.c.a.c_l" : @"Hello World",
  205. @"google.c.a.c_id" : @"575315420755741863",
  206. @"google.c.a.e" : @"1",
  207. @"google.c.a.ts" : @"1522880044",
  208. @"google.c.a.udt" : @"0",
  209. @"google.c.a.m_l" : @"developer's customized label",
  210. @"from" : @"/topics/news",
  211. };
  212. NSMutableDictionary *params = [FIRMessagingAnalytics paramsForEvent:kFIRIEventNotificationOpen
  213. withNotification:notification];
  214. XCTAssertNotNil(params);
  215. XCTAssertEqualObjects(params[kFIRIParameterMessageIdentifier], @"575315420755741863");
  216. XCTAssertEqualObjects(params[kFIRIParameterMessageName], @"Hello World");
  217. XCTAssertEqualObjects(params[kFIRParameterLabel], @"developer's customized label");
  218. XCTAssertEqualObjects(params[kFIRIParameterTopic], @"/topics/news");
  219. XCTAssertEqualObjects([params[kFIRIParameterMessageTime] stringValue], @"1522880044");
  220. XCTAssertEqualObjects(params[kFIRIParameterMessageDeviceTime], @"0");
  221. }
  222. - (void)testInvalidDataInParamsForLogging {
  223. NSString *composerIdentifier = @"Hellow World";
  224. NSDictionary *notification = @{
  225. @"google.c.a.e" : @(YES),
  226. @"google.c.a.c_l" : composerIdentifier,
  227. @"google.c.a.c_id" : @"575315420755741863",
  228. @"google.c.a.m_l" : @"developer's customized label",
  229. @"google.c.a.ts" : @"1522880044",
  230. @"from" : @"/topics/news",
  231. @"google.c.a.udt" : @"0",
  232. };
  233. NSMutableDictionary *params = [FIRMessagingAnalytics paramsForEvent:kFIRIEventNotificationOpen
  234. withNotification:notification];
  235. XCTAssertNil(params);
  236. notification = @{
  237. @"google.c.a.e" : @"1",
  238. @"google.c.a.c_l" : [composerIdentifier dataUsingEncoding:NSUTF8StringEncoding],
  239. @"google.c.a.c_id" : @"575315420755741863",
  240. @"google.c.a.m_l" : @"developer's customized label",
  241. @"google.c.a.ts" : @"1522880044",
  242. @"from" : @"/topics/news",
  243. @"google.c.a.udt" : @"0",
  244. };
  245. params = [FIRMessagingAnalytics paramsForEvent:kFIRIEventNotificationOpen
  246. withNotification:notification];
  247. XCTAssertNil(params[kFIRIParameterMessageName]);
  248. XCTAssertEqualObjects(params[kFIRIParameterMessageIdentifier], @"575315420755741863");
  249. XCTAssertEqualObjects(params[kFIRIParameterTopic], @"/topics/news");
  250. notification = @{
  251. @"google.c.a.e" : @"1",
  252. @"google.c.a.c_l" : composerIdentifier,
  253. @"google.c.a.c_id" : @(575315420755741863),
  254. @"google.c.a.m_l" : @"developer's customized label",
  255. @"google.c.a.ts" : @"1522880044",
  256. @"from" : @"/topics/news",
  257. @"google.c.a.udt" : @"0",
  258. };
  259. params = [FIRMessagingAnalytics paramsForEvent:kFIRIEventNotificationOpen
  260. withNotification:notification];
  261. XCTAssertEqualObjects(params[kFIRIParameterMessageName], composerIdentifier);
  262. XCTAssertNil(params[kFIRIParameterMessageIdentifier]);
  263. XCTAssertEqualObjects(params[kFIRIParameterTopic], @"/topics/news");
  264. notification = @{
  265. @"google.c.a.e" : @"1",
  266. @"google.c.a.c_l" : composerIdentifier,
  267. @"google.c.a.c_id" : @"575315420755741863",
  268. @"google.c.a.m_l" : @"developer's customized label",
  269. @"google.c.a.ts" : @"0",
  270. @"from" : @"/topics/news",
  271. @"google.c.a.udt" : @"12345678",
  272. };
  273. params = [FIRMessagingAnalytics paramsForEvent:kFIRIEventNotificationOpen
  274. withNotification:notification];
  275. XCTAssertEqualObjects(params[kFIRIParameterMessageName], composerIdentifier);
  276. XCTAssertEqualObjects(params[kFIRIParameterMessageIdentifier], @"575315420755741863");
  277. XCTAssertEqualObjects(params[kFIRParameterLabel], @"developer's customized label");
  278. XCTAssertEqualObjects(params[kFIRIParameterTopic], @"/topics/news");
  279. XCTAssertNil(params[kFIRIParameterMessageTime]);
  280. XCTAssertEqualObjects(params[kFIRIParameterMessageDeviceTime], @"12345678");
  281. notification = @{
  282. @"google.c.a.e" : @"1",
  283. @"google.c.a.c_l" : composerIdentifier,
  284. @"google.c.a.c_id" : @"575315420755741863",
  285. @"google.c.a.m_l" : @"developer's customized label",
  286. @"google.c.a.ts" : @(0),
  287. @"from" : @"/topics/news",
  288. @"google.c.a.udt" : @"12345678",
  289. };
  290. params = [FIRMessagingAnalytics paramsForEvent:kFIRIEventNotificationOpen
  291. withNotification:notification];
  292. XCTAssertEqualObjects(params[kFIRIParameterMessageName], composerIdentifier);
  293. XCTAssertNil(params[kFIRIParameterMessageTime]);
  294. XCTAssertEqualObjects(params[kFIRIParameterMessageDeviceTime], @"12345678");
  295. }
  296. - (void)testConversionTracking {
  297. // Notification contains "google.c.a.tc" key.
  298. NSDictionary *notification = @{
  299. @"aps" : @{@"alert" : @"to check the reporting format"},
  300. @"gcm.message_id" : @"0:1522880049414338%944841cd944841cd",
  301. @"gcm.n.e" : @"1",
  302. @"google.c.a.c_l" : @"Hello World",
  303. @"google.c.a.c_id" : @"575315420755741863",
  304. @"google.c.a.e" : @"1",
  305. @"google.c.a.ts" : @"1522880044",
  306. @"google.c.a.udt" : @"0",
  307. @"google.c.a.m_l" : @"developer's customized label",
  308. @"google.c.a.tc" : @"1",
  309. @"from" : @"/topics/news",
  310. };
  311. NSDictionary *params = @{
  312. kFIRIParameterSource : kReengagementSource,
  313. kFIRIParameterMedium : kReengagementMedium,
  314. kFIRIParameterCampaign : @"575315420755741863"
  315. };
  316. __block XCTestExpectation *expectation = [self expectationWithDescription:@"completion"];
  317. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  318. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  319. XCTAssertEqualObjects(origin, kFIREventOriginFCM);
  320. XCTAssertEqualObjects(name, @"_cmp");
  321. XCTAssertEqualObjects(parameters, params);
  322. [expectation fulfill];
  323. expectation = nil;
  324. }];
  325. [FIRMessagingAnalytics logUserPropertyForConversionTracking:notification toAnalytics:analytics];
  326. [self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
  327. }
  328. - (void)testConversionTrackingUserProperty {
  329. // Notification contains "google.c.a.tc" key.
  330. NSDictionary *notification = @{
  331. @"aps" : @{@"alert" : @"to check the reporting format"},
  332. @"gcm.message_id" : @"0:1522880049414338%944841cd944841cd",
  333. @"gcm.n.e" : @"1",
  334. @"google.c.a.c_l" : @"Hello World",
  335. @"google.c.a.c_id" : @"575315420755741863",
  336. @"google.c.a.e" : @"1",
  337. @"google.c.a.ts" : @"1522880044",
  338. @"google.c.a.udt" : @"0",
  339. @"google.c.a.m_l" : @"developer's customized label",
  340. @"google.c.a.tc" : @"1",
  341. @"from" : @"/topics/news",
  342. };
  343. XCTestExpectation *expectation = [self expectationWithDescription:@"completion"];
  344. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  345. initWithUserPropertyHandler:^(NSString *origin, NSString *name, id value) {
  346. XCTAssertEqualObjects(origin, kFIREventOriginFCM);
  347. XCTAssertEqualObjects(name, @"_ln");
  348. XCTAssertEqualObjects(value, @"575315420755741863");
  349. [expectation fulfill];
  350. }];
  351. [FIRMessagingAnalytics logUserPropertyForConversionTracking:notification toAnalytics:analytics];
  352. [self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
  353. }
  354. - (void)testNoConversionTracking {
  355. // Notification contains "google.c.a.tc" key.
  356. NSDictionary *notification = @{
  357. @"aps" : @{@"alert" : @"to check the reporting format"},
  358. @"gcm.message_id" : @"0:1522880049414338%944841cd944841cd",
  359. @"gcm.n.e" : @"1",
  360. @"google.c.a.c_l" : @"Hello World",
  361. @"google.c.a.c_id" : @"575315420755741863",
  362. @"google.c.a.e" : @"1",
  363. @"google.c.a.ts" : @"1522880044",
  364. @"google.c.a.udt" : @"0",
  365. @"google.c.a.m_l" : @"developer's customized label",
  366. @"from" : @"/topics/news",
  367. };
  368. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  369. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  370. XCTAssertTrue(NO);
  371. }];
  372. [FIRMessagingAnalytics logUserPropertyForConversionTracking:notification toAnalytics:analytics];
  373. }
  374. - (void)testLogMessage {
  375. NSDictionary *notification = @{
  376. @"google.c.a.e" : @"1",
  377. };
  378. [FIRMessagingAnalytics logMessage:notification toAnalytics:nil];
  379. OCMVerify([self.logClassMock logEvent:OCMOCK_ANY withNotification:notification toAnalytics:nil]);
  380. }
  381. - (void)testLogOpenNotification {
  382. NSDictionary *notification = @{
  383. @"google.c.a.e" : @"1",
  384. };
  385. [FIRMessagingAnalytics logOpenNotification:notification toAnalytics:nil];
  386. OCMVerify([self.logClassMock logUserPropertyForConversionTracking:notification toAnalytics:nil]);
  387. OCMVerify([self.logClassMock logEvent:kFIRIEventNotificationOpen
  388. withNotification:notification
  389. toAnalytics:nil]);
  390. }
  391. @end
  392. #endif