FROM:https://developers.google.com/analytics/devguides/collection/android/v4/#screen-view


Google Analytics SDK v4 for Android - Getting Started


This document describes how to get started using the Google Analytics SDK v4 for Android.

  1. Before You Begin
  1. Getting Started
    1. 1. Update AndroidManifest.xml
    2. 2. Initialize Trackers
    3. 3. Create a configuration XML file
    4. 4. Send a Screen View
  1. Next steps

Before you Begin

Before implementing the SDK, make sure you have the following:

  • Install the Android SDK
  • Download the Google Play Services SDK
  • At least one Google Analytics property and app view (profile) to which to send data from your app.
Note: The SDK can be used and will work on devices that do not have Google Play Services. In this case the SDK will automatically fall back to local dispatching.

Getting Started

There are three steps to getting started with the SDK:

  1. Update AndroidManifest.xml
  2. Initialize Trackers
  3. Create a Configuration XML file

This guide uses code snippets from the Mobile Playground sample application included with theGoogle Play Services SDK. The complete source for this project is available in:<android-sdk-directory>/extras/google/google_play_services/analytics/mobileplayground.

After completing these steps, you'll be able to measure the followingwith Google Analytics:

  • App installations
  • Active users and demographics
  • Screens and user engagement
  • Crashes and exceptions

1. Updating AndroidManifest.xml

Update your AndroidManifest.xml file by adding the followingpermissions:

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

2. Initialize Trackers

With the new SDK, developers should manage the trackers themselves. To ensure that the metrics are not over-counted, it is highly recommended that the tracker be created and managed in theApplication class.

In the following example, three trackers are created and represented byAPP_TRACKER, GLOBAL_TRACKER, ECOMMERCE_TRACKER. They are used throughout the application for different purposes.

 /**  * Enum used to identify the tracker that needs to be used for tracking.  *  * A single tracker is usually enough for most purposes. In case you do need multiple trackers,  * storing them all in Application object helps ensure that they are created only once per  * application instance.  */  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>();

Next, the Application class can provide a method to get the tracker that is requested and can create them on demand if needed. Note that the tracker can be created from aPROPERTY_ID using analytics.newTracker(PROPERTY_ID) or it can be created from a xml resource file asanalytics.newTracker(R.xml.global_tracker).

 synchronized Tracker getTracker(TrackerName trackerId) {   if (!mTrackers.containsKey(trackerId)) {    GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);    Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)      : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)        : analytics.newTracker(R.xml.ecommerce_tracker);    mTrackers.put(trackerId, t);   }   return mTrackers.get(trackerId);  }

3. Create a configuration XML file

Configuration settings can be managed using resources defined in XML. For example, if you have a global tracker you could create a file calledglobal_tracker.xml in your project's res/xml directory and add the following resources:

<?xml version="1.0" encoding="utf-8"?> <resources>  <integer name="ga_sessionTimeout">300</integer>   <!-- Enable automatic Activity measurement -->   <bool name="ga_autoActivityTracking">true</bool>   <!-- The screen names that will appear in reports -->   <screenName name="com.google.android.gms.analytics.samples.mobileplayground.ScreenviewFragment">     AnalyticsSampleApp ScreenView   </screenName>   <screenName name="com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment">     AnalyticsSampleApp EcommerceView   </screenName>   <!-- The following value should be replaced with correct property id. -->   <string name="ga_trackingId">UA-XXXXXXX-Y</string> </resources> 

Your lint checker may warn you about the use of the figure dash ('-') in your tracking ID. You can suppress that warning by adding additional attributes to your<resources> tag:

<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">
Important: Do not encode dashes in thega_trackingId string. Doing so will prevent you from seeing anydata in your reports.

See the configuration parameters reference for thecomplete list of parameters you can use to configure your implementation.

Your app is now setup to send data to GoogleAnalytics.

4. Send a Screen View

To send a screen view, set the screen field values on the tracker, then send the hit:

    // Get tracker.     Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(       TrackerName.APP_TRACKER);     // Set screen name.     // Where path is a String representing the screen name.     t.setScreenName(path);     // Send a screen view.     t.send(new HitBuilders.AppViewBuilder().build());

Next steps

You can do much more with Google Analytics, including measuringcampaigns, in-app payments and transactions, and user interaction events.

Review the MobileApp Implementation Guide for an overview of how to use Google Analytics tomeasure user interactions and answer questions about app usage.

The following developer guides provide additional details on how to implementGoogle Analytics features in your app:

  • Advanced Configuration – Learn more about advanced configuration options, including using multiple trackers.
  • Measuring Campaigns – Learn how to implement campaign measurement to understand which channels and campaigns are driving app installs.
  • Measuring Events – Learn how to measure user engagement with interactive content like buttons, videos, and other media using Events.
  • Measuring In-App Payments – Learn how to measure in-app payments and transactions.
  • User timings – Learn how to measure user timings in your app to measure load times, engagement with media, and more.
  • Configuration Parameters – See the complete list of configuration parameters.

本页面中的内容已获得知识共享署名3.0许可,并且代码示例已获得Apache 2.0许可;另有说明的情况除外。有关详情,请参阅我们的网站政策。

Last updated 九月 11, 2014.


展示:






Demo :http://pan.baidu.com/s/1hqEFOyS

更多相关文章

  1. 获得位置信息 android
  2. Android(安卓)官方通知工具类Notification
  3. Android/iOS内嵌Unity开发示例
  4. Flutter下载更新App的方法示例
  5. Android之——多线程断点续传下载示例
  6. Android如何使布局中图(ImageButton)和文字(TextView)同时获得press
  7. Android/iOS内嵌Unity开发示例
  8. Android(安卓)应用程序(APK) 如何获得系统签名权限 强制关闭程序
  9. Android入门教程四十二之GridView(网格视图)的基本使用

随机推荐

  1. 对android NDK的认识
  2. Android(安卓)ColorDrawable那些事
  3. android Shape Drawable美化圆角图形
  4. android service 本地 远程 总结
  5. Android中MQTT的简单实现(只是连接到服务
  6. 关于ADT中“空指针”异常的处理
  7. android 开发 Activity 与intent
  8. 实现在Android简单封装类似JQuery异步请
  9. [Android(安卓)UI界面] ListView与arrays
  10. IOS和Android的区别[转]