Skip to content

Commit

Permalink
Improve the API
Browse files Browse the repository at this point in the history
  • Loading branch information
mscwilson committed Dec 3, 2024
1 parent 638ab01 commit 22538da
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,16 @@ class TrackerWebViewInterfaceV2Test {
@Throws(JSONException::class, InterruptedException::class)
fun tracksEventWithAllOptions() {
val data = "{\"schema\":\"iglu:etc\",\"data\":{\"key\":\"val\"}}"
val atomic = "{\"eventName\":\"ue\",\"trackerVersion\":\"webview\"," +
"\"useragent\":\"Chrome\",\"pageUrl\":\"http://snowplow.com\"," +
"\"pageTitle\":\"Snowplow\",\"referrer\":\"http://google.com\"," +
"\"pingXOffsetMin\":10,\"pingXOffsetMax\":20,\"pingYOffsetMin\":30," +
"\"pingYOffsetMax\":40,\"category\":\"cat\",\"action\":\"act\"," +
"\"property\":\"prop\",\"label\":\"lbl\",\"value\":10.0}"

webInterface!!.trackWebViewEvent(
eventName = "ue",
trackerVersion = "webview",
useragent = "Chrome",
selfDescribingEventData = data,
pageUrl = "http://snowplow.com",
pageTitle = "Snowplow",
referrer = "http://google.com",
pingXOffsetMin = 10,
pingXOffsetMax = 20,
pingYOffsetMin = 30,
pingYOffsetMax = 40,
category = "cat",
action = "act",
property = "prop",
label = "lbl",
value = 10.0
atomicProperties = atomic
)

Thread.sleep(200)
Expand Down Expand Up @@ -124,10 +116,7 @@ class TrackerWebViewInterfaceV2Test {

// track an event using the second tracker
webInterface!!.trackWebViewEvent(
eventName = "pv",
trackerVersion = "webview",
useragent = "Chrome",
pageUrl = "http://snowplow.com",
atomicProperties = "{\"eventName\":\"pv\",\"trackerVersion\":\"webview\"}",
trackers = arrayOf("ns2")
)
Thread.sleep(200)
Expand All @@ -139,12 +128,7 @@ class TrackerWebViewInterfaceV2Test {
assertEquals("pv", networkConnection2.allRequests[0].payload.map[Parameters.EVENT])

// tracks using default tracker if not specified
webInterface!!.trackWebViewEvent(
eventName = "pp",
trackerVersion = "webview",
useragent = "Chrome",
pageUrl = "http://snowplow.com",
)
webInterface!!.trackWebViewEvent(atomicProperties = "{}")
Thread.sleep(200)
waitForEvents(networkConnection, 1)

Expand All @@ -155,13 +139,9 @@ class TrackerWebViewInterfaceV2Test {
@Test
@Throws(JSONException::class, InterruptedException::class)
fun tracksEventWithEntity() {
val entities = "[{\"schema\":\"iglu:com.example/etc\",\"data\":{\"key\":\"val\"}}]"
webInterface!!.trackWebViewEvent(
eventName = "pp",
trackerVersion = "webview",
useragent = "Chrome",
pageUrl = "http://snowplow.com",
entities = entities
atomicProperties = "{}",
entities = "[{\"schema\":\"iglu:com.example/etc\",\"data\":{\"key\":\"val\"}}]"
)
Thread.sleep(200)
waitForEvents(networkConnection, 1)
Expand Down Expand Up @@ -199,12 +179,9 @@ class TrackerWebViewInterfaceV2Test {
plugin
)

val data = "{\"schema\":\"iglu:etc\",\"data\":{\"key\":\"val\"}}"
webInterface!!.trackWebViewEvent(
eventName = "se",
trackerVersion = "webview",
useragent = "Chrome",
selfDescribingEventData = data,
atomicProperties = "{\"eventName\":\"se\",\"trackerVersion\":\"webview\"}",
selfDescribingEventData = "{\"schema\":\"iglu:etc\",\"data\":{\"key\":\"val\"}}",
trackers = arrayOf(namespace)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import com.snowplowanalytics.snowplow.payload.SelfDescribingJson
* Allows the tracking of JavaScript events from WebViews.
*/
class WebViewReader(
val eventName: String,
val trackerVersion: String,
val useragent: String,
val selfDescribingEventData: SelfDescribingJson? = null,
val eventName: String? = null,
val trackerVersion: String? = null,
val useragent: String? = null,
val pageUrl: String? = null,
val pageTitle: String? = null,
val referrer: String? = null,
Expand All @@ -42,11 +42,10 @@ class WebViewReader(
override val dataPayload: Map<String, Any?>
get() {
val payload = HashMap<String, Any?>()
payload[Parameters.EVENT] = eventName
payload[Parameters.TRACKER_VERSION] = trackerVersion
payload[Parameters.USERAGENT] = useragent

if (selfDescribingEventData != null) payload[Parameters.WEBVIEW_EVENT_DATA] = selfDescribingEventData
if (eventName != null) payload[Parameters.EVENT] = eventName
if (trackerVersion != null) payload[Parameters.TRACKER_VERSION] = trackerVersion
if (useragent != null) payload[Parameters.USERAGENT] = useragent
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,50 @@ import java.util.*

/**
* JavaScript interface used to provide an API for tracking events from WebViews.
* This V2 interface works with the WebView tracker v0.3.0+.
*/
class TrackerWebViewInterfaceV2 {
@JavascriptInterface
@Throws(JSONException::class)
fun trackWebViewEvent(
eventName: String,
trackerVersion: String,
useragent: String,
atomicProperties: String,
selfDescribingEventData: String? = null,
pageUrl: String? = null,
pageTitle: String? = null,
referrer: String? = null,
category: String? = null,
action: String? = null,
label: String? = null,
property: String? = null,
value: Double? = null,
pingXOffsetMin: Int? = null,
pingXOffsetMax: Int? = null,
pingYOffsetMin: Int? = null,
pingYOffsetMax: Int? = null,
entities: String? = null,
trackers: Array<String>? = null
) {
val atomic = JSONObject(atomicProperties)

val event = WebViewReader(
eventName,
trackerVersion,
useragent,
parseSelfDescribingEventData(selfDescribingEventData),
pageUrl,
pageTitle,
referrer,
category,
action,
label,
property,
value,
pingXOffsetMin,
pingXOffsetMax,
pingYOffsetMin,
pingYOffsetMax
selfDescribingEventData = parseSelfDescribingEventData(selfDescribingEventData),
eventName = getProperty(atomic, "eventName")?.toString(),
trackerVersion = getProperty(atomic, "trackerVersion")?.toString(),
useragent = getProperty(atomic, "useragent")?.toString(),
pageUrl = getProperty(atomic, "pageUrl")?.toString(),
pageTitle = getProperty(atomic, "pageTitle")?.toString(),
referrer = getProperty(atomic, "referrer")?.toString(),
category = getProperty(atomic, "category")?.toString(),
action = getProperty(atomic, "action")?.toString(),
label = getProperty(atomic, "label")?.toString(),
property = getProperty(atomic, "property")?.toString(),
value = getProperty(atomic, "value")?.toString()?.toDoubleOrNull(),
pingXOffsetMin = getProperty(atomic, "pingXOffsetMin")?.toString()?.toIntOrNull(),
pingXOffsetMax = getProperty(atomic, "pingXOffsetMax")?.toString()?.toIntOrNull(),
pingYOffsetMin = getProperty(atomic, "pingYOffsetMin")?.toString()?.toIntOrNull(),
pingYOffsetMax = getProperty(atomic, "pingYOffsetMax")?.toString()?.toIntOrNull(),
)
trackEvent(event, entities, trackers)
}


private fun getProperty(atomicProperties: JSONObject, property: String) = try {
if (atomicProperties.has(property)) {
atomicProperties.get(property)
} else {
null
}
} catch (e: JSONException) {
null
}

@Throws(JSONException::class)
private fun trackEvent(event: AbstractEvent, contextEntities: String?, trackers: Array<String>?) {
if (contextEntities != null) {
Expand Down Expand Up @@ -117,7 +116,6 @@ class TrackerWebViewInterfaceV2 {
val itemJson = entitiesJson.getJSONObject(i)
val item = jsonToMap(itemJson)
val selfDescribingJson = createSelfDescribingJson(item)

if (selfDescribingJson != null) {
entities.add(selfDescribingJson)
}
Expand Down

0 comments on commit 22538da

Please sign in to comment.