Skip to content

Commit

Permalink
Add search retention pixel for NetP (#2267)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
quanganhdo authored Dec 18, 2023
1 parent b5ee323 commit 4508d82
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Core/Pixel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
26 changes: 26 additions & 0 deletions DuckDuckGo/TabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -306,6 +316,10 @@ class TabViewController: UIViewController {
if #available(iOS 16.4, *) {
registerForInspectableWebViewNotifications()
}

#if NETWORK_PROTECTION
observeNetPConnectionStatusChanges()
#endif
}

@available(iOS 16.4, *)
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4508d82

Please sign in to comment.