Skip to content

Commit ff17695

Browse files
authored
[MBL-2962] Add ppo v3/v4 analytics and fix incorrect total count (#2751)
* Add new analytics * Fix total count * Add comment explaining total count
1 parent ee86ba5 commit ff17695

3 files changed

Lines changed: 53 additions & 16 deletions

File tree

Kickstarter-Framework/Sources/Kickstarter-Framework/Kickstarter-iOS/Features/PledgedProjectsOverview/PPOViewModel.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ final class PPOViewModel: ObservableObject, PPOViewModelInputs, PPOViewModelOutp
164164
let latestLoadedResults = self.paginator.$results
165165
.compactMap { results in
166166
results.hasLoaded ? results.values
167-
.ppoAnalyticsProperties(total: results.total, page: results.page) : nil
167+
.ppoAnalyticsProperties(page: results.page) : nil
168168
}
169169

170170
// Analytics: When view appears, the next time it loads, send a PPO dashboard open
@@ -255,8 +255,10 @@ final class PPOViewModel: ObservableObject, PPOViewModelInputs, PPOViewModelOutp
255255
properties: overallProperties
256256
)
257257
case .manageLivePledge:
258-
// TODO(MBL-2962): Add analytics event.
259-
break
258+
AppEnvironment.current.ksrAnalytics.trackPPOManageLivePledge(
259+
project: cardModel.projectAnalytics,
260+
properties: overallProperties
261+
)
260262
}
261263
}
262264

@@ -372,14 +374,16 @@ final class PPOViewModel: ObservableObject, PPOViewModelInputs, PPOViewModelOutp
372374
}
373375

