Skip to content

Commit

Permalink
支持 item 操作
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjiajie committed Jul 18, 2018
1 parent 7a060cf commit f057ff6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
6 changes: 3 additions & 3 deletions 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.1.6</version>
<version>3.1.7</version>
<description>The official Java SDK of Sensors Analytics</description>
<url>http://sensorsdata.cn</url>

Expand All @@ -28,8 +28,8 @@

<developers>
<developer>
<name>Yuhan ZOU</name>
<email>zouyuhan@sensorsdata.cn</email>
<name>Feng Jiajie</name>
<email>fengjiajie@sensorsdata.cn</email>
<url>http://sensorsdata.cn</url>
</developer>
</developers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,31 @@ public void profileDelete(String distinctId, boolean isLoginId) throws InvalidAr
addEvent(distinctId, isLoginId, null, "profile_delete", null, new HashMap<String, Object>());
}

/**
* 设置 item
*
* @param itemType item 类型
* @param itemId item ID
* @param properties item 相关属性
* @throws InvalidArgumentException 取值不符合规范抛出该异常
*/
public void itemSet(String itemType, String itemId, Map<String, Object> properties)
throws InvalidArgumentException {
addItem(itemType, itemId, "item_set", properties);
}

/**
* 删除 item
*
* @param itemType item 类型
* @param itemId item ID
* @throws InvalidArgumentException 取值不符合规范抛出该异常
*/
public void itemDelete(String itemType, String itemId)
throws InvalidArgumentException {
addItem(itemType, itemId, "item_delete", null);
}

/**
* 立即发送缓存中的所有日志
*/
Expand Down Expand Up @@ -1076,6 +1101,40 @@ private void addEvent(String distinctId, boolean isLoginId, String originDistinc
this.consumer.send(event);
}

private void addItem(String itemType, String itemId, String actionType,
Map<String, Object> properties) throws InvalidArgumentException {
assertKeyWithRegex("Item Type", itemType);
assertKey("Item Id", itemId);
assertProperties(actionType, properties);

String eventProject = null;
if (properties != null && properties.containsKey("$project")) {
eventProject = (String) properties.get("$project");
properties.remove("$project");
}

Map<String, Object> eventProperties = new HashMap<String, Object>();
if (properties != null) {
eventProperties.putAll(properties);
}

Map<String, String> libProperties = getLibProperties();

Map<String, Object> record = new HashMap<String, Object>();
record.put("type", actionType);
record.put("time", System.currentTimeMillis());
record.put("properties", eventProperties);
record.put("lib", libProperties);

if (eventProject != null) {
record.put("project", eventProject);
}

record.put("item_type", itemType);
record.put("item_id", itemId);
this.consumer.send(record);
}

private Map<String, String> getLibProperties() {
Map<String, String> libProperties = new HashMap<String, String>();
libProperties.put("$lib", "Java");
Expand Down

0 comments on commit f057ff6

Please sign in to comment.