Skip to content

Instantly share code, notes, and snippets.

@ShivamDev31
Last active March 29, 2019 06:34
Show Gist options
  • Save ShivamDev31/ce7597f0f3545baf0f820e807ffe5f7d to your computer and use it in GitHub Desktop.
Save ShivamDev31/ce7597f0f3545baf0f820e807ffe5f7d to your computer and use it in GitHub Desktop.
Implementation of Krux including user consents.
package com.newsuk.analytics
import android.app.Application
import android.os.Bundle
import com.krux.androidsdk.aggregator.KruxEventAggregator
import com.newsuk.rx.SchedulingStrategy
import com.newsuk.thesun.mobile.BuildConfig
import com.newsuk.thesun.mobile.advert.DeviceIdProvider
interface KruxConsentProvider {
fun init()
fun sendKruxConsentSignals()
}
private const val CONFIG_ID = BuildConfig.KRUX_CONFIG_ID
class DefaultKruxConsentProvider(private val context: Application,
private val deviceIdProvider: DeviceIdProvider,
private val schedulingStrategyFactory: SchedulingStrategy.Factory) : KruxConsentProvider {
override fun init() {
KruxEventAggregator.initialize(context, CONFIG_ID, null, BuildConfig.DEBUG, DefaultKruxConsentCallback())
sendKruxConsentSignals()
}
override fun sendKruxConsentSignals() {
deviceIdProvider.get()
.compose(schedulingStrategyFactory.create())
.subscribe { deviceId -> setKruxConxentSignals(deviceId.id().get()) }
.dispose()
}
private fun setKruxConxentSignals(googleAdvertisementId: String) {
val consentGetAttributes = getIdParameters(googleAdvertisementId)
val idAttributes = getIdParameters(googleAdvertisementId)
val consentSetAttributes = getConsentAttributes()
addPolicyRegimeParameter(consentGetAttributes)
addPolicyRegimeParameter(consentSetAttributes)
KruxEventAggregator.consentSetRequest(consentSetAttributes)
KruxEventAggregator.consentGetRequest(consentGetAttributes)
KruxEventAggregator.consumerRemoveRequest(idAttributes)
KruxEventAggregator.consumerPortabilityRequest(idAttributes)
}
private fun addPolicyRegimeParameter(attributes: Bundle) {
attributes.putString("pr", "gdpr")
}
private fun getIdParameters(googleAdvertisementId: String): Bundle {
val attributeBundle = Bundle()
attributeBundle.putString("idv", googleAdvertisementId)
attributeBundle.putString("dt", "aaid")
attributeBundle.putString("idt", "device")
return attributeBundle
}
private fun getConsentAttributes(): Bundle {
val attributeBundle = Bundle()
attributeBundle.putInt("dc", 1)
attributeBundle.putInt("cd", 1)
attributeBundle.putInt("tg", 1)
attributeBundle.putInt("al", 1)
attributeBundle.putInt("sh", 1)
attributeBundle.putInt("re", 1)
return attributeBundle
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment