Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: event tracking, identify customer (#29)
- Loading branch information
Showing
33 changed files
with
1,028 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,96 @@ | ||
package io.customer.example | ||
|
||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.appcompat.app.AppCompatActivity | ||
import io.customer.base.comunication.Action | ||
import io.customer.base.data.ErrorResult | ||
import io.customer.base.data.Success | ||
import io.customer.sdk.CustomerIO | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import java.util.* | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContentView(R.layout.activity_main) | ||
// there are two ways to identify customer | ||
|
||
// 1st way | ||
// makeSynchronousRequest() | ||
|
||
// 2nd way | ||
// makeAsynchronousRequest() | ||
|
||
// log events | ||
makeEventsRequests() | ||
} | ||
|
||
private val outputCallback = Action.Callback<Unit> { result -> | ||
when (result) { | ||
is ErrorResult -> Log.v("ErrorResult", result.error.getDisplayMessage()) | ||
is Success -> Log.v("Success", "Success") | ||
} | ||
} | ||
|
||
data class Fol(val a: String, val c: Int) | ||
|
||
private fun makeEventsRequests() { | ||
CustomerIO.instance().track( | ||
name = "string event", | ||
attributes = mapOf( | ||
"value" to "string test", | ||
"target" to 1 | ||
) | ||
).enqueue(outputCallback) | ||
CustomerIO.instance().track( | ||
name = "int event", | ||
attributes = mapOf("value" to 1) | ||
).enqueue(outputCallback) | ||
CustomerIO.instance().track( | ||
name = "long event", | ||
attributes = mapOf("value" to 1L) | ||
).enqueue(outputCallback) | ||
CustomerIO.instance().track( | ||
name = "array event", | ||
attributes = mapOf("value" to listOf("1", "2")) | ||
).enqueue(outputCallback) | ||
CustomerIO.instance().track( | ||
name = "date event", | ||
attributes = mapOf("value" to Date()) | ||
).enqueue(outputCallback) | ||
CustomerIO.instance().track( | ||
name = "timestamp event", | ||
attributes = mapOf("value" to Date().time) | ||
).enqueue(outputCallback) | ||
CustomerIO.instance().track( | ||
name = "custom class event", | ||
attributes = mapOf("value" to Fol(a = "aa", c = 1)) | ||
) | ||
} | ||
|
||
private fun makeAsynchronousRequest() { | ||
CustomerIO.instance() | ||
.identify( | ||
identifier = "identifier", | ||
attributes = mapOf("email" to "testemail@email.com") | ||
).enqueue(outputCallback) | ||
} | ||
|
||
private fun makeSynchronousRequest() { | ||
CoroutineScope(Dispatchers.IO).launch { | ||
when ( | ||
val result = | ||
CustomerIO.instance() | ||
.identify("sample@email.com@email.com", mapOf("speed" to "slow")) | ||
.execute() | ||
) { | ||
is ErrorResult -> Log.v("ErrorResult", result.error.cause.toString()) | ||
is Success -> Log.v("Success", "Success") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
package io.customer.example | ||
|
||
import android.app.Application | ||
import io.customer.sdk.CustomerIO | ||
|
||
class MainApplication : Application() | ||
class MainApplication : Application() { | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
CustomerIO.Builder( | ||
siteId = "YOUR-SITE-ID", | ||
apiKey = "YOUR-API-KEY", | ||
appContext = this | ||
).build() | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
base/src/main/java/io/customer/base/comunication/Action.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.customer.base.comunication | ||
|
||
import androidx.annotation.WorkerThread | ||
import io.customer.base.data.Result | ||
|
||
/** | ||
* | ||
* [Action] can be used by following ways: | ||
* [execute]: Action may be executed synchronously or asynchronously with [enqueue] | ||
* | ||
*/ | ||
interface Action<T : Any> { | ||
/** | ||
* Synchronously send the request and return its response. Only call this from a background thread. | ||
*/ | ||
@WorkerThread | ||
public fun execute(): Result<T> | ||
|
||
/** | ||
* Asynchronously send the request and notify callback of its response. | ||
*/ | ||
fun enqueue(callback: Callback<T>) | ||
|
||
/** | ||
* Executes the call asynchronously, on a background thread. Safe to call from the main | ||
* thread. | ||
* | ||
* To get notified of the result and handle errors, use enqueue(callback) instead. | ||
*/ | ||
fun enqueue(): Unit = enqueue {} | ||
|
||
/** | ||
* Cancels the execution of the call, if cancellation is supported for the operation. | ||
* | ||
* Note that calls can not be cancelled when running them with [execute]. | ||
*/ | ||
fun cancel() | ||
|
||
fun interface Callback<T : Any> { | ||
fun onResult(result: Result<T>) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
package io.customer.base.error | ||
|
||
public open class ErrorDetail( | ||
public val message: String? = null, | ||
public val cause: Throwable = Throwable() | ||
) | ||
open class ErrorDetail( | ||
val message: String? = null, | ||
val statusCode: StatusCode = StatusCode.Unknown, | ||
val cause: Throwable = Throwable() | ||
) { | ||
fun getDisplayMessage(): String { | ||
if (message != null) { | ||
return message | ||
} | ||
if (statusCode != StatusCode.Unknown) { | ||
return statusCode.getMessage() | ||
} | ||
return cause.message ?: statusCode.getMessage() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.customer.base.error | ||
|
||
enum class StatusCode(val code: Int) { | ||
UnIdentifiedUser(1), | ||
|
||
Continue(100), | ||
SwitchingProtocols(101), | ||
Processing(102), | ||
|
||
OK(200), | ||
Created(201), | ||
Accepted(202), | ||
NonAuthoritativeInformation(203), | ||
NoContent(204), | ||
ResetContent(205), | ||
PartialContent(206), | ||
MultiStatus(207), | ||
AlreadyReported(208), | ||
IMUsed(226), | ||
|
||
MultipleChoices(300), | ||
MovedPermanently(301), | ||
Found(302), | ||
SeeOther(303), | ||
NotModified(304), | ||
UseProxy(305), | ||
TemporaryRedirect(307), | ||
PermanentRedirect(308), | ||
|
||
BadRequest(400), | ||
Unauthorized(401), | ||
PaymentRequired(402), | ||
Forbidden(403), | ||
NotFound(404), | ||
MethodNotAllowed(405), | ||
NotAcceptable(406), | ||
ProxyAuthenticationRequired(407), | ||
RequestTimeout(408), | ||
Conflict(409), | ||
Gone(410), | ||
LengthRequired(411), | ||
PreconditionFailed(412), | ||
PayloadTooLarge(413), | ||
URITooLong(414), | ||
UnsupportedMediaType(415), | ||
RangeNotSatisfiable(416), | ||
ExpectationFailed(417), | ||
IAmATeapot(418), | ||
MisdirectedRequest(421), | ||
UnprocessableEntity(422), | ||
Locked(423), | ||
FailedDependency(424), | ||
UpgradeRequired(426), | ||
PreconditionRequired(428), | ||
TooManyRequests(429), | ||
RequestHeaderFieldsTooLarge(431), | ||
UnavailableForLegalReasons(451), | ||
|
||
InternalServerError(500), | ||
NotImplemented(501), | ||
BadGateway(502), | ||
ServiceUnavailable(503), | ||
GatewayTimeout(504), | ||
HTTPVersionNotSupported(505), | ||
VariantAlsoNegotiates(506), | ||
InsufficientStorage(507), | ||
LoopDetected(508), | ||
NotExtended(510), | ||
NetworkAuthenticationRequired(511), | ||
|
||
Unknown(0); | ||
|
||
// Custom description for the Error to describe the error that happened. | ||
fun getMessage(): String { | ||
return when (this) { | ||
Unauthorized -> "HTTP request responded with 401. Configure the SDK with valid credentials." | ||
UnIdentifiedUser -> "Customer has not yet been identified." | ||
else -> this.name | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
package="io.customer.sdk"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
|
||
<application | ||
android:hardwareAccelerated="true" | ||
android:networkSecurityConfig="@xml/network_security_config" | ||
tools:targetApi="n" /> | ||
</manifest> |
Oops, something went wrong.