Skip to content

Commit

Permalink
Merge pull request #88 from MetaMask/update-analytics
Browse files Browse the repository at this point in the history
Update analytics
  • Loading branch information
elefantel authored Sep 21, 2023
2 parents ec7db63 + 94d397a commit e744806
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions Sources/metamask-ios-sdk/Classes/Analytics/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public enum Event: String {
case connectionRequest = "sdk_connect_request_started"
case reconnectionRequest = "sdk_reconnect_request_started"
case connected = "sdk_connection_established"
case connectionAuthorised = "sdk_connection_authorized"
case connectionRejected = "sdk_connection_rejected"
Expand Down
19 changes: 13 additions & 6 deletions Sources/metamask-ios-sdk/Classes/Communication/SocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class SocketClient: CommunicationClient {
}

var isConnected: Bool = false
private var isReconnection = false
var tearDownConnection: (() -> Void)?
var onClientsTerminated: (() -> Void)?

Expand Down Expand Up @@ -111,7 +112,11 @@ class SocketClient: CommunicationClient {
guard !channel.isConnected else { return }

setupClient()
trackEvent(.connectionRequest)
if isReconnection {
trackEvent(.reconnectionRequest)
} else {
trackEvent(.connectionRequest)
}
channel.connect()
}

Expand All @@ -129,6 +134,7 @@ class SocketClient: CommunicationClient {
private func configureSession() {
if let config = fetchSessionConfig(), config.isValid {
channelId = config.sessionId
isReconnection = true
} else {
// purge any existing session info
store.deleteData(for: SESSION_KEY)
Expand Down Expand Up @@ -185,12 +191,9 @@ private extension SocketClient {

// MARK: Clients connected event

channel.on(ClientEvent.clientsConnected(on: channelId)) { [weak self] data in
guard let self = self else { return }
channel.on(ClientEvent.clientsConnected(on: channelId)) { data in
Logging.log("Clients connected: \(data)")

self.trackEvent(.connected)

// for debug purposes only
NotificationCenter.default.post(
name: NSNotification.Name("connection"),
Expand Down Expand Up @@ -303,7 +306,10 @@ private extension SocketClient {
guard
let keyExchangeMessage = Message<KeyExchangeMessage>.message(from: message),
let nextKeyExchangeMessage = keyExchange.nextMessage(keyExchangeMessage.message)
else { return }
else {
trackEvent(.connected)
return
}

sendMessage(nextKeyExchangeMessage, encrypt: false)

Expand Down Expand Up @@ -470,6 +476,7 @@ extension SocketClient {
switch event {
case .connected,
.disconnected,
.reconnectionRequest,
.connectionAuthorised,
.connectionRejected:
break
Expand Down
5 changes: 4 additions & 1 deletion Sources/metamask-ios-sdk/Classes/Ethereum/Ethereum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public extension Ethereum {
}

func terminateConnection() {
delegate?.trackEvent(.connectionRejected)
if connected {
delegate?.trackEvent(.connectionRejected)
}

let error = RequestError(from: ["message": "The connection request has been rejected"])
submittedRequests.forEach { key, value in
submittedRequests[key]?.error(error)
Expand Down

0 comments on commit e744806

Please sign in to comment.