Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkout release events clean #794

Merged
merged 9 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 75 additions & 3 deletions Library/Koala/Koala.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ public final class Koala {
}
}

public enum CheckoutContext {
case backThisPage
case projectPage

var trackingString: String {
switch self {
case .backThisPage: return "Back this page"
case .projectPage: return "Project page"
}
}
}

/**
Determines the place from which the update was presented.

Expand Down Expand Up @@ -566,6 +578,25 @@ public final class Koala {

// MARK: - Checkout Events

public func trackBackThisButtonClicked(project: Project, screen: CheckoutContext) {
let props = properties(project: project)
.withAllValuesFrom(["screen": screen.trackingString])

self.track(event: "Back this Project Button Clicked", properties: props)
}

public func trackSelectRewardButtonClicked(
project: Project,
reward: Reward?,
backing: Backing?,
screen: CheckoutContext
) {
let props = properties(project: project, reward: reward, backing: backing)
.withAllValuesFrom(["screen": screen.trackingString])

self.track(event: "Select Reward Button Clicked", properties: props)
}

public func trackCheckoutCancel(
project: Project,
reward: Reward,
Expand Down Expand Up @@ -1255,9 +1286,17 @@ public final class Koala {
props["current_variants"] = AppEnvironment.current.config?.abExperimentsArray

// Deprecated event
self.track(event: "Project Page", properties: props.withAllValuesFrom(deprecatedProps))
self.track(
event: "Project Page",
properties: props.withAllValuesFrom(deprecatedProps)
)

self.track(
event: "Viewed Project Page",
properties: props.withAllValuesFrom(deprecatedProps)
)

self.track(event: "Viewed Project Page", properties: props)
self.track(event: "Project Page Viewed", properties: props)
}

public func trackSwipedProject(_ project: Project, refTag: RefTag?, type: SwipeType) {
Expand Down Expand Up @@ -2027,7 +2066,7 @@ private func properties(
props["update_count"] = project.stats.updatesCount
props["comments_count"] = project.stats.commentsCount

let now = MockDate().timeIntervalSince1970
let now = AppEnvironment.current.dateType.init().timeIntervalSince1970
props["hours_remaining"] = Int(ceil(max(0.0, (project.dates.deadline - now) / 3_600.0)))
props["duration"] = Int(round(project.dates.deadline - project.dates.launchedAt))

Expand All @@ -2048,6 +2087,39 @@ private func properties(
.withAllValuesFrom(loggedInUserProperties)
}

private func properties(
project: Project,
reward: Reward? = nil,
backing: Backing? = nil,
prefix: String = "project_"
) -> [String: Any] {
var props: [String: Any] = [:]

props["name"] = project.name
props["pid"] = project.id
props["category"] = project.category.name
props["has_video"] = project.video != nil
props["location"] = project.location.name
props["country"] = project.country.countryCode

let now = AppEnvironment.current.dateType.init().timeIntervalSince1970
props["hours_remaining"] = Int(ceil(max(0.0, (project.dates.deadline - now) / 3_600.0)))

props["percent_raised"] = project.stats.fundingProgress

var rewardProperties: [String: Any] = [:]
rewardProperties["backer_reward_minimum"] = reward?.minimum

var backingProperties: [String: Any] = [:]
backingProperties["pledge_total"] = backing?.amount

props["currency"] = project.country.currencyCode

return props.prefixedKeys(prefix)
.withAllValuesFrom(rewardProperties)
.withAllValuesFrom(backingProperties)
}

private func properties(update: Update, prefix: String = "update_") -> [String: Any] {
var properties: [String: Any] = [:]

Expand Down
51 changes: 45 additions & 6 deletions Library/Koala/KoalaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ final class KoalaTests: TestCase {
let project = Project.template

koala.trackProjectShow(project, refTag: .discovery, cookieRefTag: .recommended)
XCTAssertEqual(2, client.properties.count)
XCTAssertEqual(3, client.properties.count)

let properties = client.properties.last

XCTAssertEqual("Viewed Project Page", client.events.last)
XCTAssertEqual("Project Page Viewed", client.events.last)
XCTAssertEqual(project.stats.backersCount, properties?["project_backers_count"] as? Int)
XCTAssertEqual(project.country.countryCode, properties?["project_country"] as? String)
XCTAssertEqual(project.country.currencyCode, properties?["project_currency"] as? String)
Expand Down Expand Up @@ -170,7 +170,7 @@ final class KoalaTests: TestCase {
let koala = Koala(client: client, loggedInUser: loggedInUser)

koala.trackProjectShow(project, refTag: nil, cookieRefTag: nil)
XCTAssertEqual(2, client.properties.count)
XCTAssertEqual(3, client.properties.count)

let properties = client.properties.last

Expand All @@ -188,7 +188,7 @@ final class KoalaTests: TestCase {
let koala = Koala(client: client, loggedInUser: loggedInUser)

koala.trackProjectShow(project, refTag: nil, cookieRefTag: nil)
XCTAssertEqual(2, client.properties.count)
XCTAssertEqual(3, client.properties.count)

let properties = client.properties.last

Expand All @@ -206,7 +206,7 @@ final class KoalaTests: TestCase {
let koala = Koala(client: client, loggedInUser: loggedInUser)

koala.trackProjectShow(project, refTag: nil, cookieRefTag: nil)
XCTAssertEqual(2, client.properties.count)
XCTAssertEqual(3, client.properties.count)

let properties = client.properties.last

Expand All @@ -224,7 +224,7 @@ final class KoalaTests: TestCase {
let koala = Koala(client: client, loggedInUser: loggedInUser)

koala.trackProjectShow(project, refTag: nil, cookieRefTag: nil)
XCTAssertEqual(2, client.properties.count)
XCTAssertEqual(3, client.properties.count)

let properties = client.properties.last

Expand Down Expand Up @@ -467,4 +467,43 @@ final class KoalaTests: TestCase {
koala.trackDiscoveryPullToRefresh()
XCTAssertEqual(["Triggered Refresh"], client.events)
}

func testTrackBackThisButtonClicked() {
let client = MockTrackingClient()
let project = Project.template
let loggedInUser = User.template |> \.id .~ 42

let koala = Koala(client: client, loggedInUser: loggedInUser)

koala.trackBackThisButtonClicked(project: project, screen: .projectPage)

let properties = client.properties.last

XCTAssertEqual(["Back this Project Button Clicked"], client.events)
XCTAssertEqual("Project page", properties?["screen"] as? String)
}

func testTrackSelectRewardButtonClicked() {
let client = MockTrackingClient()
let reward = Reward.template
let backing = .template
|> Backing.lens.reward .~ reward
let project = .template
|> Project.lens.personalization.backing .~ backing
let loggedInUser = User.template |> \.id .~ 42

let koala = Koala(client: client, loggedInUser: loggedInUser)

koala.trackSelectRewardButtonClicked(
project: project,
reward: reward,
backing: backing,
screen: .backThisPage
)

let properties = client.properties.last

XCTAssertEqual(["Select Reward Button Clicked"], client.events)
XCTAssertEqual("Back this page", properties?["screen"] as? String)
}
}
7 changes: 7 additions & 0 deletions Library/ViewModels/ProjectPamphletViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ public final class ProjectPamphletViewModel: ProjectPamphletViewModelType, Proje
.map(cookieFrom(refTag:project:))
.skipNil()
.observeValues { AppEnvironment.current.cookieStorage.setCookie($0) }

// Tracking
project
.takeWhen(self.backThisProjectTappedProperty.signal)
.observeValues {
AppEnvironment.current.koala.trackBackThisButtonClicked(project: $0, screen: .projectPage)
}
}

private let backThisProjectTappedProperty = MutableProperty(())
Expand Down
62 changes: 48 additions & 14 deletions Library/ViewModels/ProjectPamphletViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@ final class ProjectPamphletViewModelTests: TestCase {
self.scheduler.advance()

XCTAssertEqual(
["Project Page", "Viewed Project Page"],
["Project Page", "Viewed Project Page", "Project Page Viewed"],
self.trackingClient.events, "A project page koala event is tracked."
)
XCTAssertEqual(
[RefTag.category.stringTag, RefTag.category.stringTag],
[RefTag.category.stringTag, RefTag.category.stringTag, RefTag.category.stringTag],
self.trackingClient.properties.compactMap { $0["ref_tag"] as? String },
"The ref tag is tracked in the koala event."
)
XCTAssertEqual(
[RefTag.category.stringTag, RefTag.category.stringTag],
[RefTag.category.stringTag, RefTag.category.stringTag, RefTag.category.stringTag],
self.trackingClient.properties.compactMap { $0["referrer_credit"] as? String },
"The referral credit is tracked in the koala event."
)
Expand Down Expand Up @@ -168,21 +168,24 @@ final class ProjectPamphletViewModelTests: TestCase {
self.scheduler.advance()

XCTAssertEqual(
["Project Page", "Viewed Project Page", "Project Page", "Viewed Project Page"],
[
"Project Page", "Viewed Project Page", "Project Page Viewed", "Project Page",
"Viewed Project Page", "Project Page Viewed"
],
self.trackingClient.events, "A project page koala event is tracked."
)
XCTAssertEqual(
[
RefTag.category.stringTag, RefTag.category.stringTag, RefTag.recommended.stringTag,
RefTag.recommended.stringTag
RefTag.category.stringTag, RefTag.category.stringTag, RefTag.category.stringTag,
RefTag.recommended.stringTag, RefTag.recommended.stringTag, RefTag.recommended.stringTag
],
self.trackingClient.properties.compactMap { $0["ref_tag"] as? String },
"The new ref tag is tracked in koala event."
)
XCTAssertEqual(
[
RefTag.category.stringTag, RefTag.category.stringTag, RefTag.category.stringTag,
RefTag.category.stringTag
RefTag.category.stringTag, RefTag.category.stringTag, RefTag.category.stringTag
],
self.trackingClient.properties.compactMap { $0["referrer_credit"] as? String },
"The referrer credit did not change, and is still category."
Expand Down Expand Up @@ -276,16 +279,16 @@ final class ProjectPamphletViewModelTests: TestCase {
self.scheduler.advance()

XCTAssertEqual(
["Project Page", "Viewed Project Page"],
["Project Page", "Viewed Project Page", "Project Page Viewed"],
self.trackingClient.events, "A project page koala event is tracked."
)
XCTAssertEqual(
[RefTag.category.stringTag, RefTag.category.stringTag],
[RefTag.category.stringTag, RefTag.category.stringTag, RefTag.category.stringTag],
self.trackingClient.properties.compactMap { $0["ref_tag"] as? String },
"The ref tag is tracked in the koala event."
)
XCTAssertEqual(
[RefTag.category.stringTag, RefTag.category.stringTag],
[RefTag.category.stringTag, RefTag.category.stringTag, RefTag.category.stringTag],
self.trackingClient.properties.compactMap { $0["referrer_credit"] as? String },
"The referral credit is tracked in the koala event."
)
Expand Down Expand Up @@ -313,21 +316,24 @@ final class ProjectPamphletViewModelTests: TestCase {
self.scheduler.advance()

XCTAssertEqual(
["Project Page", "Viewed Project Page", "Project Page", "Viewed Project Page"],
[
"Project Page", "Viewed Project Page", "Project Page Viewed", "Project Page",
"Viewed Project Page", "Project Page Viewed"
],
self.trackingClient.events, "A project page koala event is tracked."
)
XCTAssertEqual(
[
RefTag.category.stringTag, RefTag.category.stringTag, RefTag.recommended.stringTag,
RefTag.recommended.stringTag
RefTag.category.stringTag, RefTag.category.stringTag, RefTag.category.stringTag,
RefTag.recommended.stringTag, RefTag.recommended.stringTag, RefTag.recommended.stringTag
],
self.trackingClient.properties.compactMap { $0["ref_tag"] as? String },
"The new ref tag is tracked in koala event."
)
XCTAssertEqual(
[
RefTag.category.stringTag, RefTag.category.stringTag, RefTag.category.stringTag,
RefTag.category.stringTag
RefTag.category.stringTag, RefTag.category.stringTag, RefTag.category.stringTag
],
self.trackingClient.properties.compactMap { $0["referrer_credit"] as? String },
"The referrer credit did not change, and is still category."
Expand Down Expand Up @@ -509,4 +515,32 @@ final class ProjectPamphletViewModelTests: TestCase {
self.configurePledgeCTAViewIsLoading.assertDidNotEmitValue()
}
}

func testbackThisButton_eventTracking() {
let config = Config.template |> \.features .~ [Feature.nativeCheckout.rawValue: true]
let client = MockTrackingClient()
let project = Project.template

withEnvironment(
apiService: MockService(),
config: config,
koala: Koala(client: client)
) {
XCTAssertEqual([], client.events)

self.vm.inputs.configureWith(projectOrParam: .left(project), refTag: .discovery)
self.vm.inputs.viewDidLoad()
self.vm.inputs.viewWillAppear(animated: false)
self.vm.inputs.viewDidAppear(animated: false)

self.goToRewardsProject.assertDidNotEmitValue()
self.goToRewardsRefTag.assertDidNotEmitValue()

self.vm.inputs.backThisProjectTapped()
XCTAssertEqual(
["Project Page", "Viewed Project Page", "Project Page Viewed", "Back this Project Button Clicked"],
client.events
)
}
}
}
13 changes: 13 additions & 0 deletions Library/ViewModels/RewardCardContainerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ public final class RewardCardContainerViewModel: RewardCardContainerViewModelTyp
self.rewardSelected = reward
.takeWhen(self.pledgeButtonTappedProperty.signal)
.map { $0.id }

// Tracking
projectAndRewardOrBacking
.takeWhen(self.pledgeButtonTappedProperty.signal)
.observeValues { projectAndRewardOrBacking in
let (project, rewardOrBacking) = projectAndRewardOrBacking
AppEnvironment.current.koala.trackSelectRewardButtonClicked(
project: project,
reward: rewardOrBacking.left,
backing: rewardOrBacking.right,
screen: .backThisPage
)
}
}

private let projectAndRewardOrBackingProperty = MutableProperty<(Project, Either<Reward, Backing>)?>(nil)
Expand Down
14 changes: 14 additions & 0 deletions Library/ViewModels/RewardCardContainerViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,18 @@ final class RewardCardContainerViewModelTests: TestCase {

self.rewardSelected.assertValues([Reward.template.id])
}

func testPledgeButtonTapped_eventTracking() {
let client = MockTrackingClient()

withEnvironment(apiService: MockService(), koala: Koala(client: client)) {
XCTAssertEqual([], client.events)

self.vm.inputs.configureWith(project: .template, rewardOrBacking: .left(.template))

self.vm.inputs.pledgeButtonTapped()

XCTAssertEqual(["Select Reward Button Clicked"], client.events)
}
}
}