New Relic Browser Script in Different Enviroments

We currently have a SPA and want to use the New Relic Browser agent. This means that we have to use the copy an paste method to add it to our application.

However, we want to use different application id’s based on the environment that is being used. Is there a way to configure script to use a different application id?

Without have to duplicate the script for each environment.

If you take a look at the very bottom of the script you’ll notice a NREUM.info section that contains the Browser application ID. If you’ve already created multiple apps in the New Relic UI, you can edit the applicationID to point data elsewhere.

Some more info on this can be found here.

@cstanhope To add to Max’s comment above, if you have a way to surface the current code environment in a Javascript variable, you can write some conditional logic to set the app ID dynamically, such as this:

 <script type="text/javascript">
 
    // The loader script (everything except the config block shown above) goes here
        window.NREUM||(NREUM={}),__nr_require=function(t,e,n){
 
        // … lots of code removed here for ease of viewing …

        ["firstbyte",a()],null,"api");var m=0},{}]},{},["loader",2,9,3]);
 
    // End loader; begin conditional logic for config
        var env = {YOUR ENVIRONMENT VARIABLE HERE};
        var appID;
 
        switch (env) {
            case "production":
                appID = "123456";
                break;
            case "staging":
                appID = "4567890";
                break;
            case "dev":
                appID = "654321";
        }
 
    // Config block with appID variable
        ;NREUM.info={beacon:"bam.nr-data.net",errorBeacon:"bam.nr-data.net",
        licenseKey:"YOUR_LICENSE_KEY",applicationID:appID,sa:1,
        agent:"js-agent.newrelic.com/nr-AGENT_VERSION.min.js"}
         
</script>

Note that there are placeholders for the license key and agent version in the config block at the bottom—these should be replaced with the values from your current copy/paste script.

4 Likes