This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
a469167
commit 3a056bf
Showing
12 changed files
with
282 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
app/src/main/java/org/mozilla/fenix/search/telemetry/TrackKeyInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "" | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
app/src/main/java/org/mozilla/fenix/search/telemetry/Utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.