ASFireBaseEventManager.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // ASFireBaseEventManager.m
  3. // westkissMob
  4. //
  5. // Created by iOS on 2024/2/22.
  6. //
  7. #import "ASFireBaseEventManager.h"
  8. #import <FirebaseAnalytics/FirebaseAnalytics.h>
  9. @interface ASFireBaseEventManager()
  10. @end
  11. @implementation ASFireBaseEventManager
  12. static ASFireBaseEventManager *singleClass = nil;
  13. + (instancetype)shareInstance{
  14. static dispatch_once_t onceToken;
  15. dispatch_once(&onceToken, ^{
  16. singleClass = [[ASFireBaseEventManager alloc]init];
  17. });
  18. return singleClass;
  19. }
  20. - (void)addcartEventWithCur:(NSString *)cur value:(double)value items:(NSArray<NSDictionary<NSString *, NSString*> *>*)items {
  21. NSMutableArray *arr = [NSMutableArray array];
  22. for (NSDictionary<NSString *, NSString*> *dic in items) {
  23. NSMutableDictionary<NSString *, NSString*> *tempDic = [NSMutableDictionary dictionary];
  24. tempDic[kFIRParameterItemID] = dic[@"item_id"];
  25. tempDic[kFIRParameterItemName] = dic[@"item_name"];
  26. [arr addObject:tempDic];
  27. }
  28. if (cur.isEmpty || arr.count == 0 || value == 0) {
  29. return;
  30. }
  31. [FIRAnalytics logEventWithName:kFIREventAddToCart parameters:@{
  32. kFIRParameterCurrency:cur,
  33. kFIRParameterValue:[NSNumber numberWithDouble:value],
  34. kFIRParameterItems:arr,
  35. }];
  36. }
  37. - (void)purchaseEventWithCur:(NSString *)cur value:(double)value transaction_id:(NSString *)transaction_id item:(CartTotalsM *)carListM {
  38. NSString *affiliate = @"APP Store";
  39. #if (DEBUG)
  40. affiliate = @"Debug Store";
  41. #endif
  42. NSMutableArray *arr = [NSMutableArray array];
  43. for (CartTotalsItemsM *cartItem in carListM.items) {
  44. NSMutableDictionary<NSString *, NSString*> *tempDic = [NSMutableDictionary dictionary];
  45. tempDic[kFIRParameterItemID] = cartItem.product_id;
  46. tempDic[kFIRParameterItemName] = cartItem.name;
  47. [arr addObject:tempDic];
  48. }
  49. NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
  50. tempDic[kFIRParameterTransactionID] = transaction_id;//@"T12345";
  51. tempDic[kFIRParameterAffiliation] = affiliate;
  52. tempDic[kFIRParameterCurrency] = cur;
  53. double totalPay = carListM.grand_total.doubleValue;
  54. tempDic[kFIRParameterValue] = [NSNumber numberWithDouble:totalPay];
  55. double taxPay = carListM.tax_amount.doubleValue;
  56. tempDic[kFIRParameterTax] = [NSNumber numberWithDouble:taxPay];
  57. double shipPay = carListM.shipping_amount.doubleValue;
  58. tempDic[kFIRParameterShipping] = [NSNumber numberWithDouble:shipPay];
  59. tempDic[kFIRParameterCoupon] = carListM.coupon_code;
  60. tempDic[kFIRParameterItems] = arr;
  61. if (cur.isEmpty || arr.count == 0) {
  62. return;
  63. }
  64. [FIRAnalytics logEventWithName:kFIREventPurchase parameters:tempDic];
  65. }
  66. - (void)checkOutEventWithCur:(NSString *)cur value:(double)value items:(NSArray<NSDictionary<NSString *, NSString*> *>*)items {
  67. NSMutableArray *arr = [NSMutableArray array];
  68. for (NSDictionary<NSString *, NSString*> *dic in items) {
  69. NSMutableDictionary<NSString *, NSString*> *tempDic = [NSMutableDictionary dictionary];
  70. tempDic[kFIRParameterItemID] = dic[@"item_id"];
  71. tempDic[kFIRParameterItemName] = dic[@"item_name"];
  72. [arr addObject:tempDic];
  73. }
  74. if (cur.isEmpty || items.count == 0 || value == 0) {
  75. return;
  76. }
  77. [FIRAnalytics logEventWithName:kFIREventBeginCheckout parameters:@{
  78. kFIRParameterCurrency:cur,
  79. kFIRParameterValue:[NSNumber numberWithDouble:value],
  80. kFIRParameterItems:arr,
  81. }];
  82. }
  83. - (void)viewItemEventWithCur:(NSString *)cur value:(double)value items:(NSArray<NSDictionary<NSString *, NSString*> *>*)items {
  84. NSMutableArray *arr = [NSMutableArray array];
  85. for (NSDictionary<NSString *, NSString*> *dic in items) {
  86. NSMutableDictionary<NSString *, NSString*> *tempDic = [NSMutableDictionary dictionary];
  87. tempDic[kFIRParameterItemID] = dic[@"item_id"];
  88. tempDic[kFIRParameterItemName] = dic[@"item_name"];
  89. [arr addObject:tempDic];
  90. }
  91. if (cur.isEmpty || arr.count == 0 || value == 0) {
  92. return;
  93. }
  94. [FIRAnalytics logEventWithName:kFIREventViewItem parameters:@{
  95. kFIRParameterCurrency:cur,
  96. kFIRParameterValue:[NSNumber numberWithDouble:value],
  97. kFIRParameterItems:arr,
  98. }];
  99. }
  100. @end