Skip to content

Custom Data Collection Rules

Action

This document describes custom rules for automatic Action collection in the React Native SDK.

After enableAutoTrackUserAction is enabled, the SDK automatically collects click actions on React Native components. If the Babel plugin is not installed, the SDK continues to use the original runtime auto-collection method. After the Babel plugin is installed, the SDK adds tracking instrumentation to supported interaction handlers at compile time and generates more stable and readable action_name values.

Automatically Generate Action Names with the Babel Plugin

Supported since SDK 0.4.3.

The Babel plugin version must match the react-native-mobile package version.

Install the Plugin

Install the Babel plugin as a development dependency:

npm install --save-dev @truewatchtech/react-native-mobile-babel-plugin

Enable the plugin in the project's babel.config.js:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
'@truewatchtech/react-native-mobile-babel-plugin',
],
};

Continue to use enableAutoTrackUserAction as the runtime switch; no additional RUM configuration is required:

const rumConfig: FTRUMConfig = {
  androidAppId: Config.ANDROID_APP_ID,
  iOSAppId: Config.IOS_APP_ID,
  enableAutoTrackUserAction: true,
};

await FTReactNativeRUM.setConfig(rumConfig);

After the Babel plugin is enabled, the SDK uses only the Babel auto-collection path to avoid duplicate reporting with the original runtime path. When the plugin is not installed, the original auto-collection behavior remains unchanged.

Action Name Generation Rules

The plugin generates action_name according to the following priority:

  1. The ft-action-name static property;
  2. The custom static property configured by actionNameAttribute;
  3. The accessibilityLabel static property;
  4. trackingLabel, title, label, text, custom content properties, or component children;
  5. The JSX component name.

By default, the component name prefix is retained. For example, the following component produces an action_name of Button ("Submit"):

<Button
  ft-action-name="Submit"
  title="Pay now"
  onPress={submit}
/>

If ft-action-name is not set, the example above tries accessibilityLabel, title, and the component name in order. Explicit Action name properties support only static strings, while content properties can use expressions.

Configure Custom Components

By default, the plugin recognizes Button, Pressable, TouchableOpacity, TouchableHighlight, TouchableWithoutFeedback, TouchableNativeFeedback, Switch, and TextInput imported by name from react-native, and supports import aliases. Custom components can be configured through components.tracked:

Buttons and the Touchable series collect onPress; Pressable also collects onLongPress; Switch collects onValueChange; and TextInput collects onFocus.

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
'@truewatchtech/react-native-mobile-babel-plugin',
{
        actionNameAttribute: 'analytics-name',
        components: {
          useContent: true,
          useNamePrefix: true,
          tracked: [
            {
              name: 'CustomButton',
              contentProp: 'caption',
              handlers: [
                {
                  event: 'onPress',
                  actionType: 'submit',
                },
              ],
            },
          ],
        },
      },
    ],
  ],
};
Configuration Item Type Default Description
actionNameAttribute string - Sets an additional static Action name property with lower priority than ft-action-name
components.useContent boolean true Whether to extract the name from content properties and children
components.useNamePrefix boolean true Whether to generate a component name prefix in the format Button ("Submit"); when disabled, only Submit is generated
components.tracked Array [] Configures custom components to track
tracked[].name string - JSX component name; member components in the form Menu.Item can also be configured
tracked[].contentProp string - Specifies an additional content property
tracked[].handlers Array - Sets interaction properties to track
tracked[].handlers[].event string - The property name of the component's interaction handler, such as onPress
tracked[].handlers[].actionType string click Sets the value reported to RUM action_type; if omitted, an empty string, or whitespace only, click is used
tracked[].handlers[].mode string default When set to delayed, treats the handler as a factory function that returns the actual interaction function

tracked[] can also set useContent and useNamePrefix individually to override the defaults at the components level.

For composite components with multiple candidate names, the SDK uses the first numeric argument of the interaction handler to select the name; if the argument is not a valid index, the first candidate name is used.

Limitations

  • The plugin reports Actions through startAction(actionName, actionType, property); actionType defaults to click, or you can specify a stable, low-cardinality custom value in the handler configuration of a custom component;
  • Namespace imports in the form import * as ReactNative from 'react-native' are not automatically recognized;
  • Web builds and files under node_modules are not transformed;
  • When TextInput lacks onFocus, the plugin injects a handler only in safe scenarios where the original component name is used and no spread attributes exist;
  • ft-enable-track and ft-extra-property continue to take effect; when an event does not contain a React Native Fiber target, collection is allowed by default without adding extended properties.

Customize actionName for a Component's Click Event

After installing the Babel plugin, it is recommended to set it through ft-action-name; if the plugin is not installed, you can continue to set it through accessibilityLabel.

<Button
  title="Custom Action Name"
  ft-action-name="custom_action_name"
  accessibilityLabel="custom_action_name"
  onPress={() => {
    console.log('btn click');
  }}
/>

Do Not Collect Click Events for a Component

You can configure this by adding the ft-enable-track custom parameter and setting its value to false.

<Button
  title="Action Click"
  ft-enable-track="false"
  onPress={() => {
    console.log('btn click');
  }}
/>

Add Extra Properties to a Component's Click Event

You can configure this by adding the ft-extra-property custom parameter; the value must be a JSON string.

<Button
  title="Action 添加额外属性"
  ft-extra-property='{"e_name": "John Doe", "e_age": 30, "e_city": "New York"}'
  onPress={() => {
    console.log('btn click');
  }}
/>