diff --git a/SensorsAnalyticsSDK/pom.xml b/SensorsAnalyticsSDK/pom.xml
index a8dc8de..2e052b0 100644
--- a/SensorsAnalyticsSDK/pom.xml
+++ b/SensorsAnalyticsSDK/pom.xml
@@ -15,7 +15,7 @@
com.sensorsdata.analytics.javasdk
SensorsAnalyticsSDK
SensorsAnalyticsSDK
- 3.4.3
+ 3.4.4
The official Java SDK of Sensors Analytics
http://sensorsdata.cn
@@ -37,7 +37,7 @@
utf-8
1.7
4.5.13
- 2.9.10.7
+ 2.9.10.8
1.18.20
4.11
1.7.25
diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalytics.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalytics.java
index 546eee4..6550cf2 100755
--- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalytics.java
+++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalytics.java
@@ -45,8 +45,6 @@ public class SensorsAnalytics implements ISensorsAnalytics {
private static final String DISTINCT_ID = "distinct_id";
private static final String EVENT_NAME = "event name";
private static final String ORIGINAL_DISTINCT_ID = "Original Distinct Id";
- private static final String ITEM_TYPE = "Item Type";
- private static final String ITEM_ID = "Item Id";
public SensorsAnalytics(final Consumer consumer) {
worker = new SensorsAnalyticsWorker(consumer);
@@ -100,6 +98,20 @@ public void profileIncrement(@NonNull UserRecord userRecord) throws InvalidArgum
@Override
public void profileUnset(@NonNull UserRecord userRecord) throws InvalidArgumentException {
+ if (userRecord.getPropertyMap() == null) {
+ return;
+ }
+ for (Map.Entry property : userRecord.getPropertyMap().entrySet()) {
+ if (!PROJECT_SYSTEM_ATTR.equals(property.getKey())) {
+ if (property.getValue() instanceof Boolean) {
+ boolean value = (Boolean) property.getValue();
+ if (value) {
+ continue;
+ }
+ }
+ throw new InvalidArgumentException("The property value of " + property.getKey() + " should be true.");
+ }
+ }
dealProfile(userRecord, PROFILE_UNSET_ACTION_TYPE);
}
@@ -147,7 +159,7 @@ public void trackSignUp(@NonNull String loginId, @NonNull String anonymousId, Ma
@Override
public void profileSet(@NonNull String distinctId, @NonNull boolean isLoginId, Map properties)
throws InvalidArgumentException {
- addEvent(distinctId, isLoginId, null, PROFILE_SET_ACTION_TYPE, null, properties);
+ dealProfile(distinctId, isLoginId, properties, PROFILE_SET_ACTION_TYPE);
}
@Override
@@ -155,13 +167,13 @@ public void profileSet(@NonNull String distinctId, @NonNull boolean isLoginId, @
@NonNull Object value) throws InvalidArgumentException {
Map properties = new HashMap<>();
properties.put(property, value);
- addEvent(distinctId, isLoginId, null, PROFILE_SET_ACTION_TYPE, null, properties);
+ dealProfile(distinctId, isLoginId, properties, PROFILE_SET_ACTION_TYPE);
}
@Override
public void profileSetOnce(@NonNull String distinctId, @NonNull boolean isLoginId, Map properties)
throws InvalidArgumentException {
- addEvent(distinctId, isLoginId, null, PROFILE_SET_ONCE_ACTION_TYPE, null, properties);
+ dealProfile(distinctId, isLoginId, properties, PROFILE_SET_ONCE_ACTION_TYPE);
}
@Override
@@ -169,13 +181,13 @@ public void profileSetOnce(@NonNull String distinctId, @NonNull boolean isLoginI
@NonNull Object value) throws InvalidArgumentException {
Map properties = new HashMap<>();
properties.put(property, value);
- addEvent(distinctId, isLoginId, null, PROFILE_SET_ONCE_ACTION_TYPE, null, properties);
+ dealProfile(distinctId, isLoginId, properties, PROFILE_SET_ONCE_ACTION_TYPE);
}
@Override
public void profileIncrement(@NonNull String distinctId, @NonNull boolean isLoginId, Map properties)
throws InvalidArgumentException {
- addEvent(distinctId, isLoginId, null, PROFILE_INCREMENT_ACTION_TYPE, null, properties);
+ dealProfile(distinctId, isLoginId, properties, PROFILE_INCREMENT_ACTION_TYPE);
}
@Override
@@ -183,13 +195,13 @@ public void profileIncrement(@NonNull String distinctId, @NonNull boolean isLogi
@NonNull long value) throws InvalidArgumentException {
Map properties = new HashMap<>();
properties.put(property, value);
- addEvent(distinctId, isLoginId, null, PROFILE_INCREMENT_ACTION_TYPE, null, properties);
+ dealProfile(distinctId, isLoginId, properties, PROFILE_INCREMENT_ACTION_TYPE);
}
@Override
public void profileAppend(@NonNull String distinctId, @NonNull boolean isLoginId, Map properties)
throws InvalidArgumentException {
- addEvent(distinctId, isLoginId, null, PROFILE_APPEND_ACTION_TYPE, null, properties);
+ dealProfile(distinctId, isLoginId, properties, PROFILE_APPEND_ACTION_TYPE);
}
@Override
@@ -199,7 +211,7 @@ public void profileAppend(@NonNull String distinctId, @NonNull boolean isLoginId
values.add(value);
Map properties = new HashMap<>();
properties.put(property, values);
- addEvent(distinctId, isLoginId, null, PROFILE_APPEND_ACTION_TYPE, null, properties);
+ dealProfile(distinctId, isLoginId, properties, PROFILE_APPEND_ACTION_TYPE);
}
@Override
@@ -207,7 +219,7 @@ public void profileUnset(@NonNull String distinctId, @NonNull boolean isLoginId,
throws InvalidArgumentException {
Map properties = new HashMap<>();
properties.put(property, true);
- addEvent(distinctId, isLoginId, null, PROFILE_UNSET_ACTION_TYPE, null, properties);
+ dealProfile(distinctId, isLoginId, properties, PROFILE_UNSET_ACTION_TYPE);
}
@Override
@@ -227,12 +239,12 @@ public void profileUnset(@NonNull String distinctId, @NonNull boolean isLoginId,
throw new InvalidArgumentException("The property value of " + property.getKey() + " should be true.");
}
}
- addEvent(distinctId, isLoginId, null, PROFILE_UNSET_ACTION_TYPE, null, properties);
+ dealProfile(distinctId, isLoginId, properties, PROFILE_UNSET_ACTION_TYPE);
}
@Override
public void profileDelete(@NonNull String distinctId, @NonNull boolean isLoginId) throws InvalidArgumentException {
- addEvent(distinctId, isLoginId, null, PROFILE_DELETE_ACTION_TYPE, null, null);
+ dealProfile(distinctId, isLoginId, null, PROFILE_DELETE_ACTION_TYPE);
}
@Override
@@ -256,13 +268,12 @@ public void bind(@NonNull SensorsAnalyticsIdentity... identities) throws Invalid
if (identityMap.size() < 2) {
throw new InvalidArgumentException("The identities is invalid,you should have at least two identities.");
}
- assertIdentityMap(BIND_ID_ACTION_TYPE, identityMap);
- worker.doAddEventIdentity(identityMap, BIND_ID_ACTION_TYPE, BIND_ID, null);
+ addEventIdentity(identityMap, BIND_ID_ACTION_TYPE, BIND_ID, null);
}
@Override
public void unbind(@NonNull SensorsAnalyticsIdentity analyticsIdentity) throws InvalidArgumentException {
- addEventIdentity(analyticsIdentity, UNBIND_ID_ACTION_TYPE, UNBIND_ID, null);
+ addEventIdentity(analyticsIdentity.getIdentityMap(), UNBIND_ID_ACTION_TYPE, UNBIND_ID, null);
}
@Override
@@ -279,7 +290,7 @@ public void trackById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, @NonN
@Override
public void profileSetById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, Map properties)
throws InvalidArgumentException {
- addEventIdentity(analyticsIdentity, PROFILE_SET_ACTION_TYPE, null, properties);
+ addProfileIdentity(analyticsIdentity, PROFILE_SET_ACTION_TYPE, properties);
}
@Override
@@ -287,13 +298,13 @@ public void profileSetById(@NonNull SensorsAnalyticsIdentity analyticsIdentity,
@NonNull Object value) throws InvalidArgumentException {
Map properties = new HashMap<>();
properties.put(property, value);
- addEventIdentity(analyticsIdentity, PROFILE_SET_ACTION_TYPE, null, properties);
+ addProfileIdentity(analyticsIdentity, PROFILE_SET_ACTION_TYPE, properties);
}
@Override
public void profileSetOnceById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, Map properties)
throws InvalidArgumentException {
- addEventIdentity(analyticsIdentity, PROFILE_SET_ONCE_ACTION_TYPE, null, properties);
+ addProfileIdentity(analyticsIdentity, PROFILE_SET_ONCE_ACTION_TYPE, properties);
}
@Override
@@ -301,13 +312,13 @@ public void profileSetOnceById(@NonNull SensorsAnalyticsIdentity analyticsIdenti
@NonNull Object value) throws InvalidArgumentException {
Map properties = new HashMap<>();
properties.put(property, value);
- addEventIdentity(analyticsIdentity, PROFILE_SET_ONCE_ACTION_TYPE, null, properties);
+ addProfileIdentity(analyticsIdentity, PROFILE_SET_ONCE_ACTION_TYPE, properties);
}
@Override
public void profileIncrementById(@NonNull SensorsAnalyticsIdentity analyticsIdentity,
Map properties) throws InvalidArgumentException {
- addEventIdentity(analyticsIdentity, PROFILE_INCREMENT_ACTION_TYPE, null, properties);
+ addProfileIdentity(analyticsIdentity, PROFILE_INCREMENT_ACTION_TYPE, properties);
}
@Override
@@ -315,23 +326,23 @@ public void profileIncrementById(@NonNull SensorsAnalyticsIdentity analyticsIden
throws InvalidArgumentException {
Map properties = new HashMap<>();
properties.put(property, value);
- addEventIdentity(analyticsIdentity, PROFILE_INCREMENT_ACTION_TYPE, null, properties);
+ addProfileIdentity(analyticsIdentity, PROFILE_INCREMENT_ACTION_TYPE, properties);
}
@Override
public void profileAppendById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, Map properties)
throws InvalidArgumentException {
- addEventIdentity(analyticsIdentity, PROFILE_APPEND_ACTION_TYPE, null, properties);
+ addProfileIdentity(analyticsIdentity, PROFILE_APPEND_ACTION_TYPE, properties);
}
@Override
public void profileAppendById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, @NonNull String property,
@NonNull String value) throws InvalidArgumentException {
- List values = new ArrayList();
+ List values = new ArrayList<>();
values.add(value);
Map properties = new HashMap<>();
properties.put(property, values);
- addEventIdentity(analyticsIdentity, PROFILE_APPEND_ACTION_TYPE, null, properties);
+ addProfileIdentity(analyticsIdentity, PROFILE_APPEND_ACTION_TYPE, properties);
}
@Override
@@ -351,7 +362,7 @@ public void profileUnsetById(@NonNull SensorsAnalyticsIdentity analyticsIdentity
throw new InvalidArgumentException("The property value of [" + property.getKey() + "] should be true.");
}
}
- addEventIdentity(analyticsIdentity, PROFILE_UNSET_ACTION_TYPE, null, properties);
+ addProfileIdentity(analyticsIdentity, PROFILE_UNSET_ACTION_TYPE, properties);
}
@Override
@@ -359,12 +370,12 @@ public void profileUnsetById(@NonNull SensorsAnalyticsIdentity analyticsIdentity
throws InvalidArgumentException {
Map properties = new HashMap<>();
properties.put(property, true);
- addEventIdentity(analyticsIdentity, PROFILE_UNSET_ACTION_TYPE, null, properties);
+ addProfileIdentity(analyticsIdentity, PROFILE_UNSET_ACTION_TYPE, properties);
}
@Override
public void profileDeleteById(@NonNull SensorsAnalyticsIdentity analyticsIdentity) throws InvalidArgumentException {
- addEventIdentity(analyticsIdentity, PROFILE_DELETE_ACTION_TYPE, null, null);
+ addProfileIdentity(analyticsIdentity, PROFILE_DELETE_ACTION_TYPE, null);
}
@Override
@@ -399,6 +410,20 @@ public void profileAppendById(@NonNull IDMUserRecord idmUserRecord) throws Inval
@Override
public void profileUnsetById(@NonNull IDMUserRecord idmUserRecord) throws InvalidArgumentException {
SensorsAnalyticsUtil.assertProperties(PROFILE_UNSET_ACTION_TYPE, idmUserRecord.getPropertyMap());
+ if (idmUserRecord.getPropertyMap() == null) {
+ return;
+ }
+ for (Map.Entry property : idmUserRecord.getPropertyMap().entrySet()) {
+ if (!PROJECT_SYSTEM_ATTR.equals(property.getKey())) {
+ if (property.getValue() instanceof Boolean) {
+ boolean value = (Boolean) property.getValue();
+ if (value) {
+ continue;
+ }
+ }
+ throw new InvalidArgumentException("The property value of " + property.getKey() + " should be true.");
+ }
+ }
worker.doAddData(new SensorsData(idmUserRecord, PROFILE_UNSET_ACTION_TYPE));
}
@@ -413,34 +438,62 @@ public void shutdown() {
worker.shutdown();
}
+ private void dealProfile(String distinctId, Boolean isLoginId, Map properties, String actionType)
+ throws InvalidArgumentException {
+ SensorsAnalyticsUtil.assertProperties(actionType, properties);
+ UserRecord userRecord = UserRecord.builder()
+ .setDistinctId(distinctId)
+ .isLoginId(isLoginId)
+ .addProperties(properties)
+ .build();
+ worker.doAddData(new SensorsData(userRecord, actionType));
+ }
+
private void dealProfile(UserRecord userRecord, String actionType) throws InvalidArgumentException {
- addEvent(userRecord.getDistinctId(), userRecord.getIsLoginId(), null, actionType, null,
- userRecord.getPropertyMap());
+ SensorsAnalyticsUtil.assertProperties(actionType, userRecord.getPropertyMap());
+ worker.doAddData(new SensorsData(userRecord, actionType));
}
private void addItem(String itemType, String itemId, String actionType, Map properties)
throws InvalidArgumentException {
- SensorsAnalyticsUtil.assertKey(ITEM_TYPE, itemType);
- SensorsAnalyticsUtil.assertValue(ITEM_ID, itemId);
SensorsAnalyticsUtil.assertProperties(actionType, properties);
- worker.doAddItem(itemType, itemId, actionType, properties);
+ ItemRecord itemRecord = ItemRecord.builder()
+ .setItemId(itemId)
+ .setItemType(itemType)
+ .addProperties(properties)
+ .build();
+ worker.doAddData(new SensorsData(itemRecord, actionType));
}
private void addEvent(String distinctId, boolean isLoginId, String originDistinctId, String actionType,
String eventName, Map properties) throws InvalidArgumentException {
- SensorsAnalyticsUtil.assertValue(DISTINCT_ID, distinctId);
SensorsAnalyticsUtil.assertProperties(actionType, properties);
- if (actionType.equals(TRACK_ACTION_TYPE)) {
- SensorsAnalyticsUtil.assertKey(EVENT_NAME, eventName);
- } else if (actionType.equals(TRACK_SIGN_UP_ACTION_TYPE)) {
+ if (actionType.equals(TRACK_SIGN_UP_ACTION_TYPE)) {
SensorsAnalyticsUtil.assertValue(ORIGINAL_DISTINCT_ID, originDistinctId);
}
- worker.doAddEvent(distinctId, isLoginId, originDistinctId, actionType, eventName, properties);
+ EventRecord eventRecord = EventRecord.builder()
+ .setEventName(eventName)
+ .setDistinctId(distinctId)
+ .isLoginId(isLoginId)
+ .addProperties(properties)
+ .build();
+ SensorsData sensorsData = new SensorsData(eventRecord, actionType);
+ sensorsData.setOriginalId(originDistinctId);
+ worker.doAddData(sensorsData);
}
- private void addEventIdentity(SensorsAnalyticsIdentity analyticsIdentity, String actionType, String eventName,
+ private void addProfileIdentity(SensorsAnalyticsIdentity analyticsIdentity, String actionType,
Map properties) throws InvalidArgumentException {
- addEventIdentity(analyticsIdentity.getIdentityMap(), actionType, eventName, properties);
+ if (analyticsIdentity.getIdentityMap().isEmpty()) {
+ throw new InvalidArgumentException("The identity is empty.");
+ }
+ assertIdentityMap(actionType, analyticsIdentity.getIdentityMap());
+ SensorsAnalyticsUtil.assertProperties(actionType, properties);
+ IDMUserRecord idmUserRecord = IDMUserRecord.starter()
+ .identityMap(analyticsIdentity.getIdentityMap())
+ .addProperties(properties)
+ .build();
+ worker.doAddData(new SensorsData(idmUserRecord, actionType));
}
private void addEventIdentity(Map identityMap, String actionType, String eventName,
@@ -450,11 +503,12 @@ private void addEventIdentity(Map identityMap, String actionType
}
assertIdentityMap(actionType, identityMap);
SensorsAnalyticsUtil.assertProperties(actionType, properties);
- if (actionType.equals(TRACK_ACTION_TYPE)) {
- SensorsAnalyticsUtil.assertKey(EVENT_NAME, eventName);
- }
-
- worker.doAddEventIdentity(identityMap, actionType, eventName, properties);
+ IDMEventRecord idmEventRecord = IDMEventRecord.starter()
+ .setEventName(eventName)
+ .identityMap(identityMap)
+ .addProperties(properties)
+ .build();
+ worker.doAddData(new SensorsData(idmEventRecord, actionType));
}
diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalyticsWorker.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalyticsWorker.java
index 7848e8f..ec25d73 100644
--- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalyticsWorker.java
+++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsAnalyticsWorker.java
@@ -32,7 +32,7 @@ class SensorsAnalyticsWorker {
private final Consumer consumer;
- private final Map superProperties = new ConcurrentHashMap();
+ private final Map superProperties = new ConcurrentHashMap<>();
private boolean enableTimeFree = false;
@@ -157,7 +157,7 @@ void shutdown() {
private Map generateEventMap(String distinctId, Boolean isLoginId, String originDistinctId,
Map identity, String actionType, String eventName, Map properties) {
Map eventMap = new HashMap<>();
- eventMap.put("_track_id", new Random().nextInt());
+ eventMap.put("_track_id", SensorsAnalyticsUtil.getTrackId(properties, distinctId));
eventMap.put("type", actionType);
eventMap.put("lib", getLibProperties());
//开启历史数据导入
diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsConst.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsConst.java
index e0680b9..e5e93a6 100644
--- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsConst.java
+++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsConst.java
@@ -15,7 +15,7 @@ private SensorsConst() {
/**
* 当前JDK版本号,注意要和pom文件里面的version保持一致
*/
- public static final String SDK_VERSION = "3.4.3";
+ public static final String SDK_VERSION = "3.4.4";
/**
* 当前语言类型
*/
@@ -38,6 +38,9 @@ private SensorsConst() {
*/
public static final String BIND_ID_ACTION_TYPE = "track_id_bind";
public static final String UNBIND_ID_ACTION_TYPE = "track_id_unbind";
+
+ public static final String ITEM_TYPE = "Item Type";
+ public static final String ITEM_ID = "Item Id";
/**
* 绑定事件名称
*/
@@ -46,9 +49,12 @@ private SensorsConst() {
* 解绑事件名称
*/
public static final String UNBIND_ID = "$UnbindID";
+
+ public static final String PROPERTIES = "properties";
/**
* 系统预置属性
*/
+ public static final String TRACK_ID = "$track_id";
public static final String PROJECT_SYSTEM_ATTR = "$project";
public static final String TIME_SYSTEM_ATTR = "$time";
public static final String TOKEN_SYSTEM_ATTR = "$token";
diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsData.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsData.java
index 995b96c..50d62f7 100644
--- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsData.java
+++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/SensorsData.java
@@ -2,8 +2,11 @@
import static com.sensorsdata.analytics.javasdk.SensorsConst.TRACK_ACTION_TYPE;
+import com.sensorsdata.analytics.javasdk.bean.EventRecord;
import com.sensorsdata.analytics.javasdk.bean.IDMEventRecord;
import com.sensorsdata.analytics.javasdk.bean.IDMUserRecord;
+import com.sensorsdata.analytics.javasdk.bean.ItemRecord;
+import com.sensorsdata.analytics.javasdk.bean.UserRecord;
import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil;
import lombok.Getter;
@@ -12,7 +15,6 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
-import java.util.Random;
/**
* 神策数据格式
@@ -77,18 +79,38 @@ class SensorsData {
*/
private String itemId;
+ protected SensorsData(EventRecord eventRecord, String actionType) {
+ this(eventRecord.getDistinctId(), eventRecord.getOriginalId(), null, actionType, eventRecord.getEventName(),
+ eventRecord.getPropertyMap(), null, null, eventRecord.getTrackId());
+ }
+
+ protected SensorsData(ItemRecord itemRecord, String actionType ) {
+ this(null,null,null,actionType,null,itemRecord.getPropertyMap(),
+ itemRecord.getItemType(),itemRecord.getItemId(), itemRecord.getTrackId());
+ }
+
+ protected SensorsData (UserRecord userRecord, String actionType) {
+ this(userRecord.getDistinctId(), actionType, null, userRecord.getPropertyMap(), userRecord.getTrackId());
+ }
+
protected SensorsData(IDMUserRecord userRecord, String actionType) {
- this(userRecord.getDistinctId(), actionType, userRecord.getIdentityMap(), userRecord.getPropertyMap());
+ this(userRecord.getDistinctId(), actionType, userRecord.getIdentityMap(), userRecord.getPropertyMap(),
+ userRecord.getTrackId());
}
protected SensorsData(IDMEventRecord eventRecord) {
this(eventRecord.getDistinctId(), eventRecord.getIdentityMap(), eventRecord.getEventName(),
- eventRecord.getPropertyMap());
+ eventRecord.getPropertyMap(), eventRecord.getTrackId());
+ }
+
+ protected SensorsData(IDMEventRecord eventRecord, String actionType) {
+ this(eventRecord.getDistinctId(), null, eventRecord.getIdentityMap(), actionType,
+ eventRecord.getEventName(), eventRecord.getPropertyMap(), null, null, eventRecord.getTrackId());
}
protected SensorsData(String distinctId, String type, Map identities,
- Map properties) {
- this(distinctId, null, identities, type, null, properties, null, null);
+ Map properties, Integer trackId) {
+ this(distinctId, null, identities, type, null, properties, null, null, trackId);
}
/**
@@ -99,13 +121,13 @@ protected SensorsData(String distinctId, String type, Map identi
* @param properties 事件属性集合
*/
protected SensorsData(String distinctId, Map identities, String event,
- Map properties) {
- this(distinctId, null, identities, TRACK_ACTION_TYPE, event, properties, null, null);
+ Map properties, Integer trackId) {
+ this(distinctId, null, identities, TRACK_ACTION_TYPE, event, properties, null, null, trackId);
}
private SensorsData(String distinctId, String originalId, Map identities, String type, String event,
- Map properties, String itemType, String itemId) {
- this.trackId = new Random().nextInt();
+ Map properties, String itemType, String itemId, Integer trackId) {
+ this.trackId = trackId;
this.distinctId = distinctId;
this.originalId = originalId;
this.identities = identities;
@@ -118,6 +140,7 @@ private SensorsData(String distinctId, String originalId, Map id
this.itemId = itemId;
}
+
protected static Map generateData(SensorsData sensorsData) {
Map eventMap = new HashMap<>();
if (sensorsData.getTrackId() != null) {
@@ -156,6 +179,9 @@ protected static Map generateData(SensorsData sensorsData) {
eventMap.put("item_type", sensorsData.getItemType());
}
if (sensorsData.getProperties() != null) {
+ if (sensorsData.getTrackId() != null) {
+ sensorsData.getProperties().remove(SensorsConst.TRACK_ID);
+ }
eventMap.put("properties", sensorsData.getProperties());
}
return eventMap;
diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/EventRecord.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/EventRecord.java
index 0670b2f..081f364 100644
--- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/EventRecord.java
+++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/EventRecord.java
@@ -1,6 +1,8 @@
package com.sensorsdata.analytics.javasdk.bean;
+import com.sensorsdata.analytics.javasdk.SensorsConst;
import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException;
+import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil;
import java.io.Serializable;
import java.util.Date;
@@ -22,11 +24,21 @@ public class EventRecord implements Serializable {
private final Boolean isLoginId;
- private EventRecord(String eventName, String distinctId, Boolean isLoginId, Map propertyMap) {
+ private final Integer trackId;
+
+ private final String originalId;
+
+ private EventRecord(String eventName, String distinctId, Boolean isLoginId, Map propertyMap,
+ Integer trackId, String originalId) {
this.eventName = eventName;
this.distinctId = distinctId;
this.isLoginId = isLoginId;
+ if (isLoginId) {
+ propertyMap.put(SensorsConst.LOGIN_SYSTEM_ATTR, true);
+ }
this.propertyMap = propertyMap;
+ this.trackId = trackId;
+ this.originalId = originalId;
}
@Override
@@ -59,16 +71,24 @@ public Boolean getIsLoginId() {
return isLoginId;
}
+ public String getOriginalId() {
+ return originalId;
+ }
+
+ public Integer getTrackId() {return trackId; }
+
public static class Builder {
- private final Map propertyMap = new HashMap();
+ private final Map propertyMap = new HashMap<>();
private String eventName;
private String distinctId;
private Boolean isLoginId;
+ private Integer trackId;
+ private String originalId;
private Builder() {
}
-
public EventRecord build() throws InvalidArgumentException {
+
if (eventName == null) {
throw new InvalidArgumentException("The eventName is empty.");
}
@@ -78,7 +98,16 @@ public EventRecord build() throws InvalidArgumentException {
if (isLoginId == null) {
throw new InvalidArgumentException("The isLoginId is empty.");
}
- return new EventRecord(eventName, distinctId, isLoginId, propertyMap);
+ SensorsAnalyticsUtil.assertKey("event_name",eventName);
+ SensorsAnalyticsUtil.assertValue("distinct_id", distinctId);
+ String message = String.format("[distinct_id=%s,event_name=%s,is_login_id=%s]",distinctId,eventName,isLoginId);
+ trackId = SensorsAnalyticsUtil.getTrackId(propertyMap, message);
+ return new EventRecord(eventName, distinctId, isLoginId, propertyMap,trackId, originalId);
+ }
+
+ public EventRecord.Builder setOriginalId(String originalId) {
+ this.originalId = originalId;
+ return this;
}
public EventRecord.Builder setEventName(String eventName) {
diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMEventRecord.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMEventRecord.java
index 89aa973..adce1c4 100644
--- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMEventRecord.java
+++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMEventRecord.java
@@ -1,8 +1,8 @@
package com.sensorsdata.analytics.javasdk.bean;
-import static com.sensorsdata.analytics.javasdk.SensorsConst.LOGIN_SYSTEM_ATTR;
import static com.sensorsdata.analytics.javasdk.SensorsConst.TRACK_ACTION_TYPE;
+import com.sensorsdata.analytics.javasdk.SensorsConst;
import com.sensorsdata.analytics.javasdk.common.Pair;
import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException;
import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil;
@@ -44,12 +44,16 @@ public class IDMEventRecord extends SensorsAnalyticsIdentity {
*/
private final Map propertyMap;
+ private final Integer trackId;
+
+
private IDMEventRecord(Map identityMap, String eventName, String distinctId,
- Map propertyMap) {
+ Map propertyMap, Integer trackId) {
super(identityMap);
this.eventName = eventName;
this.distinctId = distinctId;
this.propertyMap = propertyMap;
+ this.trackId = trackId;
}
public static IDMBuilder starter() {
@@ -62,6 +66,7 @@ public static class IDMBuilder {
private final Map propertyMap = new HashMap<>();
private String eventName;
private String distinctId;
+ private Integer trackId;
public IDMEventRecord build() throws InvalidArgumentException {
SensorsAnalyticsUtil.assertKey("event_name", eventName);
@@ -73,8 +78,12 @@ public IDMEventRecord build() throws InvalidArgumentException {
}
Pair resPair =
SensorsAnalyticsUtil.checkIdentitiesAndGenerateDistinctId(distinctId, idMap);
- propertyMap.put(LOGIN_SYSTEM_ATTR, resPair.getValue());
- return new IDMEventRecord(idMap, eventName, resPair.getKey(), propertyMap);
+ if (resPair.getValue()) {
+ propertyMap.put(SensorsConst.LOGIN_SYSTEM_ATTR, true);
+ }
+ String message = String.format("[distinct_id=%s,event_name=%s]",distinctId,eventName);
+ trackId = SensorsAnalyticsUtil.getTrackId(propertyMap, message);
+ return new IDMEventRecord(idMap, eventName, resPair.getKey(), propertyMap,trackId);
}
public IDMEventRecord.IDMBuilder identityMap(Map identityMap) {
@@ -97,7 +106,6 @@ public IDMEventRecord.IDMBuilder setEventName(@NonNull String eventName) {
public IDMEventRecord.IDMBuilder setDistinctId(@NonNull String distinctId) {
this.distinctId = distinctId;
// IDM3.0 设置 distinctId,设置 $is_login_id = false,其实也可不设置
- propertyMap.put(LOGIN_SYSTEM_ATTR, false);
return this;
}
diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMUserRecord.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMUserRecord.java
index 9e9ca6c..82e003d 100644
--- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMUserRecord.java
+++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/IDMUserRecord.java
@@ -1,7 +1,7 @@
package com.sensorsdata.analytics.javasdk.bean;
-import static com.sensorsdata.analytics.javasdk.SensorsConst.LOGIN_SYSTEM_ATTR;
+import com.sensorsdata.analytics.javasdk.SensorsConst;
import com.sensorsdata.analytics.javasdk.common.Pair;
import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException;
import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil;
@@ -35,10 +35,14 @@ public class IDMUserRecord extends SensorsAnalyticsIdentity {
private final String distinctId;
- private IDMUserRecord(Map identityMap, Map propertyMap, String distinctId) {
+ private final Integer trackId;
+
+ private IDMUserRecord(Map identityMap, Map propertyMap, String distinctId,
+ Integer trackId) {
super(identityMap);
this.propertyMap = propertyMap;
this.distinctId = distinctId;
+ this.trackId = trackId;
}
public static IDMBuilder starter() {
@@ -50,12 +54,18 @@ public static class IDMBuilder {
private final Map idMap = new LinkedHashMap<>();
private String distinctId;
private final Map propertyMap = new HashMap<>();
+ private Integer trackId;
public IDMUserRecord build() throws InvalidArgumentException {
Pair resPair =
SensorsAnalyticsUtil.checkIdentitiesAndGenerateDistinctId(distinctId, idMap);
- propertyMap.put(LOGIN_SYSTEM_ATTR, resPair.getValue());
- return new IDMUserRecord(idMap, propertyMap, resPair.getKey());
+ if (resPair.getValue()) {
+ propertyMap.put(SensorsConst.LOGIN_SYSTEM_ATTR, true);
+ }
+ // 填充 distinct_id 和 目标表
+ String message = String.format("[distinct_id=%s,target_table=user]",distinctId);
+ trackId = SensorsAnalyticsUtil.getTrackId(propertyMap, message);
+ return new IDMUserRecord(idMap, propertyMap, resPair.getKey(), trackId);
}
public IDMUserRecord.IDMBuilder identityMap(Map identityMap) {
@@ -73,7 +83,6 @@ public IDMUserRecord.IDMBuilder addIdentityProperty(String key, String value) {
public IDMUserRecord.IDMBuilder setDistinctId(@NonNull String distinctId) {
this.distinctId = distinctId;
// IDM3.0 设置 distinctId,设置 $is_login_id = false,其实也可不设置
- propertyMap.put(LOGIN_SYSTEM_ATTR, false);
return this;
}
diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/ItemRecord.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/ItemRecord.java
index bd86d43..8ca2130 100644
--- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/ItemRecord.java
+++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/ItemRecord.java
@@ -1,6 +1,10 @@
package com.sensorsdata.analytics.javasdk.bean;
+import static com.sensorsdata.analytics.javasdk.SensorsConst.ITEM_ID;
+import static com.sensorsdata.analytics.javasdk.SensorsConst.ITEM_TYPE;
+
import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException;
+import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil;
import java.io.Serializable;
import java.util.Date;
@@ -25,10 +29,13 @@ public class ItemRecord implements Serializable {
private final String itemType;
- private ItemRecord(Map propertyMap, String itemId, String itemType) {
+ private final Integer trackId;
+
+ private ItemRecord(Map propertyMap, String itemId, String itemType, Integer trackId) {
this.propertyMap = propertyMap;
this.itemId = itemId;
this.itemType = itemType;
+ this.trackId = trackId;
}
public Map getPropertyMap() {
@@ -43,18 +50,22 @@ public String getItemType() {
return itemType;
}
+ public Integer getTrackId() { return trackId; }
+
public static Builder builder() {
return new Builder();
}
public static class Builder {
- private Map propertyMap = new HashMap();
+ private Map propertyMap = new HashMap<>();
private String itemId;
private String itemType;
+ private Integer trackId;
+
private Builder() {
}
@@ -65,7 +76,10 @@ public ItemRecord build() throws InvalidArgumentException {
if (null == itemType) {
throw new InvalidArgumentException("The itemType is empty.");
}
- return new ItemRecord(propertyMap, itemId, itemType);
+ SensorsAnalyticsUtil.assertKey(ITEM_TYPE, itemType);
+ SensorsAnalyticsUtil.assertValue(ITEM_ID, itemId);
+ this.trackId = null;
+ return new ItemRecord(propertyMap, itemId, itemType, trackId);
}
public ItemRecord.Builder setItemId(String itemId) {
diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/UserRecord.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/UserRecord.java
index 4408094..07afde6 100644
--- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/UserRecord.java
+++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/bean/UserRecord.java
@@ -1,6 +1,9 @@
package com.sensorsdata.analytics.javasdk.bean;
+
+import com.sensorsdata.analytics.javasdk.SensorsConst;
import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException;
+import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil;
import java.io.Serializable;
import java.util.Date;
@@ -25,10 +28,16 @@ public class UserRecord implements Serializable {
private final Boolean isLoginId;
- private UserRecord(Map propertyMap, String distinctId, Boolean isLoginId) {
+ private final Integer trackId;
+
+ private UserRecord(Map propertyMap, String distinctId, Boolean isLoginId, Integer trackId) {
this.propertyMap = propertyMap;
+ if (isLoginId) {
+ propertyMap.put(SensorsConst.LOGIN_SYSTEM_ATTR, true);
+ }
this.distinctId = distinctId;
this.isLoginId = isLoginId;
+ this.trackId = trackId;
}
public static Builder builder() {
@@ -47,10 +56,13 @@ public Map getPropertyMap() {
return propertyMap;
}
+ public Integer getTrackId() {return trackId; }
+
public static class Builder {
- private final Map propertyMap = new HashMap();
+ private final Map propertyMap = new HashMap<>();
private String distinctId;
private Boolean isLoginId;
+ private Integer trackId;
private Builder() {
}
@@ -62,7 +74,10 @@ public UserRecord build() throws InvalidArgumentException {
if (isLoginId == null) {
throw new InvalidArgumentException("The isLoginId is empty.");
}
- return new UserRecord(propertyMap, distinctId, isLoginId);
+ SensorsAnalyticsUtil.assertValue("distinct_id", distinctId);
+ String message = String.format("[distinct_id=%s,target_table=user]",distinctId);
+ trackId = SensorsAnalyticsUtil.getTrackId(propertyMap, message);
+ return new UserRecord(propertyMap, distinctId, isLoginId, trackId);
}
public UserRecord.Builder setDistinctId(String distinctId) {
diff --git a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/util/SensorsAnalyticsUtil.java b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/util/SensorsAnalyticsUtil.java
index e284665..eeb5e4c 100644
--- a/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/util/SensorsAnalyticsUtil.java
+++ b/SensorsAnalyticsSDK/src/main/java/com/sensorsdata/analytics/javasdk/util/SensorsAnalyticsUtil.java
@@ -28,6 +28,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
+import lombok.extern.slf4j.Slf4j;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -36,9 +37,11 @@
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
+import java.util.Random;
import java.util.TimeZone;
import java.util.regex.Pattern;
+@Slf4j
public class SensorsAnalyticsUtil {
private static final Pattern KEY_PATTERN = Pattern.compile(
"^((?!^distinct_id$|^original_id$|^time$|^properties$|^id$|^first_id$|^second_id$|^users$|^events$|^event$|^user_id$|^date$|^datetime$|^user_group|^user_tag)[a-zA-Z_$][a-zA-Z\\d_$]{0,99})$",
@@ -177,6 +180,19 @@ public static void assertKey(String type, String key) throws InvalidArgumentExce
}
}
+ public static Integer getTrackId(Map propertyMap, String message) {
+ try {
+ if (propertyMap.containsKey(SensorsConst.TRACK_ID)) {
+ return Integer.parseInt((String.valueOf(propertyMap.get(SensorsConst.TRACK_ID))));
+ }
+ return new Random().nextInt();
+ } catch (Exception e) {
+ log.error("Failed Get Track ID. {} ", message ,e);
+ return new Random().nextInt();
+ }
+
+ }
+
/**
* IDM3.0 校验 distinctId 和 identities 集合,并生成最终 distinctId
*
diff --git a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/BatchConsumerTestConstruct.java b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/BatchConsumerTestConstruct.java
index f631644..d527001 100644
--- a/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/BatchConsumerTestConstruct.java
+++ b/SensorsAnalyticsSDK/src/test/java/com.sensorsdata.analytics.javasdk/BatchConsumerTestConstruct.java
@@ -3,6 +3,7 @@
import com.sensorsdata.analytics.javasdk.consumer.BatchConsumer;
import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
import java.lang.reflect.Field;
@@ -24,7 +25,9 @@ public class BatchConsumerTestConstruct {
private BatchConsumer batchConsumer;
private SensorsAnalytics sa;
private List