Skip to content

Commit b94e5f6

Browse files
committed
LIBMOBILE-1116
- Added Bugsnag SDK dependency - Destination implementation - changes in README.md
1 parent 9407871 commit b94e5f6

File tree

4 files changed

+119
-5
lines changed

4 files changed

+119
-5
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Analytics-Kotlin Bugsnag
1+
# Analytics-Kotlin BugSnag
22

33
Add Bugsnag support to your applications via this plugin for [Analytics-Kotlin](https://github.com/segmentio/analytics-kotlin)
44

@@ -8,13 +8,30 @@ To install the Segment-bugsnag integration, simply add this line to your gradle
88
```
99
implementation 'com.segment.analytics.kotlin.destinations:bugsnag:<latest_version>'
1010
```
11-
1211
Or the following for Kotlin DSL
13-
1412
```
1513
implementation("com.segment.analytics.kotlin.destinations:bugsnag:<latest_version>")
1614
```
1715

16+
Also add the BugSnag Gradle plugin dependency to your Project project level build.gradle.
17+
```
18+
buildscript {
19+
dependencies {
20+
// ...
21+
classpath "com.bugsnag:bugsnag-android-gradle-plugin:7.4.1"
22+
}
23+
}
24+
```
25+
Or the following for Kotlin DSL
26+
```
27+
buildscript {
28+
dependencies {
29+
// ...
30+
classpath("com.bugsnag:bugsnag-android-gradle-plugin:7.4.1")
31+
}
32+
}
33+
```
34+
1835
## Using the Plugin in your App
1936

2037
Open the file where you setup and configure the Analytics-Kotlin library. Add this plugin to the list of imports.

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ buildscript {
2525
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0")
2626
classpath("org.jetbrains.kotlin:kotlin-serialization:1.6.0")
2727
classpath("com.android.tools.build:gradle:7.0.4")
28+
classpath("com.bugsnag:bugsnag-android-gradle-plugin:7.4.1")
2829
}
2930
}
3031

lib/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ dependencies {
5252

5353
// Partner Dependencies
5454
dependencies {
55-
// TODO add your partner deps here
55+
implementation("com.bugsnag:bugsnag-android:5.28.4")
5656
}
5757

5858
// Test Dependencies
Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,100 @@
11
package 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

Comments
 (0)