Installing and Configuring the eG Mobile Agent for React Native Applications
React Native is an open-source framework developed by Meta Platforms that allows you to build genuinely native mobile apps for both Android and iOS using a single JavaScript or TypeScript codebase.
eG Enterprise provides monitoring support for mobile applications built using the React Native framework. The first step to monitoring such applications is to install and configure the eG mobile agent. This process changes according to the framework used for building React Native applications.
Typically, React Native applications support the following frameworks:
-
Expo: Expo is the officially recommended framework for building React Native applications because it provides tools, pre-configured libraries, and cloud services that eliminate the need to deal directly with complex native iOS and Android build environments.
-
Bare React Native: A bare React Native project (often managed via the React Native Community CLI) gives you complete, direct access to the underlying native Android (/android) and iOS (/ios) directories. This approach bypasses the abstractions of managed frameworks like Expo, allowing you to write custom native code (Java/Kotlin, Objective-C/Swift) and link complex third-party libraries manually.
The sections below discuss how to install and configure the eG mobile agent for applications built using each of the aforesaid development frameworks.
Installing and Configuring the eG Mobile Agent for Bare React Native Project
To achieve this, follow the steps below:
-
Login to the application host.
-
Then, open the browser, connect to the eG management console, and then login to the eG administrative interface.
-
Follow the Infrastructure -> Add /Modify menu sequence. Figure 1 will appear. In Figure 1, pick Mobile RUM as the Component type, and click the Add New Component button to add a new Mobile RUM component.
-
Figure 2 will then appear. Provide a Nick name for the React Native application you want to monitor.
Figure 2 : Adding the target Bare React Native application as a Mobile RUM component
-
Then, select the Mobile RUM collector to which the eG Mobile Agent should send the performance metrics it gathers from the target application.
-
Next, assign a Remote agent to the Mobile RUM component. This is the agent that will pull metrics from the Mobile RUM collector and reports the same to the eG manager.
-
To install the eG mobile agent on React Native, set Application type as Hybrid.
-
Finally, click the Add button.
-
Figure 3 will then appear. Proceed to download the eG mobile agent from the manager. For that, in the right panel of Figure 3, first click on the React button. Then, click on the Download button against Download eG Agent for ReactNative.
-
The eG Mobile agent will be downloaded as a zipped file. Extract the contents of the zip to any location on the application host.
-
The extracted contents will contain a react-native-eg-agent folder. Now, drag this folder into your Bare React Native project and detach any Expo related setting.
-
Then, proceed to install the eG mobile agent by running a command in the application's root directory. This command will vary based on whether the underlying application is an Android application or iOS.
For Android, run the following command:
npm install ./react-native-eg-agent
For iOS, run the following commands one after another:
sudo npm install ./react-native-eg-agent
sudo npm i react-native-device-info
-
Then, proceed to configure the eG mobile agent. For that, do the following:
-
Login to the eG administrative interface, and follow the Admin -> Infrastructure -> Add/Modify menu sequence.
-
Select Mobile RUM as the Component type in the page that appears. All managed mobile RUM components will be displayed.
- In the list, locate the component that you added for the target React Native application and click the Modify button corresponding to it.
-
In the page that appears next, click React in the right panel. Figure 4 will then appear. Click on the Copy to Clipboard button above the code snippet displayed against 'Include this line...'.
Figure 4 : Copying the code snippet to be inserted in the React Native application's code
- Next, edit the index.tsx file in the Bare React Native project's root folder.
- Append the code snippet you copied from the eG administrative interface to this file. This will inject the eG mobile agent into your application code.
-
Finally, save the file.
-
-
If the underlying native platform is Android, then you have to make the following configuration changes additionally:
-
First, you need to update the project-level build.gradle file (located in the root directory of the target Android project) with dependencies related to the eG mobile agent. For that, you need to edit the file and include the following lines in it:
Copybuildscript {
repositories {
flatDir {
dirs '../node_modules/react-native-eg-agent/android/eg-agent'
}
}
dependencies {
classpath fileTree(include: ['*.jar'], dir: '../node_modules/react-native-eg-agent/android/eg-agent')
}
} -
Then, save the file.
-
Similarly, you will have to update the application-level build.gradle file (located in the app/ directory) with the dependencies related to the eG mobile agent. For that, you need to edit that file and include the following lines in it:
Copyapply plugin: 'EgAgentGradlePlugin'
…….
repositories {
flatDir {
dirs '../../node_modules/react-native-eg-agent/android/eg-agent'
}
}
dependencies {
implementation fileTree(dir: '../../node_modules/react-native-eg-agent/android/eg-agent' , include: ['*.jar'])
implementation 'androidx.multidex:multidex:2.0.1'
} -
Next, save the file.
-
Now, proceed to edit the gradle.properties file (in the Android project's root directory), and add the eG mobile agent to the 'ignorelist' of the file, as shown below:
Copyandroid.jetifier.ignorelist = EgAgentGradlePlugin-1.0.0.jar hybrid = trueThis instructs the Android Gradle Plugin (AGP) to skip the automatic Jetifier migration process for eG mobile agent libraries. Its because this process may sometimes spew errors and cause application crashes.
-
Then, save the gradle.properties file.
-
Next, you need to allow the target application to perform network operations. For that, you need to set the following permissions for the application:
-
android.permission.INTERNET - This permission allows the application to perform network sockets.
-
android.permission.ACCESS_NETWORK_STATE - This permission allows the application to access information about networks.
To allow these permissions, do the following:
-
Edit the AndroidManifest.xml file in the root of the project source set.
-
Under the manifest tag of the file, specify the following entries:
Copy<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> -
Also, ensure that `android:usesCleartextTraffic="true"` is added in the <application> tag, as shown by the entry highlighted in Bold below:
<application android:name=".MyApplication" android:theme="@style/AppTheme" android:usesCleartextTraffic="true">
This allows the application to transmit over unencrypted HTTP connections
-
-
Next, proceed to edit the MainApplication.java file (in the [YourProjectFolder]/android/app/src/main/java/[your]/[package]/[name]/ directory), and include the following 'import' statements in the file. These statements instruct the compiler to load the eG mobile agent and all its dependent classes and packages.
Copyimport com.eg.agent.android.eGAndroidAgent;
import com.eg.agent.android.AppPlatform;
import com.facebook.react.modules.network.ReactCookieJarContainer
import com.facebook.react.modules.network.OkHttpClientProvider
import com.eg.agent.android.networkinterceptor.InterceptorProvider -
Next, inside the onCreate method, initialize the eG mobile agent by inserting the following code:
eGAndroidAgent.withCollectorHost("<Application_Token>","<Collector_Host>").start(getApplicationContext()); eGAndroidAgent.setAppliactionPlatform(AppPlatform.React);
// Create an instance of React Native's CookieJar val cookieJarContainer = ReactCookieJarContainer() // Set up the OkHttpClient using the library OkHttpClientProvider.setOkHttpClientFactory { InterceptorProvider.getClient(cookieJarContainer) }
After inserting, make sure you replace the variables highlighted in Bold in the code above with the value displayed against appToken and host (respectively) in the code snippet ofFigure 4.
-
Finally, save the file.
-
Clean and build the project in Android Studio. Run it on a simulator or mobile device:
npx react-native run-android
-
-
If the underlying native platform is iOS, then you have to make the following configuration changes additionally:
-
First, go to the iOS folder at the prompt and run the following commands one after another:
cd ios
pod install
-
Edit the EgAgent.m file (in the Pods/Development Pods/react-native-eg-agent/ios/ folder) and update the init method within with the following line:
[egAPM.sharedInstance startWithApptoken:(@"<Application Token>") collectorHost:(@"<Collector Host>")];
After inserting the above line, make sure you replace the variables highlighted in Bold in that line with the value displayed against appToken and host (respectively) in the code snippet of Figure 4.
-
Then, save the file.
-
Finally, clean and build the project in Xcode. Run it on a simulator or mobile device:
npx react-native run-ios
-
Installing and Configuring the eG Mobile Agent for Expo Project
To achieve this, follow the steps outlined below:
-
Login to the application host.
-
Then, open the browser, connect to the eG management console, and then login to the eG administrative interface.
-
Follow the Infrastructure -> Add /Modify menu sequence. Figure 5 will appear. In Figure 5, pick Mobile RUM as the Component type, and click the Add New Component button to add a new Mobile RUM component.
-
Figure 6 will then appear. Provide a Nick name for the React Native application you want to monitor.
Figure 6 : Adding the target React Native application using Expo for monitoring
-
Then, select the Mobile RUM collector to which the eG Mobile Agent should send the performance metrics it gathers from the target application.
-
Next, assign a Remote agent to the Mobile RUM component. This is the agent that will pull metrics from the Mobile RUM collector and reports the same to the eG manager.
-
To install the eG mobile agent on React Native, set Application type as Hybrid.
-
Finally, click the Add button.
-
Figure 7 will then appear. Proceed to download the eG mobile agent from the manager. For that, in the right panel of Figure 7, first click on the React button. Then, click on the Download button against Download eG Agent for ReactNative.
-
The eG Mobile agent will be downloaded as a zipped file. Extract the contents of the zip to any location on the application host.
-
The extracted contents will contain a react-native-eg-agent folder. Now, drag this folder into your Expo project.
-
Then, Install eG React Native Agent by running the following command in the application's root directory:
npm install ./react-native-eg-agent
-
After successful installation of the agent, proceed to initialize the agent. For that, edit the app.json file (in the root directory of your Expo project). In the file, include the following lines:
“expo”: {
………
“extras”: {
“egAgent”: {
“applicationToken”: <Application Token>,
“collectorHost”: <Collector Host>
}
},
………..
“plugins”: [
[“./node_modules/react-native-eg-agent/app.plugin.cjs”],
…….
]
}
-
When inserting the lines, make sure you replace the variables highlighted in Bold above with the value of the appToken and host arguments in the code snippet displayed against 'Include this line...' in Figure 7.
-
Next, proceed to edit the MainApplication.java file (in the [YourProjectFolder]/android/app/src/main/java/[your]/[package]/[name]/ directory), and include the following 'import' statements in the file. These statements instruct the compiler to load the eG mobile agent and all its dependent classes and packages.
Copyimport com.eg.agent.android.eGAndroidAgent;
import com.eg.agent.android.AppPlatform;
import com.facebook.react.modules.network.ReactCookieJarContainer
import com.facebook.react.modules.network.OkHttpClientProvider
import com.eg.agent.android.networkinterceptor.InterceptorProvider -
Then, issue the following command to rebuild your app with the plugin changes:
npx expo prebuild --clean
-
Next, in the index.tsx file under the project root folder, and add the following code in it to start the eG mobile agent:
import eGAgent from './react-native-eg-agent';
eGAgent.setUserName("Admin"); //set the username
eGAgent.configure({ appToken: <Application Token>, host:<Collector Host>, port:<Port> , }).useReactNative().connect()
-
When adding this code, make sure you replace the variables highlighted in Bold above with the value of the appToken, host, and port arguments in the code snippet displayed against 'Include this line...' in Figure 7.
-
Then, save the file.
-
Finally, sync project with gradle files. Build and run the app by executing the following command:
npx expo run:android