Skip to content

Commit

Permalink
add trackImpression
Browse files Browse the repository at this point in the history
  • Loading branch information
vegaro committed Nov 27, 2024
1 parent b9ae356 commit a710691
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 19 deletions.
4 changes: 4 additions & 0 deletions RevenueCat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
351B51C126D450E800BD2BD7 /* OfferingsManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F575859126C08E3F00C12B97 /* OfferingsManagerTests.swift */; };
351B51C226D450E800BD2BD7 /* ProductRequestDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37E350E57B0A393455A72B40 /* ProductRequestDataTests.swift */; };
351B51C326D450F200BD2BD7 /* InMemoryCachedObjectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37E35E3250FBBB03D92E06EC /* InMemoryCachedObjectTests.swift */; };
352137322CDBA2AA00FE961B /* FeatureEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 352137312CDBA2A700FE961B /* FeatureEvent.swift */; };
3525D8A42C4AB3D600C21D99 /* CustomerCenterEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3525D8A32C4AB3D500C21D99 /* CustomerCenterEnvironment.swift */; };
35272E1B26D0029300F22C3B /* DeviceCacheSubscriberAttributesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37E35C4A795A0F056381A1B3 /* DeviceCacheSubscriberAttributesTests.swift */; };
35272E2226D0048D00F22C3B /* HTTPClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37E353CBE9CF2572A72A347F /* HTTPClientTests.swift */; };
Expand Down Expand Up @@ -1500,6 +1501,7 @@
351B517126D44EF300BD2BD7 /* MockInMemoryCachedOfferings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockInMemoryCachedOfferings.swift; sourceTree = "<group>"; };
351B517326D44F4B00BD2BD7 /* MockPaymentDiscount.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockPaymentDiscount.swift; sourceTree = "<group>"; };
351B517926D44FF000BD2BD7 /* MockRequestFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockRequestFetcher.swift; sourceTree = "<group>"; };
352137312CDBA2A700FE961B /* FeatureEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeatureEvent.swift; sourceTree = "<group>"; };
3525D8A32C4AB3D500C21D99 /* CustomerCenterEnvironment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomerCenterEnvironment.swift; sourceTree = "<group>"; };
352B7D7827BD919B002A47DD /* DangerousSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DangerousSettings.swift; sourceTree = "<group>"; };
3530C18822653E8F00D6DF52 /* AdSupport.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AdSupport.framework; path = System/Library/Frameworks/AdSupport.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -3552,6 +3554,7 @@
353DE0042CCA4EA600A8F632 /* Events */ = {
isa = PBXGroup;
children = (
352137312CDBA2A700FE961B /* FeatureEvent.swift */,
4FE6FEE42AA940B700780B45 /* StoredEvent.swift */,
4FD368B52AA7D09C00F63354 /* StoredEventSerializer.swift */,
353DE0052CCA4EAE00A8F632 /* Networking */,
Expand Down Expand Up @@ -5735,6 +5738,7 @@
2C0B98CD2797070B00C5874F /* PromotionalOffer.swift in Sources */,
57DC9F4627CC2E4900DA6AF9 /* HTTPRequest.swift in Sources */,
2DC5623024EC63730031F69B /* OperationDispatcher.swift in Sources */,
352137322CDBA2AA00FE961B /* FeatureEvent.swift in Sources */,
35D159CB2BC4396F004D8061 /* DiagnosticsPostOperation.swift in Sources */,
575642B62910116900719219 /* EligibilityStrings.swift in Sources */,
4F87610F2A5C9E490006FA14 /* PaywallData.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ protocol ManageSubscriptionsPurchaseType: Sendable {
@Sendable
func beginRefundRequest(forProduct productID: String) async throws -> RefundRequestStatus

@Sendable
func trackImpression(_ eventData: CustomerCenterEvent.Data) async throws

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ import RevenueCat
}
}

func trackImpression() {
// swiftlint:disable:next todo
// TODO: implement tracking impression
}

}

fileprivate extension String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ private final class ManageSubscriptionPurchases: ManageSubscriptionsPurchaseType
await Purchases.shared.products(productIdentifiers)
}

func trackImpression(_ eventData: CustomerCenterEvent.Data) async throws {
await Purchases.shared.track(customerCenterEvent: CustomerCenterEvent.impression(.init(), eventData))
}

}

#endif
7 changes: 7 additions & 0 deletions RevenueCatUI/CustomerCenter/Views/CustomerCenterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public struct CustomerCenterView: View {
.task {
await loadInformationIfNeeded()
}
.task {
trackImpression()
}
.environmentObject(self.viewModel)
}

Expand Down Expand Up @@ -118,6 +121,10 @@ private extension CustomerCenterView {
.applyIf(accentColor != nil, apply: { $0.tint(accentColor) })
}

func trackImpression() {
viewModel.trackImpression()
}

}

#if DEBUG
Expand Down
18 changes: 16 additions & 2 deletions Sources/CustomerCenter/Events/CustomerCenterEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import Foundation

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

// swiftlint:disable type_name

Expand All @@ -26,9 +26,19 @@ public enum CustomerCenterEvent {
/// An identifier that represents a paywall session.
public typealias SessionID = UUID

/// A `CustomerCenterView` was displayed.
var feature: Feature {
return .customerCenter
}

/// A ``CustomerCenterView`` was displayed.
case impression(CreationData, Data)

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

/// A ``CustomerCenterView`` was closed.
case close(CreationData, Data)

}

extension CustomerCenterEvent {
Expand Down Expand Up @@ -84,13 +94,17 @@ extension CustomerCenterEvent {
public var creationData: CreationData {
switch self {
case let .impression(creationData, _): return creationData
case let .surveyCompleted(creationData, _): return creationData
case let .close(creationData, _): return creationData
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Foundation

extension EventsRequest {

struct CustomerCenterEvent: FeatureEvent {
struct CustomerCenterEvent {

let id: String?
let version: Int
Expand All @@ -35,6 +35,8 @@ extension EventsRequest.CustomerCenterEvent {
enum EventType: String {

case impression = "customer_center_impression"
case close = "customer_center_close"
case surveyCompleted = "customer_center_survey_completed"

}

Expand Down Expand Up @@ -73,6 +75,8 @@ private extension CustomerCenterEvent {
var eventType: EventsRequest.CustomerCenterEvent.EventType {
switch self {
case .impression: return .impression
case .close: return .close
case .surveyCompleted: return .surveyCompleted
}

}
Expand Down
18 changes: 18 additions & 0 deletions Sources/Events/FeatureEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// FeatureEvent.swift
//
// Created by Cesar de la Vega on 6/11/24.

protocol FeatureEvent: Encodable, Sendable {

var feature: Feature { get }

}
9 changes: 0 additions & 9 deletions Sources/Events/Networking/EventsRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,4 @@ struct EventsRequest {

}

protocol FeatureEvent: Encodable {

var id: String? { get }
var version: Int { get }
var appUserID: String { get }
var sessionID: String { get }

}

extension EventsRequest: HTTPRequestBody {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Foundation

extension EventsRequest {

struct PaywallEvent: FeatureEvent {
struct PaywallEvent {

let id: String?
let version: Int
Expand Down
6 changes: 5 additions & 1 deletion Sources/Paywalls/Events/PaywallEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import Foundation

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

// swiftlint:disable type_name

Expand All @@ -26,6 +26,10 @@ public enum PaywallEvent {
/// An identifier that represents a paywall session.
public typealias SessionID = UUID

var feature: Feature {
return .paywalls
}

/// A `PaywallView` was displayed.
case impression(CreationData, Data)

Expand Down
8 changes: 4 additions & 4 deletions Sources/Paywalls/Events/PaywallEventsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Foundation
protocol PaywallEventsManagerType {

@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
func track(paywallEvent: PaywallEvent) async
func track(featureEvent: FeatureEvent) async

/// - Throws: if posting events fails
/// - Returns: the number of events posted
Expand Down Expand Up @@ -44,10 +44,10 @@ actor PaywallEventsManager: PaywallEventsManagerType {
self.store = store
}

func track(paywallEvent: PaywallEvent) async {
guard let event: StoredEvent = .init(event: paywallEvent,
func track(featureEvent: FeatureEvent) async {
guard let event: StoredEvent = .init(event: featureEvent,
userID: self.userProvider.currentAppUserID,
feature: .paywalls) else {
feature: featureEvent.feature) else {
Logger.error(Strings.paywalls.event_cannot_serialize)
return
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/Purchasing/Purchases/Purchases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,12 @@ public extension Purchases {
/// Used by `RevenueCatUI` to keep track of ``PaywallEvent``s.
func track(paywallEvent: PaywallEvent) async {
self.purchasesOrchestrator.track(paywallEvent: paywallEvent)
await self.paywallEventsManager?.track(paywallEvent: paywallEvent)
await self.paywallEventsManager?.track(featureEvent: paywallEvent)
}

/// Used by `RevenueCatUI` to keep track of ``CustomerCenterEvent``s.
func track(customerCenterEvent: CustomerCenterEvent) async {
await self.paywallEventsManager?.track(featureEvent: customerCenterEvent)
}

/// Used by `RevenueCatUI` to download customer center data
Expand Down

0 comments on commit a710691

Please sign in to comment.