고급 가이드
1. 이벤트 전송
SDK 초기화가 완료되면 데이터 트래킹을 진행하여 유저의 행동 정보를 수집할 수 있습니다. 일반적으로는 일반 이벤트로 비즈니스 시나리오의 요구를 충족할 수 있으며, 실제 비즈니스 시나리오에 따라 최초 이벤트, 업데이트 가능 이벤트 등을 사용할 수도 있습니다.
1.1 일반 이벤트
track을 호출하여 이벤트를 전송할 수 있습니다. 앞서 정리한 문서에 따라 이벤트 속성과 이벤트 전송 조건을 설정하는 것을 권장합니다. 여기서는 유저가 어떤 상품을 구매하는 경우를 예로 듭니다:
//이벤트 속성 설정
Map<String,Object> properties = new HashMap<String,Object>();
properties.put("product_name","상품명");//문자열
try {
te.track("account_id","distinct_id","product_buy",properties);
} catch (Exception e) {
System.out.println("except:"+e);
}
1.2 최초 이벤트
최초 이벤트는 디바이스 또는 기타 차원의 ID를 기준으로 한 번만 기록되는 이벤트입니다. 예를 들어 특정 디바이스에서의 활성화 이벤트를 기록하려는 경우 최초 이벤트로 데이터를 전송할 수 있습니다. 최초 이벤트 검증 기능을 사용하려면 반드시 properties에 #first_check_id 필드를 설정해야 하며, 타입은 문자열입니다.
// 최초 이벤트 전송, 이벤트 이름은 device_activation, first_check_id의 값은 device_id
Map<String, Object> properties = new HashMap<>();
properties.put("price",100);
properties.put("status",3);
properties.put("#first_check_id","device_id");
te.trackFirst("account_id", "distinct_id", "device_activation", properties);
주의: 최초 여부 검증은 서버 측에서 이루어지므로 최초 이벤트는 기본적으로 1시간 지연되어 저장됩니다.
1.3 업데이트 가능 이벤트
업데이트 가능 이벤트를 사용하면 특정 시나리오에서 이벤트 데이터를 수정해야 하는 요구를 충족할 수 있습니다. 업데이트 가능 이벤트는 해당 이벤트를 식별하는 ID를 지정해야 하며, 업데이트 가능 이벤트 객체를 생성할 때 전달합니다. AE 백엔드는 이벤트 이름과 이벤트 ID를 기준으로 업데이트할 데이터를 결정합니다.
// 업데이트 가능 이벤트 전송, 이벤트 이름은 UPDATABLE_EVENT
// 전송 후 이벤트 속성 status는 3, price는 100
Map<String, Object> properties = new HashMap<>();
properties.put("price",100);
properties.put("status",3);
te.trackUpdate("account_id","distinct_id","UPDATABLE_EVENT","test_event_id",properties);
// 전송 후 동일한 이벤트 속성 status는 5로 업데이트되고, price는 변경되지 않음
Map<String, Object> protertiesNew = new HashMap<>();
protertiesNew.put("status",5);
te.trackUpdate("account_id", "distinct_id", "UPDATABLE_EVENT", "test_event_id", protertiesNew);
1.4 덮어쓰기 가능 이벤트
덮어쓰기 가능 이벤트는 업데이트 가능 이벤트와 비슷하지만, 최신 데이터로 과거 데이터를 완전히 덮어쓴다는 점이 다릅니다. 효과 면에서는 이전 데이터를 삭제하고 최신 데이터를 저장하는 것과 같습니다. AE 백엔드는 이벤트 이름과 이벤트 ID를 기준으로 업데이트할 데이터를 결정합니다.
// 덮어쓰기 가능 이벤트 전송, 이벤트 이름은 OVERWRITE_EVENT
// 전송 후 이벤트 속성 status는 3, price는 100
Map<String, Object> properties = new HashMap<>();
properties.put("price",100);
properties.put("status",3);
te.trackOverwrite("account_id","distinct_id", "OVERWRITE_EVENT","test_event_id", properties);
// 전송 후 이벤트 속성 status는 5로 업데이트되고, price 속성은 삭제됨
Map<String, Object> protertiesNew = new HashMap<>();
protertiesNew.put("status",5);
te.trackOverwrite("account_id", "distinct_id", "OVERWRITE_EVENT", "test_event_id", protertiesNew);
2. 유저 속성
AE 플랫폼에서 지원하는 유저 속성 설정 API는 다음과 같습니다: userSet, userSetOnce, userAdd, userUnset, userDelete, userAppend, userUniqAppend.
2.1 userSet
일반적인 유저 속성은 userSet을 호출하여 설정할 수 있습니다. 이 인터페이스로 전송한 속성은 원래 속성 값을 덮어쓰며, 이전에 해당 유저 속성이 없었다면 새로 생성하고 타입은 전달된 속성의 타입과 같습니다. 여기서는 사용자 이름 설정을 예로 듭니다.
//이때 user_name은 TA
Map<String,Object> userProperties = new HashMap<String,Object>();
userProperties.put("user_name", "TA");
try {
te.userSet("account_id","distinct_id",userProperties);
} catch (Exception e) {
System.out.println("except:"+e);
}
//이때 user_name은 AE
Map<String,Object> newUserProperties = new HashMap<String,Object>();
newUserProperties.put("user_name", "AE");
try {
te.userSet("account_id","distinct_id",newUserProperties);
} catch (Exception e) {
System.out.println("except:"+e);
}
2.2 userSetOnce
전송하려는 유저 속성을 한 번만 설정하면 되는 경우 userSetOnce를 호출하여 설정할 수 있습니다. 해당 속성에 이미 값이 있으면 이 정보는 무시됩니다. 여기서는 최초 결제 시간 설정을 예로 듭니다:
//first_payment_time은 2018-01-01 01:23:45.678
Map<String,Object> userProperties = new HashMap<String,Object>();
userProperties.put("first_payment_time","2018-01-01 01:23:45.678");
try {
te.userSetOnce("account_id","distinct_id",userProperties);
} catch (Exception e) {
System.out.println("except:"+e);
}
//first_payment_time은 여전히 2018-01-01 01:23:45.678
Map<String,Object> newUserProperties = new HashMap<String,Object>();
newUserProperties.put("first_payment_time","2018-12-31 01:23:45.678");
try {
te.userSetOnce("account_id","distinct_id",newUserProperties);
} catch (Exception e) {
System.out.println("except:"+e);
}
2.3 userAdd
숫자형 속성을 업로드할 때 userAdd를 호출하여 해당 속성을 누적할 수 있습니다. 해당 속성이 아직 설정되지 않았으면 0을 할당한 후 계산합니다. 음수를 전달할 수도 있으며, 이는 빼기 연산과 같습니다. 여기서는 누적 결제 금액을 예로 듭니다.
Map<String,Object> userProperties = new HashMap<String,Object>();
userProperties.put("total_revenue",30);
//유저 속성 업로드, 이때 "total_revenue"의 값은 30
try {
te.userAdd("account_id","distinct_id",userProperties);
} catch (Exception e) {
System.out.println("except:"+e);
}
//유저 속성을 다시 업로드하면 "total_revenue"의 값은 678로 누적됨
Map<String,Object> newUserProperties = new HashMap<String,Object>();
newUserProperties.put("total_revenue",648);
try {
te.userAdd("account_id","distinct_id",newUserProperties);
} catch (Exception e) {
System.out.println("except:"+e);
}
설정하는 속성 key는 문자열이며, Value는 숫자만 허용됩니다.
2.4 userAppend
userAppend를 호출하여 배열 타입의 유저 속성에 요소를 추가할 수 있습니다.
Map<String,Object> properties = new HashMap<String,Object>();
List<String> list = new ArrayList<>();
list.add("apple");
list.add("ball");
properties.put("user_list",list);
try{
//이때 user_list의 속성 값은 ["apple","ball"]
te.userAppend("account_id", "distinct_id", properties);
} catch (Exception e) {
System.out.println("except:"+e);
}
2.5 userUniqAppend
userUniqAppend를 호출하여 배열 타입의 유저 속성에 값을 추가할 수 있습니다. userUniqAppend 인터페이스를 호출하면 추가하는 유저 속성 값의 중복이 제거되며, userAppend 인터페이스는 중복을 제거하지 않으므로 유저 속성에 중복 값이 있을 수 있습니다.
Map<String,Object> properties = new HashMap<String,Object>();
List<String> list = new ArrayList<>();
list.add("apple");
list.add("ball");
properties.put("user_list",list);
Map<String,Object> newProperties = new HashMap<String,Object>();
List<String> newList = new ArrayList<>();
newList.add("apple");
newList.add("cube");
newProperties.put("user_list", newList);
try{
//이때 user_list의 속성 값은 ["apple","ball"]
te.userAppend("account_id", "distinct_id", properties);
//이때 user_list의 속성 값은 ["apple","apple","ball","cube"]
te.userAppend("account_id", "distinct_id",newProperties);
//이때 user_list의 속성 값은 ["apple","ball","cube"]
te.userUniqAppend("account_id", "distinct_id",newProperties);
} catch (Exception e) {
System.out.println("except:"+e);
}
2.6 userUnset
유저의 유저 속성 값을 비우려면 userUnset을 호출하여 지정한 속성을 비울 수 있습니다. 해당 속성이 아직 클러스터에 생성되지 않았다면 userUnset은 해당 속성을 생성하지 않습니다
// 여러 유저 속성 초기화
try {
te.userUnset("account_id", "distinct_id", "key1", "key2", "key3");
} catch (Exception e) {
System.out.println("except:"+e);
}
user_unset: 전달 값은 비울 속성의 Key 값입니다.
2.7 userDelete
특정 유저를 삭제하려면 userDelete를 호출하여 해당 유저를 삭제할 수 있습니다. 삭제 후에는 해당 유저의 유저 속성을 더 이상 조회할 수 없지만, 해당 유저가 발생시킨 이벤트는 계속 조회할 수 있습니다. 이 작업은 되돌릴 수 없는 결과를 초래할 수 있으므로 신중하게 사용하십시오
try{
te.userDelete("account_id","distinct_id");
} catch (Exception e) {
System.out.println("except:"+e);
}
3. 기타 기능
3.1 TDBatchConsumer
데이터양이 너무 많거나 네트워크에 이상이 있으면 데이터가 유실될 위험이 있으므로 운영 환경에서는 사용하지 않는 것을 권장합니다
데이터를 일괄로 실시간으로 AE 서버에 전송하며, 별도의 전송 도구가 필요하지 않습니다. 네트워크 문제로 전송에 실패하면 3회 재시도하고, 그래도 실패하면 데이터를 버퍼에 저장합니다. 버퍼 크기는 설정할 수 있으며 기본값은 50입니다. 즉, 버퍼에 보관되는 데이터 총수는 최대 50*20입니다(20은 매번 업로드하는 batch 값이며 설정 가능).
TDAnalytics te = null;
try {
te = new TDAnalytics(new TDBatchConsumer("SERVER_URL", "APPID"));
} catch (Exception ignored){
}
파라미터 설명:
-
APPID: 프로젝트의 APPID로, AE 백엔드의 프로젝트 관리 페이지에서 확인할 수 있습니다 -
SERVER_URL: 데이터 전송 URL- 클라우드 서비스를 사용하는 경우 다음을 입력합니다: https://global-receiver-ta.thinkingdata.cn
- 프라이빗 배포 버전을 사용하는 경우 데이터 수집 주소에 도메인을 바인딩하고 HTTPS 인증서를 설정하십시오: https://데이터 수집 주소에 바인딩한 도메인
3.2 정기 플러시 기능
Config의 interval 및 autoFlush 파라미터를 설정하여 데이터 정기 전송 기능을 활성화할 수 있습니다.
TDAnalytics te = null;
// e.g. TDLogConsumer
try {
TDLoggerConsumer.Config config = new TDLoggerConsumer.Config("./log");
// The cache event is reported every 10 seconds
config.setAutoFlush(true);
config.setInterval(10);
te = new TDAnalytics(new TDLoggerConsumer(config));
} catch (Exception ignored){}
// e.g. TDBatchConsumer
try {
TDBatchConsumer.Config config = new TDBatchConsumer.Config();
// The cache event is reported every 10 seconds
config.setAutoFlush(true);
config.setInterval(10);
te = new TDAnalytics(new TDBatchConsumer("url", "appId", config));
} catch (Exception ignored){}

