diff --git a/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/tracker/TrackerWebViewInterfaceV2Test.kt b/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/tracker/TrackerWebViewInterfaceV2Test.kt index 015d15cbf..2c04c9e7c 100644 --- a/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/tracker/TrackerWebViewInterfaceV2Test.kt +++ b/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/tracker/TrackerWebViewInterfaceV2Test.kt @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) ) diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/event/WebViewReader.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/event/WebViewReader.kt index 258a30086..b48ac9e98 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/event/WebViewReader.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/event/WebViewReader.kt @@ -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, @@ -42,11 +42,10 @@ class WebViewReader( override val dataPayload: Map get() { val payload = HashMap() - 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 diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/TrackerWebViewInterfaceV2.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/TrackerWebViewInterfaceV2.kt index 8e4c7c929..d21463fe9 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/TrackerWebViewInterfaceV2.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/tracker/TrackerWebViewInterfaceV2.kt @@ -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? = 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?) { if (contextEntities != null) { @@ -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) }