From e1d929c3805639fb4be173dcc7e86dd7061a0b48 Mon Sep 17 00:00:00 2001 From: Mpendulo Ndlovu Date: Wed, 2 Oct 2024 09:41:31 +0200 Subject: [PATCH] chore: update analytics tracking --- Example/Tests/MockCommClient.swift | 2 ++ README.md | 2 +- .../Classes/API/Endpoint.swift | 2 +- .../CommunicationLayer/CommClient.swift | 1 + .../DeeplinkCommLayer/DeeplinkClient.swift | 14 +++++++-- .../SocketCommLayer/SocketClient.swift | 3 +- .../Classes/Ethereum/Ethereum.swift | 30 ++++++++++++++----- metamask-ios-sdk.podspec | 2 +- 8 files changed, 41 insertions(+), 15 deletions(-) diff --git a/Example/Tests/MockCommClient.swift b/Example/Tests/MockCommClient.swift index defc2e9..d879e81 100644 --- a/Example/Tests/MockCommClient.swift +++ b/Example/Tests/MockCommClient.swift @@ -7,6 +7,8 @@ import metamask_ios_sdk import XCTest class MockCommClient: CommClient { + var channelId: String = "randomId" + var connectCalled = false var sendMessageCalled = false var disConnectCalled = false diff --git a/README.md b/README.md index fd59860..da684f8 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Alternatively, you can add the URL directly in your project's package file: dependencies: [ .package( url: "https://github.com/MetaMask/metamask-ios-sdk", - from: "0.8.9" + from: "0.8.10" ) ] ``` diff --git a/Sources/metamask-ios-sdk/Classes/API/Endpoint.swift b/Sources/metamask-ios-sdk/Classes/API/Endpoint.swift index 44d0bb2..dd60158 100644 --- a/Sources/metamask-ios-sdk/Classes/API/Endpoint.swift +++ b/Sources/metamask-ios-sdk/Classes/API/Endpoint.swift @@ -12,7 +12,7 @@ public enum Endpoint { public var url: String { switch self { case .analytics: - return Endpoint.SERVER_URL.appending("debug") + return Endpoint.SERVER_URL.appending("evt") } } } diff --git a/Sources/metamask-ios-sdk/Classes/CommunicationLayer/CommClient.swift b/Sources/metamask-ios-sdk/Classes/CommunicationLayer/CommClient.swift index f3087b6..6095943 100644 --- a/Sources/metamask-ios-sdk/Classes/CommunicationLayer/CommClient.swift +++ b/Sources/metamask-ios-sdk/Classes/CommunicationLayer/CommClient.swift @@ -8,6 +8,7 @@ import Foundation public typealias RequestJob = () -> Void public protocol CommClient { + var channelId: String { get set } var appMetadata: AppMetadata? { get set } var sessionDuration: TimeInterval { get set } var onClientsTerminated: (() -> Void)? { get set } diff --git a/Sources/metamask-ios-sdk/Classes/CommunicationLayer/DeeplinkCommLayer/DeeplinkClient.swift b/Sources/metamask-ios-sdk/Classes/CommunicationLayer/DeeplinkCommLayer/DeeplinkClient.swift index 128564e..b1434f4 100644 --- a/Sources/metamask-ios-sdk/Classes/CommunicationLayer/DeeplinkCommLayer/DeeplinkClient.swift +++ b/Sources/metamask-ios-sdk/Classes/CommunicationLayer/DeeplinkCommLayer/DeeplinkClient.swift @@ -9,7 +9,7 @@ import Foundation public class DeeplinkClient: CommClient { private let session: SessionManager - var channelId: String = "" + public var channelId: String = "" let dappScheme: String let urlOpener: URLOpener @@ -20,6 +20,7 @@ public class DeeplinkClient: CommClient { let keyExchange: KeyExchange let deeplinkManager: DeeplinkManager + private var isConnecting = false public var sessionDuration: TimeInterval { get { @@ -93,13 +94,13 @@ public class DeeplinkClient: CommClient { let account = options["account"] ?? "" let chainId = options["chainId"] ?? "" let message = "mmsdk?scheme=\(dappScheme)&message=\(message)&channelId=\(channelId ?? "")&account=\(account)@\(chainId)" - Logging.log("DeeplinkClient:: Sending message \(message)") sendMessage(message) } } public func connect(with request: String? = nil) { track(event: .connectionRequest) + isConnecting = true sendMessage(.connect( pubkey: nil, @@ -111,7 +112,7 @@ public class DeeplinkClient: CommClient { public func track(event: Event) { let parameters: [String: Any] = [ "id": channelId, - "commLayer": "socket", + "commLayer": "deeplinking", "sdkVersion": SDKInfo.version, "url": appMetadata?.url ?? "", "dappId": SDKInfo.bundleIdentifier ?? "N/A", @@ -183,6 +184,13 @@ public class DeeplinkClient: CommClient { Logging.log("DeeplinkClient:: Ignoring response \(json)") return } + if + isConnecting, + data["accounts"] != nil, + data["chainId"] != nil { + isConnecting = false + track(event: .connected) + } handleResponse?(data) } catch { Logging.error("DeeplinkClient:: Could not convert message to json. Message: \(message)\nError: \(error)") diff --git a/Sources/metamask-ios-sdk/Classes/CommunicationLayer/SocketCommLayer/SocketClient.swift b/Sources/metamask-ios-sdk/Classes/CommunicationLayer/SocketCommLayer/SocketClient.swift index 12f697d..d7175ba 100644 --- a/Sources/metamask-ios-sdk/Classes/CommunicationLayer/SocketCommLayer/SocketClient.swift +++ b/Sources/metamask-ios-sdk/Classes/CommunicationLayer/SocketCommLayer/SocketClient.swift @@ -14,7 +14,7 @@ public class SocketClient: CommClient { private let channel: SocketChannel let urlOpener: URLOpener - var channelId: String = "" + public var channelId: String = "" public var isConnected: Bool { channel.isConnected @@ -234,6 +234,7 @@ extension SocketClient { if isV2Protocol { isReady = true Logging.log("SocketClient:: Channel supports protocol v2 communation") + track(event: .connected) } } } diff --git a/Sources/metamask-ios-sdk/Classes/Ethereum/Ethereum.swift b/Sources/metamask-ios-sdk/Classes/Ethereum/Ethereum.swift index 7765449..52b83a3 100644 --- a/Sources/metamask-ios-sdk/Classes/Ethereum/Ethereum.swift +++ b/Sources/metamask-ios-sdk/Classes/Ethereum/Ethereum.swift @@ -122,8 +122,22 @@ public class Ethereum { return self } - private func trackEvent(event: Event, parameters: [String: Any]) { - track?(event, parameters) + private func trackEvent(_ event: Event, parameters: [String: Any] = [:]) { + var params: [String: Any] = [ + "id": commClient.channelId, + "commLayer": commClient is SocketClient ? "socket" : "deeplinking", + "sdkVersion": SDKInfo.version, + "url": appMetadata?.url ?? "", + "dappId": SDKInfo.bundleIdentifier ?? "N/A", + "title": appMetadata?.name ?? "", + "platform": SDKInfo.platform + ] + + for (key, value) in parameters { + params[key] = value + } + + track?(event, params) } func updateMetadata(_ metadata: AppMetadata) { @@ -465,7 +479,7 @@ public class Ethereum { func terminateConnection() { if connected { - track?(.connectionTerminated, [:]) + trackEvent(.connectionTerminated) } let error = RequestError(from: ["message": "The connection request has been rejected"]) @@ -504,7 +518,7 @@ public class Ethereum { } } } else { - track?(.sdkRpcRequest, [ + trackEvent(.sdkRpcRequest, parameters: [ "from": "mobile", "method": request.method ]) @@ -723,7 +737,7 @@ public class Ethereum { func receiveResponse(_ data: [String: Any], id: String) { guard let request = getRequest(id: id) else { return } - track?(.sdkRpcRequestDone, [ + trackEvent(.sdkRpcRequestDone, parameters: [ "from": "mobile", "method": request.method ]) @@ -734,7 +748,7 @@ public class Ethereum { if method == .ethRequestAccounts, requestError.codeType == .userRejectedRequest { - track?(.connectionRejected, [:]) + trackEvent(.connectionRejected) } sendError(requestError, id: id) @@ -783,7 +797,7 @@ public class Ethereum { case .ethRequestAccounts: let result: [String] = data["result"] as? [String] ?? [] if let account = result.first { - track?(.connectionAuthorised, [:]) + trackEvent(.connectionAuthorised) updateAccount(account) sendResult(result, id: id) } else { @@ -850,7 +864,7 @@ public class Ethereum { let requestError = RequestError(from: error) if requestError.codeType == .userRejectedRequest { - track?(.connectionRejected, [:]) + trackEvent(.connectionRejected) } sendError(requestError, id: Ethereum.CONNECTION_ID) } diff --git a/metamask-ios-sdk.podspec b/metamask-ios-sdk.podspec index 21dc7c0..c793ec5 100644 --- a/metamask-ios-sdk.podspec +++ b/metamask-ios-sdk.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'metamask-ios-sdk' - s.version = '0.8.9' + s.version = '0.8.10' s.summary = 'Enable users to easily connect with their MetaMask Mobile wallet.' s.swift_version = '5.5'