Skip to content

Commit

Permalink
[URLSessionDispatcher] Added back detailed phone model feature matomo…
Browse files Browse the repository at this point in the history
…-org#253, which was removed in matomo-org#308
  • Loading branch information
Barry committed Feb 11, 2020
1 parent 39a2c96 commit 7122ec4
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions MatomoTracker/URLSessionDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,26 @@ public final class URLSessionDispatcher: Dispatcher {
}

private static func generateDefaultUserAgent(_ completion: @escaping (String) -> Void) {
let useragentSuffix = " MatomoTracker SDK URLSessionDispatcher"
let userAgentSuffix = " MatomoTracker SDK URLSessionDispatcher"
DispatchQueue.main.async {
#if os(OSX)
let webView = WebView(frame: .zero)
let userAgent = webView.stringByEvaluatingJavaScript(from: "navigator.userAgent") ?? ""
completion(userAgent.appending(useragentSuffix))
completion(userAgent.appending(userAgentSuffix))
#elseif os(iOS)
webView = WKWebView(frame: .zero)
webView?.evaluateJavaScript("navigator.userAgent") { (result, error) -> Void in
if let userAgent = result as? String {
completion(userAgent.appending(useragentSuffix))
if let regex = try? NSRegularExpression(pattern: "\\((iPad|iPhone);", options: .caseInsensitive),
let resultString = result as? String {
let userAgent = regex.stringByReplacingMatches(
in: resultString,
options: .withTransparentBounds,
range: NSRange(location: 0, length: resultString.count),
withTemplate: "(\(Device.makeCurrentDevice().platform);"
)
completion(userAgent.appending(userAgentSuffix))
} else {
completion(useragentSuffix)
completion(userAgentSuffix)
}

webView = nil
Expand Down

0 comments on commit 7122ec4

Please sign in to comment.