Skip to content

Commit

Permalink
[DuckPlayer] 18. Pixel Updates (#3216)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1204099484721401/1208019748580094/f

**Description**:
- Adds more missing pixels
- Renames DP pixels from `m_duck-player*` to `duckplayer*`
- Moves DuckPlayer Settings save to a background thread
  • Loading branch information
afterxleep authored Aug 12, 2024
1 parent b182c13 commit 2f38c5f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 30 deletions.
30 changes: 17 additions & 13 deletions Core/PixelEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,8 @@ extension Pixel {
case duckPlayerSettingNeverSettings
case duckPlayerSettingBackToDefault
case duckPlayerWatchOnYoutube
case duckPlayerSettingAlwaysOverlayYoutube
case duckPlayerSettingNeverOverlayYoutube
case duckPlayerContingencySettingsDisplayed
case duckPlayerContingencyLearnMoreClicked
}
Expand Down Expand Up @@ -1483,19 +1485,21 @@ extension Pixel.Event {
case .newTabPageDisplayedDaily: return "m_new_tab_page_displayed_daily"

// MARK: DuckPlayer
case .duckPlayerDailyUniqueView: return "m_duck-player_daily-unique-view"
case .duckPlayerViewFromYoutubeViaMainOverlay: return "m_duck-player_view-from_youtube_main-overlay"
case .duckPlayerViewFromYoutubeViaHoverButton: return "m_duck-player_view-from_youtube_hover-button"
case .duckPlayerViewFromYoutubeAutomatic: return "m_duck-player_view-from_youtube_automatic"
case .duckPlayerViewFromSERP: return "m_duck-player_view-from_serp"
case .duckPlayerViewFromOther: return "m_duck-player_view-from_other"
case .duckPlayerSettingAlwaysSettings: return "m_duck-player_setting_always_settings"
case .duckPlayerSettingAlwaysDuckPlayer: return "m_duck-player_setting_always_duck-player"
case .duckPlayerOverlayYoutubeImpressions: return "m_duck-player_overlay_youtube_impressions"
case .duckPlayerOverlayYoutubeWatchHere: return "m_duck-player_overlay_youtube_watch_here"
case .duckPlayerSettingNeverSettings: return "m_duck-player_setting_never_settings"
case .duckPlayerSettingBackToDefault: return "m_duck-player_setting_back-to-default"
case .duckPlayerWatchOnYoutube: return "m_duck-player_watch_on_youtube"
case .duckPlayerDailyUniqueView: return "duckplayer_daily-unique-view"
case .duckPlayerViewFromYoutubeViaMainOverlay: return "duckplayer_view-from_youtube_main-overlay"
case .duckPlayerViewFromYoutubeViaHoverButton: return "duckplayer_view-from_youtube_hover-button"
case .duckPlayerViewFromYoutubeAutomatic: return "duckplayer_view-from_youtube_automatic"
case .duckPlayerViewFromSERP: return "duckplayer_view-from_serp"
case .duckPlayerViewFromOther: return "duckplayer_view-from_other"
case .duckPlayerSettingAlwaysSettings: return "duckplayer_setting_always_settings"
case .duckPlayerSettingAlwaysDuckPlayer: return "duckplayer_setting_always_duck-player"
case .duckPlayerOverlayYoutubeImpressions: return "duckplayer_overlay_youtube_impressions"
case .duckPlayerOverlayYoutubeWatchHere: return "duckplayer_overlay_youtube_watch_here"
case .duckPlayerSettingNeverSettings: return "duckplayer_setting_never_settings"
case .duckPlayerSettingBackToDefault: return "duckplayer_setting_back-to-default"
case .duckPlayerWatchOnYoutube: return "duckplayer_watch_on_youtube"
case .duckPlayerSettingAlwaysOverlayYoutube: return "duckplayer_setting_always_overlay_youtube"
case .duckPlayerSettingNeverOverlayYoutube: return "duckplayer_setting_never_overlay_youtube"
case .duckPlayerContingencySettingsDisplayed: return "duckplayer_ios_contingency_settings-displayed"
case .duckPlayerContingencyLearnMoreClicked: return "duckplayer_ios_contingency_learn-more-clicked"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "0fbc8848e389af3bb55c182bc19ca9d5dc2f255b",
"version" : "1.4.0"
Expand Down
55 changes: 43 additions & 12 deletions DuckDuckGo/DuckPlayer/DuckPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,25 @@ protocol DuckPlayerProtocol: AnyObject {

final class DuckPlayer: DuckPlayerProtocol {

static let duckPlayerHost: String = "player"
static let commonName = "Duck Player"

struct Constants {
static let duckPlayerHost: String = "player"
static let commonName = "Duck Player"
}

private(set) var settings: DuckPlayerSettingsProtocol
private(set) weak var hostView: UIViewController?

private struct WKMessageData: Codable {
var context: String?
var featureName: String?
var method: String?
}

private enum FeatureName: String {
case page = "duckPlayerPage"
case overlay = "duckPlayer"
}

init(settings: DuckPlayerSettingsProtocol = DuckPlayerSettings()) {
self.settings = settings
}
Expand All @@ -122,20 +135,22 @@ final class DuckPlayer: DuckPlayerProtocol {
assertionFailure("DuckPlayer: expected JSON representation of UserValues")
return nil
}
settings.setMode(userValues.duckPlayerMode)
settings.setOverlayHidden(userValues.askModeOverlayHidden)

// Fire Pixels
switch userValues.duckPlayerMode {
case .enabled:
Pixel.fire(pixel: Pixel.Event.duckPlayerSettingAlwaysDuckPlayer, debounce: 2)
default:
break
Task {
// Fires pixels
await firePixels(message: message, userValues: userValues)

// Update Settings
await updateSettings(userValues: userValues)
}

return userValues
}

private func updateSettings(userValues: UserValues) async {
settings.setMode(userValues.duckPlayerMode)
settings.setOverlayHidden(userValues.askModeOverlayHidden)
}

public func getUserValues(params: Any, message: WKScriptMessage) -> Encodable? {
encodeUserValues()
}
Expand Down Expand Up @@ -202,4 +217,20 @@ final class DuckPlayer: DuckPlayerProtocol {
return InitialOverlaySettings(userValues: userValues)
}

// Accessing WKMessage needs main thread
@MainActor
private func firePixels(message: WKScriptMessage, userValues: UserValues) {

guard let messageData: WKMessageData = DecodableHelper.decode(from: message.body) else {
assertionFailure("DuckPlayer: expected JSON representation of Message")
return
}
guard let feature = messageData.featureName else { return }
let event: Pixel.Event = feature == FeatureName.page.rawValue ? .duckPlayerSettingAlwaysDuckPlayer : .duckPlayerSettingAlwaysDuckPlayer
if userValues.duckPlayerMode == .enabled {
Pixel.fire(pixel: event)
}

}

}
11 changes: 7 additions & 4 deletions DuckDuckGo/DuckPlayer/YoutubeOverlayUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,18 @@ extension YoutubeOverlayUserScript {

switch pixelName {
case "play.use":
Pixel.fire(pixel: Pixel.Event.duckPlayerViewFromYoutubeViaMainOverlay)
Pixel.fire(pixel: Pixel.Event.duckPlayerViewFromYoutubeViaMainOverlay, debounce: 2)
duckPlayerStorage.userInteractedWithDuckPlayer = true


case "play.use.thumbnail":
Pixel.fire(pixel: Pixel.Event.duckPlayerViewFromYoutubeViaHoverButton, debounce: 2)

case "play.do_not_use":
Pixel.fire(pixel: Pixel.Event.duckPlayerOverlayYoutubeWatchHere)
Pixel.fire(pixel: Pixel.Event.duckPlayerOverlayYoutubeWatchHere, debounce: 2)
duckPlayerStorage.userInteractedWithDuckPlayer = true

case "overlay":
Pixel.fire(pixel: Pixel.Event.duckPlayerOverlayYoutubeImpressions)
Pixel.fire(pixel: Pixel.Event.duckPlayerOverlayYoutubeImpressions, debounce: 2)

default:
break
Expand Down

0 comments on commit 2f38c5f

Please sign in to comment.