-
Notifications
You must be signed in to change notification settings - Fork 129
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
RUM-4313 feat: Add WebViewTracking
for Obj-c
#1854
Merged
ncreated
merged 6 commits into
develop
from
ncreated/RUM-4313/add-webview-tracking-for-obj-c
May 21, 2024
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
be82043
RUM-4313 Add WebViewTracking for Obj-c
ncreated 53536d5
RUM-4313 Update CHANGELOG.md
ncreated 1f8fc89
RUM-4313 Help compiler infering return value
ncreated a02c34d
RUM-4313 CR feedback - apply common convention for Objc interface
ncreated 29af80b
RUM-4313 CR feedback - move WVT+objc to `ObjC` folder to conform with…
ncreated bf86391
Merge branch 'develop' into ncreated/RUM-4313/add-webview-tracking-fo…
ncreated File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,5 +134,4 @@ - (void)testDDRUMConfigurationAPI { | |
|
||
#pragma clang diagnostic pop | ||
|
||
|
||
@end |
50 changes: 50 additions & 0 deletions
50
DatadogCore/Tests/DatadogObjc/ObjcAPITests/DDWebViewTracking+apiTests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
#import <XCTest/XCTest.h> | ||
@import DatadogWebViewTracking; | ||
@import WebKit; | ||
|
||
@interface WebViewMock: WKWebView | ||
@end | ||
|
||
@implementation WebViewMock | ||
@end | ||
|
||
// MARK: - DDWebViewTracking tests | ||
|
||
@interface DDWebViewTracking_apiTests : XCTestCase | ||
@end | ||
|
||
/* | ||
* `WebViewTracking` APIs smoke tests - minimal assertions, mainly check if the interface is available to Objc. | ||
*/ | ||
@implementation DDWebViewTracking_apiTests | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wunused-value" | ||
|
||
- (void)testDDWebViewTrackingAPI { | ||
WebViewMock *webView = [WebViewMock new]; | ||
[DDWebViewTracking enableWithWebView:webView | ||
hosts:[NSSet<NSString*> setWithArray:@[@"host1.com", @"host2.com"]] | ||
logsSampleRate:100.0 | ||
with:nil]; | ||
[DDWebViewTracking disableWithWebView:webView]; | ||
} | ||
|
||
- (void)testDDWebViewTrackingSessionReplayConfigurationAPI { | ||
DDWebViewTrackingSessionReplayConfiguration *config = [[DDWebViewTrackingSessionReplayConfiguration alloc] init]; | ||
XCTAssertEqual(config.privacyLevel, DDPrivacyLevelMask); | ||
config.privacyLevel = DDPrivacyLevelAllow; | ||
XCTAssertEqual(config.privacyLevel, DDPrivacyLevelAllow); | ||
config.privacyLevel = DDPrivacyLevelMaskUserInput; | ||
XCTAssertEqual(config.privacyLevel, DDPrivacyLevelMaskUserInput); | ||
} | ||
|
||
#pragma clang diagnostic pop | ||
|
||
@end |
113 changes: 113 additions & 0 deletions
113
DatadogWebViewTracking/Sources/WebViewTracking+objc.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
#if os(tvOS) | ||
#warning("Datadog WebView Tracking does not support tvOS") | ||
#else | ||
import WebKit | ||
#endif | ||
|
||
@objc | ||
public final class DDWebViewTracking: NSObject { | ||
override private init() { } | ||
|
||
/// Enables SDK to correlate Datadog RUM events and Logs from the WebView with native RUM session. | ||
/// | ||
/// If the content loaded in WebView uses Datadog Browser SDK (`v4.2.0+`) and matches specified | ||
/// `hosts`, web events will be correlated with the RUM session from native SDK. | ||
/// | ||
/// - Parameters: | ||
/// - webView: The web-view to track. | ||
/// - hosts: A set of hosts instrumented with Browser SDK to capture Datadog events from. | ||
/// - logsSampleRate: The sampling rate for logs coming from the WebView. Must be a value between `0` and `100`, | ||
/// where 0 means no logs will be sent and 100 means all will be uploaded. Default: `100`. | ||
/// - sessionReplayConfiguration: Session Replay configuration to enable linking Web and Native replays. | ||
/// - core: Datadog SDK core to use for tracking. | ||
@objc | ||
public static func enable( | ||
webView: WKWebView, | ||
hosts: Set<String> = [], | ||
logsSampleRate: Float = 100, | ||
with configuration: DDWebViewTrackingSessionReplayConfiguration? = nil | ||
) { | ||
WebViewTracking.enable( | ||
webView: webView, | ||
hosts: hosts, | ||
logsSampleRate: logsSampleRate, | ||
sessionReplayConfiguration: configuration?.toSwift | ||
) | ||
} | ||
|
||
/// Disables Datadog iOS SDK and Datadog Browser SDK integration. | ||
/// | ||
/// Removes Datadog's ScriptMessageHandler and UserScript from the caller. | ||
/// - Note: This method **must** be called when the webview can be deinitialized. | ||
/// | ||
/// - Parameter webView: The web-view to stop tracking. | ||
@objc | ||
public static func disable( | ||
webView: WKWebView | ||
) { | ||
WebViewTracking.disable(webView: webView) | ||
} | ||
} | ||
|
||
/// The Session Replay configuration to capture records coming from the web view. | ||
/// | ||
/// Setting the Session Replay configuration in `WebViewTracking` will enable transmitting replay data from | ||
/// the Datadog Browser SDK installed in the web page. Datadog will then be able to combine the native | ||
/// and web recordings in a single replay. | ||
@objc | ||
public final class DDWebViewTrackingSessionReplayConfiguration: NSObject { | ||
/// Available privacy levels for content masking. | ||
@objc | ||
public enum DDPrivacyLevel: Int { | ||
/// Record all content. | ||
case allow | ||
/// Mask all content. | ||
case mask | ||
/// Mask input elements, but record all other content. | ||
case maskUserInput | ||
|
||
internal var toSwift: WebViewTracking.SessionReplayConfiguration.PrivacyLevel { | ||
switch self { | ||
case .allow: return .allow | ||
case .mask: return .mask | ||
case .maskUserInput: return .maskUserInput | ||
} | ||
} | ||
} | ||
|
||
/// The privacy level to use for the web view replay recording. | ||
@objc public var privacyLevel: DDPrivacyLevel | ||
|
||
/// Creates Webview Session Replay configuration. | ||
/// | ||
/// - Parameters: | ||
/// - privacyLevel: The way sensitive content (e.g. text) should be masked. Default: `.mask`. | ||
@objc | ||
override public init() { | ||
self.privacyLevel = .mask | ||
} | ||
|
||
/// Creates Webview Session Replay configuration. | ||
/// | ||
/// - Parameters: | ||
/// - privacyLevel: The way sensitive content (e.g. text) should be masked. Default: `.mask`. | ||
@objc | ||
public init( | ||
privacyLevel: DDPrivacyLevel | ||
) { | ||
self.privacyLevel = privacyLevel | ||
} | ||
|
||
internal var toSwift: WebViewTracking.SessionReplayConfiguration { | ||
return .init( | ||
privacyLevel: privacyLevel.toSwift | ||
) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/change-request We aim to hide these interfaces from Swift autocompletion. As stated in the RFC, the interfaces should read:
@objc(DDWebViewTracking) @_spi(objc) public final class objc_WebViewTracking: NSObject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for flagging this 💪! I forgotten that we established this convention. Now everything should be up-to-date, including the PR comment and
+objc
file location ✅.