- Intro
- Set up your app for Firebase
- Add the Firebase Performance Monitoring SDK to your app
- Add custom tracing
- Add HTTP Request Tracing
- API
This plugin allows you to use the Firebase Performance Monitoring API in your NativeScript app.
You need to set up your app for Firebase before you can enable Firebase Performance Monitoring. To set up and initialize Firebase for your NativeScript app, follow the instructions on the documentation of the @nativescript/firebase-core plugin.
To add the Firebase Performance Monitoring to your app, follow these steps:
- Install the
@nativescript/firebase-performance
plugin by running the following command in the root directory of your project.
npm install @nativescript/firebase-performance
- Add the SDK by importing the
@nativescript/firebase-performance
module. You should import this module once in your app, ideally in the main file (e.g.app.ts
ormain.ts
).
import '@nativescript/firebase-performance';
You can use custom traces to measure the amount of time it takes for your app to complete a specific task.
You start your custom trace by calling the startTrace method with a string to identify the trace. You can then add custom attributes and metrics to the trace. Finally, you stop the trace by calling the stop method.
import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-performance';
async function customTrace() {
// Define & start a trace
const trace = await firebase().perf().startTrace('custom_trace');
// Define trace meta details
trace.putAttribute('user', 'abcd');
trace.putMetric('credits', 30);
// Stop the trace
await trace.stop();
}
To create a trace for a network request, follow these steps:
- Create an instance of the HttpMetric class with the URL to which the request is being made and the HTTP method used.
const metric = await firebase().perf().newHttpMetric(url, 'GET');
- Add custom attributes to the metric.
metric.putAttribute('user', 'abcd');
- Start the metric.
await metric.start();
- Perform the HTTP request and provide response information.
const response = await fetch(url);
metric.setHttpResponseCode(response.statusCode);
metric.setResponseContentType(response.headers['Content-Type']);
metric.setResponsePayloadSize(response.headers['Content-Length']);
- Stop the metric.
await metric.stop();
The above steps combined would look as follows:
import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-performance';
async function getRequest(url) {
// Define the network metric
const metric = await firebase().perf().newHttpMetric(url, 'GET');
// Define meta details
metric.putAttribute('user', 'abcd');
// Start the metric
await metric.start();
// Perform a HTTP request and provide response information
const response = await fetch(url);
metric.setHttpResponseCode(response.statusCode);
metric.setResponseContentType(response.headers['Content-Type']);
metric.setResponsePayloadSize(response.headers['Content-Length']);
// Stop the metric
await metric.stop();
return response.toJSON();
}
// Call API
getRequest('https://api.com').then((json) => {
console.log(json);
});
import { firebase } from '@nativescript/firebase-core';
performanceAndroid: com.google.firebase.perf.FirebasePerformance = firebase().perf().android;
A read-only
property that returns the Performance Monitoring instance for Android.
import { firebase } from '@nativescript/firebase-core';
performanceIOS: FIRPerformance = firebase().perf().ios;
A read-only
property that returns the Performance Monitoring instance for iOS.
import { firebase } from '@nativescript/firebase-core';
performanceApp: FirebaseApp = firebase().perf().app;
A read-only
property that returns the FirebaseApp instance for the current app.
import { firebase } from '@nativescript/firebase-core';
isPerformanceCollectionEnabled: boolean = firebase().perf().isPerformanceCollectionEnabled;
// or
firebase().perf().isPerformanceCollectionEnabled = true;
A read-write
property that returns true
or false
depending on whether performance monitoring is enabled or not. You can also set this property to enable or disable performance monitoring.
import { firebase } from '@nativescript/firebase-core';
httpMetric: HttpMetric = firebase().perf().newHttpMetric(url, httpMethod);
Creates a new HttpMetric instance, used to represent an HTTP request tracing, with the given URL and httpMethod.
Parameter | Type | Description |
---|---|---|
url |
string |
|
httpMethod |
HttpMethod |
import { firebase } from '@nativescript/firebase-core';
trace: Trace = firebase().perf().newTrace(identifier);
Creates a new Trace instance with the given identifier.
Parameter | Type | Description |
---|---|---|
identifier |
string |
import { firebase } from '@nativescript/firebase-core';
trace: Trace = firebase().perf().startTrace(identifier);
Creates and starts a new Trace instance with the given identifier.
Parameter | Type | Description |
---|---|---|
identifier |
string |
import { firebase } from '@nativescript/firebase-core';
httpMetricAndroid: com.google.firebase.perf.metrics.HttpMetric = httpMetric.android;
A read-only
property that returns the HttpMetrics instance for Android.
import { firebase } from '@nativescript/firebase-core';
httpMetricIOS: FIRHTTPMetric = httpMetric.ios;
A read-only
property that returns the HttpMetric instance for iOS.
someAttribute: string = httpMetric.getAttribute(attribute);
Returns the value for the specified attribute.
Parameter | Type | Description |
---|---|---|
attribute |
string |
The name of the attribute to retrieve the value for. |
attributes: { [key: string]: string } = httpMetric.getAttributes();
httpMetric.putAttribute(attribute, value);
For the description of this method, see putAttribute() on the Firebase documentation.
Parameter | Type | Description |
---|---|---|
attribute |
string |
The name of the attribute to set. |
value |
string |
The value of the attribute to set. |
httpMetric.removeAttribute(attribute);
Remove the specified attribute from the tracing metric.
httpMetric.setHttpResponseCode(code);
Parameter | Type | Description |
---|---|---|
code |
number |
The HTTP response code. |
httpMetric.setRequestPayloadSize(bytes);
Parameter | Type | Description |
---|---|---|
bytes |
number |
The size of the request payload. |
httpMetric.setResponseContentType(contentType);
Parameter | Type | Description |
---|---|---|
contentType |
string |
The content type of the HTTP response. Examples: text/html , application/json |
httpMetric.start();
Marks the start of an HTTP request/response tracing.
httpMetric.stop();
Marks the end time of the response and queues the network request metric on the device for transmission.
traceAndroid: com.google.firebase.perf.metrics.Trace = trace.android;
A read-only
property that returns the Trace instance for Android.
traceIOS: FIRTrace = trace.ios;
A read-only
property that returns the Trace instance for iOS.
someAttribute: string = trace.getAttribute(attribute);
Returns the value for the specified attribute of the trace.
Parameter | Type | Description |
---|---|---|
attribute |
string |
The name of the attribute to retrieve the value for. |
someMetric: number = trace.getMetric(metricName);
Gets the value of the metric with the given name in the current trace. For more, information see getMetric() on the Firebase documentation.
Parameter | Type | Description |
---|---|---|
metricName |
string |
The name of the metric to retrieve the value for. |
metrics: { [key: string]: number } = trace.getMetrics();
trace.incrementMetric(metricName, incrementBy);
For the description of this method, see incrementMetric() on the Firebase documentation.
Parameter | Type | Description |
---|---|---|
metricName |
string |
The name of the trace metric to increment. |
incrementBy |
number |
The value to increment the metric by. |
trace.putAttribute(attribute, value);
For the description of this method, see putAttribute() on the Firebase documentation.
Parameter | Type | Description |
---|---|---|
attribute |
string |
|
value |
string |
trace.putMetric(metricName, value);
For the description of this method, see putMetric() on the Firebase documentation.
Parameter | Type | Description |
---|---|---|
metricName |
string |
The name of the trace metric to set. |
value |
number |
The value to set the metric to. |
trace.removeMetric(metricName);
Parameter | Type | Description |
---|---|---|
metricName |
string |
The name of the trace metric to remove from the Trace instance. |
trace.start();
Marks the start time of the trace.
trace.stop();
Marks the end time of the trace and queues the trace on the device for transmission.
Apache License Version 2.0