Skip to content

Commit

Permalink
Release 3.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiangliu committed Oct 25, 2022
1 parent fc78153 commit d0be037
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 23 deletions.
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<groupId>com.sensorsdata.analytics.javasdk</groupId>
<name>SensorsAnalyticsSDK</name>
<artifactId>SensorsAnalyticsSDK</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
<description>The official Java SDK of Sensors Analytics</description>
<url>http://sensorsdata.cn</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ void profileUnsetById(@NonNull SensorsAnalyticsIdentity analyticsIdentity, @NonN

void profileDelete(@NonNull String key, @NonNull String value) throws InvalidArgumentException;

void profileDelete(@NonNull Long userId) throws InvalidArgumentException;

void itemSet(@NonNull ItemSchema itemSchema) throws InvalidArgumentException;

void itemDelete(@NonNull ItemSchema itemSchema) throws InvalidArgumentException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,12 @@ public void profileDelete(@NonNull String key, @NonNull String value) throws Inv
worker.doSchemaData(new SensorsSchemaData(userSchema, PROFILE_DELETE_ACTION_TYPE));
}

@Override
public void profileDelete(@NonNull Long userId) throws InvalidArgumentException {
UserSchema userSchema = UserSchema.init().setUserId(userId).start();
worker.doSchemaData(new SensorsSchemaData(userSchema, PROFILE_DELETE_ACTION_TYPE));
}

