From 04e9da4065fd153b7601603d6d1144fed5bfda00 Mon Sep 17 00:00:00 2001 From: rxumoz Date: Wed, 9 Jun 2021 05:59:14 +0800 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/10300 - Fix Baidu search telemetry (https://github.com/mozilla-mobile/fenix/pull/19127) --- .../assets/extensions/ads/manifest.template.json | 2 ++ .../extensions/cookies/manifest.template.json | 4 ++-- .../search/telemetry/BaseSearchTelemetry.kt | 8 ++++---- .../org/mozilla/fenix/search/telemetry/Utils.kt | 16 +++++++++++++++- .../incontent/InContentTelemetryTest.kt | 10 +++++----- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/src/main/assets/extensions/ads/manifest.template.json b/app/src/main/assets/extensions/ads/manifest.template.json index 8f4ef48f6a..b098b593b1 100644 --- a/app/src/main/assets/extensions/ads/manifest.template.json +++ b/app/src/main/assets/extensions/ads/manifest.template.json @@ -13,6 +13,8 @@ "include_globs": [ "https://www.google.*/search*", "https://www.bing.com/search*", + "https://www.baidu.com/*", + "https://m.baidu.com/*", "https://duckduckgo.com/*" ], "js": ["ads.js"], diff --git a/app/src/main/assets/extensions/cookies/manifest.template.json b/app/src/main/assets/extensions/cookies/manifest.template.json index 06440bccfa..f202a32f16 100644 --- a/app/src/main/assets/extensions/cookies/manifest.template.json +++ b/app/src/main/assets/extensions/cookies/manifest.template.json @@ -12,8 +12,8 @@ "matches": ["https://*/*"], "include_globs": [ "https://www.google.*/search*", - "https://www.baidu.com/from=844b/s*", - "https://www.baidu.com/from=844b/baidu*", + "https://www.baidu.com/*", + "https://m.baidu.com/*", "https://*search.yahoo.com/search*", "https://www.bing.com/search*", "https://duckduckgo.com/*" diff --git a/app/src/main/java/org/mozilla/fenix/search/telemetry/BaseSearchTelemetry.kt b/app/src/main/java/org/mozilla/fenix/search/telemetry/BaseSearchTelemetry.kt index 60712a43d3..385fed6d07 100644 --- a/app/src/main/java/org/mozilla/fenix/search/telemetry/BaseSearchTelemetry.kt +++ b/app/src/main/java/org/mozilla/fenix/search/telemetry/BaseSearchTelemetry.kt @@ -52,10 +52,10 @@ abstract class BaseSearchTelemetry { ), SearchProviderModel( name = "baidu", - regexp = "^https:\\/\\/www\\.baidu\\.com\\/from=844b\\/(?:s|baidu)", - queryParam = "wd", - codeParam = "tn", - codePrefixes = listOf("34046034_", "monline_"), + regexp = "^https:\\/\\/m\\.baidu\\.com(?:.*)\\/s", + queryParam = "word", + codeParam = "from", + codePrefixes = listOf("1000969a"), followOnParams = listOf("oq") ), SearchProviderModel( diff --git a/app/src/main/java/org/mozilla/fenix/search/telemetry/Utils.kt b/app/src/main/java/org/mozilla/fenix/search/telemetry/Utils.kt index 38bf1179c3..2ab395f1b8 100644 --- a/app/src/main/java/org/mozilla/fenix/search/telemetry/Utils.kt +++ b/app/src/main/java/org/mozilla/fenix/search/telemetry/Utils.kt @@ -22,6 +22,20 @@ internal fun getTrackKey( if (provider.codeParam.isNotEmpty()) { code = uri.getQueryParameter(provider.codeParam) + if (code.isNullOrEmpty() && + provider.name == "baidu" && + uri.toString().contains("from=")) { + code = uri.toString().substringAfter("from=", "") + .substringBefore("/", "") + } + + // Glean doesn't allow code starting with a figure + if (code != null && code.isNotEmpty()) { + val codeStart = code.first() + if (codeStart.isDigit()) { + code = "_$code" + } + } // Try cookies first because Bing has followOnCookies and valid code, but no // followOnParams => would tracks organic instead of sap-follow-on @@ -33,7 +47,7 @@ internal fun getTrackKey( } // For Bing if it didn't have a valid cookie and for all the other search engines - if (hasValidCode(code, provider)) { + if (hasValidCode(uri.getQueryParameter(provider.codeParam), provider)) { val channel = uri.getQueryParameter(CHANNEL_KEY) val type = getSapType(provider.followOnParams, paramSet) return TrackKeyInfo(provider.name, type, code, channel).createTrackKey() diff --git a/app/src/test/java/org/mozilla/fenix/search/telemetry/incontent/InContentTelemetryTest.kt b/app/src/test/java/org/mozilla/fenix/search/telemetry/incontent/InContentTelemetryTest.kt index 36e30f6cde..72ff256c18 100644 --- a/app/src/test/java/org/mozilla/fenix/search/telemetry/incontent/InContentTelemetryTest.kt +++ b/app/src/test/java/org/mozilla/fenix/search/telemetry/incontent/InContentTelemetryTest.kt @@ -84,11 +84,11 @@ class InContentTelemetryTest { @Test fun `track baidu sap metric`() { - val url = "https://www.baidu.com/from=844b/s?wd=aaa&tn=34046034_firefox" + val url = "https://m.baidu.com/s?from=1000969a&word=aaa" telemetry.trackPartnerUrlTypeMetric(url, listOf()) - verify { metrics.track(Event.SearchInContent("baidu.in-content.sap.34046034_firefox")) } + verify { metrics.track(Event.SearchInContent("baidu.in-content.sap._1000969a")) } } @Test @@ -120,11 +120,11 @@ class InContentTelemetryTest { @Test fun `track baidu sap-follow-on metric`() { - val url = "https://www.baidu.com/from=844b/s?wd=aaa&tn=34046034_firefox&oq=random" + val url = "https://m.baidu.com/s?from=1000969a&word=aaa&oq=random" telemetry.trackPartnerUrlTypeMetric(url, listOf()) - verify { metrics.track(Event.SearchInContent("baidu.in-content.sap-follow-on.34046034_firefox")) } + verify { metrics.track(Event.SearchInContent("baidu.in-content.sap-follow-on._1000969a")) } } @Test @@ -165,7 +165,7 @@ class InContentTelemetryTest { @Test fun `track baidu organic metric`() { - val url = "https://www.baidu.com/from=844b/s?wd=aaa" + val url = "https://m.baidu.com/s?word=aaa" telemetry.trackPartnerUrlTypeMetric(url, listOf())