Skip to content

Commit

Permalink
Start adding support for self-desc events
Browse files Browse the repository at this point in the history
  • Loading branch information
mscwilson committed Nov 26, 2024
1 parent 0229c27 commit c00f3d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,25 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.snowplowanalytics.core.constants.Parameters
import com.snowplowanalytics.core.emitter.Executor
import com.snowplowanalytics.core.media.MediaSchemata.eventSchema
import com.snowplowanalytics.core.media.MediaSchemata.playerSchema
import com.snowplowanalytics.core.media.MediaSchemata.sessionSchema
import com.snowplowanalytics.core.media.controller.MediaPingInterval
import com.snowplowanalytics.core.media.controller.MediaSessionTracking
import com.snowplowanalytics.core.media.controller.MediaTrackingImpl
import com.snowplowanalytics.core.media.controller.TimerInterface
import com.snowplowanalytics.core.tracker.TrackerWebViewInterface
import com.snowplowanalytics.core.tracker.TrackerWebViewInterfaceV2
import com.snowplowanalytics.core.utils.Util.objectMapToString
import com.snowplowanalytics.snowplow.Snowplow.createTracker
import com.snowplowanalytics.snowplow.Snowplow.removeAllTrackers
import com.snowplowanalytics.snowplow.configuration.NetworkConfiguration
import com.snowplowanalytics.snowplow.configuration.PluginConfiguration
import com.snowplowanalytics.snowplow.configuration.TrackerConfiguration
import com.snowplowanalytics.snowplow.controller.TrackerController
import com.snowplowanalytics.snowplow.event.SelfDescribing
import com.snowplowanalytics.snowplow.media.configuration.MediaTrackingConfiguration
import com.snowplowanalytics.snowplow.media.entity.MediaPlayerEntity
import com.snowplowanalytics.snowplow.media.event.*
import com.snowplowanalytics.snowplow.network.HttpMethod
import com.snowplowanalytics.snowplow.payload.SelfDescribingJson
import com.snowplowanalytics.snowplow.tracker.InspectableEvent
import com.snowplowanalytics.snowplow.tracker.MockNetworkConnection
import com.snowplowanalytics.snowplow.util.TimeTraveler
import junit.framework.TestCase
import org.json.JSONException
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.util.*
import kotlin.time.DurationUnit
import kotlin.time.toDuration

@RunWith(AndroidJUnit4::class)
class TrackerWebViewInterfaceV2Test {
private var webInterface: TrackerWebViewInterfaceV2? = null
private val trackedEvents: MutableList<InspectableEvent> = mutableListOf()
private val firstEvent: InspectableEvent
get() = trackedEvents.first()
private var tracker: TrackerController? = null

@Before
Expand Down Expand Up @@ -97,7 +73,9 @@ class TrackerWebViewInterfaceV2Test {
Thread.sleep(200)

assertEquals(1, trackedEvents.size)
val payload = firstEvent.payload
assertEquals("webViewEvent", trackedEvents.first().name)

val payload = trackedEvents.first().payload
assertEquals("pp", payload[Parameters.EVENT])
assertEquals("webview", payload[Parameters.TRACKER_VERSION])
assertEquals("Firefox", payload[Parameters.USERAGENT])
Expand Down Expand Up @@ -127,7 +105,9 @@ class TrackerWebViewInterfaceV2Test {
Thread.sleep(200)

assertEquals(1, trackedEvents.size)
val payload = firstEvent.payload
assertEquals("webViewEvent", trackedEvents.first().name)

val payload = trackedEvents.first().payload
assertEquals("se", payload[Parameters.EVENT])
assertEquals("webview2", payload[Parameters.TRACKER_VERSION])
assertEquals("Firefox", payload[Parameters.USERAGENT])
Expand All @@ -138,6 +118,26 @@ class TrackerWebViewInterfaceV2Test {
assertEquals(10.0, payload[Parameters.SE_VALUE])
}

@Test
@Throws(JSONException::class, InterruptedException::class)
fun tracksSelfDescribingEvent() {
val data = "[{\"schema\":\"http://schema.com\",\"data\":{\"key\":\"val\"}}]"
webInterface!!.trackWebViewEvent(
eventName = "ue",
trackerVersion = "webview2",
useragent = "Firefox",
selfDescribingEventData = data
)

Thread.sleep(200)

assertEquals(1, trackedEvents.size)
assertEquals("webViewEvent", trackedEvents.first().name)

val payload = trackedEvents.first().payload
assertEquals(data, payload["changeThis"])
}

@Test
@Throws(JSONException::class, InterruptedException::class)
fun tracksEventWithCorrectTracker() {
Expand Down Expand Up @@ -196,8 +196,8 @@ class TrackerWebViewInterfaceV2Test {
Thread.sleep(200)

assertEquals(1, trackedEvents.size)
val entity1 = firstEvent.entities[0]
val entity2 = firstEvent.entities[1]
val entity1 = trackedEvents.first().entities[0]
val entity2 = trackedEvents.first().entities[1]

assertEquals("http://schema.com", entity1.map["schema"] as? String)
assertEquals("val", (entity1.map["data"] as? Map<*, *>)?.get("key"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WebViewReader(
val eventName: String,
val trackerVersion: String,
val useragent: String,
val selfDescribingEventData: String? = null,
val pageUrl: String? = null,
val pageTitle: String? = null,
val referrer: String? = null,
Expand All @@ -45,6 +46,7 @@ class WebViewReader(
payload[Parameters.EVENT] = eventName
payload[Parameters.TRACKER_VERSION] = trackerVersion
payload[Parameters.USERAGENT] = useragent
if (selfDescribingEventData != null) payload["changeThis"] = selfDescribingEventData
if (pageUrl != null) payload[Parameters.PAGE_URL] = pageUrl
if (pageTitle != null) payload[Parameters.PAGE_TITLE] = pageTitle
if (referrer != null) payload[Parameters.PAGE_REFR] = referrer
Expand All @@ -61,5 +63,5 @@ class WebViewReader(
}

override val name: String
get() = "internal_use_only"
get() = "webViewEvent"
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TrackerWebViewInterfaceV2 {
eventName: String,
trackerVersion: String,
useragent: String,
selfDescribingEventData: String? = null,
pageUrl: String? = null,
pageTitle: String? = null,
referrer: String? = null,
Expand All @@ -53,6 +54,7 @@ class TrackerWebViewInterfaceV2 {
eventName,
trackerVersion,
useragent,
selfDescribingEventData,
pageUrl,
pageTitle,
referrer,
Expand Down

0 comments on commit c00f3d6

Please sign in to comment.