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

analyticsv2 PoC #169

Merged
merged 13 commits into from
May 21, 2024
3 changes: 2 additions & 1 deletion Shared/CommonAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import AppsFlyerStaticInjections
import FirebaseStaticInjections
import dydxStateManager
import dydxViews
import dydxAnalytics

open class CommonAppDelegate: ParticlesAppDelegate {
open var notificationTag: String {
Expand Down Expand Up @@ -169,7 +170,7 @@ open class CommonAppDelegate: ParticlesAppDelegate {

open override func applicationDidBecomeActive(_ application: UIApplication) {
super.applicationDidBecomeActive(application)
Tracking.shared?.log(event: .appStart)
Tracking.shared?.log(event: AnalyticsEventV2.AppStart())
dydxRatingService.shared?.launchedApp()
}

Expand Down
15 changes: 11 additions & 4 deletions Utilities/Utilities/_Tracker/_Shared/Tracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public extension TrackingProtocol {
func log(event: String, data: [String: Any]?) {
log(event: event, data: data, revenue: nil)
}
func log(trackableEvent: TrackableEvent) {
log(event: trackableEvent.name, data: trackableEvent.customParameters, revenue: nil)
}
// func log(trackableEvent: TrackableEvent) {
// log(event: trackableEvent.name, data: trackableEvent.customParameters, revenue: nil)
// }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted this in another commit 😓


func setUserInfo(key: String, value: String?) {
var userInfo = userInfo ?? [String: String?]()
Expand All @@ -34,11 +34,18 @@ public class Tracking {
public static var shared: TrackingProtocol?
}

public protocol TrackableEvent {
public protocol TrackableEvent: CustomStringConvertible {
var name: String { get }
var customParameters: [String: Any] { get }
}

public extension TrackableEvent {
var description: String {
let sorted = customParameters.sorted { $0.key < $1.key }
return "dydxAnalytics event \(name) with data: \(sorted)"
}
}

public protocol TrackingViewProtocol: ScreenIdentifiable {
func logScreenView()
}
Expand Down
11 changes: 10 additions & 1 deletion dydx/dydx.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "1c0055be4aba3ed4d97a2f62f05486877839310f65e2cbdd18b067bef06a04a3",
"originHash" : "975d00e29efb8d2ca017c5e61df90418ac01f7d7143e85a3f9ddb4eb982154e4",
"pins" : [
{
"identity" : "bigint",
Expand Down Expand Up @@ -46,6 +46,15 @@
"revision" : "48134b5460435cc9d048223ad7560ee2e40f3d4a"
}
},
{
"identity" : "percy-xcui-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/percy/percy-xcui-swift",
"state" : {
"revision" : "a2e9a86dfc3f5b69ef53cbda28a0ea71098c9f77",
"version" : "1.0.0"
}
},
{
"identity" : "qrcode",
"kind" : "remoteSourceControl",
Expand Down
123 changes: 66 additions & 57 deletions dydx/dydxAnalytics/dydxAnalytics/AnalyticsEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,76 +67,85 @@ public extension AnalyticsEventV2 {
}
}

public enum AnalyticsEventV2: TrackableEvent, CustomDebugStringConvertible {
// TODO: add and replace all events
case appStart
case navigatePage(screen: ScreenIdentifiable)
case deepLinkHandled(url: String, succeeded: Bool)
case notificationPermissionsChanged(isAuthorized: Bool)
case onboardingStepChanged(step: OnboardingStep, state: OnboardingState)

public var name: String {
switch self {
case .navigatePage:
return "NavigatePage"
case .deepLinkHandled:
return "DeeplinkHandled"
case .appStart:
return "AppStart"
case .onboardingStepChanged:
return "OnboardingStepChanged"
case .notificationPermissionsChanged:
return "NotificationPermissionsChanged"
public enum AnalyticsEventV2 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruixhuang note I am nesting them in AnalyticsEventV2 so that we can use Xcode's autofill after typing AnalyticsEventV2., also to keep them contained. Lmk if u think this is not worth it and I can put them at the global level.

public struct AppStart: TrackableEvent {
public var name: String { "AppStart" }
public var customParameters: [String: Any] { [:] }

public init() {}
}

public struct NavigatePage: TrackableEvent {
public let screen: ScreenIdentifiable

public var name: String { "NavigatePage" }
public var customParameters: [String: Any] {[
"mobile_path": screen.mobilePath,
"path": screen.correspondingWebPath as Any,
// for firebase auto-generated dashboard(s)
"\(AnalyticsParameterScreenClass)": screen.screenClass,
"\(AnalyticsParameterScreenName)": screen.mobilePath
]}

public init(screen: ScreenIdentifiable) {
self.screen = screen
}
}

public var customParameters: [String: Any] {
switch self {
case .appStart:
return [:]
case .navigatePage(let screen):
return [
"mobile_path": screen.mobilePath,
"path": screen.correspondingWebPath as Any,
// for firebase auto-generated dashboard(s)
"\(AnalyticsParameterScreenClass)": screen.screenClass,
"\(AnalyticsParameterScreenName)": screen.mobilePath
]
case .deepLinkHandled(let url, let succeeded):
return [
"successfully_handled": succeeded,
"url": url
]
case .onboardingStepChanged(let step, let state):
return [
"step": step.rawValue,
"state": state.rawValue
]
case .notificationPermissionsChanged(let isAuthorized):
return [
"is_authorized": isAuthorized
]
public struct DeepLinkHandled: TrackableEvent {
let url: String
let succeeded: Bool

public var name: String { "DeeplinkHandled" }
public var customParameters: [String: Any] {[
"url": url,
"succeeded": succeeded
]}

public init(url: String, succeeded: Bool) {
self.url = url
self.succeeded = succeeded
}
}

public var debugDescription: String {
let sorted = customParameters.sorted { $0.key < $1.key }
return "dydxAnalytics event \(name) with data: \(sorted)"
public struct NotificationPermissionsChanged: TrackableEvent {
let isAuthorized: Bool

public var name: String { "NotificationPermissionsChanged" }
public var customParameters: [String: Any] {[
"is_authorized": isAuthorized
]}

public init(isAuthorized: Bool) {
self.isAuthorized = isAuthorized
}
}

public struct OnboardingStepChanged: TrackableEvent {
let step: OnboardingStep
let state: OnboardingState

public var name: String { "OnboardingStepChanged" }
public var customParameters: [String: Any] {[
"step": step.rawValue,
"state": state.rawValue
]}

public init(step: OnboardingStep, state: OnboardingState) {
self.step = step
self.state = state
}
}
}

public extension TrackingProtocol {
func log(event: AnalyticsEventV2) {
log(event: event.name, data: event.customParameters)
switch event {
case .navigatePage:
// for firebase auto-generated dashboard(s)
func log(event: TrackableEvent) {
if let event = event as? AnalyticsEventV2.NavigatePage {
log(event: AnalyticsEventScreenView, data: event.customParameters)
default:
break
}
log(event: event.name, data: event.customParameters)
#if DEBUG
Console.shared.log(event.debugDescription)
Console.shared.log(event.description)
#endif
}
}
2 changes: 1 addition & 1 deletion dydx/dydxAnalytics/dydxAnalytics/OnboardingAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class OnboardingAnalytics {
} else {
state = .walletConnected
}
Tracking.shared?.log(event: .onboardingStepChanged(step: step, state: state))
Tracking.shared?.log(event: AnalyticsEventV2.OnboardingStepChanged(step: step, state: state))
}
.store(in: &subscriptions)
}
Expand Down
2 changes: 1 addition & 1 deletion dydxV4/dydxV4/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class AppDelegate: CommonAppDelegate {
}

override func deepLinkHandled(deeplink: URL, successful: Bool) {
Tracking.shared?.log(event: .deepLinkHandled(url: deeplink.absoluteString, succeeded: successful))
Tracking.shared?.log(event: AnalyticsEventV2.DeepLinkHandled(url: deeplink.absoluteString, succeeded: successful))
}

/// Prioritized CoinbaseWalletSDK handling of the deeplink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ final class dydxNotificationHandlerDelegate: NSObject, NotificationHandlerDelega
if lastPermission != permission {
if permission == .denied {
if lastPermission == .notDetermined || lastPermission == .authorized {
Tracking.shared?.log(event: .notificationPermissionsChanged(isAuthorized: false))
Tracking.shared?.log(event: AnalyticsEventV2.NotificationPermissionsChanged(isAuthorized: false))
}
if lastPermission == .authorized {
// sendTokenDeletion()
}
} else if permission == .authorized {
if lastPermission == .notDetermined || lastPermission == .denied {
Tracking.shared?.log(event: .notificationPermissionsChanged(isAuthorized: true))
Tracking.shared?.log(event: AnalyticsEventV2.NotificationPermissionsChanged(isAuthorized: true))
}
// sendTokenUpdate()
}
Expand Down
3 changes: 2 additions & 1 deletion dydxV4/dydxV4/_Tracking/TrackingViewController+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Utilities
import PlatformParticles
import dydxPresenters
import dydxAnalytics

extension TrackingViewController: ScreenIdentifiable, TrackingViewProtocol {
public var mobilePath: String {
Expand Down Expand Up @@ -38,7 +39,7 @@ extension TrackingViewController: ScreenIdentifiable, TrackingViewProtocol {
}

public func logScreenView() {
Tracking.shared?.log(event: .navigatePage(screen: self))
Tracking.shared?.log(event: AnalyticsEventV2.NavigatePage(screen: self))
}

}
Expand Down
Loading