您好我正在尝试集成Google Analytics,但我无法找到我的Google Analytics帐户中显示的任何分析数据。我正在使用下面提到的链接: -
但我无法获得结果,也无法获得正确的路径/方式或正确的详细教程,以了解如何在Android应用中集成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);
}
}
My 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>
任何帮助都将受到全心欢迎。 提前谢谢。
答案 0 :(得分:9)
问题中给出的上述代码效果很好。 您需要做的就是在设置代码并将Jar文件Download Google Analytics Jar file 添加到Lib之后。等待24至48小时。它会显示您已注册的应用程序的所有事件和分析。
创建Google Analytic帐户并下载Jar文件后,在应用程序的 lib 文件夹中添加Jar文件
Google Analytic Implementation: -
对于分析部分您只需要将 analytic.xml 文件包含在问题中提到的值文件夹中。
然后在 MainActivity 中定义private EasyTracker easyTracker = null;
。
现在在onCreate(Bundle savedInstanceState)
方法中,只需编写以下代码行。那么你也可以为任何Listners编写以下代码,例如在任何按钮单击。
/*
*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);
现在,您可以在其中跟踪和分析您的应用程序和活动。
答案 1 :(得分:2)
我详细介绍了将Google Analytics集成到现有应用here中的步骤。当我发布一个新的应用程序时,我总是回到这些说得很好的说明。
答案 2 :(得分:1)
Fisrt我们必须创建Google分析跟踪ID Goolge analytics sign in and enable api and get track id
之后将该跟踪ID放在.xml代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="ga_sessionTimeout">300</integer>
<bool name="ga_autoActivityTracking">true</bool>
<string name="ga_trackingId">"place your track id"</string>
<string name="ga_sampleFrequency">100.0</string>
<bool name="ga_reportUncaughtExceptions">true</bool>
<screenName name="com.aquadeals.seller.HomeMainActivity">DashBoard Screen</screenName>
3.之后我们可以更改清单文件非常重要,以便在下面添加此代码 添加权限
4. 将您的应用程序名称更改为google analytics类名称示例“MyApplication.java”
<application
android:name=".app.MyApplication"
5。之后添加使用互联网发送和接收广播事件的服务
<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" />
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
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" />
公共决赛阶段AnalyticsTrackers {
public enum Target {APP,}
private static AnalyticsTrackers sInstance;
public static synchronized void initialize(Context context)
{
if (sInstance != null)
{
throw new IllegalStateException("Extra call to initialize analytics trackers");
}
sInstance = new AnalyticsTrackers(context);
}
public static synchronized AnalyticsTrackers getInstance()
{
if (sInstance == null) {
throw new IllegalStateException("Call initialize() before getInstance()");
}
return sInstance;
}
private final Map<Target, Tracker> mTrackers = new HashMap<Target, Tracker>();
private final Context mContext;
private AnalyticsTrackers(Context context)
{
mContext = context.getApplicationContext();
}
public synchronized Tracker get(Target target)
{
if (!mTrackers.containsKey(target))
{
Tracker tracker;
switch (target)
{
case APP:
tracker = GoogleAnalytics.getInstance(mContext).newTracker(R.xml.app_tracker);
break;
default:
throw new IllegalArgumentException("Unhandled analytics target " + target);
}
mTrackers.put(target, tracker);
}
return mTrackers.get(target);
}
}
并添加此AnalyticsApplication.java
public class AnalyticsApplication extends MultiDexApplication
{
private Tracker mTracker;
private static AnalyticsApplication mInstance;
@Override
public void onCreate()
{
super.onCreate();
mInstance = this;
AnalyticsTrackers.initialize(this);
AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP);
}
synchronized public Tracker getDefaultTracker()
{
if (mTracker == null)
{
GoogleAnalytics analytics = GoogleAnalytics.getInstance(AnalyticsApplication.this);
mTracker = analytics.newTracker(R.xml.app_tracker);
}
return mTracker;
}
public synchronized Tracker getGoogleAnalyticsTracker()
{
AnalyticsTrackers analyticsTrackers = AnalyticsTrackers.getInstance();
return analyticsTrackers.get(AnalyticsTrackers.Target.APP);
}
public void trackEvent(String category, String action, String label)
{
Tracker t = getDefaultTracker();
t.send(new HitBuilders.EventBuilder().setCategory(category).setAction(action).setLabel(label).build());
}
public static synchronized AnalyticsApplication getInstance()
{
return mInstance;
}
public void trackScreenView(String screenName)
{
Tracker t = getGoogleAnalyticsTracker();
t.setScreenName(screenName);
t.send(new HitBuilders.ScreenViewBuilder().build());
GoogleAnalytics.getInstance(this).dispatchLocalHits();
}
public void trackException(Exception e)
{
if (e != null) {
Tracker t = getGoogleAnalyticsTracker();
t.send(new HitBuilders.ExceptionBuilder()
.setDescription( new StandardExceptionParser(this, null)
.getDescription(Thread.currentThread().getName(), e))
.setFatal(false)
.build()
);
}
}
}
最后我们可以在mainactivity.java类中添加代码,或者您需要类 初始化Step1
AnalyticsApplication application1;
私人追踪者mTracker;
步骤:2
application1 =(AnalyticsApplication)getApplication();
mTracker = application1.getDefaultTracker();
如果您在google analytics中显示此代码,则显示您在xml中提到的屏幕名称。
步骤:3为跟踪事件添加此代码
mTracker.send(new HitBuilders.EventBuilder()
.setCategory("DashBoard ")
.setAction("View Bookings Pressed")
.build());
步骤:4跟踪异常行为代码添加到catch块
catch (Exception e) {
** AnalyticsApplication.getInstance().trackException(e);**
e.printStackTrace();
}
快乐编码..