From ce3975e839cb45eea897c4a1433a69dc73ec7687 Mon Sep 17 00:00:00 2001 From: Daniel Bernal Date: Thu, 14 Nov 2024 15:18:33 +0100 Subject: [PATCH] Move FE Overlay Pixel to the Native layer (#3550) 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 --- .../DuckPlayerTabExtension.swift | 32 +++++++++++++++++++ .../YoutubeOverlayUserScript.swift | 7 ++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift b/DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift index 85b73b3bc0..a05d0e03ba 100644 --- a/DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift +++ b/DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift @@ -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 { @@ -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 @@ -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) diff --git a/DuckDuckGo/YoutubePlayer/YoutubeOverlayUserScript.swift b/DuckDuckGo/YoutubePlayer/YoutubeOverlayUserScript.swift index 4d20a6ccd4..5342bea1ec 100644 --- a/DuckDuckGo/YoutubePlayer/YoutubeOverlayUserScript.swift +++ b/DuckDuckGo/YoutubePlayer/YoutubeOverlayUserScript.swift @@ -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 }