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

chore: update analytics tracking #191

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Example/Tests/MockCommClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import metamask_ios_sdk
import XCTest

class MockCommClient: CommClient {
var channelId: String = "randomId"
elefantel marked this conversation as resolved.
Show resolved Hide resolved

var connectCalled = false
var sendMessageCalled = false
var disConnectCalled = false
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
]
```
Expand Down
2 changes: 1 addition & 1 deletion Sources/metamask-ios-sdk/Classes/API/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -20,6 +20,7 @@ public class DeeplinkClient: CommClient {

let keyExchange: KeyExchange
let deeplinkManager: DeeplinkManager
private var isConnecting = false

public var sessionDuration: TimeInterval {
get {
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -234,6 +234,7 @@ extension SocketClient {
if isV2Protocol {
isReady = true
Logging.log("SocketClient:: Channel supports protocol v2 communation")
track(event: .connected)
}
}
}
Expand Down
30 changes: 22 additions & 8 deletions Sources/metamask-ios-sdk/Classes/Ethereum/Ethereum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -504,7 +518,7 @@ public class Ethereum {
}
}
} else {
track?(.sdkRpcRequest, [
trackEvent(.sdkRpcRequest, parameters: [
"from": "mobile",
"method": request.method
])
Expand Down Expand Up @@ -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
])
Expand All @@ -734,7 +748,7 @@ public class Ethereum {
if
method == .ethRequestAccounts,
requestError.codeType == .userRejectedRequest {
track?(.connectionRejected, [:])
trackEvent(.connectionRejected)
}
sendError(requestError, id: id)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion metamask-ios-sdk.podspec
Original file line number Diff line number Diff line change
@@ -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'

Expand Down