diff --git a/Library/Koala/Koala.swift b/Library/Koala/Koala.swift index 8787fc3f5c..a843d28711 100644 --- a/Library/Koala/Koala.swift +++ b/Library/Koala/Koala.swift @@ -599,6 +599,13 @@ public final class Koala { } } + public func trackCancelPledgeButtonClicked(project: Project, backing: Backing) { + let props = properties(project: project, loggedInUser: self.loggedInUser) + .withAllValuesFrom(["pledge_total": backing.amount]) + + self.track(event: "Cancel Pledge Button Clicked", properties: props) + } + public func trackSelectRewardButtonClicked( project: Project, reward: Reward?, diff --git a/Library/Koala/KoalaTests.swift b/Library/Koala/KoalaTests.swift index 8883b6a006..898dd530b4 100644 --- a/Library/Koala/KoalaTests.swift +++ b/Library/Koala/KoalaTests.swift @@ -568,4 +568,17 @@ final class KoalaTests: TestCase { XCTAssertEqual(["Select Reward Button Clicked"], client.events) XCTAssertEqual("Back this page", properties?["screen"] as? String) } + + func testTrackCancelPledgeButtonClicked() { + let client = MockTrackingClient() + let loggedInUser = User.template + + let koala = Koala(client: client, loggedInUser: loggedInUser) + + koala.trackCancelPledgeButtonClicked( + project: .template, + backing: .template + ) + XCTAssertEqual(["Cancel Pledge Button Clicked"], client.events) + } } diff --git a/Library/ViewModels/CancelPledgeViewModel.swift b/Library/ViewModels/CancelPledgeViewModel.swift index 7caa7b047b..b7ada5be13 100644 --- a/Library/ViewModels/CancelPledgeViewModel.swift +++ b/Library/ViewModels/CancelPledgeViewModel.swift @@ -37,9 +37,13 @@ public final class CancelPledgeViewModel: CancelPledgeViewModelType, CancelPledg ) .map(first) + let project = initialData + .map(first) let backing = initialData .map(second) + let projectAndBacking = Signal.combineLatest(project, backing) + self.cancellationDetailsAttributedText = Signal.merge( initialData, initialData.takeWhen(self.traitCollectionDidChangeProperty.signal) @@ -93,6 +97,15 @@ public final class CancelPledgeViewModel: CancelPledgeViewModelType, CancelPledg cancelPledgeEvent.map { $0.isTerminating }.mapConst(true) ) .skipRepeats() + + // Tracking + projectAndBacking + .takeWhen(self.cancelPledgeButtonTappedProperty.signal) + .observeValues { AppEnvironment.current.koala.trackCancelPledgeButtonClicked( + project: $0, + backing: $1 + ) + } } private let cancelPledgeButtonTappedProperty = MutableProperty(()) diff --git a/Library/ViewModels/CancelPledgeViewModelTests.swift b/Library/ViewModels/CancelPledgeViewModelTests.swift index 6cbfbfb5c3..e5680dd645 100644 --- a/Library/ViewModels/CancelPledgeViewModelTests.swift +++ b/Library/ViewModels/CancelPledgeViewModelTests.swift @@ -222,4 +222,19 @@ final class CancelPledgeViewModelTests: TestCase { self.cancelPledgeError.assertValues(["You can't cancel your pledge right now."]) } } + + func testTrackingEvents() { + let mockService = MockService(cancelBackingResult: .init(success: .init())) + + withEnvironment(apiService: mockService) { + self.vm.inputs.configure(with: .template, backing: .template) + self.vm.inputs.viewDidLoad() + + XCTAssertEqual([], self.trackingClient.events) + + self.vm.inputs.cancelPledgeButtonTapped() + + XCTAssertEqual(["Cancel Pledge Button Clicked"], self.trackingClient.events) + } + } }