Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
For #25281 - Allow the Pocket section to settle before reporting impr…
Browse files Browse the repository at this point in the history
…essions

Currently just a hack that will ensure more reliable data.
This will have to be re-evaluated after the homescreen is migrated to compose.
  • Loading branch information
Mugurell authored and mergify[bot] committed May 26, 2022
1 parent dfa5281 commit 0b97266
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ private const val URI_PARAM_UTM_KEY = "utm_source"
private const val POCKET_STORIES_UTM_VALUE = "pocket-newtab-android"
private const val POCKET_FEATURE_UTM_KEY_VALUE = "utm_source=ff_android"

/**
* The Pocket section may appear first on the homescreen and be fully constructed
* to then be pushed downwards when other elements appear.
* This can lead to overcounting impressions with multiple such events being possible
* without the user actually having time to see the stories or scrolling to see the Pocket section.
*/
private const val MINIMUM_TIME_TO_SETTLE_MS = 1000

/**
* Placeholder [PocketStory] allowing to combine other items in the same list that shows stories.
* It uses empty values for it's properties ensuring that no conflict is possible since real stories have
Expand Down Expand Up @@ -264,12 +272,16 @@ private fun Modifier.onShown(
@FloatRange(from = 0.0, to = 1.0) threshold: Float,
onVisible: () -> Unit,
): Modifier {
val initialTime = System.currentTimeMillis()

return composed {
val context = LocalContext.current
var wasEventReported by remember { mutableStateOf(false) }

onGloballyPositioned { coordinates ->
if (!wasEventReported && coordinates.isVisible(context, threshold)) {
if (!wasEventReported && coordinates.isVisible(context, threshold) &&
System.currentTimeMillis() - initialTime > MINIMUM_TIME_TO_SETTLE_MS
) {
wasEventReported = true
onVisible()
}
Expand Down

0 comments on commit 0b97266

Please sign in to comment.