11package com.segment.analytics.kotlin.destinations.bugsnag
22
3- class BugsnagDestination {
3+ import android.app.Activity
4+ import android.content.Context
5+ import android.os.Bundle
6+ import com.bugsnag.android.Client
7+ import com.segment.analytics.kotlin.android.plugins.AndroidLifecycle
8+ import com.segment.analytics.kotlin.core.*
9+ import com.segment.analytics.kotlin.core.platform.DestinationPlugin
10+ import com.segment.analytics.kotlin.core.platform.Plugin
11+ import com.segment.analytics.kotlin.core.platform.plugins.logger.log
12+ import com.segment.analytics.kotlin.core.utilities.toContent
13+ import kotlinx.serialization.Serializable
14+ import kotlinx.serialization.json.JsonObject
15+
16+ class BugsnagDestination : DestinationPlugin (), AndroidLifecycle {
17+ companion object {
18+ private const val BUGSNAG_FULL_KEY = " Bugsnag"
19+ private const val VIEWED_EVENT_FORMAT = " Viewed %s Screen"
20+ }
21+ internal var bugsnagSettings: BugsnagSettings ? = null
22+
23+ override val key: String = BUGSNAG_FULL_KEY
24+ internal var client: Client ? = null
25+
26+ override fun update (settings : Settings , type : Plugin .UpdateType ) {
27+ super .update(settings, type)
28+ if (settings.hasIntegrationSettings(this )) {
29+ this .bugsnagSettings =
30+ settings.destinationSettings(key, BugsnagSettings .serializer())
31+ if (type == Plugin .UpdateType .Initial ) {
32+ if (bugsnagSettings!= null ) {
33+ analytics.log(" Client(context, ${bugsnagSettings?.apiKey} )" )
34+ initializeBugsnagClient()
35+ }
36+ }
37+ }
38+ }
39+
40+ override fun identify (payload : IdentifyEvent ): BaseEvent {
41+ val traitsMap = payload.traits.asStringMap()
42+ val email: String = traitsMap[" email" ] ? : " "
43+ val name: String = traitsMap[" name" ]? : " "
44+ client?.setUser(payload.userId, email, name)
45+ val userKey = " User"
46+ for ((key, value) in traitsMap) {
47+ client?.addMetadata(userKey, key, value)
48+ analytics.log(" client!!.addMetadata($userKey , $key , $value )" )
49+ }
50+ return payload
51+ }
52+
53+ override fun screen (payload : ScreenEvent ): BaseEvent {
54+ leaveBreadcrumb(String .format(VIEWED_EVENT_FORMAT , payload.name))
55+ return payload
56+ }
57+
58+ override fun track (payload : TrackEvent ): BaseEvent {
59+ leaveBreadcrumb(payload.event)
60+ return payload
61+ }
62+
63+ override fun onActivityCreated (activity : Activity ? , savedInstanceState : Bundle ? ) {
64+ super .onActivityCreated(activity, savedInstanceState)
65+ analytics.log(" client.context = ${activity?.localClassName} " )
66+ client?.context = activity?.localClassName
67+
68+ }
69+
70+ private fun initializeBugsnagClient ():Client {
71+ if (client == null ) {
72+ client = Client (analytics.configuration.application as Context , bugsnagSettings!! .apiKey)
73+ client!! .context = (analytics.configuration.application as Context ).packageName
74+ }
75+ return client!!
76+ }
77+ private fun leaveBreadcrumb (event : String ) {
78+ client?.leaveBreadcrumb(event)
79+ analytics.log(" client?.leaveBreadcrumb($event )" )
80+ }
81+
82+ fun getUnderlyingInstance (): Client ? {
83+ return client
84+ }
85+ }
86+ /* *
87+ * Bugsnag Settings data class.
88+ */
89+ @Serializable
90+ data class BugsnagSettings (
91+ // Bugsnag API key
92+ var apiKey : String ,
93+ // Distinguish errors that happen in different stages of app's release process e.g 'production', 'development', etc.
94+ var releaseStage : String ,
95+ // Use SSL When Sending Data to Bugsnag, by default enabled in Segment dashboard
96+ var useSSL : Boolean
97+ )
98+ private fun JsonObject.asStringMap (): Map <String , String > = this .mapValues { (_, value) ->
99+ value.toContent().toString()
4100}
0 commit comments