message  Send Feedback

HttpMetric

Metric used to collect data for network requests/responses. A new instance must be used for every request/response.

Methods


Methods

getAttribute

getAttribute(
  attribute: string
): string | null;

Returns the value of an attribute. Returns null if it does not exist.

Example

const attribute = metric.getAttribute('user_role');

Parameters

  • attribute: string
    Name of the attribute to fetch the value of

getAttributes

getAttributes(): { [key: string]: string };

Returns an object of all the currently added attributes.

Example

const attributes = metric.getAttributes();
attributes.forEach(($) => {
console.log($);
});

putAttribute

putAttribute(
  attribute: string,
  value: string
): any;

Sets a String value for the specified attribute. Updates the value of the attribute if it already exists. The maximum number of attributes that can be added is 5.

Example

metric.putAttribute('user_role', 'admin');

Parameters

  • attribute: string
    Name of the attribute. Max length is 40 chars.
  • value: string
    Value of the attribute. Max length is 100 chars.

removeAttribute

removeAttribute(
  attribute: string
): any;

Removes an already added attribute. Does nothing if attribute does not exist.

Example

metric.removeAttribute('user_role');

Parameters

  • attribute: string
    Name of the attribute to be removed.

setHttpResponseCode

setHttpResponseCode(
  code: number | null
): any;

Sets the httpResponse code of the request.

Example

const response = await fetch(url);
metric.setHttpResponseCode(response.status);

This is required for every request, if you do not provide this your metric will not be captured.

Parameters

  • code: number | null
    Value must be greater than 0. Set to null to remove. Invalid usage will be logged natively.

setRequestPayloadSize

setRequestPayloadSize(
  bytes: number | null
): any;

Sets the size of the request payload.

Example

const response = await fetch(url);
metric.setRequestPayloadSize(response.headers.get('Content-Type'));

Parameters

  • bytes: number | null
    Value must be greater than 0. Set to null to remove. Invalid usage will be logged natively.

setResponseContentType

setResponseContentType(
  contentType: string | null
): any;

Content type of the response e.g. text/html or application/json.

Example

const response = await fetch(url);
metric.setResponsePayloadSize(response.headers.get('Content-Type'));

Parameters

  • contentType: string | null
    Valid string of MIME type. Set to null to remove. Invalid usage will be logged natively.

setResponsePayloadSize

setResponsePayloadSize(
  bytes: number | null
): any;

Sets the size of the response payload.

Example

const response = await fetch(url);
metric.setResponsePayloadSize(response.headers.get('Content-Length'));

Parameters

  • bytes: number | null
    Value must be greater than 0. Set to null to remove. Invalid usage will be logged natively.

start

start(): Promise<null>;

Marks the start time of the request. Does nothing if already started.

Example

const metric = firebase.perf().newHttpMetric('https://api.com/login', 'POST');
await metric.start();

stop

stop(): Promise<null>;

Marks the end time of the response and queues the network request metric on the device for transmission. Does nothing if already stopped.

Example

const metric = firebase.perf().newHttpMetric('https://api.com/login', 'POST');
await metric.start();
metric.putAttribute('user_role', 'admin');
await metric.stop();

React Native Market

Looking for pre-built React Native apps to kick start your next project? Check out the React Native Market by Invertase.

Your purchases help support our open-source projects such as React Native Firebase. All items are currently 50% off to celebrate the release of React Native Firebase version 6.
shopping_cartVisit React Native Market