newrelic_mobile 0.0.1-dev.0 copy "newrelic_mobile: ^0.0.1-dev.0" to clipboard
newrelic_mobile: ^0.0.1-dev.0 copied to clipboard

outdated

Flutter plugin for NewRelic Mobile.

New Relic Flutter Agent #

This agent allows you to instrument Flutter apps with help of native New Relic Android and iOS agents. The New Relic SDKs collect crashes, network traffic, and other information for hybrid apps using native components.

NOTE: This agent SDK is not yet officially supported. If you’re interested in participating in our Limited Preview, contact Support or your account representative.

Features #

  • Capture Dart errors
  • Network Request tracking
  • Distributed Tracing
  • Future errors tracking
  • Capture interactions and the sequence in which they were created
  • Pass user information to New Relic to track user sessions
  • Screen tracking via NavigationObserver
  • Automatic detection and reporting of the app-is-not-responding cases (ANR).

Current Support: #

  • Android API 24+
  • iOS 10
  • Depends on New Relic iOS/XCFramework and Android agents

Requirements #

Installation #

Install NewRelic plugin into your dart project by adding it to dependecies in your pubspec.yaml


dependencies:
  newrelic_mobile: 0.0.1
  

Flutter Setup #

Now open your main.dart and add the following code to launch NewRelic (don't forget to put proper application tokens):


import 'package:newrelic_mobile/newrelic_mobile.dart';


  var appToken = "";

  if (Platform.isAndroid) {
    appToken = "<android app token>";
  } else if (Platform.isIOS) {
    appToken = "<ios app token>";
  }

 runZonedGuarded(() {
    WidgetsFlutterBinding.ensureInitialized();
    FlutterError.onError = NewrelicMobile.onError;
    NewrelicMobile.startAgent(appToken);
    runApp(MyApp());
  },(Object error,StackTrace stackTrace){
    NewrelicMobile.recordError(error, stackTrace);
  });

  class MyApp extends StatelessWidget {
  ....

AppToken is platform-specific. You need to generate the seprate token for Android and iOS apps.

Screen Tracking Events #

In order to track navigation events you have to add the NewRelicNavigationObserver to your MaterialApp, WidgetsApp or CupertinoApp.

You should provide a name to route settings: RouteSettings(name: 'Your Route Name'). The root route name / will be replaced by root "/" for clarity's sake.


import 'package:newrelic_mobile/newrelic_navigation_observer.dart';

//....

MaterialApp(
  navigatorObservers: [
    NewRelicNavigationObserver(),
  ],
  // other parameters
)


Android Setup #

  1. Add the following changes to android/build.gradle:
  buildscript {
    ...
    repositories {
      ...
      mavenCentral()
    }
    dependencies {
      ...
      classpath "com.newrelic.agent.android:agent-gradle-plugin:6.7.0"
    }
  }
  1. Apply the newrelic plugin to the top of the android/app/build.gradle file::
  apply plugin: "com.android.application"
  apply plugin: 'newrelic' // <-- add this

  1. Make sure your app requests INTERNET and ACCESS_NETWORK_STATE permissions by adding these lines to your AndroidManifest.xml
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Usage #

See the examples below, and for more detail, see New Relic IOS SDK doc or Android SDK.

startInteraction(interactionName: string): Promise<InteractionId>; #

Track a method as an interaction.

InteractionId is string.

endInteraction(id: InteractionId): void; #

End an interaction (Required). This uses the string ID for the interaction you want to end. This string is returned when you use startInteraction().

          var id = await NewrelicMobile.startInteraction("Getting Data from Service");
              try {
                var dio = Dio();
                var response = await dio.get(
                    'https://reqres.in/api/users?delay=15');
                   print(response);
                  NewrelicMobile.endInteraction(id);
                  Timeline.finishSync();
              } catch (e) {
                print(e);
              }

setAttribute(name: string, value: boolean | number | string): void; #

Creates a session-level attribute shared by multiple mobile event types. Overwrites its previous value and type each time it is called.

   NewrelicMobile.setAttribute('RNCustomAttrNumber', 37);

setUserId(userId: string): void; #

Set a custom user identifier value to associate user sessions with analytics events and attributes.

   NewrelicMobile.setUserId("RN12934");

recordBreadcrumb(name: string, attributes?: {[key: string]: boolean | number | string}): void; #

Track app activity/screen that may be helpful for troubleshooting crashes.

  NewrelicMobile.recordBreadcrumb("Button Got Pressed on Screen 3"),

recordCustomEvent(eventType: string, eventName?: string, attributes?: {[key: string]: boolean | number | string}): void; #

Creates and records a custom event for use in New Relic Insights.

  NewrelicMobile.recordCustomEvent("Major",eventName: "User Purchase",eventAttributes: {"item1":"Clothes","price":34.00}),
          child: const Text('Record Custom Event'),

Manual Error reporting #

You can register non fatal exceptions using the following method:

try {
  some_code_that_throws_error();
} catch (ex) {
  NewrelicMobile.recordError(error, StackTrace.current);
}