Skip to content

Commit

Permalink
add analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Oct 15, 2024
1 parent 379f7cb commit 6d1c7a2
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
81 changes: 81 additions & 0 deletions dydx/dydxAnalytics/dydxAnalytics/AnalyticsEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public extension AnalyticsEventV2 {
}
}

public extension AnalyticsEventV2 {
enum VaultAnalyticsInputType: String {
case deposit = "DEPOSIT"
case withdraw = "WITHDRAW"
}
}

public enum AnalyticsEventV2 {
public struct AppStart: TrackableEvent {
public var name: String { "AppStart" }
Expand Down Expand Up @@ -136,6 +143,80 @@ public enum AnalyticsEventV2 {
self.state = state
}
}

public struct VaultFormPreviewStep: TrackableEvent {
let type: VaultAnalyticsInputType
let amount: Double

public var name: String { "VaultFormPreviewStep" }
public var customParameters: [String: Any] {[
"amount": amount,
"operation": type.rawValue
]}

public init(amount: Double, type: VaultAnalyticsInputType) {
self.amount = amount
self.type = type
}
}

public struct AttemptVaultOperation: TrackableEvent {
let type: VaultAnalyticsInputType
let amount: Double?
let slippage: Double?

public var name: String { "AttemptVaultOperation" }
public var customParameters: [String: Any] {
var dict: [String: Any] = [
"operation": type.rawValue
]
if let amount {
dict["amount"] = amount
}
if let slippage {
dict["slippage"] = slippage
}
return dict
}

public init(type: VaultAnalyticsInputType, amount: Double?, slippage: Double?) {
self.type = type
self.amount = amount
self.slippage = slippage
}
}

public struct SuccessfulVaultOperation: TrackableEvent {
let type: VaultAnalyticsInputType
let amount: Double
let amountDiff: Double

public var name: String { "SuccessfulVaultOperation" }
public var customParameters: [String: Any] {[
"operation": type.rawValue,
"amount": amount,
"amountDiff": amountDiff
]}

public init(type: VaultAnalyticsInputType, amount: Double, amountDiff: Double) {
self.type = type
self.amount = amount
self.amountDiff = amountDiff
}
}

public struct VaultOperationProtocolError: TrackableEvent {
let type: VaultAnalyticsInputType

public var name: String { "VaultOperationProtocolError" }
public var customParameters: [String: Any] {[
"operation": type.rawValue
]}

public init(type: VaultAnalyticsInputType) {
self.type = type
}
}
}

public extension TrackingProtocol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import FloatingPanel
import PlatformRouting
import dydxFormatter
import Combine
import dydxAnalytics
import class Abacus.Subaccount
import class Abacus.Vault
import class Abacus.VaultFormValidationResult
Expand Down Expand Up @@ -196,20 +197,31 @@ private class dydxVaultDepositWithdrawConfirmationViewPresenter: HostedViewPrese
switch transferType {
case .deposit:
guard let subaccountNumber = subaccount?.subaccountNumber, let amount = self?.amount else { return }
Tracking.shared?.log(event: AnalyticsEventV2.AttemptVaultOperation(type: transferType.analyticsInputType,
amount: amount,
slippage: nil))
result = await CosmoJavascript.shared.depositToMegavault(subaccountNumber: subaccountNumber, amountUsdc: amount)
case .withdraw:
guard let subaccountTo = formValidationResult.submissionData?.withdraw?.subaccountTo,
let shares = formValidationResult.submissionData?.withdraw?.shares,
let minAmount = formValidationResult.submissionData?.withdraw?.minAmount else { return }
Tracking.shared?.log(event: AnalyticsEventV2.AttemptVaultOperation(type: transferType.analyticsInputType,
amount: formValidationResult.summaryData.estimatedAmountReceived?.doubleValue,
slippage: formValidationResult.summaryData.estimatedSlippage?.doubleValue))
result = await CosmoJavascript.shared.withdrawFromMegavault(subaccountTo: subaccountTo, shares: shares, minAmount: minAmount)
}
DispatchQueue.main.async { [weak self] in
switch result {
case .success:
Tracking.shared?.log(event: AnalyticsEventV2.SuccessfulVaultOperation(type: transferType.analyticsInputType,
amount: self?.amount ?? 0,
// TODO: update
amountDiff: 0))
Router.shared?.navigate(to: RoutingRequest(path: "/action/dismiss", params: ["shouldPrioritizeDismiss": true]), animated: true, completion: nil)
AbacusStateManager.shared.refreshVaultAccount()
case .failure(let error):
self?.viewModel?.submitState = .enabled
Tracking.shared?.log(event: AnalyticsEventV2.VaultOperationProtocolError(type: transferType.analyticsInputType))
ErrorInfo.shared?.info(title: nil, message: error.message, error: error)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import PlatformRouting
import dydxFormatter
import Combine
import Abacus
import dydxAnalytics

public class dydxVaultDepositWithdrawViewBuilder: NSObject, ObjectBuilderProtocol {
public func build<T>() -> T? {
Expand Down Expand Up @@ -204,12 +205,13 @@ private class dydxVaultDepositWithdrawViewPresenter: HostedViewPresenter<dydxVau

private func updateSubmitAction(amount: Double, transferType: dydxViews.VaultTransferType) {
viewModel?.submitAction = {
Tracking.shared?.log(event: AnalyticsEventV2.VaultFormPreviewStep(amount: amount, type: transferType.analyticsInputType))
Router.shared?.navigate(to: RoutingRequest(path: transferType.confirmScreenPath, params: ["amount": amount]), animated: true, completion: nil)
}
}

private func updateErrorAlert(formValidationResult: VaultFormValidationResult) {
guard let error = formValidationResult.errors.first, formValidationResult.submissionData != nil else {
guard let error = formValidationResult.errors.first, viewModel?.amount != nil else {
viewModel?.inputInlineAlert = nil
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Utilities
import SwiftUI
import dydxAnalytics

public enum VaultTransferType: CaseIterable, RadioButtonContentDisplayable {
case deposit
Expand Down Expand Up @@ -75,4 +76,11 @@ public enum VaultTransferType: CaseIterable, RadioButtonContentDisplayable {
case .withdraw: return DataLocalizer.localize(path: "APP.VAULTS.CROSS_ACCOUNT")
}
}

public var analyticsInputType: AnalyticsEventV2.VaultAnalyticsInputType {
switch self {
case .deposit: return .deposit
case .withdraw: return .withdraw
}
}
}

0 comments on commit 6d1c7a2

Please sign in to comment.