GA追踪代码详解


GA追踪代码详解  


GA的作用就是获取你的应用程序的用户使用量,用户的点击行为,事件,地点等,还可以统计应用崩溃的详情等。
首先进入谷歌www.google.com/analytics/的GA网址,然后注册一个账号,里面会有android和ios的接入指南:
https://developers.google.com/analytics/devguides/collection/android/v3/
https://developers.google.com/analytics/devguides/collection/ios/v3/
下面的这个是使用的API:
https://developers.google.com/analytics/devguides/collection/android/v3/#manifest
按照里面的步骤一步一步来就行了。需要注意的一点就是每个activity都要配置API中的onstart和onstop方法。
还需要在API指引的地方下载一个libGoogleAnalyticsServices.jar,引入工程就可以了。


下面是API的具体内容:
Google Analytics SDK for Android v3 (Beta) - Getting Started

This document describes how to get started using the Google Analytics SDK for Android v3.
Migrating from v1 or v2? Check out our Migration Guide to get started using v3.

    Before You Begin

	

    Getting Started
        1. Updating AndroidManifest.xml
        2. Adding EasyTracker methods
        3. Creating your analytics.xml file

	

    Next steps

Before you Begin

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

    Android developer SDK (available for Windows, Mac OS X, and Linux)
    Google Analytics SDK for Android v3 (with libGoogleAnalyticsServices.jar included in your project's /libs directory and build path)
    An Android app that you can use to implement the Google Analytics
    A new Google Analytics app property and view (profile).

Getting Started

There are three steps to getting started with the SDK:

    Update AndroidManifest.xml
    Add EasyTracker methods
    Create your analytics.xml file

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

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

1. Updating AndroidManifest.xml

Update your AndroidManifest.xmle file by adding the following permissions:

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

2. Adding EasyTracker methods

Add the send methods to the onStart() and onStop() methods of each of your Activities as in the following example:

package com.example.app;

import android.app.Activity;

import com.google.analytics.tracking.android.EasyTracker;

/**
 * An example Activity using Google Analytics and EasyTracker.
 */
public class myTrackedActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  @Override
  public void onStart() {
    super.onStart();
    ... // The rest of your onStart() code.
    EasyTracker.getInstance(this).activityStart(this);  // Add this method.
  }

  @Override
  public void onStop() {
    super.onStop();
    ... // The rest of your onStop() code.
    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
  }
}

3. Creating your analytics.xml file

When you use EasyTracker, global configuration settings are managed using resources defined in XML. Create a file called analytics.xml in your project'sres/values directory and add the following resources:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <!--Replace placeholder ID with your tracking ID-->
  <string name="ga_trackingId">UA-XXXX-Y</string>

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

  <!--Enable automatic exception tracking-->
  <bool name="ga_reportUncaughtExceptions">true</bool>
</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 the ga_trackingId string. Doing so will prevent you from seeing any data in your reports.

See the analytics.xml parameters reference for the complete list of parameters you can use to configure your implementation.

Congratulations! Your app is now setup to send data to Google Analytics.

①   对事件的操作和规范:

在onclick的事件点击方法中加入如下代码即可:

点击下载推荐应用界面按钮的点击下载事件的事例:

Tracker t = GoogleAnalytics.getInstance(this).getTracker("UA-xxxxxxxx-5");

         t.set(Fields.SCREEN_NAME, "推荐应用界面");

                   t.send(MapBuilder.createEvent("推荐应用", "下载最新版的apk","推荐应用-升级版", null).build());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值