Skip to content

Manual Integration

How to Integrate the SDK Without ft-plugin

Guance uses Android Gradle Plugin Transformation for code injection to enable automatic data collection. However, due to compatibility issues, there may be cases where ft-plugin or ft-plugin-legacy cannot be used. The affected scope includes:

  • Action and Resource in RUM
  • Automatic capture of android.util.Log
  • Automatic capture of Java and Kotlin println console logs
  • Automatic symbol file upload

Currently, for such cases, you can use the manual integration approach.

Application Startup Event

For the source code example, see DemoForManualSet.kt.

// Application
@Override
public void onCreate() {
    super.onCreate();
    // Must be called before SDK initialization
    FTAutoTrack.startApp(null);
    // Configure the SDK
    setSDK(this);
}
// Application
override fun onCreate() {
    super.onCreate()
    // Must be called before SDK initialization
    FTAutoTrack.startApp(null)
    // Configure the SDK
    setSDK(this)
}

Manually Adding User Actions

Events such as button clicks must be added manually at the point where they are triggered. Here, a Button onClick event is used as an example. For the source code example, see ManualActivity.kt:

view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        FTRUMGlobalManager.get().startAction("[action button]", "click");
    }
});
view.setOnClickListener {
    FTRUMGlobalManager.get().startAction("[action button]", "click")
}

Manually Integrating OkHttp Resource / Trace

OkHttp can be integrated with Resource and Trace through addInterceptor and eventListener. See the example below. For the source code example, see ManualActivity.kt:

OkHttpClient.Builder builder = new OkHttpClient.Builder()
    .addInterceptor(new FTTraceInterceptor())
    .addInterceptor(new FTResourceInterceptor())
    .eventListenerFactory(new FTResourceEventListener.FTFactory());
//.eventListenerFactory(new FTResourceEventListener.FTFactory(true));
OkHttpClient client = builder.build();
val builder = OkHttpClient.Builder()
    .addInterceptor(FTTraceInterceptor())
    .addInterceptor(FTResourceInterceptor())
    .eventListenerFactory(FTResourceEventListener.FTFactory())
    //.eventListenerFactory(new FTResourceEventListener.FTFactory(true))
val client = builder.build()

Other network frameworks require you to implement FTRUMGlobalManager.startResource, stopResource, addResource, and FTTraceManager.getTraceHeader yourself. For the specific implementation, see the source code example ManualActivity.kt.

Manually Integrating OkHttp Visitor ID

When using ft-sdk >= 1.7.6-alpha04, first complete initialization as described in Visitor ID, then install the interceptor for the business OkHttpClient via FTVisitorIdInterceptor.install(builder). If you only need to inject the Visitor ID Header, you do not need to install Resource or Trace interceptors:

OkHttpClient.Builder builder = new OkHttpClient.Builder();
FTVisitorIdInterceptor.install(builder);
OkHttpClient client = builder.build();
val builder = OkHttpClient.Builder()
FTVisitorIdInterceptor.install(builder)
val client = builder.build()

If you also want to collect Resource / Trace at the same time, call install(builder) after adding the other interceptors in the previous example and before builder.build(). This method places the Visitor ID application interceptor before FTResourceInterceptor and appends the redirect protection interceptor to the end of the current network interceptor list; call it after you have finished configuring the other interceptors.

Use install(builder) instead of only adding addInterceptor(new FTVisitorIdInterceptor()); otherwise redirect protection will be missing. Calling install repeatedly on the same Builder will not install the Visitor ID interceptor multiple times, nor will it automatically enable Resource or Trace collection.

Manually Integrating OkHttp WebSocket Resource

When using ft-sdk >= 1.7.5, if you are not using the ft-plugin that supports automatic WebSocket instrumentation, you can explicitly collect OkHttp WebSocket handshake Resources via FTWebSocket. First enable FTRUMConfig.setEnableTraceUserResource(true); existing Resource URL filtering and RUM sampling rules still apply. Collection only covers successful handshakes, server rejections, or failures; it does not measure connection duration or collect message content.

For a small number of call sites, you can use FTWebSocket.newWebSocket() directly:

WebSocket socket = FTWebSocket.newWebSocket(okHttpClient, request, listener);
val socket = FTWebSocket.newWebSocket(okHttpClient, request, listener)

If your project provides a WebSocket factory uniformly through dependency injection, you can wrap it once and then inject WebSocket.Factory:

WebSocket.Factory factory = FTWebSocket.wrap(okHttpClient);
WebSocket socket = factory.newWebSocket(request, listener);
val factory: WebSocket.Factory = FTWebSocket.wrap(okHttpClient)
val socket = factory.newWebSocket(request, listener)

wrap() supports repeated calls; even if ft-plugin >= 1.3.8 is also enabled, the handshake Resource for the same request is collected only once. If a third-party library holds the OkHttpClient internally and neither exposes the newWebSocket() call site nor allows passing in WebSocket.Factory, you still need to use ft-plugin for automatic instrumentation.

Manual WebView Configuration

FTAutoTrack.setUpWebView(webview);
// Configure before the loadUrl operation
FTAutoTrack.setUpWebView(webview)
// Configure before the loadUrl operation

Feedback

Is this page helpful?