Plugins
Performance
@nativescript/firebase-performance 
Contents 
- Intro
- Set up your app for Firebase
- Add the Firebase Performance Monitoring SDK to your app
- Add custom tracing
- Add HTTP Request Tracing
- API
Intro 
This plugin allows you to use the Firebase Performance Monitoring API in your NativeScript app.
Set up your app for Firebase 
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.
Add the Firebase Performance Monitoring SDK to your app 
To add the Firebase Performance Monitoring to your app, follow these steps:
- Install the @nativescript/firebase-performanceplugin 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-performancemodule. You should import this module once in your app, ideally in the main file (e.g.app.tsormain.ts).
import '@nativescript/firebase-performance'Add custom tracing 
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()
}Add HTTP Request Tracing 
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)
})API 
Performance class 
android 
import { firebase } from '@nativescript/firebase-core'
performanceAndroid: com.google.firebase.perf.FirebasePerformance =
  firebase().perf().androidA read-only property that returns the Performance Monitoring instance for Android.
ios 
import { firebase } from '@nativescript/firebase-core'
performanceIOS: FIRPerformance = firebase().perf().iosA read-only property that returns the Performance Monitoring instance for iOS.
app 
import { firebase } from '@nativescript/firebase-core'
performanceApp: FirebaseApp = firebase().perf().appA read-only property that returns the FirebaseApp instance for the current app.
isPerformanceCollectionEnabled 
import { firebase } from '@nativescript/firebase-core'
isPerformanceCollectionEnabled: boolean =
  firebase().perf().isPerformanceCollectionEnabled
// or
firebase().perf().isPerformanceCollectionEnabled = trueA 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.
newHttpMetric() 
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 | 
newTrace() 
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 | 
startTrace() 
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 | 
HttpMetric class 
android 
import { firebase } from '@nativescript/firebase-core'
httpMetricAndroid: com.google.firebase.perf.metrics.HttpMetric =
  httpMetric.androidA read-only property that returns the HttpMetrics instance for Android.
ios 
import { firebase } from '@nativescript/firebase-core'
httpMetricIOS: FIRHTTPMetric = httpMetric.iosA read-only property that returns the HttpMetric instance for iOS.
getAttribute() 
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. | 
getAttributes() 
attributes: { [key: string]: string } = httpMetric.getAttributes();putAttribute() 
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. | 
removeAttribute() 
httpMetric.removeAttribute(attribute)Remove the specified attribute from the tracing metric.
setHttpResponseCode() 
httpMetric.setHttpResponseCode(code)| Parameter | Type | Description | 
|---|---|---|
| code | number | The HTTP response code. | 
setRequestPayloadSize() 
httpMetric.setRequestPayloadSize(bytes)| Parameter | Type | Description | 
|---|---|---|
| bytes | number | The size of the request payload. | 
setResponseContentType() 
httpMetric.setResponseContentType(contentType)| Parameter | Type | Description | 
|---|---|---|
| contentType | string | The content type of the HTTP response. Examples: text/html,application/json | 
start() 
httpMetric.start()Marks the start of an HTTP request/response tracing.
stop() 
httpMetric.stop()Marks the end time of the response and queues the network request metric on the device for transmission.
Trace class 
android 
traceAndroid: com.google.firebase.perf.metrics.Trace = trace.androidA read-only property that returns the Trace instance for Android.
ios 
traceIOS: FIRTrace = trace.iosA read-only property that returns the Trace instance for iOS.
getAttribute() 
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. | 
getMetric() 
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. | 
getMetrics() 
metrics: { [key: string]: number } = trace.getMetrics();incrementMetric() 
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. | 
putAttribute() 
trace.putAttribute(attribute, value)For the description of this method, see putAttribute() on the Firebase documentation.
| Parameter | Type | Description | 
|---|---|---|
| attribute | string | |
| value | string | 
putMetric() 
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. | 
removeMetric() 
trace.removeMetric(metricName)| Parameter | Type | Description | 
|---|---|---|
| metricName | string | The name of the trace metric to remove from the Trace instance. | 
start() 
trace.start()Marks the start time of the trace.
stop() 
trace.stop()Marks the end time of the trace and queues the trace on the device for transmission.
License 
Apache License Version 2.0
- Previous
- Messaging
- Next
- Remote Config

