Event
A repeatable user-defined action.
Declaration
typealias Event = Tips.EventOverview
Use an event when you want to track an action that can occur one or more times in your app (such as a user logging in). Then use donate() to donate to the event when the action occurs, increasing the event count by one.
Creating an event with no associated donation value
The example below creates a landmarksAppDidOpen event with no associated donation value and donates it anytime ContentView appears:
struct LandmarkTips: App {
static let landmarksAppDidOpen = Tips.Event(id: "landmarksAppDidOpen")
var body: some Scene {
WindowGroup {
ContentView()
.onAppear { Self.landmarksAppDidOpen.sendDonation() }
}
}
}The example below creates a display rule for LandmarkFeatureTip based on the landmarksAppDidOpen event.
struct LandmarkFeatureTip: Tip {
var rules: [Rule] {
// Tip will only display when the landmarksAppDidOpen event has been donated 3 or more times in the last week.
#Rule(LandmarkTips.landmarksAppDidOpen) {
$0.donations.donatedWithin(.week).count >= 3
}
}
}Creating an event with an associated donation value
The example below creates a didViewLandmarkDetail event with an associated donation value and donates it anytime the LandmarkDetail appears:
struct LandmarkDetail: View {
static let didViewLandmarkDetail = Tips.Event<DidViewLandmark>(id: "didViewLandmarkDetail")
struct DidViewLandmark: Codable, Sendable {
let landmarkID: Int
let landmarkName: String
}
var landmark: Landmark
var body: some View {
ScrollView {
MapView(coordinate: landmark.locationCoordinate)
}
.onAppear {
Self.didViewLandmarkDetail.sendDonation(.init(landmarkID: landmark.id, landmarkName: landmark.name))
}
}
}The example below creates a display rule for LandmarkDetailTip based on the didViewLandmarkDetail event.
struct LandmarkDetailTip: Tip {
var rules: [Rule] {
// Tip will only display when the didViewLandmarkDetail has been donated 3 or more times for landmarks not named "Wilbere Bowl".
#Rule(LandmarkDetail.didViewLandmarkDetail) {
$0.donations.filter({ $0.landmarkName != "Wilbere Bowl" }).count > 3
}
}
}Filtering an event’s donations
TipKit provides methods on Sequence for filtering an event’s donations within rule predicates.
Use Swift/Sequence/donatedWithin(_:) to filter donations by recency:
#Rule(AppEvents.didLogin) {
$0.donations.donatedWithin(.week).count >= 3
}Use Swift/Sequence/largestSubset(groupedBy:) and Swift/Sequence/smallestSubset(groupedBy:) to find the most or least frequent donation values:
#Rule(LandmarkDetail.didViewLandmarkDetail) {
// Show tip when the most-viewed landmark has been viewed 5+ times.
$0.donations.largestSubset(groupedBy: \.landmarkID).count >= 5
}These methods can be combined to build complex eligibility conditions:
#Rule(LandmarkDetail.didViewLandmarkDetail) {
// Show tip when the least-viewed landmark in the past month has been viewed at least twice.
$0.donations.donatedWithin(.month).smallestSubset(groupedBy: \.landmarkID).count >= 2
}