@@ -3,17 +3,20 @@ package com.segment.analytics.destinations.mydestination.testapp
33import android.app.Application
44import android.content.Context
55import android.util.Log
6+ import com.onetrust.otpublishers.headless.Public.OTCallback
67import com.onetrust.otpublishers.headless.Public.OTPublishersHeadlessSDK
7- import com.segment.analytics.kotlin.core.Analytics
8+ import com.onetrust.otpublishers.headless.Public.Response.OTResponse
89import com.segment.analytics.kotlin.android.Analytics
10+ import com.segment.analytics.kotlin.core.Analytics
911import com.segment.analytics.kotlin.core.platform.policies.CountBasedFlushPolicy
1012import com.segment.analytics.kotlin.core.platform.policies.FrequencyFlushPolicy
11- import com.segment.analytics.kotlin.destinations.consent.ConsentBlockingPlugin
12- import com.segment.analytics.kotlin.destinations.consent.ConsentManagementPlugin
13+ import com.segment.analytics.kotlin.destinations.consent.ConsentManager
1314import com.segment.analytics.kotlin.destinations.consent.onetrust.OneTrustConsentCategoryProvider
15+ import org.json.JSONException
16+ import org.json.JSONObject
1417import sovran.kotlin.SynchronousStore
1518
16- class MainApplication : Application () {
19+ class MainApplication : Application () {
1720
1821 companion object {
1922 const val TAG = " main"
@@ -25,48 +28,99 @@ class MainApplication: Application() {
2528 lateinit var otPublishersHeadlessSDK: OTPublishersHeadlessSDK
2629
2730 // Update these:
28- private const val SEGMENT_WRITE_KEY = " <Your Segment WRITEKEY>"
29- const val DOMAIN_URL = " <Your OneTrust Domain URL>"
30- const val DOMAIN_ID = " <Your OneTrust Domain ID>"
31- const val WEBHOOK_URL = " <Your webhook.site webhook url>"
31+ // private const val SEGMENT_WRITE_KEY = "<Your Segment WRITEKEY>"
32+ // const val DOMAIN_URL = "<Your OneTrust Domain URL>"
33+ // const val DOMAIN_ID = "<Your OneTrust Domain ID>"
34+ // const val WEBHOOK_URL = "<Your webhook.site webhook url>"
35+
36+
37+ private const val SEGMENT_WRITE_KEY = " Iqc4U4x6130y1XFCLIP40N3WSTHXkTPz"
38+ const val DOMAIN_URL = " cdn.cookielaw.org"
39+ // const val DOMAIN_ID = "72eadd89-8bfb-4d82-aed4-20a33299b6d1-test"
40+ const val DOMAIN_ID = " 14747048-7048-4b70-9aab-d1862f43f034-test"
41+ // const val DOMAIN_ID = "foo"
42+ const val WEBHOOK_URL = " https://webhook.site/9bbdf672-fd47-4235-8c81-922bc7145f22"
43+
44+
45+ const val LANGUAGE_CODE = " en"
46+ }
47+
48+
49+ private fun getGroupIds (domainGroupData : JSONObject ): List <String > {
50+ val result: MutableList <String > = ArrayList ()
51+ try {
52+ val groups = domainGroupData.getJSONArray(" Groups" )
53+ for (i in 0 until groups.length()) {
54+ val group = groups.getJSONObject(i)
55+ val groupId = group.getString(" OptanonGroupId" )
56+ result.add(groupId)
57+ }
58+ } catch (ex: JSONException ) {
59+ ex.printStackTrace()
60+ }
61+ return result
3262 }
33-
3463
3564 override fun onCreate () {
3665 super .onCreate()
3766
3867 appContext = this
39- otPublishersHeadlessSDK = OTPublishersHeadlessSDK (this )
4068
41- Analytics .debugLogsEnabled = true
4269
70+
71+ Analytics .debugLogsEnabled = true
4372 analytics = Analytics (SEGMENT_WRITE_KEY , applicationContext) {
4473 this .collectDeviceId = true
4574 this .trackApplicationLifecycleEvents = true
4675 this .trackDeepLinks = true
47- this .flushPolicies = listOf (
76+ this .flushPolicies = mutableListOf (
4877 CountBasedFlushPolicy (1 ), // Flush after each event
4978 FrequencyFlushPolicy (5000 ) // Flush after 5 Seconds
5079 )
5180 }
5281
53- // List of categories we care about; we will query OneTrust SDK locally on the status
54- // of these categories when stamping an event with consent status.
55- val categories = listOf<String >(" C0001" , " C0002" )
56- val consentCategoryProvider = OneTrustConsentCategoryProvider (otPublishersHeadlessSDK, categories)
82+
83+ analytics.add(WebhookPlugin (WEBHOOK_URL ))
84+
85+ otPublishersHeadlessSDK = OTPublishersHeadlessSDK (this )
86+
87+ val consentCategoryProvider = OneTrustConsentCategoryProvider (otPublishersHeadlessSDK)
5788 val store = SynchronousStore ()
58- val consentPlugin = ConsentManagementPlugin (store, consentCategoryProvider)
59- val consentBlockingPlugin = ConsentBlockingPlugin (" Segment.io" , store, true )
6089
61- // Add the Consent Plugin directly to analytics
90+ val consentPlugin = ConsentManager (store, consentCategoryProvider)
91+
6292 analytics.add(consentPlugin)
6393
94+ otPublishersHeadlessSDK.startSDK(
95+ DOMAIN_URL ,
96+ DOMAIN_ID ,
97+ LANGUAGE_CODE ,
98+ null ,
99+ false ,
100+ object : OTCallback {
101+ override fun onSuccess (otSuccessResponse : OTResponse ) {
102+ // do logic to render UI getOTSDKData();
103+ val otData =
104+ MainApplication .otPublishersHeadlessSDK.bannerData.toString()
105+ Log .d(TAG , " OT onSuccess: otData: $otData " )
106+
107+ val categories =
108+ getGroupIds(MainApplication .otPublishersHeadlessSDK.domainGroupData)
109+
110+ Log .d(TAG , " Setting up Analytics with categories: ${categories} " )
111+ consentCategoryProvider.setCategoryList(categories)
112+ consentPlugin.start()
113+ }
114+
115+ override fun onFailure (otErrorResponse : OTResponse ) {
116+ // Use below method to get errorCode and errorMessage.
117+ val errorCode = otErrorResponse.responseCode
118+ val errorDetails = otErrorResponse.responseMessage
119+ // Use toString() to log complete OT response
64120
65- // Add the WebhookPlugin that will post to given WEBHOOK_URL
66- val webhookDestinationPlugin = WebhookPlugin (WEBHOOK_URL )
67- // Add the webhook destination plugin into the main timeline
68- analytics.add(webhookDestinationPlugin)
69- // Add the blocking plugin to this destination
70- webhookDestinationPlugin.add(ConsentBlockingPlugin (" Webhook" , store))
121+ Log .i(TAG , otErrorResponse.toString())
122+ }
123+ }
124+ )
71125 }
72126}
0 commit comments