parent
a4691675a1
commit
3a056bf850
@ -0,0 +1,28 @@
|
|||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
browser.runtime.onMessage.addListener(notify);
|
||||||
|
|
||||||
|
function sendMessageToTabs(tabs, cookies) {
|
||||||
|
for (let tab of tabs) {
|
||||||
|
browser.tabs.sendMessage(
|
||||||
|
tab.id,
|
||||||
|
{ cookies }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function notify(message) {
|
||||||
|
if (message.checkCookies) {
|
||||||
|
browser.cookies.getAll({})
|
||||||
|
.then(cookies => {
|
||||||
|
browser.tabs.query({
|
||||||
|
currentWindow: true,
|
||||||
|
active: true
|
||||||
|
}).then(tabs => {
|
||||||
|
sendMessageToTabs(tabs, cookies);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
package org.mozilla.fenix.search.telemetry
|
||||||
|
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A data class that tracks key information about a Search Engine Result Page (SERP).
|
||||||
|
*
|
||||||
|
* @property provider The name of the search provider.
|
||||||
|
* @property type The search access point type (SAP). This is either "organic", "sap" or
|
||||||
|
* "sap-follow-on".
|
||||||
|
* @property code The search URL's `code` query parameter.
|
||||||
|
* @property channel The search URL's `channel` query parameter.
|
||||||
|
*/
|
||||||
|
internal data class TrackKeyInfo(
|
||||||
|
var provider: String,
|
||||||
|
var type: String,
|
||||||
|
var code: String?,
|
||||||
|
var channel: String? = null
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* Returns the track key information into the following string format:
|
||||||
|
* `<provider>.in-content.[sap|sap-follow-on|organic].[code|none](.[channel])?`.
|
||||||
|
*/
|
||||||
|
fun createTrackKey(): String {
|
||||||
|
return "${provider.toLowerCase(Locale.ROOT)}.in-content" +
|
||||||
|
".${type.toLowerCase(Locale.ROOT)}" +
|
||||||
|
".${code?.toLowerCase(Locale.ROOT) ?: "none"}" +
|
||||||
|
if (!channel?.toLowerCase(Locale.ROOT).isNullOrBlank())
|
||||||
|
".${channel?.toLowerCase(Locale.ROOT)}"
|
||||||
|
else ""
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
package org.mozilla.fenix.search.telemetry
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import org.json.JSONObject
|
||||||
|
|
||||||
|
private const val SEARCH_TYPE_SAP_FOLLOW_ON = "sap-follow-on"
|
||||||
|
private const val SEARCH_TYPE_SAP = "sap"
|
||||||
|
private const val SEARCH_TYPE_ORGANIC = "organic"
|
||||||
|
private const val CHANNEL_KEY = "channel"
|
||||||
|
|
||||||
|
internal fun getTrackKey(
|
||||||
|
provider: SearchProviderModel,
|
||||||
|
uri: Uri,
|
||||||
|
cookies: List<JSONObject>
|
||||||
|
): String {
|
||||||
|
val paramSet = uri.queryParameterNames
|
||||||
|
var code: String? = null
|
||||||
|
|
||||||
|
if (provider.codeParam.isNotEmpty()) {
|
||||||
|
code = uri.getQueryParameter(provider.codeParam)
|
||||||
|
|
||||||
|
// Try cookies first because Bing has followOnCookies and valid code, but no
|
||||||
|
// followOnParams => would tracks organic instead of sap-follow-on
|
||||||
|
if (provider.followOnCookies.isNotEmpty()) {
|
||||||
|
// Checks if engine contains a valid follow-on cookie, otherwise return default
|
||||||
|
getTrackKeyFromCookies(provider, uri, cookies)?.let {
|
||||||
|
return it.createTrackKey()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// For Bing if it didn't have a valid cookie and for all the other search engines
|
||||||
|
if (hasValidCode(code, provider)) {
|
||||||
|
val channel = uri.getQueryParameter(CHANNEL_KEY)
|
||||||
|
val type = getSapType(provider.followOnParams, paramSet)
|
||||||
|
return TrackKeyInfo(provider.name, type, code, channel).createTrackKey()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default to organic search type if no code parameter was found.
|
||||||
|
return TrackKeyInfo(provider.name, SEARCH_TYPE_ORGANIC, code).createTrackKey()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getTrackKeyFromCookies(
|
||||||
|
provider: SearchProviderModel,
|
||||||
|
uri: Uri,
|
||||||
|
cookies: List<JSONObject>
|
||||||
|
): TrackKeyInfo? {
|
||||||
|
// Especially Bing requires lots of extra work related to cookies.
|
||||||
|
for (followOnCookie in provider.followOnCookies) {
|
||||||
|
val eCode = uri.getQueryParameter(followOnCookie.extraCodeParam)
|
||||||
|
if (eCode == null || !followOnCookie.extraCodePrefixes.any { prefix ->
|
||||||
|
eCode.startsWith(prefix)
|
||||||
|
}) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this cookie is present, it's probably an SAP follow-on.
|
||||||
|
// This might be an organic follow-on in the same session, but there
|
||||||
|
// is no way to tell the difference.
|
||||||
|
for (cookie in cookies) {
|
||||||
|
if (cookie.getString("name") != followOnCookie.name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val valueList = cookie.getString("value")
|
||||||
|
.split("=")
|
||||||
|
.map { item -> item.trim() }
|
||||||
|
|
||||||
|
if (valueList.size == 2 && valueList[0] == followOnCookie.codeParam &&
|
||||||
|
followOnCookie.codePrefixes.any { prefix ->
|
||||||
|
valueList[1].startsWith(
|
||||||
|
prefix
|
||||||
|
)
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
return TrackKeyInfo(provider.name, SEARCH_TYPE_SAP_FOLLOW_ON, valueList[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getSapType(followOnParams: List<String>, paramSet: Set<String>): String {
|
||||||
|
return if (followOnParams.any { param -> paramSet.contains(param) }) {
|
||||||
|
SEARCH_TYPE_SAP_FOLLOW_ON
|
||||||
|
} else {
|
||||||
|
SEARCH_TYPE_SAP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hasValidCode(code: String?, provider: SearchProviderModel): Boolean =
|
||||||
|
code != null && provider.codePrefixes.any { prefix -> code.startsWith(prefix) }
|
@ -1,23 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
package org.mozilla.fenix.search.telemetry.incontent
|
|
||||||
|
|
||||||
import java.util.Locale
|
|
||||||
|
|
||||||
internal data class TrackKeyInfo(
|
|
||||||
var providerName: String,
|
|
||||||
var type: String,
|
|
||||||
var code: String?,
|
|
||||||
var channel: String? = null
|
|
||||||
) {
|
|
||||||
fun createTrackKey(): String {
|
|
||||||
return "${providerName.toLowerCase(Locale.ROOT)}.in-content" +
|
|
||||||
".${type.toLowerCase(Locale.ROOT)}" +
|
|
||||||
".${code?.toLowerCase(Locale.ROOT) ?: "none"}" +
|
|
||||||
if (!channel?.toLowerCase(Locale.ROOT).isNullOrBlank())
|
|
||||||
".${channel?.toLowerCase(Locale.ROOT)}"
|
|
||||||
else ""
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue