Skip to content

Commit

Permalink
Add factory to CustomTrackingEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
dimamo5 committed Apr 30, 2019
1 parent 38af754 commit cfc344d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ val event =
}
""".trimIndent()
Velocidi.getInstance().track(CustomTrackingEvent(JSONObject(event)))
Velocidi.getInstance().track(CustomTrackingEventFactory.buildFromJSON(event))
```

### Match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.velocidi.UserId
import com.velocidi.Velocidi
import com.velocidi.events.CustomTrackingEventFactory
import com.velocidi.events.PageView

import kotlinx.android.synthetic.main.activity_main.*
Expand All @@ -28,7 +29,7 @@ class MainActivity : AppCompatActivity() {
Velocidi.getInstance().track(PageView("pageView", "MobileApp", "client1"))

// OR
// Velocidi.getInstance().track(CustomTrackingEvent(JSONObject(event)))
// Velocidi.getInstance().track(CustomTrackingEventFactory.buildFromJSON(event))
}

match_button.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import kotlinx.serialization.internal.StringDescriptor
import org.json.JSONObject

@Serializable
class CustomTrackingEvent(@Transient val json: JSONObject = JSONObject()) :
TrackingEvent(json.getString("type")) {
override val siteId: String = json.getString("siteId")
override val clientId: String = json.getString("clientId")
class CustomTrackingEvent(
private val eventType: String, // Must be a declared as a propriety so kotlinx can properly serialize this class
override val siteId: String,
override val clientId: String,
@Transient val extraAttributes: JSONObject = JSONObject()
) : TrackingEvent(eventType) {

override fun serialize(): String =
jsonSerilizer().stringify(serializer(), this).trim('"').replace("\\", "")
Expand All @@ -18,15 +20,36 @@ class CustomTrackingEvent(@Transient val json: JSONObject = JSONObject()) :
override val descriptor: SerialDescriptor = StringDescriptor.withName("CustomTrackingEvent")

override fun serialize(encoder: Encoder, obj: CustomTrackingEvent) {
encoder.encodeString(obj.json.toString())
}

fun extractAttribute(json: JSONObject, attr: String): String? {
return try {
json.getString(attr)
} catch (e: org.json.JSONException) {
null
}
val mergedJson = JSONObject(obj.extraAttributes.toString())

mergedJson
.put("type", obj.type)
.put("siteId", obj.siteId)
.put("clientId", obj.clientId)

encoder.encodeString(mergedJson.toString())
}
}
}

object CustomTrackingEventFactory {
/**
* Factory method to create a CustomTrackingEvent from a JSON
*
* @param json Json to be parsed
* @return Properly populated CustomTrackingEvent
*/
fun buildFromJSON(json: String): CustomTrackingEvent {
val jsonObj = JSONObject(json)
val type = jsonObj.getString("type")
val siteId = jsonObj.getString("siteId")
val clientId = jsonObj.getString("clientId")

jsonObj.remove("type")
jsonObj.remove("siteId")
jsonObj.remove("clientId")

return CustomTrackingEvent(type, siteId, clientId, jsonObj)
}
}
25 changes: 16 additions & 9 deletions velocidi-sdk/src/test/kotlin/com/velocidi/TrackingEventsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,49 @@ class TrackingEventsTest {
)

@Test
fun customTrackingEventCreation() {
fun customTrackingEventFactory() {

val event = """
{
"type": "custom",
"siteId": "0",
"clientId": "id1"
"clientId": "id1",
"test": "testString"
}"""

val eventObj = CustomTrackingEvent(JSONObject(event))
val eventObj = CustomTrackingEventFactory.buildFromJSON(event)
assertThat(eventObj.type).isEqualTo("custom")
assertThat(eventObj.siteId).isEqualTo("0")
assertThat(eventObj.clientId).isEqualTo("id1")
assertThat(eventObj.extraAttributes.toString()).isEqualTo("""{"test":"testString"}""")
}

@Test(expected = JSONException::class)
fun invalidCustomTrackingEvent() {
val invalidEvent = """{"siteId": "0"}"""
CustomTrackingEvent(JSONObject(invalidEvent))
CustomTrackingEventFactory.buildFromJSON(invalidEvent)
}

@Test
fun customTrackingEventSerialization() {
val event =
"""
{
"type": "custom",
"siteId": "0",
"clientId": "id1",
"test": {
"a": 1
}
},
"type": "custom",
"siteId": "0",
"clientId": "id1"
}
""".trimIndent()

val eventObj = CustomTrackingEvent(JSONObject(event))
val eventObj = CustomTrackingEvent(
"custom",
"0",
"id1",
JSONObject(""" {"test": {"a": 1}} """)
)

assertThat(eventObj.serialize().prettyPrintJson()).isEqualTo(event.prettyPrintJson())
}
Expand Down

0 comments on commit cfc344d

Please sign in to comment.