Skip to content

UniApp App Integration


Introduction

This document is the entry page for the UniApp RUM SDK. It provides the essential information for first-time integration, installation methods, reading path, detailed configuration entry points, advanced scenario entry points, and FAQ.

If you need the parameter table, API descriptions, manual collection examples, and runtime capabilities, go to the topic pages below.

Reading Path

Read the topics in the following order:

  1. To upgrade from 0.2.x, read the UniApp 0.3.0 Migration Guide first.
  2. For first-time integration, choose an integration method first; for manual integration, see Quick Start.
  3. Complete Installation according to your actual integration method.
  4. After completing SDK initialization, continue with SDK Initialization and RUM Configuration.
  5. If you need log collection and distributed tracing, continue with Log Configuration and Trace Configuration.
  6. If you need tags, masking, or WebView collection, continue with the corresponding advanced topics.

Prerequisites

When integrating GC-UniPlugin 0.3.0 or later into a uni-app application, use HBuilderX 4.25.0 or later.

Note: If you have enabled the RUM Headless service, the prerequisites are already configured and you can start the app integration directly.

For local environment deployment, complete the following data receiver configuration. When using the public DataWay, you do not need to deploy DataKit yourself.

App Integration

  1. Go to RUM > Create Application;
  2. Create Android, iOS, and HarmonyOS applications separately based on the actual release platforms;
  3. Save the RUM App ID for each platform, and pass them as androidAppId, iOSAppId, and harmonyAppId respectively when initializing RUM;
  4. In "Choose an Integration Method", select Agent automatic integration or manual integration, then complete "Integration Configuration".

Choose an Integration Method

On the application configuration page, select "Agent Automatic Integration" or "Manual Integration", then complete "Integration Configuration". For existing applications, you can go directly to the configuration page to continue the integration.

Integration Method Use Cases Entry Point
Agent Automatic Integration Want a coding assistant to analyze the project and complete SDK integration Copy the prompt generated by the console and hand it to the coding assistant in the application code repository
Manual Integration Want to install the SDK and configure parameters yourself Confirm the data reporting method, then complete the configuration according to this document and Quick Start

Agent Automatic Integration

  1. On the configuration page of the current application, select "Agent Automatic Integration".
  2. Copy the full prompt from "Integration Configuration" and make sure it uses the integration information of the current application.
  3. Open the coding assistant in the application code repository, paste the prompt, and let the Agent analyze the project and complete the SDK configuration according to the integration guide.
  4. Build and run the application, trigger operations such as page views, and confirm in the Explorer of the current application that data is received.

When using a cross-platform framework, tell the Agent about the framework and target platforms, and provide the corresponding application information for each platform.

If the page indicates that your personal API Key or temporary authorization code is not ready, complete it as prompted and copy the prompt again.

Manual Integration

On the application configuration page, select "Manual Integration" and confirm the data reporting method:

  • Public DataWay: prepare datawayUrl and clientToken;
  • Local environment deployment: prepare datakitUrl.

Complete SDK installation, initialization, and verification as described in Installation and Quick Start below. If you already have an SDK configuration, check and reuse it first to avoid duplicate initialization.

Installation

Local Use

Source code: TrueWatchTech/datakit-uniapp-native-plugin

The structure of the downloaded SDK package is as follows:

|-- datakit-uniapp-native-plugin
  |-- Hbuilder_Example                         // HBuilderX sample project and ready-to-use uni_modules
    |-- uni_modules
      |-- GC-JSPlugin                         // JS automatic collection
        |-- js_sdk
          |-- Action/GCActionTracking.js      // Action automatic collection on the HarmonyOS side
          |-- Error/GCErrorTracking.js        // Error automatic collection
          |-- Request/GCResourceTracking.js   // uni.request Resource and Trace automatic collection
          |-- View/GCViewTracking.js          // Recommended global View automatic collector
        |-- index.js
        |-- package.json
      |-- GC-UniPlugin                        // Main Android, iOS, and HarmonyOS UTS plugin
        |-- utssdk
        |-- setup.js                          // Bridges the JS collectors and the UTS SDK
        |-- package.json
      |-- GC-UniSessionReplay                 // Optional Android and iOS Session Replay plugin
  |-- native-projects                         // Native build, packaging, and integration verification projects
  |-- dist                                    // Public release artifacts
    |-- native-sdk-hybrid                     // Offline packaging artifacts for uni-app applications
    |-- unimp-host-extension                  // GC-UniPlugin native dependency libraries used by the host app

