1.259.0 • Published 1 day ago

@newrelic/browser-agent v1.259.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 day ago

New Relic Browser Agent

The New Relic browser agent instruments your web application or site and provides observability into performance, errors, and other behaviors.

  • The instructions on this page pertain to installing the browser agent as an NPM package.

  • The browser agent is also generally available as a copy-paste JavaScript snippet and via auto-injection by backend apps. See Install the browser agent for info on these alternatives. Releases are deployed to customers gradually, so the version available via these other methods often lags the current release of this package.

  • For questions and feedback on this package, please visit the Explorer's Hub, New Relic's community support forum.

  • Looking to contribute to the browser agent code base? See DEVELOPING.md for instructions on building and testing the browser agent library, and CONTRIBUTING.md for general guidance.

Adding the agent package to your project

To make the agent available to your application, install via NPM or Yarn.

$ npm install @newrelic/browser-agent --save
$ yarn add @newrelic/browser-agent

Creating an app in New Relic

Before instrumenting your app using the NPM package, a Browser App should be configured in New Relic. This may be done with or without a corresponding APM agent. Once the app has been created, the Copy/Paste JavaScript code on the app's Application settings page will contain the configuration values needed to define options when instantiating the agent via the NPM package.

  1. If a browser app does not already exist, create one:
    • From the New Relic navigation panel, click Add Data.
    • Select the Browser monitoring data source.
    • Choose the APM or Copy/Paste method.
    • Select or name your app and click Enable.
  2. From the navigation panel, select Browser to view browser apps.
  3. Select the desired app and navigate to the Application settings page.
  4. From the Copy/Paste JavaScript box, copy the configuration values assigned to the NREUM object (init, info, and loader_config). You will use these configuration values when instantiating the agent using the NPM package.

Instantiating the agent

For best results, import and instantiate the BrowserAgent class as close to the top of the head element of your app's HTML output as possible. The specific location and method will vary based on your application's architecture or framework. See Library Support for more information.

Populate the options parameter using configuration values found in the the Copy/Paste JavaScript box in your browser app's Application settings page in New Relic.

import { BrowserAgent } from '@newrelic/browser-agent/loaders/browser-agent'

// Populate using values in copy-paste JavaScript snippet.
const options = {
  init: { ... }, // NREUM.init
  info: { ... }, // NREUM.info
  loader_config: { ...} // NREUM.loader_config
}

// The agent loader code executes immediately on instantiation.
new BrowserAgent(options)

Turning off features at runtime

Features may be turned off at runtime, which may have a meaningful peformance impact in some cases.

In the example below, the page_view_timing and session_trace features are disabled.

const options = {
  info: { ... },
  loader_config: { ... },
  init: {
    page_view_timing: { enabled: false },
    session_trace: { enabled: false },
    ...
  }
}

The following features may be disabled by adding init entries as shown above. Note that the page_view_event feature may not be disabled.

  • ajax
  • jserrors
  • metrics
  • page_action
  • page_view_timing
  • session_replay
  • session_trace
  • spa

See the New Relic documentation site for information on the above features.

Composing a custom agent with selected feature modules

The examples above use the BrowserAgent class, which is the best option for most use cases. The class makes all browser agent features available but provides the ability to turn off individual features selectively.

Using the base Agent class, it is also possible to compose a custom agent by passing an array called features in the options object, containing only the desired feature modules. Depending on which features are included, this may yield a smaller loader script and improved performance.

The example below includes three feature modules: Metrics, PageViewEvent, and PageViewTiming.

import { Agent } from '@newrelic/browser-agent/loaders/agent'
import { Metrics } from '@newrelic/browser-agent/features/metrics'
import { PageViewEvent } from '@newrelic/browser-agent/features/page_view_event'
import { PageViewTiming } from '@newrelic/browser-agent/features/page_view_timing'

const options = {
  info: { ... },
  loader_config: { ... },
  init: { ... }
}

new Agent({
  ...options,
  features: [
    Metrics,
    PageViewEvent,
    PageViewTiming
  ]
})

The following feature modules are available for inclusion in the features array:

