Skip to content

Commit

Permalink
feat: add preset traffic source attributes (#78)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoweii <[email protected]>
  • Loading branch information
zhu-xiaowei and xiaoweii authored Apr 10, 2024
1 parent cd37cc4 commit d0f6d2b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 6 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,24 @@ public void onCreate() {
}
```
#### 3.2 Initialize the SDK with global attributes and custom configuration

The following example code shows how to add traffic source fields as global attributes when initializing the SDK.

```java
import software.aws.solution.clickstream.ClickstreamAnalytics;

public void onCreate() {
super.onCreate();
try{
ClickstreamAttribute globalAttributes = ClickstreamAttribute.builder()
.add("_traffic_source_name", "Summer promotion")
.add("_traffic_source_medium", "Search engine")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_SOURCE, "amazon")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_MEDIUM, "cpc")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN, "summer_promotion")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN_ID, "summer_promotion_01")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_TERM, "running_shoes")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CONTENT, "banner_ad_1")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID, "amazon_ad_123")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID_PLATFORM, "amazon_ads")
.build();
ClickstreamConfiguration configuration = new ClickstreamConfiguration()
.withAppId("your appId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,5 +326,60 @@ public static class Attr {
* screen unique id.
*/
public static final String SCREEN_UNIQUE_ID = ReservedAttribute.SCREEN_UNIQUE_ID;

/**
* traffic source source.
*/
public static final String TRAFFIC_SOURCE_SOURCE = "_traffic_source_source";

/**
* traffic source medium.
*/
public static final String TRAFFIC_SOURCE_MEDIUM = "_traffic_source_medium";

/**
* traffic source campaign.
*/
public static final String TRAFFIC_SOURCE_CAMPAIGN = "_traffic_source_campaign";

/**
* traffic source campaign id.
*/
public static final String TRAFFIC_SOURCE_CAMPAIGN_ID = "_traffic_source_campaign_id";

/**
* traffic source term.
*/
public static final String TRAFFIC_SOURCE_TERM = "_traffic_source_term";

/**
* traffic source content.
*/
public static final String TRAFFIC_SOURCE_CONTENT = "_traffic_source_content";

/**
* traffic source clid.
*/
public static final String TRAFFIC_SOURCE_CLID = "_traffic_source_clid";

/**
* traffic source clid platform.
*/
public static final String TRAFFIC_SOURCE_CLID_PLATFORM = "_traffic_source_clid_platform";

/**
* app install channel.
*/
public static final String APP_INSTALL_CHANNEL = "_app_install_channel";

/**
* event value.
*/
public static final String VALUE = "_value";

/**
* event currency.
*/
public static final String CURRENCY = "_currency";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public void testAddItem() throws Exception {
ClickstreamEvent event = ClickstreamEvent.builder()
.name("testItem")
.add(ClickstreamAnalytics.Item.ITEM_ID, 123)
.add(ClickstreamAnalytics.Attr.VALUE, 11000)
.add(ClickstreamAnalytics.Attr.CURRENCY, "USD")
.setItems(new ClickstreamItem[] {item1, item2})
.build();
ClickstreamAnalytics.recordEvent(event);
Expand Down Expand Up @@ -274,7 +276,7 @@ public void testAddItem() throws Exception {
public void testAddGlobalAttribute() throws Exception {
long timestamp = System.currentTimeMillis();
ClickstreamAttribute globalAttribute = ClickstreamAttribute.builder()
.add("channel", "HUAWEI")
.add(ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL, "Amazon Store")
.add("level", 5.1)
.add("class", 6)
.add("timestamp", timestamp)
Expand All @@ -296,7 +298,8 @@ public void testAddGlobalAttribute() throws Exception {
JSONObject jsonObject = new JSONObject(eventString);
JSONObject attribute = jsonObject.getJSONObject("attributes");

Assert.assertEquals("HUAWEI", attribute.getString("channel"));
Assert.assertEquals("Amazon Store",
attribute.getString(ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL));
Assert.assertEquals(5.1, attribute.getDouble("level"), 0.01);
Assert.assertEquals(6, attribute.getInt("class"));
Assert.assertTrue(attribute.getBoolean("isOpenNotification"));
Expand All @@ -309,6 +312,46 @@ public void testAddGlobalAttribute() throws Exception {
}
}

/**
* test add traffic source attribute.
*
* @throws Exception exception
*/
@Test
public void testAddGlobalAttributeForTrafficSource() throws Exception {
ClickstreamAttribute globalAttribute = ClickstreamAttribute.builder()
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_SOURCE, "amazon")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_MEDIUM, "cpc")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN, "summer_promotion")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN_ID, "summer_promotion_01")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_TERM, "running_shoes")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CONTENT, "banner_ad_1")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID, "amazon_ad_123")
.add(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID_PLATFORM, "amazon_ads")
.add(ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL, "amazon_store")
.build();
ClickstreamAnalytics.addGlobalAttributes(globalAttribute);
ClickstreamAnalytics.recordEvent("testEvent");
assertEquals(1, dbUtil.getTotalNumber());
try (Cursor cursor = dbUtil.queryAllEvents()) {
cursor.moveToFirst();
String eventString = cursor.getString(2);
JSONObject jsonObject = new JSONObject(eventString);
JSONObject attribute = jsonObject.getJSONObject("attributes");
Assert.assertEquals("amazon", attribute.getString(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_SOURCE));
Assert.assertEquals("cpc", attribute.getString(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_MEDIUM));
Assert.assertEquals("summer_promotion",
attribute.getString(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN));
Assert.assertEquals("summer_promotion_01",
attribute.getString(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN_ID));
Assert.assertEquals("running_shoes", attribute.getString(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_TERM));
Assert.assertEquals("banner_ad_1", attribute.getString(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CONTENT));
Assert.assertEquals("amazon_ad_123", attribute.getString(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID));
Assert.assertEquals("amazon_ads",
attribute.getString(ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID_PLATFORM));
Assert.assertEquals("amazon_store", attribute.getString(ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL));
}
}

/**
* test add delete global attribute.
Expand All @@ -318,7 +361,7 @@ public void testAddGlobalAttribute() throws Exception {
@Test
public void testDeleteGlobalAttribute() throws Exception {
ClickstreamAttribute globalAttribute = ClickstreamAttribute.builder()
.add("channel", "HUAWEI")
.add(ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL, "Amazon Store")
.add("level", 5.1)
.add("class", 6)
.add("isOpenNotification", true)
Expand All @@ -340,7 +383,8 @@ public void testDeleteGlobalAttribute() throws Exception {
JSONObject jsonObject = new JSONObject(eventString);
JSONObject attribute = jsonObject.getJSONObject("attributes");

Assert.assertEquals("HUAWEI", attribute.getString("channel"));
Assert.assertEquals("Amazon Store",
attribute.getString(ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL));
Assert.assertFalse(attribute.has("level"));
Assert.assertTrue(attribute.getBoolean("isOpenNotification"));
}
Expand Down

0 comments on commit d0f6d2b

Please sign in to comment.