Copy the following directories from Hbuilder_Example/uni_modules of the corresponding version in the SDK source repository to the uni_modules directory of your business project:

uni_modules/
├── GC-JSPlugin
└── GC-UniPlugin

If you need Session Replay, install the optional GC-UniSessionReplay as well. Session Replay currently supports Android and iOS only.

All installed modules must use the same release version. When upgrading, replace them together and do not mix JS, UTS, or Session Replay modules of different versions.

GC-UniPlugin is added starting from 0.3.0 and replaces the legacy nativeplugins/GCUniPlugin native plugin. It is a UTS module, so you do not need to copy it to nativeplugins or register the old local native plugin in manifest.json. GC-JSPlugin continues to provide the common JS APIs and collectors.

Load the UTS Bridge

Load setup.js once, first thing in the application entry point:

import '@/uni_modules/GC-UniPlugin/setup.js';

Import SDK APIs from GC-UniPlugin to get the type checking and parameter hints that HBuilderX provides based on UTS interfaces; import JS collectors from GC-JSPlugin:

import {
    mobileAgent,
    rum,
    logger,
    tracer
} from '@/uni_modules/GC-UniPlugin';

import {
    gcErrorTracking,
    gcViewTracking,
    gcResourceTracking,
    gcActionTracking
} from '@/uni_modules/GC-JSPlugin';

Marketplace Plugin Method

The marketplace plugin method is not currently available; use Local Use to complete the integration.

uni Mini Program SDK Installation

In this section, "uni mini program" specifically refers to a project developed with uni-app, built into a wgt resource package with HBuilderX, and running inside a host app. It does not refer to platform mini programs such as WeChat or Alipay.

Installing the uni mini program SDK consists of two parts: the uni mini program project and the host app:

Integration Location Installation Content Main Responsibilities
uni mini program project GC-JSPlugin Provide common JS APIs and collect View, Error, Resource, and Action
Host app Native SDK + uni mini program host extension Initialize the SDK, collect native data, and provide native Modules to the uni mini program

Development Debugging and wgt Release

During development debugging and when building the wgt resource package, the uni mini program project only needs GC-JSPlugin:

uni_modules/
└── GC-JSPlugin

GC-JSPlugin is delivered as a complete directory; the source directory in the SDK repository is Hbuilder_Example/uni_modules/GC-JSPlugin/. Copy this directory to uni_modules/ of the uni mini program project, so the final path is uni_modules/GC-JSPlugin/.

Do not install or import GC-UniPlugin in the uni mini program project, and do not load GC-UniPlugin/setup.js; otherwise the UTS code becomes part of the project's compilation dependencies.

Import SDK objects and JS collectors uniformly from GC-JSPlugin:

import {
    mobileAgent,
    rum,
    logger,
    tracer,
    gcErrorTracking,
    gcViewTracking,
    gcResourceTracking,
    gcActionTracking
} from '@/uni_modules/GC-JSPlugin';

Starting from 0.3.0, newly integrated projects have the host app complete SDK and RUM, Log, and Trace initialization. The uni mini program does not call mobileAgent.sdkConfig(), rum.setConfig(), logger.setConfig(), or tracer.setConfig() again, and only calls the following runtime APIs:

  • bindRUMUserData(), unbindRUMUserData();
  • appendGlobalContext(), appendRUMGlobalContext(), appendLogGlobalContext(), appendBridgeContext();
  • The manual data collection and header retrieval APIs provided by rum, logger, and tracer.

When upgrading an existing 0.2.x uni mini program project, you can keep the original initialization approach and continue to obtain Modules through uni.requireNativePlugin(), without immediately rewriting your business code:

const mobileAgent = uni.requireNativePlugin('GCUniPlugin-MobileAgent');
const rum = uni.requireNativePlugin('GCUniPlugin-RUM');
const logger = uni.requireNativePlugin('GCUniPlugin-Logger');
const tracer = uni.requireNativePlugin('GCUniPlugin-Tracer');

For new projects, importing uniformly from GC-JSPlugin is recommended. For details on migrating older versions, see Upgrade from 0.2.x.

Host App Integration

The host app must integrate the Native SDK for the corresponding platform and the 0.3.0 or later version of the uni mini program host extension:

Host Platform Host Extension Form Release Artifact Release Path in SDK Repository
Android Android UniModule gc-uniplugin-<version>.aar dist/unimp-host-extension/android/
iOS DCUniModule GC-UniPlugin-App.xcframework dist/unimp-host-extension/ios/
HarmonyOS ETS native extension GCUniPlugin.har dist/unimp-host-extension/harmony/

Release artifact names are subject to the actual file names in the release package of the version you use.

The host extension must register the following module IDs with the uni mini program runtime, so that the uni mini program can call native capabilities through the common JS APIs or uni.requireNativePlugin():

GCUniPlugin-MobileAgent
GCUniPlugin-RUM
GCUniPlugin-Logger
GCUniPlugin-Tracer

The host app must complete the following dependency integration and Module registration operations for each platform:

iOS
  1. Add the dependency library.

    The host app must integrate the Native SDK according to the iOS Native SDK installation instructions. Using Swift Package Manager is recommended; add the TrueWatchSDK Package Product to the host app Target. The Native SDK version must match the UniApp SDK release version; see the Change Log of the corresponding version for the specific version.

    Add GC-UniPlugin-App.xcframework from the release package to the host app Target: in Xcode, go to TARGETS -> Build Phases -> Link Binary With Libraries, click "+", choose Add Other -> Add Files..., and select the file. This XCFramework is a static library; keep it as Do Not Embed in TARGETS -> General -> Frameworks, Libraries, and Embedded Content.

  2. Register the GCUniPlugin Modules when the application starts:

    - (BOOL)application:(UIApplication *)application
            didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [WXSDKEngine registerModule:@"GCUniPlugin-MobileAgent"
                           withClass:NSClassFromString(@"FTMobileUniModule")];
        [WXSDKEngine registerModule:@"GCUniPlugin-RUM"
                           withClass:NSClassFromString(@"FTRUMModule")];
        [WXSDKEngine registerModule:@"GCUniPlugin-Logger"
                           withClass:NSClassFromString(@"FTLogModule")];
        [WXSDKEngine registerModule:@"GCUniPlugin-Tracer"
                           withClass:NSClassFromString(@"FTTracerModule")];
        return YES;
    }
    