import { Ajax } from '@newrelic/browser-agent/features/ajax';
import { JSErrors } from '@newrelic/browser-agent/features/jserrors';
import { Metrics } from '@newrelic/browser-agent/features/metrics';
import { PageAction } from '@newrelic/browser-agent/features/page_action';
import { PageViewEvent } from '@newrelic/browser-agent/features/page_view_event';
import { PageViewTiming } from '@newrelic/browser-agent/features/page_view_timing';
import { SessionTrace } from '@newrelic/browser-agent/features/session_trace';
import { Spa } from '@newrelic/browser-agent/features/spa';

Deploying one or more "micro" agents per page

The examples above use the Agent class at their core, which is ideal for most cases as it will automatically detect page-level events across your web application.

Using the MicroAgent class, it is possible to skip the "auto" instrumentation phases of the other loader types, and provide a very small agent designed for capturing data in a controlled manner via the API interfaces. The MicroAgent captures a distinct PageView event when instantiated, and additional PageAction and JavaScriptError events may be captured by calling the noticeError and addPageAction methods.

Because it does not wrap the page-level globals in the same way as the base Agent class, the MicroAgent is not only smaller but can easily be instantiated multiple times on a single page with low overhead, with each instance configured to report to a different Browser App entity in New Relic if desired. This accommodates specialized use cases, such as segmented UI designs (e.g., the micro front-end pattern) or applications requiring subsets of manually-handled data to be reported to different application entities.

The example below illustrates how to instantiate and interact with two separate MicroAgent instances on one page.

import { MicroAgent } from '@newrelic/browser-agent/loaders/micro-agent'

const options_1 = {
  info: { ... }, // configuration for application 1
  loader_config: { ... },
  init: { ... }
}

const microAgent1 = new MicroAgent(options_1)

const options_2 = {
  info: { ... }, // configuration for application 2
  loader_config: { ... },
  init: { ... }
}

const microAgent2 = new MicroAgent(options_2)

// manually handle a JavaScript Error with microAgent1
try{
  ...
} catch(err) {
  microAgent1.noticeError(err)
}

// manually capture a Page Action with microAgent2
microAgent2.addPageAction('myData', {hello: 'world'})

Browser Agent APIs

All Browser Agent APIs are exposed for use in two ways:

  • Via the newrelic window-level global object
  • At the top-level of the Agent instance

Please see our official documentation for more information about the Browser Agent APIs.

  newrelic.noticeError(...)
  // or
  const NrAgent = new BrowserAgent(...)
  NrAgent.noticeError(...)

Session Replay

The Session Replay feature is now available for limited free use by all customers. The data collected by this feature will become billable starting May 15th, 2024. Please see the Session Replay documentation to get started using this new feature.

Supported browsers

Our supported browser list can be accessed here.

If you desire more control over how the agent is built, our package also includes the source code. The source code is written to target the latest ECMAScript standards (ES2022). Please note that if you import the browser agent source code instead of the transpiled distribution code, you may need to alter your project's build system to apply transformations to the browser agent source code. Without these transforms, the code produced by your project's build system may not be compatible with your desired target browsers. Below is an example of how to import the source code.

// Note the /src/ in the import paths below
import { Agent } from '@newrelic/browser-agent/src/loaders/agent'
import { Metrics } from '@newrelic/browser-agent/src/features/metrics'
import { PageViewEvent } from '@newrelic/browser-agent/src/features/page_view_event'
import { PageViewTiming } from '@newrelic/browser-agent/src/features/page_view_timing'

const options = {
  info: { ... },
  loader_config: { ... },
  init: { ... }
}

new Agent({
  ...options,
  features: [
    Metrics,
    PageViewEvent,
    PageViewTiming
  ]
})

Neither the browser agent development team nor the New Relic support teams can support the numerous build systems that exist in the JavaScript ecosystem. If you run into issues, you may be asked to revert to importing from the transpiled distribution code.

DON'T mix imports between the browser agent source code and transpiled distribution code. The below will break.

// THIS IS BAD - do not do this
import { Agent } from '@newrelic/browser-agent/loaders/agent'
import { Metrics } from '@newrelic/browser-agent/src/features/metrics'

Library Support

The browser agent is written to be agnostic to any JavaScript library or framework. The agent exposes a number of API methods that can be incorporated into libraries and frameworks. For example, export or make available the initialized agent and create a new error boundary in your react application that calls browserAgent.noticeError().

Server-Side Rendering

