Skip to content

Commit 6690706

Browse files
authored
Update to latest analytics-kotlin 1.16.1 and make the testapp more useful by adding the consent plugin to the timeline. (#10)
1 parent 0d7d23d commit 6690706

File tree

8 files changed

+114
-4
lines changed

8 files changed

+114
-4
lines changed

lib/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ android {
4242

4343
dependencies {
4444
implementation("com.segment:sovran-kotlin:1.3.1")
45-
implementation("com.segment.analytics.kotlin:android:1.14.0")
45+
implementation("com.segment.analytics.kotlin:android:1.16.1")
4646
implementation("androidx.multidex:multidex:2.0.1")
4747
implementation("androidx.core:core-ktx:1.10.1")
4848

testapp/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ android {
3838

3939
dependencies {
4040
implementation(project(mapOf("path" to ":lib")))
41-
41+
implementation("com.segment:sovran-kotlin:1.3.1")
42+
implementation("com.segment.analytics.kotlin:android:1.16.1")
4243
implementation("androidx.core:core-ktx:1.7.0")
4344
implementation("androidx.appcompat:appcompat:1.4.1")
4445
implementation("com.google.android.material:material:1.6.0")

testapp/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
33

4+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
47
<application
8+
android:name=".MainApplication"
59
android:allowBackup="true"
610
android:icon="@mipmap/ic_launcher"
711
android:label="@string/app_name"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.segment.analytics.destinations.mydestination.testapp
2+
3+
import com.segment.analytics.kotlin.destinations.consent.ConsentCategoryProvider
4+
5+
/**
6+
* A toy implementation of a ConsentCategoryProvider. Normally, an implementation would
7+
* be tied to a CMP tool like OneTrust and get information regarding consent from it, but
8+
* this will work for our simple use-case.
9+
*/
10+
class CustomConsentCategoryProvider: ConsentCategoryProvider {
11+
var categories: List<String>? = null
12+
override fun setCategoryList(categories: List<String>) {
13+
this.categories = categories
14+
}
15+
16+
override fun getCategories(): Map<String, Boolean> {
17+
val result = mutableMapOf<String,Boolean>()
18+
19+
// Allow all categories
20+
categories?.forEach { cat -> result.put(cat, true) }
21+
22+
return result
23+
}
24+
}

testapp/src/main/java/com/segment/analytics/destinations/mydestination/testapp/MainActivity.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,29 @@ package com.segment.analytics.destinations.mydestination.testapp
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.widget.Button
6+
import com.google.android.material.snackbar.Snackbar
7+
import java.text.SimpleDateFormat
8+
import java.util.Date
59

610
class MainActivity : AppCompatActivity() {
11+
12+
companion object {
13+
val TAG = "main-activity"
14+
}
715
override fun onCreate(savedInstanceState: Bundle?) {
816
super.onCreate(savedInstanceState)
917
setContentView(R.layout.activity_main)
18+
19+
val button = findViewById<Button>(R.id.sendTrackEventButton)
20+
button.setOnClickListener {v ->
21+
val sdf = SimpleDateFormat("M/dd/yyyy hh:mm:ss.SSS")
22+
val currentDate = sdf.format(Date())
23+
MainApplication.analytics.track("Test Event ${currentDate}")
24+
25+
if (v != null) {
26+
Snackbar.make(v, "Track Event created.", Snackbar.LENGTH_LONG).show()
27+
}
28+
}
1029
}
1130
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.segment.analytics.destinations.mydestination.testapp
2+
3+
import android.app.Application
4+
import com.segment.analytics.kotlin.core.Analytics
5+
import com.segment.analytics.kotlin.android.Analytics
6+
import com.segment.analytics.kotlin.core.platform.policies.CountBasedFlushPolicy
7+
import com.segment.analytics.kotlin.core.platform.policies.FrequencyFlushPolicy
8+
import com.segment.analytics.kotlin.destinations.consent.ConsentManager
9+
import org.json.JSONException
10+
import org.json.JSONObject
11+
import sovran.kotlin.SynchronousStore
12+
13+
14+
class MainApplication: Application() {
15+
16+
val SEGMENT_WRITE_KEY = "<writekey>"
17+
companion object {
18+
lateinit var analytics: Analytics
19+
const val TAG = "application"
20+
}
21+
22+
private fun getGroupIds(domainGroupData: JSONObject): List<String> {
23+
val result: MutableList<String> = ArrayList()
24+
try {
25+
val groups = domainGroupData.getJSONArray("Groups")
26+
for (i in 0 until groups.length()) {
27+
val group = groups.getJSONObject(i)
28+
val groupId = group.getString("OptanonGroupId")
29+
result.add(groupId)
30+
}
31+
} catch (ex: JSONException) {
32+
ex.printStackTrace()
33+
}
34+
return result
35+
}
36+
37+
override fun onCreate() {
38+
super.onCreate()
39+
40+
Analytics.debugLogsEnabled = true
41+
analytics = Analytics(SEGMENT_WRITE_KEY, applicationContext) {
42+
this.collectDeviceId = true
43+
this.trackApplicationLifecycleEvents = true
44+
this.trackDeepLinks = true
45+
this.flushPolicies = mutableListOf(
46+
CountBasedFlushPolicy(1), // Flush after each event
47+
FrequencyFlushPolicy(5000) // Flush after 5 Seconds
48+
)
49+
}
50+
51+
val consentCategoryProvider = CustomConsentCategoryProvider()
52+
val store = SynchronousStore()
53+
54+
val consentPlugin = ConsentManager(store, consentCategoryProvider)
55+
56+
analytics.add(consentPlugin)
57+
}
58+
59+
60+
}

testapp/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<TextView
9+
<Button
10+
android:id="@+id/sendTrackEventButton"
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13+
android:text="@string/send_track_event"
1314
app:layout_constraintBottom_toBottomOf="parent"
1415
app:layout_constraintLeft_toLeftOf="parent"
1516
app:layout_constraintRight_toRightOf="parent"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<resources>
22
<string name="app_name">testApp</string>
3+
<string name="send_track_event">Send Track Event</string>
34
</resources>

0 commit comments

Comments
 (0)