FIRMessagingAnalyticsTest.m 20 KB

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