gpt4 book ai didi

android - 如何在 Android 应用程序中集成 Google Analytics

转载 作者:IT老高 更新时间:2023-10-28 23:00:49 24 4
gpt4 key购买 nike

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

developers.google.com link 1

some link 1

some link 2

developer.google link 2

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

我的代码如下:-

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

我的 Analytics Helper 类如下:-

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>

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

最佳答案

问题中给出的上述代码运行良好。您需要做的就是设置代码并添加 Jar 文件 Download Google Analytics Jar file 到你的图书馆。只需等待 24 到 48 小时。它会显示您注册的应用程序的所有事件和分析。

已编辑答案:-

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

Google 分析实现:-

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

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

现在在您的 onCreate(Bundle savedInstanceState) 方法中只需编写以下代码行。好吧,您还可以为任何 Listner 编写以下代码,例如在任何按钮单击上。

    /*
*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() 方法中,只需编写以下代码,它就会为您的 Activity 启动 Tracking and Analytics session 。

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

现在在您的 onStop() 方法中,只需编写以下代码,它将关闭或停止此 Activity 的跟踪 session 。

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

现在您可以在其中跟踪和分析您的应用程序和 Activity 。

关于android - 如何在 Android 应用程序中集成 Google Analytics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24782901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com