如何将Google Analytics整合到Android应用中?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (93)

我正在尝试整合Google Analytics,但我无法找到显示在我的Google Analytics帐户上的分析数据。

我的代码如下所示:

    public class MainActivity extends Activity {
    GoogleAnalytics tracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         // Get tracker.
        Tracker t = ((AnalyticsHelper) MainActivity.this.getApplication()).getTracker(
            TrackerName.APP_TRACKER);
     // Set the dispatch period in seconds.
        GAServiceManager.getInstance().setLocalDispatchPeriod(8);
    }

    @Override
    protected void onStart() {
        super.onStart();
        EasyTracker.getInstance(this).activityStart(this);
     // Set the dispatch period in seconds.
        GAServiceManager.getInstance().setLocalDispatchPeriod(8);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EasyTracker.getInstance(this).activityStop(this);
    }
}

我的Google Analytics帮助程序类如下所示:

public class AnalyticsHelper extends Application {

     // The following line should be changed to include the correct property id.
    private static final String PROPERTY_ID = "UA-xxxxxxxx-x"; // My Property id.

    public static int GENERAL_TRACKER = 0;

    public enum TrackerName {
        APP_TRACKER, // Tracker used only in this app.
        GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
        ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
    }

    HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

    public AnalyticsHelper()
    {
        super();
    }

    synchronized Tracker getTracker(TrackerName trackerId) {
        if (!mTrackers.containsKey(trackerId)) {

            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            analytics.getLogger().setLogLevel(LogLevel.VERBOSE);
            Tracker t = null;
            if(trackerId==TrackerName.APP_TRACKER){
                t= analytics.getTracker(PROPERTY_ID);
            }
                 mTrackers.put(trackerId, t);
                }
        return mTrackers.get(trackerId);
    }
}

我的xml目录中的分析xml文件如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
            tools:ignore="TypographyDashes">

     <!--  The following value should be replaced with correct property id. -->
    <string name="ga_trackingId">UA-xxxxxxxx-X</string>

   <!--Enable automatic activity tracking-->
  <bool name="ga_autoActivityTracking">true</bool>

  <!--Enable automatic exception tracking-->
  <bool name="ga_reportUncaughtExceptions">true</bool>

</resources>
提问于
用户回答回答于

你可以创建Google Analytics帐户并下载Jar文件后,将Jar文件添加到应用程序的lib文件夹中:-

对于分析部分,你只需要将analytic.xml文件包含在问题中提到的值文件夹中即可。

然后private EasyTracker easyTracker = null;在你的MainActivity中定义。

现在在你的onCreate(Bundle savedInstanceState)方法中,只需编写以下代码就行。那么你也可以为任何Listners编写下面的代码,例如任何Button Click:

    /*
    *For Google Analytics...
    */
    easyTracker = EasyTracker.getInstance(MainActivity.this); // It Tracks your Activity...
    easyTracker.send(MapBuilder.createEvent("SomeValue(StoryPage)",
            "SomeMoreValue(AuthorName) , "SomeMoreValueAgain(StoryTitle)", null).build()); //This line creates the event for keeping logs and other Analytical stuffs concerned to this Activity of Application...
//In the above example we had Tracked the session for the MainActivity and also Analysed how many time this activity was opened, which Author story and which Story was read.

现在在你的onStart()方法中,只需编写以下代码,它就会为你的活动启动跟踪和分析会话:

EasyTracker.getInstance(this).activityStart(this);

现在在你的onStop()方法中,只需编写以下代码,它将关闭或停止此活动的跟踪会话:

EasyTracker.getInstance(this).activityStop(this);
用户回答回答于

我有详细的步骤,谷歌Analytics集成到现有的应用。当我发布一个新的应用程序时,我回归参照这些运行良好的说明。

扫码关注云+社区