RCNPersonalization.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2019 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 "FirebaseRemoteConfig/Sources/RCNPersonalization.h"
  17. #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
  18. #import "FirebaseRemoteConfig/Sources/RCNConfigValue_Internal.h"
  19. @implementation RCNPersonalization
  20. - (instancetype)initWithAnalytics:(id<FIRAnalyticsInterop> _Nullable)analytics {
  21. self = [super init];
  22. if (self) {
  23. self->_analytics = analytics;
  24. self->_loggedChoiceIds = [[NSMutableDictionary alloc] init];
  25. }
  26. return self;
  27. }
  28. - (void)logArmActive:(NSString *)rcParameter config:(NSDictionary *)config {
  29. NSDictionary *ids = config[RCNFetchResponseKeyPersonalizationMetadata];
  30. NSDictionary<NSString *, FIRRemoteConfigValue *> *values = config[RCNFetchResponseKeyEntries];
  31. if (ids.count < 1 || values.count < 1 || !values[rcParameter]) {
  32. return;
  33. }
  34. NSDictionary *metadata = ids[rcParameter];
  35. if (!metadata) {
  36. return;
  37. }
  38. NSString *choiceId = metadata[kChoiceId];
  39. if (choiceId == nil) {
  40. return;
  41. }
  42. // Listeners like logArmActive() are dispatched to a serial queue, so loggedChoiceIds should
  43. // contain any previously logged RC parameter / choice ID pairs.
  44. if (self->_loggedChoiceIds[rcParameter] == choiceId) {
  45. return;
  46. }
  47. self->_loggedChoiceIds[rcParameter] = choiceId;
  48. [self->_analytics logEventWithOrigin:kAnalyticsOriginPersonalization
  49. name:kExternalEvent
  50. parameters:@{
  51. kExternalRcParameterParam : rcParameter,
  52. kExternalArmValueParam : values[rcParameter].stringValue,
  53. kExternalPersonalizationIdParam : metadata[kPersonalizationId],
  54. kExternalArmIndexParam : metadata[kArmIndex],
  55. kExternalGroupParam : metadata[kGroup]
  56. }];
  57. [self->_analytics logEventWithOrigin:kAnalyticsOriginPersonalization
  58. name:kInternalEvent
  59. parameters:@{kInternalChoiceIdParam : choiceId}];
  60. }
  61. @end