Skip to content

Commit

Permalink
Do not track the screen view again when app comes to foreground (close
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Dec 11, 2023
1 parent 701086e commit af54ad7
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2015-2023 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.snowplow.tracker

import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.snowplowanalytics.core.constants.TrackerConstants
import com.snowplowanalytics.core.utils.NotificationCenter
import com.snowplowanalytics.snowplow.Snowplow
import com.snowplowanalytics.snowplow.Snowplow.removeAllTrackers
import com.snowplowanalytics.snowplow.configuration.Configuration
import com.snowplowanalytics.snowplow.configuration.NetworkConfiguration
import com.snowplowanalytics.snowplow.configuration.PluginConfiguration
import com.snowplowanalytics.snowplow.controller.TrackerController
import com.snowplowanalytics.snowplow.event.ScreenView
import com.snowplowanalytics.snowplow.event.Structured
import com.snowplowanalytics.snowplow.network.HttpMethod
import com.snowplowanalytics.snowplow.payload.SelfDescribingJson
import org.junit.After
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import java.util.*

@RunWith(AndroidJUnit4::class)
class ScreenViewAutotrackingTest {

@After
fun tearDown() {
removeAllTrackers()
}

// --- TESTS
@Test
fun addsEntitiesToEvent() {
var numberOfScreenViews = 0

val countPlugin = PluginConfiguration("test")
countPlugin.afterTrack {
if (it.schema == TrackerConstants.SCHEMA_SCREEN_VIEW) {
numberOfScreenViews += 1
}
}

createTracker(listOf(countPlugin))
Thread.sleep(200)

NotificationCenter.postNotification("SnowplowScreenView", mapOf(
"event" to ScreenView(name = "Screen1").activityClassName("Screen1")
))
Thread.sleep(200)

NotificationCenter.postNotification("SnowplowScreenView", mapOf(
"event" to ScreenView(name = "Screen1").activityClassName("Screen1")
))
Thread.sleep(200)

NotificationCenter.postNotification("SnowplowScreenView", mapOf(
"event" to ScreenView(name = "Screen2").activityClassName("Screen2")
))
Thread.sleep(200)

Assert.assertEquals(2, numberOfScreenViews)
}

// --- PRIVATE
private val context: Context
get() = InstrumentationRegistry.getInstrumentation().targetContext

private fun createTracker(configurations: List<Configuration>): TrackerController {
val networkConfig = NetworkConfiguration(MockNetworkConnection(HttpMethod.POST, 200))
return Snowplow.createTracker(
context,
namespace = "testScreenView" + Math.random().toString(),
network = networkConfig,
configurations = configurations.toTypedArray()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class ScreenState : State {
private var transitionType: String? = null
private var fragmentClassName: String? = null
private var fragmentTag: String? = null
private var activityClassName: String? = null
private var activityTag: String? = null
var activityClassName: String? = null
var activityTag: String? = null

init {
id = uUIDString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Tracker(
private val stateManager = StateManager()

fun getScreenState(): ScreenState? {
val state = stateManager.trackerState.getState("ScreenContext")
val state = stateManager.trackerState.getState(ScreenStateMachine.ID)
?: // Legacy initialization
return ScreenState()

Expand Down Expand Up @@ -324,8 +324,19 @@ class Tracker(
private val receiveScreenViewNotification: FunctionalObserver = object : FunctionalObserver() {
override fun apply(data: Map<String, Any>) {
if (screenViewAutotracking) {
val event = data["event"] as? Event?
event?.let { track(it) }
val event = data["event"] as? ScreenView?
event?.let { event ->
getScreenState()?.let { state ->
// don't track if screen view is for the same activity as the last one
if (
event.activityClassName?.isEmpty() != false ||
event.activityClassName != state.activityClassName ||
event.activityTag != state.activityTag
) {
track(event)
}
} ?: track(event)
}
}
}
}
Expand Down

0 comments on commit af54ad7

Please sign in to comment.