374376
extension Sequence where Element == PPOProjectCardViewModel {
375-
func ppoAnalyticsProperties(total: Int?, page: Int?) -> KSRAnalytics.PledgedProjectOverviewProperties {
377+
func ppoAnalyticsProperties(page: Int?) -> KSRAnalytics.PledgedProjectOverviewProperties {
376378
var paymentFailedCount: Int = 0
377379
var cardAuthRequiredCount: Int = 0
378380
var surveyAvailableCount: Int = 0
379381
var addressLocksSoonCount: Int = 0
380382
var pledgeManagementCount: Int = 0
381383

382384
var fundedProjectCount: Int = 0
385+
var liveProjectCount: Int = 0
386+
var unsuccessfulPledgeCount: Int = 0
383387

384388
for viewModel in self {
385389
switch viewModel.card.tierType {
@@ -396,11 +400,9 @@ extension Sequence where Element == PPOProjectCardViewModel {
396400
case .surveySubmitted, .pledgeCollected, .addressConfirmed, .awaitingReward, .rewardReceived:
397401
fundedProjectCount += 1
398402
case .campaignLive, .campaignFunded, .campaignEnded:
399-
// TODO(MBL-2962): Add analytics for live projects.
400-
continue
403+
liveProjectCount += 1
401404
case .campaignFailed, .pledgeDropped, .pledgeCanceled:
402-
// TODO(MBL-2962): Add analytics for failed/canceled pledges.
403-
continue
405+
unsuccessfulPledgeCount += 1
404406
}
405407
}
406408

@@ -411,7 +413,8 @@ extension Sequence where Element == PPOProjectCardViewModel {
411413
paymentFailedCount: paymentFailedCount,
412414
cardAuthRequiredCount: cardAuthRequiredCount,
413415
fundedProjectCount: fundedProjectCount,
414-
total: total,
416+
liveProjectCount: liveProjectCount,
417+
unsuccessfulPledgeCount: unsuccessfulPledgeCount,
415418
page: page
416419
)
417420
}

Library/Sources/Library/Library/Paginator.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ public class Paginator<Envelope, Value: Equatable, Cursor: Equatable, SomeError:
231231
if allValues.count == 0 {
232232
results = .empty
233233
} else if let nextCursor, !newValues.isEmpty {
234+
// Warning: This total count may be incorrect. It should represent the total number of
235+
// cards that a user has, but because of the way the fetch works on the backend, that
236+
// number is currently unknown. The backend returns the number of cards in the fetch
237+
// itself instead.
234238
let total = self.totalFromEnvelope(envelope)
235239
results = .someLoaded(values: allValues, cursor: nextCursor, total: total, page: newPage)
236240
} else {

Library/Sources/Library/Library/Tracking/KSRAnalytics.swift

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public final class KSRAnalytics {
154154
case logInOrSignUp
155155
case logInSubmit
156156
case messageCreatorInitiate
157+
case nativeManagePledge
157158
case pledgeConfirm
158159
case pledgeInitiate
159160
case pledgeSubmit
@@ -193,6 +194,7 @@ public final class KSRAnalytics {
193194
case .fixPledgeInitiate: return "fix_pledge_initiate"
194195
case .forgotPassword: return "forgot_password"
195196
case .messageCreatorInitiate: return "message_creator_initiate"
197+
case .nativeManagePledge: return "native_manage_pledge"
196198
case .onboardingClose: return "close"
197199
case .onboardingNext: return "next"
198200
case .onboardingGetNotified: return "get_notified"
@@ -573,7 +575,8 @@ public final class KSRAnalytics {
573575
let paymentFailedCount: Int
574576
let cardAuthRequiredCount: Int
575577
let fundedProjectCount: Int
576-
let total: Int?
578+
let liveProjectCount: Int
579+
let unsuccessfulPledgeCount: Int
577580
let page: Int?
578581

579582
var alertCardCount: Int {
@@ -584,14 +587,24 @@ public final class KSRAnalytics {
584587
self.cardAuthRequiredCount
585588
}
586589

590+
// Calculate the total number of cards that have been fetched.
591+
// This ensures the total count is consistent with other counts.
592+
var total: Int {
593+
return self.alertCardCount +
594+
self.fundedProjectCount +
595+
self.liveProjectCount +
596+
self.unsuccessfulPledgeCount
597+
}
598+
587599
public init(
588600
addressLocksSoonCount: Int,
589601
surveyAvailableCount: Int,
590602
pledgeManagementCount: Int,
591603
paymentFailedCount: Int,
592604
cardAuthRequiredCount: Int,
593605
fundedProjectCount: Int,
594-
total: Int?,
606+
liveProjectCount: Int,
607+
unsuccessfulPledgeCount: Int,
595608
page: Int?
596609
) {
597610
self.addressLocksSoonCount = addressLocksSoonCount
@@ -600,7 +613,8 @@ public final class KSRAnalytics {
600613
self.paymentFailedCount = paymentFailedCount
601614
self.cardAuthRequiredCount = cardAuthRequiredCount
602615
self.fundedProjectCount = fundedProjectCount
603-
self.total = total
616+
self.liveProjectCount = liveProjectCount
617+
self.unsuccessfulPledgeCount = unsuccessfulPledgeCount
604618
self.page = page
605619
}
606620
}
@@ -802,6 +816,23 @@ public final class KSRAnalytics {
802816
)
803817
}
804818

819+
public func trackPPOManageLivePledge(
820+
project: any ProjectAnalyticsProperties,
821+
properties: PledgedProjectOverviewProperties
822+
) {
823+
let props = contextProperties(
824+
ctaContext: .nativeManagePledge,
825+
page: .projectAlerts
826+
)
827+
.withAllValuesFrom(projectProperties(from: project))
828+
.withAllValuesFrom(pledgedProjectOverviewProperties(from: properties))
829+
830+
self.track(
831+
event: SegmentEvent.ctaClicked.rawValue,
832+
properties: props
833+
)
834+
}
835+
805836
public func trackPPOViewProjectDetails(
806837
project: any ProjectAnalyticsProperties,
807838
properties: PledgedProjectOverviewProperties
@@ -1762,11 +1793,10 @@ private func pledgedProjectOverviewProperties(
17621793

17631794
result["notification_count_alert_card"] = properties.alertCardCount
17641795
result["notification_count_funded_project"] = properties.fundedProjectCount
1796+
result["notification_count_live_project"] = properties.liveProjectCount
1797+
result["notification_count_unsuccessful_pledge"] = properties.unsuccessfulPledgeCount
17651798

1766-
if let total = properties.total {
1767-
result["notification_count_total"] = total
1768-
}
1769-
1799+
result["notification_count_total"] = properties.total
17701800
if let page = properties.page {
17711801
result["context_page_number"] = page
17721802
}

0 commit comments

Comments
 (0)