Android
  1. Add the dependency library:

    Copy gc-uniplugin-<version>.aar from the release package to the libs directory of the host project. Add the Maven repository according to the Android Native SDK Gradle configuration, and add the following dependencies in build.gradle:

    dependencies {
        implementation files('libs/gc-uniplugin-<version>.aar')
        implementation 'com.truewatch.ft.mobile.sdk.tracker.agent:ft-sdk:<version>'
        implementation 'com.truewatch.ft.mobile.sdk.tracker.agent:ft-native:<version>'
        implementation 'com.alibaba:fastjson:1.2.83'
        implementation 'com.google.code.gson:gson:2.8.5'
    }
    

    The Native SDK version must match the UniApp SDK release version; see the Change Log of the corresponding version for the specific version.

  2. Register the GCUniPlugin Modules in Application.onCreate():

    public class App extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            try {
                WXSDKEngine.registerModule("GCUniPlugin-MobileAgent", FTSDKUniModule.class);
                WXSDKEngine.registerModule("GCUniPlugin-RUM", FTRUMModule.class);
                WXSDKEngine.registerModule("GCUniPlugin-Logger", FTLogModule.class);
                WXSDKEngine.registerModule("GCUniPlugin-Tracer", FTTracerModule.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
HarmonyOS

Put GCUniPlugin.har into the libs directory of the host project, and add the UniMP runtime and local extension dependencies in oh-package.json5:

{
    "dependencies": {
        "@dcloudio/uni-app-runtime": "5.2.32026080401",
        "@truewatchtech/gc-uniplugin": "file:./libs/GCUniPlugin.har"
    }
}

After adding the dependencies, run ohpm install in the project directory.

The host extension provides initializeNativeSDK() and registerNativeModules(). The host must first initialize the SDK in UIAbility.onCreate(); then, after init() finishes initializing the uni mini program runtime and before the uni mini program is opened for the first time, register the Modules:

import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { init } from '@dcloudio/uni-app-runtime';
import {
    initializeNativeSDK,
    registerNativeModules
} from '@truewatchtech/gc-uniplugin';
import { SDK_STARTUP_CONFIG } from '../config/SDKStartupConfig';

export default class EntryAbility extends UIAbility {
    onCreate(): void {
        initializeNativeSDK(this.context, SDK_STARTUP_CONFIG);
    }

    onWindowStageCreate(windowStage: window.WindowStage): void {
        init(this, windowStage, { debug: true });
        registerNativeModules(this.context);

        // Open the uni mini program after this.
    }
}

SDK_STARTUP_CONFIG is provided by the host app and contains the initialization configurations for Mobile, RUM, Log, and Trace. The uni mini program does not call the corresponding initialization APIs again.

Initialization and Debugging

The host app must complete Native SDK initialization and Module registration before opening the uni mini program, and ensure that the final application contains only one Native SDK and one set of SDK singletons. The uni mini program does not need to initialize the SDK again.

For Native SDK initialization on Android and iOS hosts, see Android SDK Initialization and iOS SDK Initialization respectively; HarmonyOS hosts use initializeNativeSDK() described above to complete initialization.

When a uni mini program project runs in a regular debugging base that does not integrate the host extension, GC-JSPlugin degrades missing native calls to no-ops, so page loading is not blocked when uni.requireNativePlugin() returns null. This mode is only for debugging JS pages and collection logic and cannot verify native data reporting; full joint debugging must be performed in a real host app.

Upgrade from 0.2.x

0.3.0 uses different migration approaches for regular uni-app projects and uni mini programs: regular uni-app projects use GC-UniPlugin to replace the legacy native plugin, while uni mini program projects continue to integrate only GC-JSPlugin, with the host app upgrading the Native SDK and host extension.

For the complete dependency structure, API imports, initialization locations, compatibility approaches, and before/after code comparisons, see the UniApp 0.3.0 Migration Guide.

Detailed Configuration Entry Points

Configuration Description

  • Quick Start: the shortest path for first-time integration.
  • SDK Initialization: basic configuration, user binding, disabling the SDK, clearing the cache, and active synchronization.
  • RUM Configuration: RUM initialization configuration and Action/View/Error/Resource collection capabilities.
  • Log Configuration: Log initialization configuration and log printing.
  • Trace Configuration: Trace initialization configuration and distributed tracing.

Advanced Scenarios

FAQ

Difference Between Android Cloud Packaging and Offline Packaging

Android cloud packaging and offline packaging use two different integration logics. Offline packaging follows the same approach as Android Native SDK integration and can apply the Android Gradle Plugin in the host project; cloud packaging cannot apply this plugin, so some capabilities are implemented inside the UniApp plugin.

Therefore, offline packaging usually provides more complete native automatic collection capabilities. sdkConfig distinguishes the two packaging methods through offlinePackage:

  • Android cloud packaging: keep the default value false;
  • Android offline packaging: set it to true;
  • Existing 0.2.x uni mini program projects that still call sdkConfig() on the JS side: set it to true;
  • New uni mini program integrations of 0.3.0 or later have the host app initialize the SDK, so this parameter is not involved, and the uni mini program does not call sdkConfig() again.

If you need to collect App startup, Native pages, clicks, network requests, and WebView data in offline packaging or in a uni mini program host app, you also need to configure the Android Gradle Plugin in the host project.

Others