Skip to content

Commit

Permalink
Move FE Overlay Pixel to the Native layer (#3550)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1204099484721401/1208754606104631/f
Tech Design URL:
CC:

**Description**:
- Move the `m_mac_duck-player_overlay_youtube_impressions` pixel to the
native layer
  • Loading branch information
afterxleep authored Nov 14, 2024
1 parent 5323716 commit ce3975e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
32 changes: 32 additions & 0 deletions DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ final class DuckPlayerTabExtension {
}
}

private func fireOverlayShownPixelIfNeeded(url: URL) {

guard duckPlayer.isAvailable,
duckPlayer.mode == .alwaysAsk,
url.isYoutubeWatch else {
return
}

// Static variable for debounce logic
let debounceInterval: TimeInterval = 1.0
let now = Date()

struct Debounce {
static var lastFireTime: Date?
}

// Check debounce condition and update timestamp if firing
guard Debounce.lastFireTime == nil || now.timeIntervalSince(Debounce.lastFireTime!) >= debounceInterval else {
return
}

Debounce.lastFireTime = now
PixelKit.fire(GeneralPixel.duckPlayerOverlayYoutubeImpressions)
}
}

extension DuckPlayerTabExtension: YoutubeOverlayUserScriptDelegate {
Expand Down Expand Up @@ -191,6 +215,11 @@ extension DuckPlayerTabExtension: NavigationResponder {
return decidePolicyWithDisabledDuckPlayer(for: navigationAction)
}

// Fires the Overlay Shown Pixel if not coming from DuckPlayer's Watch in Youtube
if !navigationAction.sourceFrame.url.isDuckPlayer {
fireOverlayShownPixelIfNeeded(url: navigationAction.url)
}

// session restoration will try to load real www.youtube-nocookie.com url
// we need to redirect it to custom duck:// scheme handler which will load
// www.youtube-nocookie.com as a simulated request
Expand Down Expand Up @@ -288,6 +317,9 @@ extension DuckPlayerTabExtension: NavigationResponder {
webView.load(URLRequest(url: .duckPlayer(videoID, timestamp: timestamp)))
}

// Fire Overlay Shown Pixels
fireOverlayShownPixelIfNeeded(url: navigation.url)

// Fire DuckPlayer Overlay Temporary Pixels
if let url = navigation.request.url {
duckPlayerOverlayUsagePixels.handleNavigationAndFirePixels(url: url, duckPlayerMode: duckPlayer.mode)
Expand Down
7 changes: 5 additions & 2 deletions DuckDuckGo/YoutubePlayer/YoutubeOverlayUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ extension YoutubeOverlayUserScript {
case "play.do_not_use":
duckPlayerPreferences.youtubeOverlayAnyButtonPressed = true
PixelKit.fire(GeneralPixel.duckPlayerOverlayYoutubeWatchHere)
case "overlay":
PixelKit.fire(GeneralPixel.duckPlayerOverlayYoutubeImpressions)

// Moved to DuckPlayerTabExtension
// See :https://app.asana.com/0/1203249713006009/1208754606104659/f
// case "overlay":
// PixelKit.fire(GeneralPixel.duckPlayerOverlayYoutubeImpressions)
default:
break
}
Expand Down

0 comments on commit ce3975e

Please sign in to comment.