Skip to content

Commit

Permalink
[MBL-1124] Add tracking for late pledges (#2021)
Browse files Browse the repository at this point in the history
* Update project properties to include late pledge info

* Track checkout events for late pledges

fix missing parenthesis

* Explicitly pass in baseReward to checkout vc

* Fix tests
  • Loading branch information
ifosli authored Apr 4, 2024
1 parent fd1a221 commit 7661516
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 12 deletions.
12 changes: 10 additions & 2 deletions Library/Tracking/KSRAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ public final class KSRAnalytics {
.withAllValuesFrom(checkoutProperties(from: checkoutData, and: reward))

switch pledgeViewContext {
case .pledge:
case .pledge, .latePledge:
props = props.withAllValuesFrom(contextProperties(page: .checkout))
case .changePaymentMethod:
props = props.withAllValuesFrom(contextProperties(page: .changePayment))
Expand Down Expand Up @@ -1309,7 +1309,6 @@ private func projectProperties(
props["category"] = project.category.parentAnalyticsName
props["category_id"] = project.category.parentId
props["percent_raised"] = project.stats.percentFunded
props["state"] = project.state.rawValue
props["current_pledge_amount"] = project.stats.pledged
props["current_amount_pledged_usd"] = rounded(project.stats.totalAmountPledgedUsdCurrency ?? 0, places: 2)
props["goal_usd"] = rounded(project.stats.goalUsdCurrency, places: 2)
Expand All @@ -1320,6 +1319,15 @@ private func projectProperties(
props["updates_count"] = project.stats.updatesCount
props["is_repeat_creator"] = project.creator.isRepeatCreator ?? false

if featurePostCampaignPledgeEnabled() {
props["late_pledge_enabled"] = project.postCampaignPledgingEnabled
props["state"] = project.isInPostCampaignPledgingPhase
? "post_campaign"
: project.state.rawValue
} else {
props["state"] = project.state.rawValue
}

let now = dateType.init().date
props["hours_remaining"] = project.dates.hoursRemaining(from: now, using: calendar)
props["duration"] = project.dates.duration(using: calendar)
Expand Down
6 changes: 4 additions & 2 deletions Library/ViewModels/ConfirmDetailsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,12 @@ public class ConfirmDetailsViewModel: ConfirmDetailsViewModelType, ConfirmDetail
initialData,
bonusOrPledgeUpdatedAmount,
optionalShippingSummaryData,
pledgeTotal
pledgeTotal,
baseReward
)
)
.map { checkoutId, otherData -> PostCampaignCheckoutData in
let (initialData, bonusOrReward, shipping, pledgeTotal) = otherData
let (initialData, bonusOrReward, shipping, pledgeTotal, baseReward) = otherData
var rewards = initialData.rewards
var bonus = bonusOrReward
if let reward = rewards.first, reward.isNoReward {
Expand All @@ -371,6 +372,7 @@ public class ConfirmDetailsViewModel: ConfirmDetailsViewModelType, ConfirmDetail

return PostCampaignCheckoutData(
project: initialData.project,
baseReward: baseReward,
rewards: rewards,
selectedQuantities: initialData.selectedQuantities,
bonusAmount: bonus == 0 ? nil : bonus,
Expand Down
1 change: 1 addition & 0 deletions Library/ViewModels/ConfirmDetailsViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ final class ConfirmDetailsViewModelTests: TestCase {
self.createCheckoutSuccess.assertDidEmitValue()
let expectedValue = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: expectedRewards,
selectedQuantities: selectedQuantities,
bonusAmount: expectedBonus,
Expand Down
74 changes: 66 additions & 8 deletions Library/ViewModels/PostCampaignCheckoutViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Stripe

public struct PostCampaignCheckoutData: Equatable {
public let project: Project
public let baseReward: Reward
public let rewards: [Reward]
public let selectedQuantities: SelectedRewardQuantities
public let bonusAmount: Double?
Expand Down Expand Up @@ -97,13 +98,13 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
self.configurePaymentMethodsViewControllerWithValue = configurePaymentMethodsData
.compactMap { data -> PledgePaymentMethodsValue? in
guard let user = AppEnvironment.current.currentUser else { return nil }
guard let reward = data.rewards.first else { return nil }
let reward = data.baseReward

return (user, data.project, reward, data.context, data.refTag, data.total, .paymentIntent)
}

self.goToLoginSignup = initialData.takeWhen(self.goToLoginSignupSignal)
.map { (LoginIntent.backProject, $0.project, $0.rewards.first) }
.map { (LoginIntent.backProject, $0.project, $0.baseReward) }

let isLoggedIn = Signal.merge(initialData.ignoreValues(), self.userSessionStartedSignal)
.map { _ in AppEnvironment.current.currentUser }
Expand Down Expand Up @@ -326,15 +327,12 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
data: PostCampaignCheckoutData,
paymentIntent: String
) -> PostCampaignPaymentAuthorizationData? in
guard let firstReward = data.rewards.first else {
// There should always be a reward - we create a special "no reward" reward if you make a monetary pledge
return nil
}
let baseReward = data.baseReward

return PostCampaignPaymentAuthorizationData(
project: data.project,
hasNoReward: firstReward.isNoReward,
subtotal: firstReward.isNoReward ? firstReward.minimum : calculateAllRewardsTotal(
hasNoReward: baseReward.isNoReward,
subtotal: baseReward.isNoReward ? baseReward.minimum : calculateAllRewardsTotal(
addOnRewards: data.rewards,
selectedQuantities: data.selectedQuantities
),
Expand Down Expand Up @@ -453,6 +451,66 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
self.checkoutTerminatedProperty.signal.mapConst(true),
checkoutCompleteSignal.signal.mapConst(true)
)

// MARK: - Tracking

// Page viewed event
initialData
.observeValues { data in
AppEnvironment.current.ksrAnalytics.trackCheckoutPaymentPageViewed(
project: data.project,
reward: data.baseReward,
pledgeViewContext: data.context,
checkoutData: self.trackingDataFromCheckoutParams(data),
refTag: data.refTag
)
}

// Pledge button tapped event
initialData
.takeWhen(self.submitButtonTappedProperty.signal)
.observeValues { data in
AppEnvironment.current.ksrAnalytics.trackPledgeSubmitButtonClicked(
project: data.project,
reward: data.baseReward,
typeContext: .creditCard,
checkoutData: self.trackingDataFromCheckoutParams(data),
refTag: data.refTag
)
}

// Apple pay button tapped event
initialData
.takeWhen(self.applePayButtonTappedSignal)
.observeValues { data in
AppEnvironment.current.ksrAnalytics.trackPledgeSubmitButtonClicked(
project: data.project,
reward: data.baseReward,
typeContext: .applePay,
checkoutData: self.trackingDataFromCheckoutParams(data, isApplePay: true),
refTag: data.refTag
)
}
}

// MARK: - Helpers

private func trackingDataFromCheckoutParams(
_ data: PostCampaignCheckoutData,
isApplePay: Bool = false
)
-> KSRAnalytics.CheckoutPropertiesData {
return checkoutProperties(
from: data.project,
baseReward: data.baseReward,
addOnRewards: data.rewards,
selectedQuantities: data.selectedQuantities,
additionalPledgeAmount: data.bonusAmount ?? 0,
pledgeTotal: data.total,
shippingTotal: data.shipping?.total ?? 0,
checkoutId: data.checkoutId,
isApplePay: isApplePay
)
}

// MARK: - Inputs
Expand Down
12 changes: 12 additions & 0 deletions Library/ViewModels/PostCampaignCheckoutViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
Expand Down Expand Up @@ -80,6 +81,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [reward.id: 3],
bonusAmount: 0,
Expand Down Expand Up @@ -121,6 +123,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [reward.id: 3],
bonusAmount: 0,
Expand Down Expand Up @@ -169,6 +172,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward1,
rewards: [reward1, reward2],
selectedQuantities: [reward1.id: 1, reward2.id: 2],
bonusAmount: 5,
Expand Down Expand Up @@ -216,6 +220,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
Expand Down Expand Up @@ -270,6 +275,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
Expand Down Expand Up @@ -320,6 +326,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
Expand Down Expand Up @@ -379,6 +386,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
Expand Down Expand Up @@ -429,6 +437,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
Expand Down Expand Up @@ -474,6 +483,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
Expand Down Expand Up @@ -520,6 +530,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
Expand Down Expand Up @@ -552,6 +563,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

let data = PostCampaignCheckoutData(
project: project,
baseReward: reward,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
Expand Down

0 comments on commit 7661516

Please sign in to comment.