首页 > 解决方案 > 如何在我的 Android 应用中获取 Google 点击 ID (gclid)?

问题描述

我在 google adsense 中有一个针对我的 android 应用程序的广告,我知道当用户点击广告时,google 会创建包含用户信息的链接以进行跟踪。所以我想在其中获取该链接和 gclid 参数。我怎样才能做到这一点?

我已经尝试使用意图和 google play install referrer 来执行此操作,但意图返回 null,并且 google play install referrer 返回空字符串或有时 FEATURE_NOT_SUPPORTED

这是我获取代码的意图:

        val intent = this.intent
        val uri = intent?.data
        urlFromIntent = uri.toString()

显现:

       <receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
                  android:enabled="true"
                  android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>
        <service android:name="com.google.android.gms.analytics.CampaignTrackingService"
                 android:enabled="true"
                 android:exported="false" />

而我的 google play 安装引用代码:


        mRefferClient = InstallReferrerClient.newBuilder(this).build()
        mRefferClient.startConnection(object : InstallReferrerStateListener {

            @SuppressLint("SwitchIntDef")
            override fun onInstallReferrerSetupFinished(responseCode: Int) {
                when (responseCode) {
                    InstallReferrerClient.InstallReferrerResponse.OK -> {
                        try {
                            if (BuildConfig.DEBUG) Log.d("InstallReferrerState", "OK")
                            var response = mRefferClient.installReferrer
                            urlFromReferClient = response.installReferrer
                            urlFromReferClient += ";" + response.referrerClickTimestampSeconds
                            urlFromReferClient += ";" + response.installBeginTimestampSeconds
                            mRefferClient.endConnection()
                        } catch (e: RemoteException) {
                            urlFromReferClient = e.toString()
                        }
                    }
                    InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED -> {
                        urlFromReferClient = "FEATURE_NOT_SUPPORTED"
                }
                    InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE -> {
                        urlFromReferClient = "SERVICE_UNAVAILABLE"
                    }
                }
            }

            override fun onInstallReferrerServiceDisconnected() {
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
            }
        })

标签: androidfirebasegoogle-analyticsgoogle-ads-apiadsense

解决方案


推荐阅读