mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
incorporate androidlib
This commit is contained in:
parent
20cf3f0825
commit
5eb5cd1421
63 changed files with 1451 additions and 108 deletions
|
|
@ -0,0 +1,49 @@
|
|||
package com.geeksville.mesh.analytics
|
||||
|
||||
import com.google.firebase.analytics.FirebaseAnalytics
|
||||
|
||||
/**
|
||||
* Created by kevinh on 12/24/14.
|
||||
*/
|
||||
class DataPair(val name: String, valueIn: Any?) {
|
||||
val value = valueIn ?: "null"
|
||||
|
||||
/// An accumulating firebase event - only one allowed per event
|
||||
constructor(d: Double) : this(FirebaseAnalytics.Param.VALUE, d)
|
||||
constructor(d: Int) : this(FirebaseAnalytics.Param.VALUE, d)
|
||||
}
|
||||
|
||||
public interface AnalyticsProvider {
|
||||
|
||||
// Turn analytics logging on/off
|
||||
fun setEnabled(on: Boolean)
|
||||
|
||||
/**
|
||||
* Store an event
|
||||
*/
|
||||
fun track(event: String, vararg properties: DataPair): Unit
|
||||
|
||||
/**
|
||||
* Only track this event if using a cheap provider (like google)
|
||||
*/
|
||||
fun trackLowValue(event: String, vararg properties: DataPair): Unit
|
||||
|
||||
fun endSession(): Unit
|
||||
fun startSession(): Unit
|
||||
|
||||
/**
|
||||
* Set persistent ID info about this user, as a key value pair
|
||||
*/
|
||||
fun setUserInfo(vararg p: DataPair)
|
||||
|
||||
/**
|
||||
* Increment some sort of anyalytics counter
|
||||
*/
|
||||
fun increment(name: String, amount: Double = 1.0)
|
||||
|
||||
fun sendScreenView(name: String)
|
||||
fun endScreenView()
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
package com.geeksville.mesh.analytics
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import com.geeksville.mesh.android.AppPrefs
|
||||
import com.geeksville.mesh.android.GeeksvilleApplication
|
||||
import com.geeksville.mesh.android.Logging
|
||||
|
||||
/**
|
||||
* Implement our analytics API using firebase analtics
|
||||
*/
|
||||
class GoogleAnalytics(context: Context) : AnalyticsProvider, Logging {
|
||||
|
||||
val t = com.google.firebase.analytics.FirebaseAnalytics.getInstance(context)
|
||||
|
||||
init {
|
||||
val pref = AppPrefs(context)
|
||||
t.setUserId(pref.getInstallId())
|
||||
}
|
||||
|
||||
override fun setEnabled(on: Boolean) {
|
||||
t.setAnalyticsCollectionEnabled(on)
|
||||
}
|
||||
|
||||
override fun endSession() {
|
||||
track("End Session")
|
||||
// Mint.flush() // Send results now
|
||||
}
|
||||
|
||||
override fun trackLowValue(event: String, vararg properties: DataPair) {
|
||||
track(event, *properties)
|
||||
}
|
||||
|
||||
override fun track(event: String, vararg properties: DataPair) {
|
||||
debug("Analytics: track $event")
|
||||
|
||||
val bundle = Bundle()
|
||||
properties.forEach {
|
||||
when (it.value) {
|
||||
is Double -> bundle.putDouble(it.name, it.value)
|
||||
is Int -> bundle.putLong(it.name, it.value.toLong())
|
||||
is Long -> bundle.putLong(it.name, it.value)
|
||||
is Float -> bundle.putDouble(it.name, it.value.toDouble())
|
||||
else -> bundle.putString(it.name, it.value.toString())
|
||||
}
|
||||
}
|
||||
t.logEvent(event, bundle)
|
||||
}
|
||||
|
||||
override fun startSession() {
|
||||
debug("Analytics: start session")
|
||||
// automatic with firebase
|
||||
}
|
||||
|
||||
override fun setUserInfo(vararg p: DataPair) {
|
||||
p.forEach { t.setUserProperty(it.name, it.value.toString()) }
|
||||
}
|
||||
|
||||
override fun increment(name: String, amount: Double) {
|
||||
//Mint.logEvent("$name increment")
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a google analyics screen view event
|
||||
*/
|
||||
override fun sendScreenView(name: String) {
|
||||
debug("Analytics: start screen $name")
|
||||
GeeksvilleApplication.currentActivity?.let {
|
||||
t.setCurrentScreen(
|
||||
it, name, null
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun endScreenView() {
|
||||
// debug("Analytics: end screen")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
package com.geeksville.mesh.analytics
|
||||
|
||||
|
||||
import android.content.Context
|
||||
import com.geeksville.mesh.android.AppPrefs
|
||||
import com.geeksville.mesh.android.Logging
|
||||
import com.mixpanel.android.mpmetrics.MixpanelAPI
|
||||
import org.json.JSONObject
|
||||
|
||||
|
||||
class MixpanelAnalytics(context: Context, apiToken: String, pushToken: String? = null) :
|
||||
AnalyticsProvider, Logging {
|
||||
// Initialize the library with your
|
||||
// Mixpanel project token, MIXPANEL_TOKEN, and a reference
|
||||
// to your application context.
|
||||
// See mixpanel docs at https://mixpanel.com/help/reference/android
|
||||
val mixpanel: MixpanelAPI = MixpanelAPI.getInstance(context, apiToken)
|
||||
val people = mixpanel.getPeople()!!
|
||||
|
||||
init {
|
||||
// fixupMixpanel()
|
||||
|
||||
// Assign a unique ID
|
||||
val pref = AppPrefs(context)
|
||||
val id = pref.getInstallId()
|
||||
debug("Connecting to mixpanel $id")
|
||||
mixpanel.identify(id)
|
||||
people.identify(id)
|
||||
}
|
||||
|
||||
private fun makeJSON(properties: Array<out DataPair>) =
|
||||
if (properties.isEmpty())
|
||||
null
|
||||
else {
|
||||
val r = JSONObject()
|
||||
properties.forEach { r.put(it.name, it.value) }
|
||||
r
|
||||
}
|
||||
|
||||
override fun trackLowValue(event: String, vararg properties: DataPair) {
|
||||
}
|
||||
|
||||
override fun setEnabled(on: Boolean) {
|
||||
if (on) mixpanel.optInTracking() else mixpanel.optOutTracking()
|
||||
}
|
||||
|
||||
override fun track(event: String, vararg properties: DataPair) {
|
||||
|
||||
debug("Tracking $event")
|
||||
val obj = makeJSON(properties)
|
||||
|
||||
mixpanel.track(event, obj)
|
||||
}
|
||||
|
||||
override fun endSession() {
|
||||
// track("End Session")
|
||||
mixpanel.flush()
|
||||
}
|
||||
|
||||
override fun startSession() {
|
||||
track("Start Session")
|
||||
}
|
||||
|
||||
override fun setUserInfo(vararg p: DataPair) {
|
||||
mixpanel.registerSuperProperties(makeJSON(p))
|
||||
}
|
||||
|
||||
override fun increment(name: String, amount: Double) {
|
||||
mixpanel.people.increment(name, amount)
|
||||
}
|
||||
|
||||
override fun sendScreenView(name: String) {
|
||||
// too verbose for mixpanel
|
||||
track(name)
|
||||
}
|
||||
|
||||
override fun endScreenView() {}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package com.geeksville.mesh.analytics
|
||||
|
||||
|
||||
/**
|
||||
* Created by kevinh on 12/24/14.
|
||||
*/
|
||||
|
||||
// Mint.initAndStartSession(MyActivity.this, "01a9c628");
|
||||
|
||||
/* disable for now because something in gradle doesn't like their lib repo
|
||||
import com.splunk.mint.Mint
|
||||
|
||||
class SplunkAnalytics(val context: Context, apiToken: String) : AnalyticsProvider, Logging {
|
||||
|
||||
private var inited = false
|
||||
|
||||
init {
|
||||
try {
|
||||
Mint.initAndStartSession(context, apiToken)
|
||||
inited = true
|
||||
} catch(ex: Exception) {
|
||||
error("exception logging failed to init")
|
||||
}
|
||||
}
|
||||
|
||||
override fun endSession() {
|
||||
track("End Session")
|
||||
if (inited)
|
||||
Mint.closeSession(context)
|
||||
// Mint.flush() // Send results now
|
||||
}
|
||||
|
||||
override fun trackLowValue(event: String, vararg properties: DataPair) {
|
||||
}
|
||||
|
||||
override fun track(event: String, vararg properties: DataPair) {
|
||||
if (inited)
|
||||
Mint.logEvent(event)
|
||||
}
|
||||
|
||||
override fun startSession() {
|
||||
if (inited) {
|
||||
Mint.startSession(context)
|
||||
track("Start Session")
|
||||
}
|
||||
}
|
||||
|
||||
override fun setUserInfo(vararg p: DataPair) {
|
||||
if (inited)
|
||||
p.forEach { Mint.addExtraData(it.name, it.value.toString()) }
|
||||
}
|
||||
|
||||
override fun increment(name: String, amount: Double) {
|
||||
if (inited)
|
||||
Mint.logEvent("$name increment")
|
||||
}
|
||||
|
||||
override fun sendScreenView(name: String) {
|
||||
}
|
||||
|
||||
override fun endScreenView() {
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package com.geeksville.mesh.analytics
|
||||
|
||||
/**
|
||||
* Created by kevinh on 1/12/16.
|
||||
*/
|
||||
class TeeAnalytics(vararg providersIn: AnalyticsProvider) : AnalyticsProvider {
|
||||
|
||||
val providers = providersIn
|
||||
|
||||
override fun track(event: String, vararg properties: DataPair) {
|
||||
providers.forEach { it.track(event, *properties) }
|
||||
}
|
||||
|
||||
override fun trackLowValue(event: String, vararg properties: DataPair) {
|
||||
providers.forEach { it.trackLowValue(event, *properties) }
|
||||
}
|
||||
|
||||
override fun endSession() {
|
||||
providers.forEach { it.endSession() }
|
||||
}
|
||||
|
||||
override fun increment(name: String, amount: Double) {
|
||||
providers.forEach { it.increment(name, amount) }
|
||||
}
|
||||
|
||||
override fun setUserInfo(vararg p: DataPair) {
|
||||
providers.forEach { it.setUserInfo(*p) }
|
||||
}
|
||||
|
||||
override fun startSession() {
|
||||
providers.forEach { it.startSession() }
|
||||
}
|
||||
|
||||
override fun sendScreenView(name: String) {
|
||||
providers.forEach { it.sendScreenView(name) }
|
||||
}
|
||||
|
||||
override fun endScreenView() {
|
||||
providers.forEach { it.endScreenView() }
|
||||
}
|
||||
|
||||
override fun setEnabled(on: Boolean) {
|
||||
providers.forEach { it.setEnabled(on) }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue