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 1 commit
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
Next Next commit
Fix analytics and dismiss
  • Loading branch information
joshdholtz committed Dec 27, 2024
commit 7e79358a5d371a6ec9d1d6e5f92199089547bc51
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
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