Skip to content

Commit

Permalink
[fea]SDK domain migration
Browse files Browse the repository at this point in the history
  • Loading branch information
chaofeng01 committed Jun 27, 2024
1 parent 69199ca commit e02feed
Show file tree
Hide file tree
Showing 20 changed files with 289 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.tiktok.appevents.TTPurchaseItem;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
Expand Down Expand Up @@ -305,12 +303,7 @@ public void handleMessage(Message message) {

// common purchase event
// Order with 2 items in cart
TTPurchaseItem item1 =new TTPurchaseItem
(10.5f, 2, "tshirt", "tshirt_sku_1");
TTPurchaseItem item2 = new TTPurchaseItem
(12.5f, 1, "tshirt", "tshirt_sku_2");
TikTokBusinessSdk.trackEvent("Purchase",
TTPurchaseItem.buildPurchaseProperties("USD", item1, item2));
TikTokBusinessSdk.trackEvent("Purchase");

// start delay track & force flush
TikTokBusinessSdk.startTrack();
Expand Down Expand Up @@ -371,14 +364,7 @@ public void handleMessage(Message message) {
.put("jsonObject", new JSONObject().put("nestedJSONObject", 1));
TikTokBusinessSdk.trackEvent("AchieveLevel", props);

// common purchase event
// Order with 2 items in cart
TTPurchaseItem item1 =new TTPurchaseItem
(10.5f, 2, "tshirt", "tshirt_sku_1");
TTPurchaseItem item2 = new TTPurchaseItem
(12.5f, 1, "tshirt", "tshirt_sku_2");
TikTokBusinessSdk.trackEvent("Purchase",
TTPurchaseItem.buildPurchaseProperties("USD", item1, item2));
TikTokBusinessSdk.trackEvent("Purchase");

// start delay track & force flush
// this will only trigger LaunchAPP event
Expand Down Expand Up @@ -491,14 +477,7 @@ public void handleMessage(Message message) {
// 2 total success
assertEquals(2, messageHandler.totalSuccessfulRequests);

// common purchase event
// Order with 2 items in cart
TTPurchaseItem item1 =new TTPurchaseItem
(10.5f, 2, "tshirt", "tshirt_sku_1");
TTPurchaseItem item2 = new TTPurchaseItem
(12.5f, 1, "tshirt", "tshirt_sku_2");
TikTokBusinessSdk.trackEvent("Purchase",
TTPurchaseItem.buildPurchaseProperties("USD", item1, item2));
TikTokBusinessSdk.trackEvent("Purchase");

sleep(5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class TikTokBusinessSdk {
/**
* api treckEvent sent Domain
*/
private static String apiTrackDomain = "business-api.tiktok.com";
private static String apiTrackDomain = "analytics.us.tiktok.com";
/**
* {@link LogLevel} of initialized sdk
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import static com.tiktok.util.TTConst.*;

import org.json.JSONException;
import org.json.JSONObject;

class TTAutoEventsManager {

private static final String TAG = TTAutoEventsManager.class.getCanonicalName();
Expand Down Expand Up @@ -67,7 +70,13 @@ private void trackFirstInstallEvent() {

/* check and track InstallApp. */
if (shouldTrackAppLifecycleEvents(AutoEvents.InstallApp)) {
appEventLogger.track(AutoEvents.InstallApp.name, null);
try {
JSONObject props = new JSONObject();
props.putOpt(TRACK_TYPE, TRACK_TYPE_AUTO);
appEventLogger.track(AutoEvents.InstallApp.name, props);
} catch (JSONException e) {
e.printStackTrace();
}
}

store.set(hm);
Expand All @@ -91,7 +100,13 @@ void track2DayRetentionEvent() {
Date now = new Date();
if (shouldTrackAppLifecycleEvents(AutoEvents.SecondDayRetention)
&& isSatisfyRetention(firstLaunchTime, now)) {
appEventLogger.track(AutoEvents.SecondDayRetention.name, null);
try {
JSONObject props = new JSONObject();
props.putOpt(TRACK_TYPE, TRACK_TYPE_AUTO);
appEventLogger.track(AutoEvents.SecondDayRetention.name, props);
} catch (JSONException e) {
e.printStackTrace();
}
store.set(TTSDK_APP_2DR_TIME, timeFormat.format(now));
}
} catch (ParseException ignored) {
Expand All @@ -100,7 +115,13 @@ && isSatisfyRetention(firstLaunchTime, now)) {

private void trackLaunchEvent() {
if (shouldTrackAppLifecycleEvents(AutoEvents.LaunchAPP)) {
appEventLogger.track(AutoEvents.LaunchAPP.name, null);
try {
JSONObject props = new JSONObject();
props.putOpt(TRACK_TYPE, TRACK_TYPE_AUTO);
appEventLogger.track(AutoEvents.LaunchAPP.name, props);
} catch (JSONException e) {
e.printStackTrace();
}
store.set(TTSDK_APP_LAST_LAUNCH, timeFormat.format(new Date()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

package com.tiktok.appevents;

import static com.tiktok.appevents.contents.TTContentsEventConstants.Params.EVENT_PROPERTY_ORDER_ID;
import static com.tiktok.util.TTConst.TRACK_TYPE;
import static com.tiktok.util.TTConst.TRACK_TYPE_AUTO;
import static com.tiktok.util.TTConst.TTSDK_EXCEPTION_SDK_CATCH;

import org.json.JSONArray;
Expand All @@ -25,7 +28,12 @@ static JSONObject getPurchaseProps(TTPurchaseInfo purchaseInfo) {
try {
productId = purchaseInfo.getPurchase().getString("productId");
JSONObject skuDetail = purchaseInfo.getSkuDetails();
return getPurchaseProperties(productId, skuDetail);
JSONObject purchaseProperties = getPurchaseProperties(productId, skuDetail);
if(purchaseInfo.isAutoTrack() && purchaseProperties != null){
purchaseProperties.putOpt(TRACK_TYPE, TRACK_TYPE_AUTO);
purchaseProperties.putOpt(EVENT_PROPERTY_ORDER_ID, purchaseInfo.getPurchase().optString("orderId"));
}
return purchaseProperties;
} catch (JSONException e) {
TTCrashHandler.handleCrash(TAG, e, TTSDK_EXCEPTION_SDK_CATCH);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class TTPurchaseInfo {
private final JSONObject purchase;
private final JSONObject skuDetails;
private String eventId;
private boolean isAutoTrack;

public static class InvalidTTPurchaseInfoException extends Exception {

Expand Down Expand Up @@ -92,4 +93,12 @@ private boolean isValidSkuDetails(JSONObject skuDetails) {
public String getEventId() {
return eventId;
}

public boolean isAutoTrack() {
return isAutoTrack;
}

public void setAutoTrack(boolean autoTrack) {
isAutoTrack = autoTrack;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static JSONObject getBusinessSDKConfig(Map<String, Object> options) {
paramsMap.put("tiktok_app_id", TikTokBusinessSdk.getFirstTTAppIds());
paramsMap.putAll(options);

String url = TTUtil.mapToString("https://business-api.tiktok.com/open_api/business_sdk_config/get/", paramsMap);
String url = TTUtil.mapToString("https://analytics.us.tiktok.com/api/v1/app_sdk/config", paramsMap);
logger.debug(url);
String result = HttpRequestUtil.doGet(url, getHeadParamMap);
logger.debug(result);
Expand Down Expand Up @@ -136,7 +136,7 @@ public static synchronized List<TTAppEvent> reportAppEvent(JSONObject basePayloa
successfulRequests = 0;
notifyChange();
// dynamic req domain and version
String url = "https://" + TikTokBusinessSdk.getApiTrackDomain() + "/open_api/" + TikTokBusinessSdk.getApiAvailableVersion() + "/app/batch/";
String url = "https://" + TikTokBusinessSdk.getApiTrackDomain() + "/api/v1/app_sdk/batch";

List<TTAppEvent> failedEventsToBeSaved = new ArrayList<>();
List<TTAppEvent> failedEventsToBeDiscarded = new ArrayList<>();
Expand Down Expand Up @@ -324,7 +324,7 @@ public static <T> List<List<T>> averageAssign(List<T> sourceList, int splitNum)
}

public static String reportMonitorEvent(JSONObject stat) {
String url = "https://" + TikTokBusinessSdk.getApiTrackDomain() + "/open_api/" + TikTokBusinessSdk.getApiAvailableVersion() + "/app/monitor/";
String url = "https://" + TikTokBusinessSdk.getApiTrackDomain() + "/api/v1/app_sdk/monitor";
return HttpRequestUtil.doPost(url, headParamMap, stat.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public static JSONObject getBasePayload() {
// the context part that does not change
private static JSONObject getImmutableContextForApi(TTAppEvent event) throws JSONException {
if (contextForApiCache != null) {
freshTTAppID(contextForApiCache, event);
freshOsVersion(contextForApiCache, event);
return contextForApiCache;
}
Expand All @@ -80,32 +79,10 @@ private static JSONObject getImmutableContextForApi(TTAppEvent event) throws JSO
TikTokBusinessSdk.getAppEventLogger().monitorMetric("did_end", meta, null);
} catch (Exception ignored) {}
contextForApiCache = contextBuilder(adIdInfo);
freshTTAppID(contextForApiCache, event);
freshOsVersion(contextForApiCache, event);
return contextForApiCache;
}

private static void freshTTAppID(JSONObject contextForApiCache, TTAppEvent event) {
try {
JSONObject app = contextForApiCache.getJSONObject("app");
if (event != null && app != null) {
JSONArray tiktokAppIds = new JSONArray();
if (event.getTiktokAppIds() != null) {
for (int i = 0; i < event.getTiktokAppIds().size(); i++) {
tiktokAppIds.put(event.getTiktokAppIds().get(i));
}
app.remove("tiktok_app_id");
app.put("tiktok_app_ids", tiktokAppIds);
}
} else {
app.remove("tiktok_app_ids");
app.put("tiktok_app_id", TikTokBusinessSdk.getTTAppId());
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}

private static void freshOsVersion(JSONObject contextForApiCache, TTAppEvent event) {
try {
JSONObject device = contextForApiCache.getJSONObject("device");
Expand Down Expand Up @@ -211,6 +188,7 @@ private static JSONObject contextBuilder(@Nullable TTIdentifierFactory.AdIdInfo
library.put("version", SystemInfoUtil.getSDKVersion());

JSONObject context = new JSONObject();
app.put("tiktok_app_id", TikTokBusinessSdk.getTTAppId());
context.put("app", app);
context.put("library", library);
context.put("device", device);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ interface Params {
String EVENT_PROPERTY_CURRENCY = "currency";
String EVENT_PROPERTY_VALUE = "value";
String EVENT_PROPERTY_CONTENTS = "contents";
String EVENT_PROPERTY_ORDER_ID = "order_id";
}

enum Currency {
AED, ARS, AUD, BDT, BHD, BIF, BOB, BRL, CAD, CHF, CLP, CNY, COP, CRC, CZK, DKK, DZD, EGP,
EUR, GBP, GTQ, HKD, HNL, HUF, IDR, ILS, INR, ISK, JPY, KES, KHR, KRW, KWD, KZT, MAD, MOP,
MXN, MYR, NGN, NIO, NOK, NZD, OMR, PEN, PHP, PKR, PLN, PYG, QAR, RON, RUB, SAR, SEK, SGD,
THB, TRY, TWD, UAH, USD, VES, VND, ZAR, BGN, IQD,JOD, LBP, TZS
THB, TRY, TWD, UAH, USD, VES, VND, ZAR, BGN, IQD, JOD, LBP, TZS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ private static void querySkuAndTrack(List<String> skus, Purchase purchase, boole
List<TTPurchaseInfo> purchaseInfos = new ArrayList<>();
try {
for (SkuDetails skuDetails : skuDetailsList) {
purchaseInfos.add(new TTPurchaseInfo(new JSONObject(purchase.getOriginalJson()),
new JSONObject(skuDetails.getOriginalJson())));
TTPurchaseInfo purchaseInfo = new TTPurchaseInfo(new JSONObject(purchase.getOriginalJson()),
new JSONObject(skuDetails.getOriginalJson()));
purchaseInfo.setAutoTrack(true);
purchaseInfos.add(purchaseInfo);
}
TikTokBusinessSdk.trackGooglePlayPurchase(purchaseInfos);
} catch (Throwable e) {
Expand Down
Loading

0 comments on commit e02feed

Please sign in to comment.