Get SPA+Pro Browser agent script using REST API/Node.js lib

Hello,

I’m trying to automate Browser Agent update process. My Node.js App is running on vercel (uses AWS lambda behind the scenes). It’s not possible to inject script at runtime using newrelic.getBrowserTimingHeader() because most of the time my app hosted from cdn cache.

So my solution is to download fresh Agent script before building an app. Looks like newrelic lib can not be used to fetch the script only, because it requires an active transaction. I can’t find the way to get the script only or access internal config object. Underlying collector.newrelic.com API seems not to be intended for public usage.

I’ve found the doc wich list options to get script programmatically but it turns out that v2 API won’t work with SPA (it always returns non-spa agent code) and the page with possible solutions for spa has no solutions at all :sweat_smile:

Am I missing something or the only way to automate the update of SPA+PRO Browser Agent script is to download the actual script from loader endpoint and manually add all the required configuration?

Hey there @mybox ! These are great questions and I’m happy to help with some suggestions.

The v2 Rest API for browser applications does only return the Lite script loader version. Though I would be happy to file a feature idea for better configurability via API for CRUD functions with browser applications. In addition, we’ve been adding more features to the Nerd Graph API, see documentation here, though again I would file a feature request as I’m not seeing the browser create functions yet.

And you’re right that currently we don’t have an automated API solution for creating these applications. The APM agent auto-injecting the browser agent script is the closest to automation as the other option (copy/paste it inline and deploy) is indeed manual.

Here’s another post that you may find interesting or helpful as well:

Question re: getBrowserTimingHeader and caching

The other suggestion I could toss out here is loading the script from an external source file. While this isn’t a standard installation practice, quite a few people are using it for various reasons including for CSP. You could possibly follow the browser release notes or open source repository and when a new agent is released, grab the updated JavaScript from our CDN, replace your file and deploy your application.

Otherwise, I’m happy to file a feature idea on your behalf for better API capabilities or if you have any other Node-specific questions, I can also loop in a colleague to assist with the agent’s auto-inject install. Please let me know how I can help, thanks!

1 Like

@cfrankenfield thank you for such detailed reply!

I’ve ended up with a script that I run before build step. This script downloads a fresh agent loader from CDN (https://js-agent.newrelic.com/nr-loader-spa-current.min.js), then concatenates script source with configuration (NREUM.init, NREUM.loader_config, NREUM.info) and writes that data to a local file which will be picked up during build. The easiest way to get configuration vars (NREUM) is from the copy-paste version of the script.

Though I would be happy to file a feature idea for better configurability via API

It would be really cool. I think integration should not be so complicated and must be automated (developers are lazy people :joy:). Npm newrelic lib can also be modified to allow generating a script (like I’ve done manually) in one line.