Skip to content

Commit

Permalink
Add survey option event
Browse files Browse the repository at this point in the history
  • Loading branch information
vegaro committed Nov 29, 2024
1 parent a2e3ca9 commit 3b03d83
Show file tree
Hide file tree
Showing 16 changed files with 382 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ protocol CustomerCenterPurchasesType: Sendable {
promotionalOffer: PromotionalOffer
) async throws -> PurchaseResultData

func track(customerCenterEvent: CustomerCenterEvent)
func track(customerCenterEvent: any CustomerCenterEventType)

}
11 changes: 11 additions & 0 deletions RevenueCatUI/CustomerCenter/Data/CustomerCenterEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ struct SupportKey: EnvironmentKey {

}

struct CustomerCenterPresentationModeKey: EnvironmentKey {

static let defaultValue: CustomerCenterPresentationMode = .default

}

extension CustomerCenterConfigData.Localization {

/// Default ``CustomerCenterConfigData.Localization`` value for Environment usage
Expand Down Expand Up @@ -70,4 +76,9 @@ extension EnvironmentValues {
set { self[SupportKey.self] = newValue }
}

var customerCenterPresentationMode: CustomerCenterPresentationMode {
get { self[CustomerCenterPresentationModeKey.self] }
set { self[CustomerCenterPresentationModeKey.self] = newValue }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class CustomerCenterPurchases: CustomerCenterPurchasesType {
)
}

func track(customerCenterEvent: CustomerCenterEvent) {
func track(customerCenterEvent: any CustomerCenterEventType) {
Purchases.shared.track(customerCenterEvent: customerCenterEvent)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ class FeedbackSurveyViewModel: ObservableObject {

func handleAction(
for option: CustomerCenterConfigData.HelpPath.FeedbackSurvey.Option,
darkMode: Bool,
displayMode: CustomerCenterPresentationMode,
dismissView: () -> Void
) async {
if let customerCenterActionHandler = self.customerCenterActionHandler {
customerCenterActionHandler(.feedbackSurveyCompleted(option.id))
trackSurveyOptionChosen(option: option, darkMode: darkMode, displayMode: displayMode)
}

if let promotionalOffer = option.promotionalOffer,
Expand Down Expand Up @@ -105,4 +108,31 @@ extension FeedbackSurveyViewModel {
}
}

// MARK: - Events
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
private extension FeedbackSurveyViewModel {

func trackSurveyOptionChosen(option: CustomerCenterConfigData.HelpPath.FeedbackSurvey.Option,
darkMode: Bool,
displayMode: CustomerCenterPresentationMode) {
let isSandbox = purchasesProvider.isSandbox
let surveyOptionData = CustomerCenterSurveyOptionChosenEvent.Data(locale: .current,
darkMode: darkMode,
isSandbox: isSandbox,
displayMode: displayMode,
pathID: "path_id",
surveyOptionID: option.id,
surveyOptionTitleKey: option.title,
additionalContext: nil,
revisionID: 0)
let event = CustomerCenterSurveyOptionChosenEvent.surveyOptionChosen(CustomerCenterEvent.CreationData(),
surveyOptionData)
purchasesProvider.track(customerCenterEvent: event)
}

}

#endif
1 change: 1 addition & 0 deletions RevenueCatUI/CustomerCenter/Views/CustomerCenterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public struct CustomerCenterView: View {
.environment(\.localization, configuration.localization)
.environment(\.appearance, configuration.appearance)
.environment(\.supportInformation, configuration.support)
.environment(\.customerCenterPresentationMode, self.mode)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions RevenueCatUI/CustomerCenter/Views/FeedbackSurveyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct FeedbackSurveyView: View {
private var appearance: CustomerCenterConfigData.Appearance
@Environment(\.colorScheme)
private var colorScheme
@Environment(\.customerCenterPresentationMode)
private var mode: CustomerCenterPresentationMode

@Binding
private var isPresented: Bool
Expand All @@ -57,6 +59,8 @@ struct FeedbackSurveyView: View {
onOptionSelected: { option in
await self.viewModel.handleAction(
for: option,
darkMode: self.colorScheme == .dark,
displayMode: self.mode,
dismissView: self.dismissView
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ struct ManageSubscriptionsView: View {

if let purchaseInformation = self.viewModel.purchaseInformation {
Section {
SubscriptionDetailsView(
purchaseInformation: purchaseInformation,
refundRequestStatus: self.viewModel.refundRequestStatus)
SubscriptionDetailsView(purchaseInformation: purchaseInformation,
refundRequestStatus: self.viewModel.refundRequestStatus)
}
}

Expand Down
137 changes: 127 additions & 10 deletions Sources/CustomerCenter/Events/CustomerCenterEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,70 @@

import Foundation

/// An event to be sent by the `RevenueCatUI` SDK.
public enum CustomerCenterEvent: FeatureEvent {

// swiftlint:disable type_name
public protocol CustomerCenterEventType {

/// An identifier that represents a customer center event.
public typealias ID = UUID
typealias ID = UUID

// swiftlint:enable type_name

/// An identifier that represents a paywall session.
public typealias SessionID = UUID
typealias SessionID = UUID

associatedtype EventData

var creationData: CustomerCenterEvent.CreationData { get }
var data: EventData { get }

}

extension CustomerCenterEvent: CustomerCenterEventType {
public typealias EventData = Data
}
extension CustomerCenterSurveyOptionChosenEvent: CustomerCenterEventType {
public typealias EventData = Data
}

/// An event to be sent by the `RevenueCatUI` SDK.
public enum CustomerCenterEvent: FeatureEvent {

// swiftlint:disable type_name

var feature: Feature {
return .customerCenter
}

var eventDiscriminator: String? {
switch self {
case .impression: return "impression"
}
}

/// The Customer Center was displayed.
case impression(CreationData, Data)

}

/// An event to be sent by the `RevenueCatUI` SDK.
public enum CustomerCenterSurveyOptionChosenEvent: FeatureEvent {

// swiftlint:disable type_name

var feature: Feature {
return .customerCenter
}

var eventDiscriminator: String? {
switch self {
case .surveyOptionChosen: return "survey_option_chosen"
}
}

/// A feedback survey was completed with a particular option.
case surveyOptionChosen(CreationData, Data)

}

extension CustomerCenterEvent {

/// The creation data of a ``CustomerCenterEvent``.
Expand All @@ -54,10 +96,6 @@ extension CustomerCenterEvent {

}

}

extension CustomerCenterEvent {

/// The content of a ``CustomerCenterEvent``.
public struct Data {

Expand All @@ -84,6 +122,48 @@ extension CustomerCenterEvent {

}

extension CustomerCenterSurveyOptionChosenEvent {

public typealias CreationData = CustomerCenterEvent.CreationData

public struct Data {

public var localeIdentifier: String
public var darkMode: Bool
public var isSandbox: Bool
public var displayMode: CustomerCenterPresentationMode
public var pathID: String
public var surveyOptionID: String
public var surveyOptionTitleKey: String
public var additionalContext: String?
public var revisionID: Int

public init(
locale: Locale,
darkMode: Bool,
isSandbox: Bool,
displayMode: CustomerCenterPresentationMode,
pathID: String,
surveyOptionID: String,
surveyOptionTitleKey: String,
additionalContext: String? = nil,
revisionID: Int
) {
self.localeIdentifier = locale.identifier
self.darkMode = darkMode
self.isSandbox = isSandbox
self.displayMode = displayMode
self.pathID = pathID
self.surveyOptionID = surveyOptionID
self.surveyOptionTitleKey = surveyOptionTitleKey
self.additionalContext = additionalContext
self.revisionID = revisionID
}

}

}

extension CustomerCenterEvent {

/// - Returns: the underlying ``CustomerCenterEvent/CreationData-swift.struct`` for this event.
Expand All @@ -102,8 +182,45 @@ extension CustomerCenterEvent {

}

extension CustomerCenterSurveyOptionChosenEvent {

/// - Returns: the underlying ``CustomerCenterSurveyOptionChosenEvent/CreationData-swift.struct`` for this event.
public var creationData: CreationData {
switch self {
case let .surveyOptionChosen(creationData, _): return creationData
}
}

/// - Returns: the underlying ``CustomerCenterSurveyOptionChosenEvent/Data-swift.struct`` for this event.
public var data: Data {
switch self {
case let .surveyOptionChosen(_, surveyData): return surveyData
}
}

}

// MARK: -

extension CustomerCenterEvent.CreationData: Equatable, Codable, Sendable {}
extension CustomerCenterEvent.Data: Equatable, Codable, Sendable {}
extension CustomerCenterEvent: Equatable, Codable, Sendable {}

extension CustomerCenterSurveyOptionChosenEvent.Data: Equatable, Codable, Sendable {

private enum CodingKeys: String, CodingKey {

case localeIdentifier = "localeIdentifier"
case darkMode = "darkMode"
case isSandbox = "isSandbox"
case displayMode = "displayMode"
case pathID = "pathId"
case surveyOptionID = "surveyOptionId"
case surveyOptionTitleKey = "surveyOptionTitleKey"
case additionalContext = "additionalContext"
case revisionID = "revisionId"

}

}
extension CustomerCenterSurveyOptionChosenEvent: Equatable, Codable, Sendable {}
Loading

0 comments on commit 3b03d83

Please sign in to comment.