Google Analytics未显示活跃用户

时间:2015-10-06 05:04:48

标签: android google-analytics

我浏览了此文档https://developers.google.com/analytics/devguides/collection/android/v4/以获取Google Analytics,但无法显示活跃用户,但我知道了哪些活动。

这是Manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:name=".AnalyticsApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

现在这是我的MainActivity,我正在调用跟踪代码:

public class MainActivity extends AppCompatActivity {
private Tracker mTracker;
private final static String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Obtain the shared Tracker instance.

    AnalyticsApplication application = (AnalyticsApplication) getApplication();
    mTracker = application.getDefaultTracker();

    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendAnalytics();
        }
    });

}

private void sendAnalytics() {
    String name = MainActivity.class.getSimpleName();
    Log.i(TAG, "Setting screen name: " + name);
    mTracker.setScreenName("Image~" + name);
    Log.i(TAG,"Image Name: " + mTracker.toString());
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());



}

应用程序类:

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;


public class AnalyticsApplication extends Application {
private Tracker mTracker;


synchronized public Tracker getDefaultTracker() {
    if (mTracker == null) {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        GoogleAnalytics.getInstance(this).setLocalDispatchPeriod(15);
        // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
        mTracker = analytics.newTracker(R.xml.global_tracker);
    }
    return mTracker;
}

}

1 个答案:

答案 0 :(得分:3)

标题##在您AnalyticsApplication中进行如下更改..

public class AnalyticsApplication extends Application {

    private static Tracker mTracker;

    synchronized public Tracker getAnalyticsTracker() {
        if (mTracker == null) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            mTracker = analytics.newTracker(R.xml.global_tracker);
            mTracker.enableAutoActivityTracking(true);
            mTracker.enableExceptionReporting(true);
        }
        return mTracker;
    }
}

注册您的AnalyticsApplication In Manifest文件。还可以在AndroidManifest.xml

中添加以下服务和接收者
<application
    android:name=".AnalyticsApplication"
    .. >

    ...

    <!-- ANALYTICS -->
    <receiver
        android:name="com.google.android.gms.analytics.AnalyticsReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
        </intent-filter>
    </receiver>
    <service
        android:name="com.google.android.gms.analytics.AnalyticsService"
        android:enabled="true"
        android:exported="false" />

    ...

</application>

MainActivity

中使用以下跟踪器
// ANALYTICS
AnalyticsApplication mApplication = (AnalyticsApplication) getApplication();
mTracker = mApplication.getAnalyticsTracker();
mTracker.setScreenName("Main Activity");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());