A lot of new frameworks support the concept of server-side rendering the pages of an application to improve SEO and the performance of the user experience. The browser agent must be imported and instantiated within a browser context and will not work if executed on a server or in the server context of a server-side rendered application. These frameworks typically provide support for defining code that should only be run on the client. Check your frameworks documentation for more information.

Disclaimers

The session replay library shipping as part of the browser agent is not turned on by default. For information on the use of this feature, see Session Replay

Support

New Relic hosts and moderates an online forum where customers can interact with New Relic employees as well as other customers to get help and share best practices. Like all official New Relic open source projects, there's a related Community topic in the New Relic Explorers Hub. You can find this project's topic/threads here:

https://discuss.newrelic.com/c/full-stack-observability/browser

Contribute

We encourage your contributions to improve the Browser agent! Keep in mind that when you submit your pull request, you'll need to sign the CLA via the click-through using CLA-Assistant. You only have to sign the CLA one time per project.

If you have any questions, or to execute our corporate CLA (which is required if your contribution is on behalf of a company), drop us an email at opensource@newrelic.com.

For more details on how best to contribute, see CONTRIBUTING.md

A note about vulnerabilities

As noted in our security policy, New Relic is committed to the privacy and security of our customers and their data. We believe that providing coordinated disclosure by security researchers and engaging with the security community are important means to achieve our security goals.

If you believe you have found a security vulnerability in this project or any of New Relic's products or websites, we welcome and greatly appreciate you reporting it to New Relic through our bug bounty program.

If you would like to contribute to this project, review these guidelines.

To all contributors, we thank you! Without your contribution, this project would not be what it is today. We also host a community project page dedicated to the Browser agent.

License

The Browser agent is licensed under the Apache 2.0 License.

The Browser agent also uses source code from third-party libraries. Full details on which libraries are used and the terms under which they are licensed can be found in the third-party notices document.

1.259.0

1 day ago

1.258.2

2 days ago

1.258.1

2 days ago

1.258.0

10 days ago

1.257.0

21 days ago

1.256.1

24 days ago

1.256.0

28 days ago

1.255.0

1 month ago

1.254.1

1 month ago

1.254.0

2 months ago

1.253.0

2 months ago

1.252.1

2 months ago

1.252.0

3 months ago

1.251.1

3 months ago

1.251.0

4 months ago

1.250.0

4 months ago

1.249.0

5 months ago

1.243.0

7 months ago

1.243.1

7 months ago

1.247.0

6 months ago

1.239.1

8 months ago

1.239.0

8 months ago

1.242.0

8 months ago

1.246.1

6 months ago

1.246.0

7 months ago

1.238.0

9 months ago

1.241.0

8 months ago

1.245.0

7 months ago

1.237.1

9 months ago

1.237.0

10 months ago

1.244.0

7 months ago

1.240.0

8 months ago

1.248.0

6 months ago

1.236.0

11 months ago

1.232.1

12 months ago

1.232.0

1 year ago

1.235.0

11 months ago

1.234.0

11 months ago

1.233.0

12 months ago

1.233.1

11 months ago

0.1.231

1 year ago

0.1.230

1 year ago

0.1.229

1 year ago

0.0.9

2 years ago

0.0.9-beta.121

2 years ago

0.0.9-beta.120

2 years ago

0.0.9-beta.119

2 years ago

0.0.9-beta.118

2 years ago

0.0.9-beta.117

2 years ago

0.0.9-beta.116

2 years ago

0.0.9-beta.115

2 years ago

0.0.9-beta.114

2 years ago

0.0.9-beta.113

2 years ago

0.0.9-beta.112

2 years ago

0.0.9-beta.111

2 years ago

0.0.9-beta.110

2 years ago

0.0.9-beta.109

2 years ago

0.0.9-beta.108

2 years ago

0.0.9-beta.107

2 years ago

0.0.9-beta.106

2 years ago

0.0.9-beta.105

2 years ago

0.0.9-beta.104

2 years ago

0.0.9-beta.103

2 years ago

0.0.9-beta.102

2 years ago

0.0.9-beta.101

2 years ago

0.0.9-beta.100

2 years ago

0.0.9-beta.99

2 years ago

0.0.9-beta.98

2 years ago

0.0.9-beta.97

2 years ago

0.0.9-beta.96

2 years ago

