Skip to content

Commit

Permalink
Release 1.7.5
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhzh committed Jun 7, 2017
1 parent df97077 commit e2bb935
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 199 deletions.
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK/SensorsAnalyticsSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.7.4"
version = "1.7.5"

android {
compileSdkVersion 22
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public String sensorsdata_call_app() {
}
return null;
}

// @JavascriptInterface
// public void sensorsdata_track(String event) {
// SensorsDataAPI.sharedInstance(mContext).trackEventFromH5(event);
// }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sensorsdata.analytics.android.sdk;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -13,9 +14,26 @@ class EventTimer {
this.eventAccumulatedDuration = 0;
}

long duration() {
long duration = timeUnit.convert(System.currentTimeMillis() - startTime + eventAccumulatedDuration, TimeUnit.MILLISECONDS);
return duration < 0 ? 0 : duration;
String duration() {
long duration = System.currentTimeMillis() - startTime + eventAccumulatedDuration;
try {
float durationFloat;
if (timeUnit == TimeUnit.MILLISECONDS) {
durationFloat = duration;
} else if (timeUnit == TimeUnit.SECONDS) {
durationFloat = duration / 1000.0f;
} else if (timeUnit == TimeUnit.MINUTES) {
durationFloat = duration / 1000.0f / 60.0f;
} else if (timeUnit == TimeUnit.HOURS) {
durationFloat = duration / 1000.0f / 60.0f / 60.0f;
} else {
durationFloat = duration;
}
return durationFloat < 0 ? String.valueOf(0) : String.format(Locale.CHINA, "%.3f", durationFloat);
} catch (Exception e) {
e.printStackTrace();
return String.valueOf(0);
}
}

public long getStartTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public void setFlushInterval(int flushInterval) {
* 2) 本地缓存日志数目是否大于 flushBulkSize
*
* 如果满足这两个条件,则向服务器发送一次数据;如果不满足,则把数据加入到队列中,等待下次检查时把整个队列的内
* 容一并发送。需要注意的是,为了避免占用过多存储,队列最多只缓存20MB数据
* 容一并发送。需要注意的是,为了避免占用过多存储,队列最多只缓存32MB数据
*
* @return 返回本地缓存日志的最大条目数
*/
Expand Down Expand Up @@ -1197,6 +1197,9 @@ protected void appEnterBackground() {
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
if (entry != null) {
if ("$AppEnd".equals(entry.getKey().toString())) {
continue;
}
EventTimer eventTimer = (EventTimer) entry.getValue();
if (eventTimer != null) {
long eventAccumulatedDuration = eventTimer.getEventAccumulatedDuration() + System.currentTimeMillis() - eventTimer.getStartTime();
Expand Down Expand Up @@ -1491,6 +1494,79 @@ private void showDebugModeWarning() {
}
}

protected void trackEventFromH5(String eventInfo) {
try {
if (TextUtils.isEmpty(eventInfo)) {
return;
}

JSONObject eventObject = new JSONObject(eventInfo);
if (!TextUtils.isEmpty(getLoginId())) {
eventObject.put("distinct_id", getLoginId());
} else {
eventObject.put("distinct_id", getAnonymousId());
}

JSONObject propertiesObject = eventObject.optJSONObject("properties");
if (propertiesObject == null) {
return;
}

JSONObject libObject = eventObject.optJSONObject("lib");
if (libObject != null) {
if (mDeviceInfo.containsKey("$app_version")) {
libObject.put("$app_version", mDeviceInfo.get("$app_version"));
}

//update lib $app_version from super properties
JSONObject superProperties = mSuperProperties.get();
if (superProperties != null) {
if (superProperties.has("$app_version")) {
libObject.put("$app_version", superProperties.get("$app_version"));
}
}
}

if (mDeviceInfo != null) {
for (Map.Entry<String, Object> entry : mDeviceInfo.entrySet()) {
String key = entry.getKey();
if (!TextUtils.isEmpty(key)) {
if ("$lib".equals(key) || "$lib_version".equals(key)) {
continue;
}
propertiesObject.put(entry.getKey(), entry.getValue());
}
}
}

// 当前网络状况
String networkType = SensorsDataUtils.networkType(mContext);
propertiesObject.put("$wifi", networkType.equals("WIFI"));
propertiesObject.put("$network_type", networkType);

// SuperProperties
synchronized (mSuperProperties) {
JSONObject superProperties = mSuperProperties.get();
SensorsDataUtils.mergeJSONObject(superProperties, propertiesObject);
}

String type = eventObject.getString("type");
if ("track".equals(type)) {
//是否首日访问
propertiesObject.put("$is_first_day", isFirstDay());
}

mMessages.enqueueEventMessage(type, eventObject);
} catch (Exception e) {
try {
mMessages.enqueueEventMessage(EventType.TRACK.getEventType(), new JSONObject(eventInfo));
} catch (Exception ignored){
//ignore
}
e.printStackTrace();
}
}

private void trackEvent(EventType eventType, String eventName, JSONObject properties, String
originalDistinctId) throws InvalidDataException {
if (eventType.isTrack()) {
Expand Down Expand Up @@ -1536,7 +1612,12 @@ private void trackEvent(EventType eventType, String eventName, JSONObject proper
}

if (null != eventTimer) {
sendProperties.put("event_duration", eventTimer.duration());
try {
sendProperties.put("event_duration", Double.valueOf(eventTimer.duration()));
} catch (Exception e) {
sendProperties.put("event_duration", 0f);
e.printStackTrace();
}
}

JSONObject libProperties = new JSONObject();
Expand Down Expand Up @@ -1708,7 +1789,7 @@ private void assertDistinctId(String key) throws InvalidDataException {
static final int VTRACK_SUPPORTED_MIN_API = 16;

// SDK版本
static final String VERSION = "1.7.4";
static final String VERSION = "1.7.5";

static Boolean ENABLE_LOG = false;
static Boolean SHOW_DEBUG_INFO_VIEW = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,77 +38,85 @@ public void onActivityCreated(Activity activity, Bundle bundle) {

@Override
public void onActivityStarted(Activity activity) {
synchronized (mActivityLifecycleCallbacksLock) {
if (startedActivityCount == 0) {
// XXX: 注意内部执行顺序
boolean firstStart = mFirstStart.get();
if (firstStart) {
mFirstStart.commit(false);
}

try {
mSensorsDataInstance.appBecomeActive();
} catch (Exception e) {
e.printStackTrace();
}

if (SensorsDataUtils.isMainProcess(activity, mMainProcessName)) {
if (mSensorsDataInstance.isAutoTrackEnabled()) {
try {
if (!mSensorsDataInstance.isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_START)) {
JSONObject properties = new JSONObject();
properties.put("$resume_from_background", resumeFromBackground);
properties.put("$is_first_time", firstStart);
SensorsDataUtils.getScreenNameAndTitleFromActivity(properties, activity);
try {
synchronized (mActivityLifecycleCallbacksLock) {
if (startedActivityCount == 0) {
// XXX: 注意内部执行顺序
boolean firstStart = mFirstStart.get();
if (firstStart) {
mFirstStart.commit(false);
}

mSensorsDataInstance.track("$AppStart", properties);
}
try {
mSensorsDataInstance.appBecomeActive();
} catch (Exception e) {
e.printStackTrace();
}

if (!mSensorsDataInstance.isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_END)) {
mSensorsDataInstance.trackTimer("$AppEnd", TimeUnit.SECONDS);
if (SensorsDataUtils.isMainProcess(activity, mMainProcessName)) {
if (mSensorsDataInstance.isAutoTrackEnabled()) {
try {
if (!mSensorsDataInstance.isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_START)) {
JSONObject properties = new JSONObject();
properties.put("$resume_from_background", resumeFromBackground);
properties.put("$is_first_time", firstStart);
SensorsDataUtils.getScreenNameAndTitleFromActivity(properties, activity);

mSensorsDataInstance.track("$AppStart", properties);
}

if (!mSensorsDataInstance.isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_END)) {
mSensorsDataInstance.trackTimer("$AppEnd", TimeUnit.SECONDS);
}
} catch (Exception e) {
SALog.i(TAG, e);
}
} catch (Exception e) {
SALog.i(TAG, e);
}
}

// 下次启动时,从后台恢复
resumeFromBackground = true;
// 下次启动时,从后台恢复
resumeFromBackground = true;
}
}
}

startedActivityCount = startedActivityCount + 1;
startedActivityCount = startedActivityCount + 1;
}
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onActivityResumed(Activity activity) {
boolean mShowAutoTrack = true;
if (mSensorsDataInstance.isActivityAutoTrackIgnored(activity.getClass())) {
mShowAutoTrack = false;
}
if (mSensorsDataInstance.isAutoTrackEnabled() && mShowAutoTrack && !mSensorsDataInstance.isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_VIEW_SCREEN)) {
try {
JSONObject properties = new JSONObject();
properties.put("$screen_name", activity.getClass().getCanonicalName());
SensorsDataUtils.getScreenNameAndTitleFromActivity(properties, activity);

if (activity instanceof ScreenAutoTracker) {
ScreenAutoTracker screenAutoTracker = (ScreenAutoTracker) activity;

String screenUrl = screenAutoTracker.getScreenUrl();
JSONObject otherProperties = screenAutoTracker.getTrackProperties();
if (otherProperties != null) {
SensorsDataUtils.mergeJSONObject(otherProperties, properties);
}
try {
boolean mShowAutoTrack = true;
if (mSensorsDataInstance.isActivityAutoTrackIgnored(activity.getClass())) {
mShowAutoTrack = false;
}
if (mSensorsDataInstance.isAutoTrackEnabled() && mShowAutoTrack && !mSensorsDataInstance.isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_VIEW_SCREEN)) {
try {
JSONObject properties = new JSONObject();
properties.put("$screen_name", activity.getClass().getCanonicalName());
SensorsDataUtils.getScreenNameAndTitleFromActivity(properties, activity);

if (activity instanceof ScreenAutoTracker) {
ScreenAutoTracker screenAutoTracker = (ScreenAutoTracker) activity;

String screenUrl = screenAutoTracker.getScreenUrl();
JSONObject otherProperties = screenAutoTracker.getTrackProperties();
if (otherProperties != null) {
SensorsDataUtils.mergeJSONObject(otherProperties, properties);
}

mSensorsDataInstance.trackViewScreen(screenUrl, properties);
} else {
mSensorsDataInstance.track("$AppViewScreen", properties);
mSensorsDataInstance.trackViewScreen(screenUrl, properties);
} else {
mSensorsDataInstance.track("$AppViewScreen", properties);
}
} catch (Exception e) {
SALog.i(TAG, e);
}
} catch (Exception e) {
SALog.i(TAG, e);
}
} catch (Exception e) {
e.printStackTrace();
}
}

Expand All @@ -118,35 +126,39 @@ public void onActivityPaused(Activity activity) {

@Override
public void onActivityStopped(Activity activity) {
synchronized (mActivityLifecycleCallbacksLock) {
startedActivityCount = startedActivityCount - 1;

if (startedActivityCount == 0) {
try {
mSensorsDataInstance.appEnterBackground();
} catch (Exception e) {
e.printStackTrace();
}
try {
synchronized (mActivityLifecycleCallbacksLock) {
startedActivityCount = startedActivityCount - 1;

if (startedActivityCount == 0) {
try {
mSensorsDataInstance.appEnterBackground();
} catch (Exception e) {
e.printStackTrace();
}

if (SensorsDataUtils.isMainProcess(activity, mMainProcessName)) {
if (mSensorsDataInstance.isAutoTrackEnabled()) {
try {
if (!mSensorsDataInstance.isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_END)) {
JSONObject properties = new JSONObject();
SensorsDataUtils.getScreenNameAndTitleFromActivity(properties, activity);
mSensorsDataInstance.track("$AppEnd", properties);
if (SensorsDataUtils.isMainProcess(activity, mMainProcessName)) {
if (mSensorsDataInstance.isAutoTrackEnabled()) {
try {
if (!mSensorsDataInstance.isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_END)) {
JSONObject properties = new JSONObject();
SensorsDataUtils.getScreenNameAndTitleFromActivity(properties, activity);
mSensorsDataInstance.track("$AppEnd", properties);
}
} catch (Exception e) {
SALog.i(TAG, e);
}
} catch (Exception e) {
SALog.i(TAG, e);
}
}
}
try {
mSensorsDataInstance.flush();
} catch (Exception e) {
e.printStackTrace();
try {
mSensorsDataInstance.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

Expand Down
Loading

0 comments on commit e2bb935

Please sign in to comment.