在Android App中实施Google Analytics(分析)

时间:2018-12-23 04:11:56

标签: android firebase google-analytics firebase-analytics

尝试在Android App中实施Google Analytics(分析)(不提供Firebase)。看起来一切都很好。没有错误,请按照说明进行操作。但是不起作用。请帮助我在哪里可以找到错误?

我使用的指南 https://developers.google.com/analytics/devguides/collection/android/v4/?hl=en

我使用的代码示例 https://github.com/googleanalytics/cute-pets-example-android-app

我的应用程序类

class App : Application() {

private var mUserDatabase: DatabaseReference? = null
private var mAuth: FirebaseAuth? = null
private lateinit var sAnalytics: GoogleAnalytics
lateinit var sTracker: Tracker


val component: AppComponent by lazy {
    DaggerAppComponent.builder().appModule(AppModule(this)).build()
}

object AnalyticsTracker {
    lateinit var sTracker: Tracker
    private lateinit var sAnalytics: GoogleAnalytics

    fun onCreate(context: Context) {
        sAnalytics = GoogleAnalytics.getInstance(context)
        sTracker = sAnalytics.newTracker(context.getString(R.string.tracker_id))
        sTracker.enableExceptionReporting(true)
        sTracker.enableAdvertisingIdCollection(true)
        sTracker.enableAutoActivityTracking(true)
        sTracker.send(HitBuilders.ScreenViewBuilder().setCustomDimension(1, null).build())
    }
}

override fun onCreate() {
    super.onCreate()

    component.inject(this)

    AnalyticsTracker.onCreate(this)

    mAuth = FirebaseAuth.getInstance()

    if (mAuth!!.currentUser != null) {
        FirebaseDatabase.getInstance().setPersistenceEnabled(true)
        mUserDatabase = FirebaseDatabase.getInstance().reference
                .child("Users").child(mAuth!!.currentUser!!.uid)
        //TODO fix-1
        mUserDatabase!!.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                if (dataSnapshot != null) {
                    mUserDatabase!!.child("online").onDisconnect().setValue(ServerValue.TIMESTAMP)
                    mUserDatabase!!.child("online").setValue(true)
                } else {
                }
            }

            override fun onCancelled(databaseError: DatabaseError) {}
        })

    }

    //todo: provide picasso by dagger
    val builder = Picasso.Builder(this)
    builder.downloader(OkHttpDownloader(this, Integer.MAX_VALUE.toLong()))
    val built = builder.build()
    built.setIndicatorsEnabled(true)
    built.isLoggingEnabled = true
    Picasso.setSingletonInstance(built)


}


/**
 * Gets the default [Tracker] for this [Application].
 * @return tracker
 */
@Synchronized
fun getTracker(): Tracker = GoogleAnalytics.getInstance(this).newTracker(R.xml.global_tracker)

}

我如何执行和发送动作两个示例

 val eventTracker = App.AnalyticsTracker.sTracker
    val eventBuilder = HitBuilders.EventBuilder()
            .setCategory(getString(R.string.conversion_one))
            .setAction(getString(R.string.registration_started))
    eventTracker.send(eventBuilder.build())


    val tracker = App.AnalyticsTracker.sTracker
    tracker.setScreenName("AUTH Activity")
    tracker.send(HitBuilders.ScreenViewBuilder().build());

0 个答案:

没有答案