From 4508d822a24274a757859969e1ddc52539ce31c1 Mon Sep 17 00:00:00 2001 From: Anh Do <18567+quanganhdo@users.noreply.github.com> Date: Mon, 18 Dec 2023 09:26:32 -0500 Subject: [PATCH] Add search retention pixel for NetP (#2267) Task/Issue URL: https://app.asana.com/0/0/1205948683470045/f **Description**: Adds VPN search retention pixel. This is triggered in `TabViewController.webView(_:didFinish:)` **Steps to test this PR**: Perform a search in different scenarios and see if pixel firing is triggered (`TabViewController#L1130`) - First time VPN is installed - On subsequent launches with VPN connected - From different tabs - When VPN is toggled - etc --- Core/Pixel.swift | 2 +- DuckDuckGo/TabViewController.swift | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Core/Pixel.swift b/Core/Pixel.swift index 229598fd36..b6d16d54cf 100644 --- a/Core/Pixel.swift +++ b/Core/Pixel.swift @@ -203,7 +203,7 @@ public class Pixel { headers: headers) let request = APIRequest(configuration: configuration, urlSession: .session(useMainThreadCallbackQueue: true)) request.fetch { _, error in - os_log("Pixel fired %s %s", log: .generalLog, type: .debug, pixelName, "\(params)") + os_log("Pixel fired %{public}s %{public}s", log: .generalLog, type: .debug, pixelName, "\(params)") onComplete(error) } } diff --git a/DuckDuckGo/TabViewController.swift b/DuckDuckGo/TabViewController.swift index 9d1348546e..d798fb9502 100644 --- a/DuckDuckGo/TabViewController.swift +++ b/DuckDuckGo/TabViewController.swift @@ -35,6 +35,10 @@ import TrackerRadarKit import Networking import SecureStorage +#if NETWORK_PROTECTION +import NetworkProtection +#endif + // swiftlint:disable file_length // swiftlint:disable type_body_length class TabViewController: UIViewController { @@ -116,6 +120,12 @@ class TabViewController: UIViewController { private var trackersInfoWorkItem: DispatchWorkItem? +#if NETWORK_PROTECTION + private let netPConnectionObserver = ConnectionStatusObserverThroughSession() + private var netPConnectionObserverCancellable: AnyCancellable? + private var netPConnectionStatus: ConnectionStatus = .default +#endif + // Required to know when to disable autofill, see SaveLoginViewModel for details // Stored in memory on TabViewController for privacy reasons private var domainSaveLoginPromptLastShownOn: String? @@ -306,6 +316,10 @@ class TabViewController: UIViewController { if #available(iOS 16.4, *) { registerForInspectableWebViewNotifications() } + +#if NETWORK_PROTECTION + observeNetPConnectionStatusChanges() +#endif } @available(iOS 16.4, *) @@ -325,6 +339,12 @@ class TabViewController: UIViewController { #endif } + private func observeNetPConnectionStatusChanges() { + netPConnectionObserverCancellable = netPConnectionObserver.publisher + .receive(on: DispatchQueue.main) + .assign(to: \.netPConnectionStatus, onWeaklyHeld: self) + } + override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) // The email manager is pulled from the main view controller, so reconnect it now, otherwise, it's nil @@ -1104,6 +1124,12 @@ extension TabViewController: WKNavigationDelegate { linkProtection.setMainFrameUrl(nil) referrerTrimming.onFinishNavigation() urlProvidedBasicAuthCredential = nil + +#if NETWORK_PROTECTION + if webView.url?.isDuckDuckGoSearch == true, case .connected = netPConnectionStatus { + DailyPixel.fireDailyAndCount(pixel: .networkProtectionEnabledOnSearch) + } +#endif } func preparePreview(completion: @escaping (UIImage?) -> Void) {