-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnalytics.kt
More file actions
86 lines (71 loc) · 2.79 KB
/
Analytics.kt
File metadata and controls
86 lines (71 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package info.hannes.countly
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.provider.Settings
import info.hannes.logcat.BuildConfig
import ly.count.android.sdk.Countly
import ly.count.android.sdk.CountlyConfig
class Analytics : IAnalytics {
var countlyInstance: Countly = Countly.sharedInstance()
private set
override fun isInitialized(): Boolean {
return countlyInstance.isInitialized
}
private fun toPair(vararg args: Any?) = args.map { param -> Pair(param.toString(), param.toString()) }
override fun recordEvent(event: String, vararg args: Any?) {
if (isInitialized()) {
countlyInstance.events().recordEvent(event, segmentation.plus(toPair(args)), 1)
}
}
override fun recordError(message: String, vararg args: Any?) {
if (isInitialized()) {
countlyInstance.crashes().recordHandledException(RuntimeException(message))
}
}
override fun recordError(throwable: Throwable) {
if (isInitialized()) {
countlyInstance.crashes().recordHandledException(throwable)
}
}
override fun recordWarning(message: String, vararg args: Any?) {
if (isInitialized()) {
countlyInstance.crashes().recordHandledException(RuntimeException(message))
}
}
override fun onStart(activity: Activity?) {
if (isInitialized()) {
countlyInstance.onStart(activity)
}
}
override fun onStop() {
if (isInitialized()) {
countlyInstance.onStop()
}
}
companion object {
val segmentation = mutableMapOf<String, Any>()
@SuppressLint("HardwareIds")
fun initAnalytics(context: Context, loggingEnabled: Boolean = false, countlyKey: String, countlyHost: String) {
var version = ""
try {
val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
version = pInfo.versionName.toString()
} catch (ignore: PackageManager.NameNotFoundException) {
}
segmentation["app_version"] = version
segmentation["logging_version"] = BuildConfig.VERSIONNAME
val config = CountlyConfig()
.setAppKey(countlyKey)
.setContext(context)
.setDeviceId(Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID))
.setServerURL(countlyHost)
.setLoggingEnabled(loggingEnabled)
.enableAutomaticViewTracking()
.setHttpPostForced(true)
.enableCrashReporting()
Countly.sharedInstance().init(config)
}
}
}