Skip to content

Commit d3e4aa2

Browse files
committed
Implement sampling
1 parent 1abd88d commit d3e4aa2

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/FirebaseSessions.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ internal constructor(
3535
backgroundDispatcher: CoroutineDispatcher,
3636
transportFactoryProvider: Provider<TransportFactory>,
3737
) {
38-
private val sessionGenerator = SessionGenerator(collectEvents = true)
38+
private val sessionSettings = SessionsSettings(firebaseApp.applicationContext)
39+
private val sessionGenerator = SessionGenerator(collectEvents = shouldCollectEvents())
3940
private val eventGDTLogger = EventGDTLogger(transportFactoryProvider)
4041
private val sessionCoordinator =
4142
SessionCoordinator(firebaseInstallations, backgroundDispatcher, eventGDTLogger)
42-
private val sessionSettings = SessionsSettings(firebaseApp.applicationContext)
4343

4444
init {
4545
val sessionInitiator =
@@ -62,9 +62,18 @@ internal constructor(
6262
val sessionDetails = sessionGenerator.generateNewSession()
6363
val sessionEvent = SessionEvents.startSession(firebaseApp, sessionDetails, sessionSettings)
6464

65-
if (sessionDetails.collectEvents) {
66-
sessionCoordinator.attemptLoggingSessionEvent(sessionEvent)
65+
if (!sessionDetails.collectEvents) {
66+
Log.d(TAG, "Session Start event sampled.")
6767
}
68+
69+
sessionCoordinator.attemptLoggingSessionEvent(sessionEvent)
70+
}
71+
72+
/** Calculate whether we should sample events using [sessionSettings] data. */
73+
private fun shouldCollectEvents(): Boolean {
74+
// Sampling rate of 1 means we do not sample.
75+
val randomValue = Math.random()
76+
return randomValue <= sessionSettings.samplingRate
6877
}
6978

7079
companion object {

0 commit comments

Comments
 (0)