Skip to content

Add support for reportMetric message from CSS #6397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.duckduckgo.app.browser.commands.Command.SendResponseToJs
import com.duckduckgo.app.browser.commands.Command.SendSubscriptions
import com.duckduckgo.app.browser.commands.NavigationCommand.Navigate
import com.duckduckgo.app.pixels.AppPixelName
import com.duckduckgo.app.pixels.AppPixelName.DUCK_PLAYER_JS_ERROR
import com.duckduckgo.app.pixels.AppPixelName.DUCK_PLAYER_SETTING_ALWAYS_DUCK_PLAYER
import com.duckduckgo.app.pixels.AppPixelName.DUCK_PLAYER_SETTING_ALWAYS_OVERLAY_YOUTUBE
import com.duckduckgo.app.pixels.AppPixelName.DUCK_PLAYER_SETTING_ALWAYS_SERP
Expand Down Expand Up @@ -291,6 +292,19 @@ class DuckPlayerJSHelper @Inject constructor(
pixel.fire(impressionPixelName)
pixel.fire(dailyPixelName, emptyMap(), emptyMap(), Daily())
}
"reportMetric" -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we are leaking by default a lot of implementation details and using DuckPlayerPageHandler as a bypass rather than extracting messages to their own handler. Then all these messages have a meaningless trip around the tab fragment, view model, js helper, etc.
This is a perfect example of that, you could do something like this instead which will keep all the logic in impl (I would actually do an internal plugin just for duck player to not have a lot of inner classes and sort out DI but this works for now anyways).

inner class DuckPlayerReportMetricHandler(private val pixel: Pixel) : JsMessageHandler {
        override fun process(jsMessage: JsMessage, secret: String, jsMessageCallback: JsMessageCallback?) {
            try {
                val params = jsMessage.params
                val message = params.getString("message") ?: return
                val kind = params.getString("kind") ?: return
                pixel.fire(
                    DUCK_PLAYER_JS_ERROR,
                    mapOf("message" to message, "kind" to kind, "origin" to featureName),
                )
            } catch (e: Exception) {
                logcat { "Error reporting metric: $e" }
            }
        }

        override val allowedDomains: List<String> = listOf(
            runBlocking { duckPlayer.getYouTubeEmbedUrl() },
        )
        override val featureName: String = "duckPlayerPage"
        override val methods: List<String> = listOf(
            "reportMetric",
        )
    }

try {
val params = data?.getJSONObject("params") ?: return null
val message = params.getString("message") ?: return null
val kind = params.getString("kind") ?: return null
pixel.fire(
DUCK_PLAYER_JS_ERROR,
mapOf("message" to message, "kind" to kind, "origin" to featureName),
)
} catch (e: Exception) {
logcat { "Error reporting metric: $e" }
}
}
else -> {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ enum class AppPixelName(override val pixelName: String) : Pixel.PixelName {
DUCK_PLAYER_YOUTUBE_ERROR_AGE_RESTRICTED_DAILY_UNIQUE("duckplayer_youtube-age-restricted-error_daily-unique"),
DUCK_PLAYER_YOUTUBE_ERROR_NO_EMBED_DAILY_UNIQUE("duckplayer_youtube-no-embed-error_daily-unique"),
DUCK_PLAYER_YOUTUBE_ERROR_UNKNOWN_DAILY_UNIQUE("duckplayer_youtube-unknown-error_daily-unique"),
DUCK_PLAYER_JS_ERROR("duckplayer_js-error"),

MALICIOUS_SITE_PROTECTION_SETTING_TOGGLED("m_malicious-site-protection_feature-toggled"),
MALICIOUS_SITE_PROTECTION_VISIT_SITE("m_malicious-site-protection_visit-site"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class DuckPlayerScriptsJsMessaging @Inject constructor(
"openSettings",
"openInfo",
"setUserValues",
"reportMetric",
"reportPageException",
"reportInitException",
"reportYouTubeError",
Expand Down
Loading