Help prepare for 2025 → Answer 8 simple questions on our Fall Survey
Open Survey

View on GitHub

@nativescript/firebase-performance

Contents

Intro

This plugin allows you to use the Firebase Performance Monitoring API in your NativeScript app.

image

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:

  1. Install the @nativescript/firebase-performance plugin by running the following command in the root directory of your project.
cli
npm install @nativescript/firebase-performance
  1. 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 or main.ts).
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.

ts
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.
ts
const metric = await firebase().perf().newHttpMetric(url, 'GET')
  • Add custom attributes to the metric.
ts
metric.putAttribute('user', 'abcd')
  • Start the metric.
ts
await metric.start()
  • Perform the HTTP request and provide response information.
ts
const response = await fetch(url)
metric.setHttpResponseCode(response.statusCode)
metric.setResponseContentType(response.headers['Content-Type'])
metric.setResponsePayloadSize(response.headers['Content-Length'])
  • Stop the metric.
ts
await metric.stop()

The above steps combined would look as follows:

ts
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

ts
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.


ios

ts
import { firebase } from '@nativescript/firebase-core'

performanceIOS: FIRPerformance = firebase().perf().ios

A read-only property that returns the Performance Monitoring instance for iOS.


app

ts
import { firebase } from '@nativescript/firebase-core'

performanceApp: FirebaseApp = firebase().perf().app

A read-only property that returns the FirebaseApp instance for the current app.


isPerformanceCollectionEnabled

ts
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.


newHttpMetric()

ts
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.

ParameterTypeDescription
urlstring
httpMethodHttpMethod

newTrace()

ts
import { firebase } from '@nativescript/firebase-core'

trace: Trace = firebase().perf().newTrace(identifier)

Creates a new Trace instance with the given identifier.

ParameterTypeDescription
identifierstring

startTrace()

ts
import { firebase } from '@nativescript/firebase-core'

trace: Trace = firebase().perf().startTrace(identifier)

Creates and starts a new Trace instance with the given identifier.

ParameterTypeDescription
identifierstring

HttpMetric class

android

ts
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.


ios

ts
import { firebase } from '@nativescript/firebase-core'

httpMetricIOS: FIRHTTPMetric = httpMetric.ios

A read-only property that returns the HttpMetric instance for iOS.


getAttribute()

ts
someAttribute: string = httpMetric.getAttribute(attribute)

Returns the value for the specified attribute.

ParameterTypeDescription
attributestringThe name of the attribute to retrieve the value for.

getAttributes()

ts
attributes: { [key: string]: string } = httpMetric.getAttributes();

putAttribute()

ts
httpMetric.putAttribute(attribute, value)

For the description of this method, see putAttribute() on the Firebase documentation.

ParameterTypeDescription
attributestringThe name of the attribute to set.
valuestringThe value of the attribute to set.

removeAttribute()

ts
httpMetric.removeAttribute(attribute)

Remove the specified attribute from the tracing metric.


setHttpResponseCode()

ts
httpMetric.setHttpResponseCode(code)
ParameterTypeDescription
codenumberThe HTTP response code.

setRequestPayloadSize()

ts
httpMetric.setRequestPayloadSize(bytes)
ParameterTypeDescription
bytesnumberThe size of the request payload.

setResponseContentType()

ts
httpMetric.setResponseContentType(contentType)
ParameterTypeDescription
contentTypestringThe content type of the HTTP response. Examples: text/html, application/json

start()

ts
httpMetric.start()

Marks the start of an HTTP request/response tracing.


stop()

ts
httpMetric.stop()

Marks the end time of the response and queues the network request metric on the device for transmission.


Trace class

android

ts
traceAndroid: com.google.firebase.perf.metrics.Trace = trace.android

A read-only property that returns the Trace instance for Android.


ios

ts
traceIOS: FIRTrace = trace.ios

A read-only property that returns the Trace instance for iOS.


getAttribute()

ts
someAttribute: string = trace.getAttribute(attribute)

Returns the value for the specified attribute of the trace.

ParameterTypeDescription
attributestringThe name of the attribute to retrieve the value for.

getMetric()

ts
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.

ParameterTypeDescription
metricNamestringThe name of the metric to retrieve the value for.

getMetrics()

ts
metrics: { [key: string]: number } = trace.getMetrics();

incrementMetric()

ts
trace.incrementMetric(metricName, incrementBy)

For the description of this method, see incrementMetric() on the Firebase documentation.

ParameterTypeDescription
metricNamestringThe name of the trace metric to increment.
incrementBynumberThe value to increment the metric by.

putAttribute()

ts
trace.putAttribute(attribute, value)

For the description of this method, see putAttribute() on the Firebase documentation.

ParameterTypeDescription
attributestring
valuestring

putMetric()

ts
trace.putMetric(metricName, value)

For the description of this method, see putMetric() on the Firebase documentation.

ParameterTypeDescription
metricNamestringThe name of the trace metric to set.
valuenumberThe value to set the metric to.

removeMetric()

ts
trace.removeMetric(metricName)
ParameterTypeDescription
metricNamestringThe name of the trace metric to remove from the Trace instance.

start()

ts
trace.start()

Marks the start time of the trace.


stop()

ts
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