Ad
How To Use Google Analytics In Android Kotlin
In my application i want to use Google analytics to show users log!
I write below codes, but after running application it does not show me any events on google analytics dashboard!
I have application class
and initialize google analytics code into this class and use this class in fragments
or activities
!
After set redPoint for debugging, show me null Tracker and Analytics!
My App class :
class App : Application() {
private var analytics: GoogleAnalytics? = null
override fun onCreate() {
super.onCreate()
//Google Analytics
analytics = GoogleAnalytics.getInstance(this)
//Fabric initialize kits
val fabric = Fabric.Builder(this)
.kits(Crashlytics())
.debuggable(true)
.build()
Fabric.with(fabric)
}
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
MultiDex.install(this)
}
@Synchronized
fun getDefaultTracker(): Tracker? {
var tracker: Tracker? = null
analytics = GoogleAnalytics.getInstance(context) // here pass your activity instance
analytics?.let {
tracker = it.newTracker(R.xml.global_tracker)
}
return tracker
}
fun googleTracker(category: String, action: String, label: String) {
val tracker: Tracker? = getDefaultTracker()
tracker?.send(
HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label)
.build()
)
}
}
Fragment codes :
App().googleTracker(ANALYTICS_SIGNUP_CATEGORY, ANALYTICS_SIGNUP_SIGNIN_ACTION, "")
Ad
Answer
you should initialize google analytics in this function:
@Synchronized
fun getDefaultTracker(): Tracker? {
var tracker: Tracker? = null
analytics = GoogleAnalytics.getInstance(context) // here pass your activity instance
analytics?.let {
tracker = it.newTracker(R.xml.global_tracker)
}
return tracker
}
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad