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

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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 @@ -33,4 +33,5 @@ enum class DuckPlayerPixelName(override val pixelName: String) : Pixel.PixelName
DUCK_PLAYER_SETTINGS_PRESSED("duckplayer_setting_pressed"),
DUCK_PLAYER_NEWTAB_SETTING_ON("duckplayer_newtab_setting-on"),
DUCK_PLAYER_NEWTAB_SETTING_OFF("duckplayer_newtab_setting-off"),
DUCK_PLAYER_JS_ERROR("duckplayer_js-error"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package com.duckduckgo.duckplayer.impl
import android.webkit.JavascriptInterface
import android.webkit.WebView
import androidx.core.net.toUri
import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.common.utils.extensions.toTldPlusOne
import com.duckduckgo.di.scopes.ActivityScope
import com.duckduckgo.duckplayer.impl.DuckPlayerPixelName.DUCK_PLAYER_JS_ERROR
import com.duckduckgo.js.messaging.api.JsCallbackData
import com.duckduckgo.js.messaging.api.JsMessage
import com.duckduckgo.js.messaging.api.JsMessageCallback
Expand All @@ -44,6 +46,7 @@ class DuckPlayerScriptsJsMessaging @Inject constructor(
private val jsMessageHelper: JsMessageHelper,
private val dispatcherProvider: DispatcherProvider,
private val duckPlayer: DuckPlayerInternal,
pixel: Pixel,
) : JsMessaging {
private val moshi = Moshi.Builder().add(JSONObjectAdapter()).build()

Expand All @@ -52,6 +55,8 @@ class DuckPlayerScriptsJsMessaging @Inject constructor(

private val handlers = listOf(
DuckPlayerPageHandler(),
DuckPlayerReportMetricHandler(pixel),
DuckPlayerPageReportMetricHandler(pixel),
)

@JavascriptInterface
Expand Down Expand Up @@ -134,4 +139,32 @@ class DuckPlayerScriptsJsMessaging @Inject constructor(
"reportYouTubeError",
)
}

internal interface GenericDuckPlayerReportMetricHandler : JsMessageHandler {
val pixel: Pixel

override fun process(jsMessage: JsMessage, secret: String, jsMessageCallback: JsMessageCallback?) {
try {
val params = jsMessage.params.getJSONObject("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" }
}
}
}

inner class DuckPlayerReportMetricHandler(override val pixel: Pixel) : GenericDuckPlayerReportMetricHandler {

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

inner class DuckPlayerPageReportMetricHandler(override val pixel: Pixel) : GenericDuckPlayerReportMetricHandler {
override val allowedDomains: List<String> = listOf(runBlocking { duckPlayer.getYouTubeEmbedUrl() })
override val featureName: String = "duckPlayerPage"
override val methods: List<String> = listOf("reportMetric")
}
}
Loading