如何在 Android 应用程序中集成 Google Analytics [英] How to integrate Google Analytics in Android app

查看:31
本文介绍了如何在 Android 应用程序中集成 Google Analytics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试集成 Google Analytics,但我找不到任何显示在我的 Google Analytics 帐户上的分析数据.我正在使用下面提到的链接:-

Hi I am trying to integrate the Google Analytics but I am not able to find any analytics data that shows on the Google Analytics Account of mine. I am using the link mention below:-

developers.google.com 链接 1

一些链接 1

一些链接 2

developer.google 链接 2

但我无法获得结果,也无法获得有关如何将 Google Analytics 集成到 android 应用程序的正确路径/方式或正确且详细的教程.

But I am not able to get the result, nor the correct path/way or proper and Detailed Tutorial for how to integrate the Google Analytics in android app.

我的代码如下:-

    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);
    }
}

我的分析助手类如下:-

My Analytics Helper class is as follows:-

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文件如下:-

And my analytics xml file in xml directory is as follows:-

    <?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>

我们将衷心欢迎任何帮助.提前致谢.

Any help will be heart-fully welcomed. Thanks in advance.

推荐答案

问题中给出的上述代码运行良好.您需要做的就是在设置代码并添加 Jar 文件之后 将 Google Analytics Jar 文件 下载到您的 Lib.只需等待 24 到 48 小时.它会显示您注册的应用程序的所有事件和分析.

Well the above code given in the Question works well. All you need to do is after setting your code and adding the Jar file Download Google Analytics Jar file to your Lib. just wait for 24 to 48 hours. and it would show all the events and analytic for the App you had registered.

创建 Google Analytic 帐户并下载 Jar 文件后,将 Jar 文件添加到应用程序的 lib 文件夹中

Once You had Created your Google Analytic account and Downloaded the Jar file, Add the Jar file in your lib folder of your Application

Google 分析实施:-

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

Well For Analytic Part You Just need the analytic.xml file to be included in your values folder which is mentioned in the Question.

然后在您的MainActivity中定义private EasyTracker easyTracker = null;.

现在在您的 onCreate(Bundle savedInstanceState) 方法中只需编写以下代码行.那么你也可以为任何 Listners 编写以下代码,例如点击任何按钮.

And now in your onCreate(Bundle savedInstanceState) method just write the following lines of Code. Well you can also write the following code for any Listners e.g. on any 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() 方法中,只需编写以下代码,它就会为您的活动启动跟踪和分析会话.

Now in your onStart() Method, just write the following code, it starts the Tracking and Analytics session for your Activity.

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

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

And now in your onStop() Method, just write the following code, it will close or stop the Tracking session for this activity.

EasyTracker.getInstance(this).activityStop(this);

现在您可以跟踪和分析您的应用程序和其中的活动.

Now you are able to Track and Analysis your Application and Activities in It.

这篇关于如何在 Android 应用程序中集成 Google Analytics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