• 14
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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, "")

Debugging images :
enter image description here

enter image description here

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
}
  • 1
Reply Report