Skip to content

Commit

Permalink
[URLSessionDispatcher] Added back real device model in user agent fea…
Browse files Browse the repository at this point in the history
…ture matomo-org#253, which was removed in matomo-org#308
  • Loading branch information
Barry committed Mar 2, 2020
1 parent 22b5eae commit 5ab506c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
* **improvement** Added back detailed phone model feature #253. [#330](https://github.com/matomo-org/matomo-sdk-ios/pull/330)

## 7.2.0
* **feature** Added support for the Swift Package Manager. [#312](https://github.com/matomo-org/matomo-sdk-ios/pull/312)
Expand Down
19 changes: 13 additions & 6 deletions MatomoTracker/URLSessionDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,32 @@ 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
}
#elseif os(tvOS)
completion(useragentSuffix)
completion(userAgentSuffix)
#endif
}
}
Expand Down

0 comments on commit 5ab506c

Please sign in to comment.