0.0.9-beta.95

2 years ago

0.0.9-beta.94

2 years ago

0.0.9-beta.93

2 years ago

0.0.9-beta.92

2 years ago

0.0.9-beta.91

2 years ago

0.0.9-beta.90

2 years ago

0.0.9-beta.89

2 years ago

0.0.9-beta.88

2 years ago

0.0.9-beta.87

2 years ago

0.0.9-beta.86

2 years ago

0.0.9-beta.85

2 years ago

0.0.9-beta.84

2 years ago

0.0.9-beta.83

2 years ago

0.0.9-beta.82

2 years ago

0.0.9-beta.81

2 years ago

0.0.9-beta.80

2 years ago

0.0.9-beta.79

2 years ago

0.0.9-beta.78

2 years ago

0.0.9-beta.77

2 years ago

0.0.9-beta.76

2 years ago

0.0.9-beta.75

2 years ago

0.0.9-beta.74

2 years ago

0.0.9-beta.73

2 years ago

0.0.9-beta.72

2 years ago

0.0.9-beta.71

2 years ago

0.0.9-beta.70

2 years ago

0.0.9-beta.69

2 years ago

0.0.9-beta.68

2 years ago

0.0.9-beta.67

2 years ago

0.0.9-beta.66

2 years ago

0.0.9-beta.65

2 years ago

0.0.9-beta.64

2 years ago

0.0.9-beta.63

2 years ago

0.0.9-beta.62

2 years ago

0.0.9-beta.61

2 years ago

0.0.9-beta.60

2 years ago

0.0.9-beta.59

2 years ago

0.0.9-beta.58

2 years ago

0.0.9-beta.57

2 years ago

0.0.9-beta.56

2 years ago

0.0.9-beta.55

2 years ago

0.0.9-beta.54

2 years ago

0.0.9-beta.53

2 years ago

0.0.9-beta.52

2 years ago

0.0.9-beta.51

2 years ago

0.0.9-beta.50

2 years ago

0.0.9-beta.49

2 years ago

0.0.9-beta.48

2 years ago

0.0.9-beta.47

2 years ago

0.0.9-beta.46

2 years ago

0.0.9-beta.45

2 years ago

0.0.9-beta.44

2 years ago

0.0.9-beta.43

2 years ago

0.0.9-beta.42

2 years ago

0.0.9-beta.41

2 years ago

0.0.9-beta.40

2 years ago

0.0.9-beta.39

2 years ago

0.0.9-beta.38

2 years ago

0.0.9-beta.37

2 years ago

0.0.9-beta.36

2 years ago

0.0.9-beta.35

2 years ago

0.0.9-beta.34

2 years ago

0.0.9-beta.33

2 years ago

0.0.9-beta.32

2 years ago

0.0.9-beta.31

2 years ago

0.0.9-beta.30

2 years ago

0.0.9-beta.29

2 years ago

0.0.9-beta.28

2 years ago

0.0.9-beta.27

2 years ago

0.0.9-beta.26

2 years ago

0.0.9-beta.25

2 years ago

0.0.9-beta.24

2 years ago

0.0.9-beta.23

2 years ago

0.0.9-beta.22

2 years ago

0.0.9-beta.21

2 years ago

0.0.9-beta.20

2 years ago

0.0.9-beta.19

2 years ago

0.0.9-beta.18

2 years ago

0.0.9-beta.17

2 years ago

0.0.9-beta.16

2 years ago

0.0.9-beta.15

2 years ago

0.0.9-beta.14

2 years ago

0.0.9-beta.13

2 years ago

0.0.9-beta.12

2 years ago

0.0.9-beta.11

2 years ago

0.0.9-beta.10

2 years ago

0.0.9-beta.9

2 years ago

0.0.9-beta.8

2 years ago

0.0.9-beta.7

2 years ago

0.0.9-beta.6

2 years ago

0.0.9-beta.5

2 years ago

0.0.9-beta.4

2 years ago

0.0.9-beta.3

2 years ago

0.0.9-beta.2

2 years ago

0.0.9-beta.1

2 years ago

0.0.9-beta.0

2 years ago

0.0.8

2 years ago

0.0.8-beta.2

2 years ago

0.0.8-beta.1

2 years ago

0.0.8-beta.0

2 years ago

0.0.7

2 years ago