@Override
public void itemSet(@NonNull ItemSchema itemSchema) throws InvalidArgumentException {
worker.doSchemaData(new SensorsSchemaData(itemSchema, ITEM_SET_ACTION_TYPE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private SensorsConst() {
/**
* 当前JDK版本号,注意要和pom文件里面的version保持一致
*/
public static final String SDK_VERSION = "3.5.1";
public static final String SDK_VERSION = "3.5.2";
/**
* 当前语言类型
*/
Expand All @@ -33,6 +33,8 @@ private SensorsConst() {
*/
public static final String USER_SCHEMA = "users";

public static final String USER_ID_PREFIX = "SENSORS_ID:";

/**
* 事件上报类型
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.sensorsdata.analytics.javasdk.bean.schema.UserSchema;
import com.sensorsdata.analytics.javasdk.common.Pair;
import com.sensorsdata.analytics.javasdk.common.SchemaTypeEnum;
import com.sensorsdata.analytics.javasdk.util.SensorsAnalyticsUtil;

import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -100,15 +99,15 @@ public Map<String, Object> generateData() {
data.put("event", getEvent());
break;
case USER:
checkUserIdAndAddUser(data, userId, getDistinctId(), getIdentities());
checkUserIdAndAddUser(data, "id");
break;
case USER_EVENT:
data.put("event", getEvent());
checkUserIdAndAddUser(getProperties(), userId, getDistinctId(), getIdentities());
checkUserIdAndAddUser(getProperties(), "user_id");
break;
case USER_ITEM:
data.put("id", getItemId());
checkUserIdAndAddUser(getProperties(), userId, getDistinctId(), getIdentities());
checkUserIdAndAddUser(getProperties(), "user_id");
break;
default:
break;
Expand Down Expand Up @@ -140,18 +139,12 @@ public SchemaTypeEnum getSchemaTypeEnum() {
}


private void checkUserIdAndAddUser(Map<String, Object> data, Long userId, String distinctId,
Map<String, String> identities) {
if (null != userId) {
if (null != distinctId || (null != identities && !identities.isEmpty())) {
log.warn(
"data record found userId,so (distinct_id/identities) node expired.[userId:{},distinctId:{},identities:{}]",
userId, distinctId, SensorsAnalyticsUtil.toString(identities));
}
data.put("user_id", userId);
private void checkUserIdAndAddUser(Map<String, Object> data, String key) {
if (null != getUserId()) {
data.put(key, getUserId());
} else {
data.put("identities", getIdentities());
data.put("distinct_id", getDistinctId());
}
data.put("distinct_id", getDistinctId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public UserItemSchema start() throws InvalidArgumentException {
return new UserItemSchema(idMap, schema, itemId, properties, trackId, userId, distinctId);
}

public UISBuilder setUserId(@NonNull Long userId) {
this.userId = userId;
return this;
}

public UISBuilder setItemId(@NonNull String itemId) {
this.itemId = itemId;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public UserSchema start() throws InvalidArgumentException {
return new UserSchema(idMap, properties, distinctId, trackId, userId);
}


public USBuilder setUserId(@NonNull Long userId) {
this.userId = userId;
return this;
}

public USBuilder identityMap(@NonNull Map<String, String> identityMap) {
this.idMap.putAll(identityMap);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,15 @@ public static String checkUserInfo(Long userId, Map<String, String> identities,
if (identities.isEmpty() && null == userId) {
throw new InvalidArgumentException("missing user info node(identities/user_id).");
}
if (!identities.isEmpty()) {
Pair<String, Boolean> userPair = checkIdentitiesAndGenerateDistinctId(distinctId, identities);
return userPair.getKey();
if (userId != null) {
if (distinctId != null) {
assertValue("distinct_id", distinctId);
return distinctId;
} else
return SensorsConst.USER_ID_PREFIX + userId;
}
return null;
Pair<String, Boolean> userPair = checkIdentitiesAndGenerateDistinctId(distinctId, identities);
return userPair.getKey();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sensorsdata.analytics.javasdk;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -8,9 +9,11 @@
import com.sensorsdata.analytics.javasdk.bean.schema.UserEventSchema;
import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException;

import org.junit.Assert;
import org.junit.Test;

import java.util.Date;
import java.util.Map;

/**
* userEventSchema 单元测试
Expand Down Expand Up @@ -122,4 +125,57 @@ public void checkSchemaUnbind() throws InvalidArgumentException {

}


//----------------------------------v3.5.2--------------------------------------

/**
* 支持 user 数据传入 userId 作为用户标识
* <p>期望:用户数据传入 userId,最终节点中包含 userId 和 distinctId 信息 </p>
*/
@Test
public void checkUserId() throws InvalidArgumentException {
UserEventSchema userEventSchema = UserEventSchema.init()
.setUserId(123L)
.setEventName(EVENT_NAME)
.start();
sa.track(userEventSchema);
assertUESData(data);
}

/**
* 同时传入 userID 和 identities 节点
* <p>期望:userId 优先级最高,两者同时传入,最终数据中存在 userId </p>
*/
@Test
public void checkUserIdAndIdentities() throws InvalidArgumentException {
UserEventSchema userEventSchema = UserEventSchema.init()
.setUserId(123L)
.addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "eee")
.setEventName(EVENT_NAME)
.start();
sa.track(userEventSchema);
assertUESData(data);
Map<String, Object> properties = (Map<String, Object>) data.get("properties");
assertTrue(properties.containsKey("user_id"));
assertEquals(123L, properties.get("user_id"));
}

/**
* 同时传入 userID 和 distinctId 节点
* <p>期望:最终数据节点中,以传入的 distinctId 为主 </p>
*/
@Test
public void checkUserIdAndDistinctId() throws InvalidArgumentException {
UserEventSchema userEventSchema = UserEventSchema.init()
.setUserId(123L)
.setDistinctId("test")
.setEventName(EVENT_NAME)
.start();
sa.track(userEventSchema);
assertUESData(data);
Map<String, Object> properties = (Map<String, Object>) data.get("properties");
Assert.assertTrue(properties.containsKey("distinct_id"));
Assert.assertEquals("test", properties.get("distinct_id"));
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.sensorsdata.analytics.javasdk;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.sensorsdata.analytics.javasdk.bean.SensorsAnalyticsIdentity;
import com.sensorsdata.analytics.javasdk.bean.schema.UserItemSchema;
import com.sensorsdata.analytics.javasdk.bean.schema.UserSchema;
import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException;

import org.junit.Assert;
import org.junit.Test;

import java.util.Map;

/**
* TODO
*
Expand Down Expand Up @@ -74,4 +81,65 @@ public void checkIdentities() throws InvalidArgumentException {
assertUISData(data);
}


//----------------------------------v3.5.2--------------------------------------

/**
* 支持 user 数据传入 userId 作为用户标识
* <p>期望:用户数据传入 userId,最终节点中包含 userId 和 distinctId 信息 </p>
*/
@Test
public void checkUserId() throws InvalidArgumentException {
UserItemSchema userItemSchema = UserItemSchema.init()
.setItemId("eee")
.setSchema("www")
.setUserId(123L)
.start();
sa.itemSet(userItemSchema);
assertUISData(data);
}

/**
* 同时传入 userID 和 identities 节点
* <p>期望:userId 优先级最高,两者同时传入,最终数据中存在 userId </p>
*/
@Test
public void checkUserIdAndIdentities() throws InvalidArgumentException {
UserItemSchema userItemSchema = UserItemSchema.init()
.setItemId("eee")
.setSchema("www")
.setUserId(123L)
.addIdentityProperty(SensorsAnalyticsIdentity.LOGIN_ID, "login_id123")
.start();
sa.itemSet(userItemSchema);
assertUISData(data);
Map<String, Object> properties = (Map<String, Object>) data.get("properties");
assertTrue(properties.containsKey("user_id"));
}

/**
* 同时传入 userID 和 distinctId 节点
* <p>期望:最终数据节点中,以传入的 distinctId 为主 </p>
*/
@Test
public void checkUserIdAndDistinctId() throws InvalidArgumentException {
UserSchema userSchema = UserSchema.init()
.setUserId(123L)
.setDistinctId("test")
.addIdentityProperty("key1", "value1")
.addProperty("$project", "abc")
.start();
sa.profileSet(userSchema);
assertUSData(data);
Assert.assertTrue(data.containsKey("distinct_id"));
Assert.assertEquals("test", data.get("distinct_id"));
}

@Test
public void checkProfileDeleteUserId() throws InvalidArgumentException {
sa.profileDelete(123L);
assertUSData(data);
assertEquals(123L, data.get("id"));
assertNotNull(data.get("distinct_id"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sensorsdata.analytics.javasdk.bean.schema.UserSchema;
import com.sensorsdata.analytics.javasdk.exceptions.InvalidArgumentException;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
Expand Down Expand Up @@ -100,4 +101,53 @@ public void checkPreDefineProperties() throws InvalidArgumentException {

}

//----------------------------------v3.5.2--------------------------------------

/**
* 支持 user 数据传入 userId 作为用户标识
* <p>期望:用户数据传入 userId,最终节点中包含 userId 和 distinctId 信息 </p>
*/
@Test
public void checkUserId() throws InvalidArgumentException {
UserSchema userSchema = UserSchema.init()
.setUserId(123L)
.addProperty("$project", "abc")
.start();
sa.profileSet(userSchema);
assertUSData(data);
}

/**
* 同时传入 userID 和 identities 节点
* <p>期望:userId 优先级最高,两者同时传入,最终数据中存在 userId </p>
*/
@Test
public void checkUserIdAndIdentities() throws InvalidArgumentException {
UserSchema userSchema = UserSchema.init()
.setUserId(123L)
.addIdentityProperty("key1", "value1")
.addProperty("$project", "abc")
.start();
sa.profileSet(userSchema);
assertUSData(data);
Assert.assertTrue(data.containsKey("id"));
}

/**
* 同时传入 userID 和 distinctId 节点
* <p>期望:最终数据节点中,以传入的 distinctId 为主 </p>
*/
@Test
public void checkUserIdAndDistinctId() throws InvalidArgumentException {
UserSchema userSchema = UserSchema.init()
.setUserId(123L)
.setDistinctId("test")
.addIdentityProperty("key1", "value1")
.addProperty("$project", "abc")
.start();
sa.profileSet(userSchema);
assertUSData(data);
Assert.assertTrue(data.containsKey("distinct_id"));
Assert.assertEquals("test", data.get("distinct_id"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ protected void assertUESData(Map<String, Object> data) {
assertTrue("数据节点 properties 类型不正确!", properties instanceof Map);
Map<String, String> proMap = (Map<String, String>) properties;
assertTrue("数据中用户信息节点(identities+distinct_id/user_id)丢失!",
(proMap.containsKey("identities") && proMap.containsKey("distinct_id")) || proMap.containsKey("user_id"));
(proMap.containsKey("identities") || proMap.containsKey("user_id")) && proMap.containsKey("distinct_id"));
}

protected void assertEventIdentitiesInfo(Map<String, Object> data, String expectDistinctId) {
Expand Down Expand Up @@ -259,7 +259,7 @@ protected void assertISData(Map<String, Object> data) {
protected void assertUSData(Map<String, Object> data) {
assertSchemaDataNode(data);
assertTrue("数据中用户信息节点(identities+distinct_id/user_id)丢失!",
(data.containsKey("identities") && data.containsKey("distinct_id")) || data.containsKey("user_id"));
(data.containsKey("identities") || data.containsKey("id")) && data.containsKey("distinct_id"));
}

/**
Expand All @@ -272,7 +272,7 @@ protected void assertUISData(Map<String, Object> data) {
assertTrue("数据节点 properties 类型不正确!", properties instanceof Map);
Map<String, String> proMap = (Map<String, String>) properties;
assertTrue("数据中用户信息节点(identities+distinct_id/user_id)丢失!",
(proMap.containsKey("identities") && proMap.containsKey("distinct_id")) || proMap.containsKey("user_id"));
(proMap.containsKey("identities") || proMap.containsKey("user_id")) && proMap.containsKey("distinct_id"));
assertTrue("数据节点 properties 内节点个数不正常!", proMap.size() >= 2);
}

Expand Down

0 comments on commit d0be037

Please sign in to comment.