首先是官网的地址:http://dev.umeng.com/analytics/android-doc/integration#2_5
然后是注意实例化到Application中的代码:
private void setUMEngAnalsys() {
//开启Debug模式 上线可关闭
MobclickAgent.setDebugMode(true);
// SDK在统计Fragment时,需要关闭Activity自带的页面统计,
// 然后在每个页面中重新集成页面统计的代码(包括调用了 onResume 和 onPause 的Activity)。
MobclickAgent.openActivityDurationTrack(false);
MobclickAgent.setAutoLocation(true);
// MobclickAgent.setSessionContinueMillis(1000);
MobclickAgent.updateOnlineConfig(this);
}
权限和key的配置参照官网文档 应该没问题:
在Gradle依赖中添加:
dependencies {
compile 'com.umeng.analytics:analytics:latest.integration'}
如果无法正常集成请添加如下代码 :
allprojects {
repositories {
mavenCentral()
}
}
manifest的配置主要包括添加权限,填写Appkey和填写渠道id三部分,代码示例如下:
<manifest……> <uses-sdk android:minSdkVersion="4"></uses-sdk> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <application ……> …… <activity ……/> <meta-data android:value="YOUR_APP_KEY" android:name="UMENG_APPKEY"></meta-data> <meta-data android:value="Channel ID" android:name="UMENG_CHANNEL"/> </application> </manifest>
最后举例说明:
1.
MobclickAgent.onEvent(Context context, String eventId);
context 指当前的Activity
eventId 为当前统计的事件ID。
示例:统计微博应用中"转发"事件发生的次数,那么在转发的函数里调用
MobclickAgent.onEvent(mContext,"Forward");
2.
MobclickAgent.onEvent(Context context, String eventId, HashMap map);
map 为当前事件的属性和取值(Key-Value键值对)。
示例:统计电商应用中“购买”事件发生的次数,以及购买的商品类型及数量,那么在购买的函数里调用:
HashMap<String,String> map = new HashMap<String,String>();
map.put("type","book");
map.put("quantity","3");
MobclickAgent.onEvent(mContext, "purchase", map);
3.
MobclickAgent.onEventValue(Context context, String id, Map<String,String> m, int du)
id 为事件ID
map 为当前事件的属性和取值
du 为当前事件的数值为当前事件的数值,取值范围是-2,147,483,648 到 +2,147,483,647 之间的有符号整数,即int 32类型,如果数据超出了该范围,会造成数据丢包,影响数据统计的准确性。
示例:统计一次音乐播放,包括音乐类型,作者和播放时长,可以在音乐播放结束后这么调用:
int duration = 12000; //开发者需要自己计算音乐播放时长
Map<String, String> map_value = new HashMap<String, String>();
map_value.put("type" , "popular" );
map_value.put("artist" , "JJLin" );
MobclickAgent.onEventValue(this, "music" , map_value, duration);
这里注意一个地方如果要统计类似分类GridView的类别信息,比如这样的图:
参照如下方式:
KeshiTongji.java
package com.kangxin.patient.module;
import java.util.HashMap;
public class KeshiTongji {
public static final HashMap<String, String> map = new HashMap<String, String>();
public static void addmap() {
map.put("6", "neurology");// 神经内科
map.put("5", "neurosurgery");// 神经外科
map.put("19", "ophthalmology");// 眼科
map.put("16", "ENT");// 耳鼻咽喉科
map.put("43", "Stom atology");// 口腔科
map.put("25", "Head and neck surgery");// 头颈外科
map.put("12", "Thoracic surgery");// 胸外科
map.put("33", "Cardiac surgery");// 心脏外科
map.put("44", "Respiratory");// 呼吸内科
map.put("3", "Cardiovascular internal medicine");// 心血管内科
map.put("21", "Digestive internal medicine");// 消化内科
map.put("8", "General surgery");// 普通外科
map.put("30", "Nephrology");// 肾脏内科
map.put("34", "Spine surgery");// 脊柱外科
map.put("35", "Joint branch");// 骨关节科
map.put("28", "hand surgery");// 手外科
map.put("23", "burns surgery");// 烧伤科
map.put("36", "Sports discipline");// 运动学科
map.put("15", "gynaecology");// 妇科
map.put("38", "obstetric");// 产科
map.put("48", "Breast surgery");// 乳腺外科
map.put("31", "Son of internal");// 儿内科
map.put("39", "Son of surgery");// 儿外科
map.put("37", "oncology");// 肿瘤科
map.put("13", "dermatology");// 皮肤科
map.put("29", "endocrinology");// 内分泌科
map.put("24", "Rheumatology");// 风湿免疫科
map.put("22", "Hematology");// 血液内科
map.put("17", "Allergy branch");// 变态反应科
map.put("9", "Radiology department");// 放射科
map.put("49", "Occupational medicine");// 职业病科
map.put("0", "Vascular medicine");// 血管内科
map.put("45", "Palliative care office");// 姑息关怀科
map.put("50", "Geriatrics");// 老年科
map.put("51", "orthopaedic");// 整形科
map.put("52", "pain");// 疼痛科
map.put("4", "urology");// 泌尿外科
map.put("0", "male");// 男科
}
}
调用方法:
KeshiTongji.addmap();
Set<String> mapSet = KeshiTongji.map.keySet();
Iterator<String> iterator = mapSet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
if (key.equals(listItem.getId() + "")) {
MobclickAgent.onEvent(mContext,
KeshiTongji.map.get(key));
}
}
我的撸撸写了一个类 更方便,这里放出来大家自己看,牛的一比~
public class MobEventHelper {
/**
* 封装事件统计 keyValue一定要成对出现
* @param ctx
* @param eventId
* @param keyValue
*/
public static void onEvent(Context ctx, String eventId, String ...keyValue) {
if (keyValue == null || keyValue.length < 2) {
MobclickAgent.onEvent(ctx, eventId);
return;
}
HashMap<String, String> map = new HashMap<>();
int length = keyValue.length;
for (int i = 0; i < length; i+=2) {
if (i + 1 >= length) { break;}
map.put(keyValue[i], keyValue[i+1]);
}
MobclickAgent.onEvent(ctx, eventId, map);
}
}
调用的时候很方便 你看~
//友盟统计bufen
MobclickAgent.onEvent(PayChooseActivityQrCodeYuanOld.this, "payway_back");
//payway_back是友盟对应的key
//友盟统计bufen
MobEventHelper.onEvent(PayChooseActivity.this, "payway_item", "pay_way", "alipay");
//payway_item 是友盟对应的key pay_way 是map的key alipay是map的value
总结:大部分的操作都是直接传Id对应友盟后台上传的key,格式:
click1,点击,0;
click2,注册,0;
click3,登陆,0
注:
格式: .txt或.csv;UTF-8编码
字段: 事件id, 事件名称, 事件类型(1表示计算事件,0表示计数事件)。
字段用英文逗号分隔;每个事件一行。
扫描我们的时光~