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

[Paywalls V2] Fix analytics and dismiss #4620

Merged
merged 2 commits into from
Dec 30, 2024
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
33 changes: 33 additions & 0 deletions RevenueCatUI/Templates/V2/PaywallsV2View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ struct PaywallsV2View: View {
@Environment(\.horizontalSizeClass)
private var horizontalSizeClass

@Environment(\.colorScheme)
private var colorScheme

@EnvironmentObject
private var purchaseHandler: PurchaseHandler

@StateObject
private var introOfferEligibilityContext: IntroOfferEligibilityContext

@StateObject
private var paywallStateManager: PaywallStateManager

private let paywallComponentsData: PaywallComponentsData
private let offering: Offering
private let onDismiss: () -> Void

public init(
Expand All @@ -52,6 +60,8 @@ struct PaywallsV2View: View {
showZeroDecimalPlacePrices: Bool,
onDismiss: @escaping () -> Void
) {
self.paywallComponentsData = paywallComponentsData
self.offering = offering
self.onDismiss = onDismiss
self._introOfferEligibilityContext = .init(
wrappedValue: .init(introEligibilityChecker: introEligibilityChecker)
Expand Down Expand Up @@ -84,6 +94,18 @@ struct PaywallsV2View: View {
)
.environment(\.screenCondition, ScreenCondition.from(self.horizontalSizeClass))
.environmentObject(self.introOfferEligibilityContext)
.disabled(self.purchaseHandler.actionInProgress)
.onAppear {
self.purchaseHandler.trackPaywallImpression(
self.createEventData()
)
}
.onDisappear { self.purchaseHandler.trackPaywallClose() }
.onChangeOf(self.purchaseHandler.purchased) { purchased in
if purchased {
self.onDismiss()
}
}
.task {
await self.introOfferEligibilityContext.computeEligibility(for: paywallState.packages)
}
Expand All @@ -93,6 +115,17 @@ struct PaywallsV2View: View {
}
}

private func createEventData() -> PaywallEvent.Data {
return .init(
offering: self.offering,
paywallComponentsData: self.paywallComponentsData,
sessionID: .init(),
displayMode: .fullScreen,
locale: .current,
darkMode: self.colorScheme == .dark
)
}

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
Expand Down
9 changes: 7 additions & 2 deletions RevenueCatUI/Templates/V2/Previews/PreviewMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ struct PreviewRequiredEnvironmentProperties: ViewModifier {
let screenCondition: ScreenCondition
let componentViewState: ComponentViewState
let packageContext: PackageContext?
let colorScheme: ColorScheme

func body(content: Content) -> some View {
content
.environmentObject(IntroOfferEligibilityContext(introEligibilityChecker: .default()))
.environmentObject(PurchaseHandler.default())
.environmentObject(self.packageContext ?? Self.defaultPackageContext)
.environment(\.screenCondition, screenCondition)
.environment(\.componentViewState, componentViewState)
.environment(\.colorScheme, colorScheme)
}

}
Expand All @@ -45,12 +48,14 @@ extension View {
func previewRequiredEnvironmentProperties(
screenCondition: ScreenCondition = .compact,
componentViewState: ComponentViewState = .default,
packageContext: PackageContext? = nil
packageContext: PackageContext? = nil,
colorScheme: ColorScheme = .light
) -> some View {
self.modifier(PreviewRequiredEnvironmentProperties(
screenCondition: screenCondition,
componentViewState: componentViewState,
packageContext: packageContext
packageContext: packageContext,
colorScheme: colorScheme
))
}
}
Expand Down
21 changes: 21 additions & 0 deletions Sources/Paywalls/Events/PaywallEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ extension PaywallEvent {
public var localeIdentifier: String
public var darkMode: Bool

#if PAYWALL_COMPONENTS
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public init(
offering: Offering,
paywallComponentsData: PaywallComponentsData,
sessionID: SessionID,
displayMode: PaywallViewMode,
locale: Locale,
darkMode: Bool
) {
self.init(
offeringIdentifier: offering.identifier,
paywallRevision: paywallComponentsData.revision,
sessionID: sessionID,
displayMode: displayMode,
localeIdentifier: locale.identifier,
darkMode: darkMode
)
}
#endif

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public init(
offering: Offering,
Expand Down