Skip to content

Commit

Permalink
Release 1.6.31
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhzh committed Dec 8, 2016
1 parent c9594c8 commit d845f6f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
4 changes: 2 additions & 2 deletions 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.6.30"
version = "1.6.31"

android {
compileSdkVersion 21
Expand All @@ -12,7 +12,7 @@ android {
resourcePrefix "sensors_analytics_"

defaultConfig {
minSdkVersion 10
minSdkVersion 11
targetSdkVersion 21
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -418,7 +420,7 @@ public void disableActivityForVTrack(String canonicalName) {
* 该功能自动追踪 App 的一些行为,例如 SDK 初始化、App 启动 / 关闭、进入页面 等等,具体信息请参考文档:
* https://sensorsdata.cn/manual/android_sdk.html
*
* 该功能仅在 API 16 及以上版本中生效,默认关闭
* 该功能仅在 API 14 及以上版本中生效,默认关闭
*/
public void enableAutoTrack() {
mAutoTrack = true;
Expand Down Expand Up @@ -633,6 +635,36 @@ public void trackInstallation(String eventName, JSONObject properties)
throws InvalidDataException {
boolean firstTrackInstallation = mFirstTrackInstallation.get();
if (firstTrackInstallation) {
try {
if (properties == null) {
properties = new JSONObject();
}

if (!hasUtmProperties(properties)) {
Map<String, String> utmMap = new HashMap<>();
utmMap.put("SENSORS_ANALYTICS_UTM_SOURCE", "$utm_source");
utmMap.put("SENSORS_ANALYTICS_UTM_MEDIUM", "$utm_medium");
utmMap.put("SENSORS_ANALYTICS_UTM_TERM", "$utm_term");
utmMap.put("SENSORS_ANALYTICS_UTM_CONTENT", "$utm_content");
utmMap.put("SENSORS_ANALYTICS_UTM_CAMPAIGN", "$utm_campaign");

for (Map.Entry<String, String> entry : utmMap.entrySet()) {
if (entry != null) {
String utmValue = getApplicationMetaData(entry.getKey());
if (!TextUtils.isEmpty(utmValue)) {
properties.put(entry.getValue(), utmValue);
}
}
}
}

if (!hasUtmProperties(properties)) {
properties.put("$ios_install_source", "");
}
} catch (Exception e) {
e.printStackTrace();
}

// 先发送 track
trackEvent(EventType.TRACK, eventName, properties, null);

Expand All @@ -643,6 +675,33 @@ public void trackInstallation(String eventName, JSONObject properties)
}
}

private String getApplicationMetaData(String metaKey) {
try {
ApplicationInfo appInfo = mContext.getApplicationContext().getPackageManager()
.getApplicationInfo(mContext.getApplicationContext().getPackageName(),
PackageManager.GET_META_DATA);
return appInfo.metaData.getString(metaKey);
} catch (Exception e) {
return "";
}
}

private boolean hasUtmProperties(JSONObject properties) {
if (properties == null) {
return false;
}

if (properties.has("$utm_source") ||
properties.has("$utm_medium") ||
properties.has("$utm_term") ||
properties.has("$utm_content") ||
properties.has("$utm_campaign")) {
return true;
}

return false;
}

/**
* 调用track接口,追踪一个带有属性的事件
*
Expand Down Expand Up @@ -1353,6 +1412,33 @@ public void onActivityResumed(Activity activity) {
JSONObject properties = new JSONObject();
properties.put("$screen_name", activity.getClass().getCanonicalName());

/**
* 尝试读取页面 title
*/
try {
String activityTitle = activity.getTitle().toString();
ActionBar actionBar = activity.getActionBar();
if (actionBar != null) {
if (!TextUtils.isEmpty(actionBar.getTitle())) {
activityTitle = actionBar.getTitle().toString();
}
}
if (TextUtils.isEmpty(activityTitle)) {
PackageManager packageManager = activity.getPackageManager();
if (packageManager != null) {
ActivityInfo activityInfo = packageManager.getActivityInfo(activity.getComponentName(), 0);
if (activityInfo != null) {
activityTitle = activityInfo.loadLabel(packageManager).toString();
}
}
}
if (!TextUtils.isEmpty(activityTitle)) {
properties.put("$title", activityTitle);
}
} catch (Exception e) {
e.printStackTrace();
}

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

Expand Down Expand Up @@ -1601,7 +1687,7 @@ private static void mergeJSONObject(final JSONObject source, JSONObject dest)
static final int VTRACK_SUPPORTED_MIN_API = 16;

// SDK版本
static final String VERSION = "1.6.30";
static final String VERSION = "1.6.31";

static Boolean ENABLE_LOG = false;

Expand Down

0 comments on commit d845f6f

Please sign in to comment.