Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
improvement: add parameters to get global configuration to help debug…
Browse files Browse the repository at this point in the history
…ging
  • Loading branch information
qiucheng committed Feb 25, 2021
1 parent ffe52d2 commit 44cd5d3
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 17 deletions.
9 changes: 5 additions & 4 deletions business-core/src/main/java/com/tiktok/TikTokBusinessSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.tiktok.appevents.TTCrashHandler;
import com.tiktok.appevents.TTPurchaseInfo;
import com.tiktok.appevents.TTUserInfo;
import com.tiktok.util.TTConst;
import com.tiktok.util.TTLogger;

import org.json.JSONObject;
Expand Down Expand Up @@ -429,7 +430,7 @@ public static class TTConfig {
/* auto init flag check in manifest */
private boolean autoStart = true;
/* disable custom auto events */
private List<String> disabledEvents;
private List<TTConst.AutoEvents> disabledEvents;

/**
* Read configs from <meta-data>
Expand Down Expand Up @@ -529,23 +530,23 @@ public TTConfig disableAutoEvents() {
* to disable auto event tracking for InstallApp event
*/
public TTConfig disableInstallLogging() {
this.disabledEvents.add("InstallApp");
this.disabledEvents.add(TTConst.AutoEvents.InstallApp);
return this;
}

/**
* to disable auto event tracking for LaunchAPP event
*/
public TTConfig disableLaunchLogging() {
this.disabledEvents.add("LaunchAPP");
this.disabledEvents.add(TTConst.AutoEvents.LaunchAPP);
return this;
}

/**
* to disable auto event tracking for 2Dretention event
*/
public TTConfig disableRetentionLogging() {
this.disabledEvents.add("2Dretention");
this.disabledEvents.add(TTConst.AutoEvents.SecondDayRetention);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@

import com.tiktok.TikTokBusinessSdk;
import com.tiktok.util.SystemInfoUtil;
import com.tiktok.util.TTConst;
import com.tiktok.util.TTLogger;
import com.tiktok.util.TTUtil;

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand All @@ -39,7 +42,7 @@ public class TTAppEventLogger {
// whether to trigger automatic events in the lifeCycle callbacks provided by Android
final boolean lifecycleTrackEnable;
// custom auto event disable, events will be disabled when disabledEvents.contains(event)
final List<String> disabledEvents;
final List<TTConst.AutoEvents> disabledEvents;
/**
* Logger util
*/
Expand Down Expand Up @@ -67,7 +70,7 @@ public static List<TTAppEvent> getSuccessfulEvents() {
return TTRequest.getSuccessfullySentRequests();
}

public TTAppEventLogger(boolean lifecycleTrackEnable, List<String> disabledEvents) {
public TTAppEventLogger(boolean lifecycleTrackEnable, List<TTConst.AutoEvents> disabledEvents) {
logger = new TTLogger(TAG, TikTokBusinessSdk.getLogLevel());
this.lifecycleTrackEnable = lifecycleTrackEnable;
this.disabledEvents = disabledEvents;
Expand Down Expand Up @@ -337,7 +340,16 @@ public void fetchGlobalConfig(int delaySeconds) {
addToLater(() -> {
try {
logger.info("Fetching global config....");
JSONObject requestResult = TTRequest.getBusinessSDKConfig();

Map<String, Object> options = new HashMap<>();
options.put("disable" + TTConst.AutoEvents.InstallApp.name,
!this.autoEventsManager.shouldTrackAppLifecycleEvents(TTConst.AutoEvents.InstallApp));
options.put("disable" + TTConst.AutoEvents.LaunchAPP.name,
!this.autoEventsManager.shouldTrackAppLifecycleEvents(TTConst.AutoEvents.LaunchAPP));
options.put("disable" + TTConst.AutoEvents.SecondDayRetention.name,
!this.autoEventsManager.shouldTrackAppLifecycleEvents(TTConst.AutoEvents.SecondDayRetention));

JSONObject requestResult = TTRequest.getBusinessSDKConfig(options);

if (requestResult == null) {
logger.info("Opt out of initGlobalConfig because global config is null, either api returns error or access token is not correct");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.tiktok.appevents;

import com.tiktok.TikTokBusinessSdk;
import com.tiktok.util.TTConst;
import com.tiktok.util.TTKeyValueStore;

import java.text.ParseException;
Expand Down Expand Up @@ -38,7 +39,7 @@ public TTAutoEventsManager(TTAppEventLogger appEventLogger) {
store = new TTKeyValueStore(TikTokBusinessSdk.getApplicationContext());
}

private boolean shouldTrackAppLifecycleEvents(String event) {
Boolean shouldTrackAppLifecycleEvents(TTConst.AutoEvents event) {
return appEventLogger.lifecycleTrackEnable
&& !appEventLogger.disabledEvents.contains(event);
}
Expand All @@ -65,8 +66,8 @@ private void trackFirstInstallEvent() {
hm.put(TTSDK_APP_FIRST_INSTALL, timeFormat.format(now));

/* check and track InstallApp. */
if (shouldTrackAppLifecycleEvents("InstallApp")) {
appEventLogger.track("InstallApp", null);
if (shouldTrackAppLifecycleEvents(AutoEvents.InstallApp)) {
appEventLogger.track(AutoEvents.InstallApp.name, null);
}

store.set(hm);
Expand All @@ -88,18 +89,18 @@ void track2DayRetentionEvent() {
try {
Date firstLaunchTime = timeFormat.parse(firstInstall);
Date now = new Date();
if (shouldTrackAppLifecycleEvents("2Dretention")
if (shouldTrackAppLifecycleEvents(AutoEvents.SecondDayRetention)
&& isSatisfyRetention(firstLaunchTime, now)) {
appEventLogger.track("2Dretention", null);
appEventLogger.track(AutoEvents.SecondDayRetention.name, null);
store.set(TTSDK_APP_2DR_TIME, timeFormat.format(now));
}
} catch (ParseException ignored) {
}
}

private void trackLaunchEvent() {
if (shouldTrackAppLifecycleEvents("LaunchAPP")) {
appEventLogger.track("LaunchAPP", null);
if (shouldTrackAppLifecycleEvents(AutoEvents.LaunchAPP)) {
appEventLogger.track(AutoEvents.LaunchAPP.name, null);
store.set(TTSDK_APP_LAST_LAUNCH, timeFormat.format(new Date()));
}
}
Expand Down
15 changes: 13 additions & 2 deletions business-core/src/main/java/com/tiktok/appevents/TTRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.tiktok.BuildConfig;
import com.tiktok.TikTokBusinessSdk;
import com.tiktok.util.HttpRequestUtil;
import com.tiktok.util.SystemInfoUtil;
import com.tiktok.util.TTConst;
import com.tiktok.util.TTLogger;
import com.tiktok.util.TTUtil;
Expand Down Expand Up @@ -57,10 +58,20 @@ class TTRequest {
getHeadParamMap.put("User-Agent", ua);
}

public static JSONObject getBusinessSDKConfig() {
public static JSONObject getBusinessSDKConfig(Map<String, Object> options) {
logger.info("Try to fetch global configs");
getHeadParamMap.put("access-token", TikTokBusinessSdk.getAccessToken());
String url = "https://ads.tiktok.com/open_api/business_sdk_config/get/?app_id=" + TikTokBusinessSdk.getAppId();
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("app_id", TikTokBusinessSdk.getAppId());
// the rest params are for the sake of simplicity of debugging
paramsMap.put("client", "android");
paramsMap.put("sdk_version", SystemInfoUtil.getSDKVersion());
paramsMap.put("app_name", SystemInfoUtil.getAppName());
paramsMap.put("app_version", SystemInfoUtil.getAppVersionName());
paramsMap.putAll(options);

String url = "https://ads.tiktok.com/open_api/business_sdk_config/get/?" + TTUtil.mapToString(paramsMap, "&");
logger.debug(url);
String result = HttpRequestUtil.doGet(url, getHeadParamMap);
logger.debug(result);
JSONObject config = null;
Expand Down
12 changes: 12 additions & 0 deletions business-core/src/main/java/com/tiktok/util/TTConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ public static enum ApiErrorCodes {
this.code = code;
}
}

public static enum AutoEvents {
InstallApp("InstallApp"),
SecondDayRetention("2Dretention"),
LaunchAPP("LaunchAPP");

public String name;

AutoEvents(String name) {
this.name = name;
}
}
}
28 changes: 28 additions & 0 deletions business-core/src/main/java/com/tiktok/util/TTUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;
import java.util.UUID;

import static com.tiktok.util.TTConst.TTSDK_APP_ANONYMOUS_ID;
Expand Down Expand Up @@ -72,4 +73,31 @@ public static String getOrGenAnoId(Context context, boolean forceGenerate) {
}
return anoId;
}

public static String escapeHTML(String s) {
StringBuilder out = new StringBuilder(Math.max(16, s.length()));
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '"' || c == '\'' || c == '<' || c == '>' || c == '&') {
out.append("&#");
out.append((int) c);
out.append(';');
} else {
out.append(c);
}
}
return out.toString();
}

public static String mapToString(Map<String, Object> map, String separator) {
if (map.isEmpty()) {
return "";
}
StringBuffer buf = new StringBuffer();
for (Map.Entry<String, Object> entry : map.entrySet()) {
buf.append(entry.getKey() + "=" + escapeHTML(entry.getValue().toString()) + "&");
}
String finalStr = buf.toString();
return finalStr.substring(0, finalStr.length() - 1);
}
}
2 changes: 1 addition & 1 deletion samples/internalmonitor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation project(':business-core')
// implementation 'com.github.bytedance:tiktok-business-android-sdk:1.0.0-alpha.10'
// implementation 'com.github.bytedance:tiktok-business-android-sdk:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected void onCreate(Bundle savedInstanceState) {

TikTokBusinessSdk.TTConfig ttConfig =
new TikTokBusinessSdk.TTConfig(getApplication())
.disableLaunchLogging()
.setAppId(appId)
// you may switch between setting a wrong token or not setting token at call
.setAccessToken(correctToken)
Expand Down

0 comments on commit 44cd5d3

Please sign in to comment.