---
title: Instrument browser monitoring with Java agent API
source: https://docs.newrelic.com/docs/apm/agents/java-agent/instrumentation/instrument-browser-monitoring-java-agent-api
---

The [browser monitoring](https://docs.newrelic.com/docs/browser/new-relic-browser/getting-started/introduction-new-relic-browser) capability gives you visibility into how your users are interacting with your Java application by using a JavaScript snippet (or "agent") to instrument your app's webpages. To install the browser agent via the UI, follow the procedures to [install the browser agent](https://docs.newrelic.com/docs/browser/new-relic-browser/installation/install-new-relic-browser-agent). For example, you can:

-   Enable an APM-monitored app.
-   Enable with the copy/paste method.
-   Enable by using the REST API.

You can also [manually instrument](#manual_instrumentation) your webpages by using the Java agent API, as explained in this document.

## Use Java agent API [#manual_instrumentation]

If your framework does not allow you to enable browser monitoring from our UI, with the copy/paste method, or by using our REST API, you can manually instrument monitoring for your webpages. To do this, use our [Java agent API](https://docs.newrelic.com/docs/agents/java-agent/api-guides/guide-using-java-agent-api).

1.  Make sure you are using the [latest Java agent version](https://docs.newrelic.com/docs/release-notes/agent-release-notes/java-release-notes).
2.  Disable auto-instrumentation.

    **Disable auto-instrumentation on all pages**

    1.  Set the [`auto_instrument`](https://docs.newrelic.com/docs/agents/java-agent/configuration/java-agent-configuration-config-file#bm-auto_instrument) flag under `browser_monitoring` to `false` in your `newrelic.yml`.

        ```yml
        browser_monitoring:
          auto_instrument: false
        ```
    2.  Restart your application.
    3.  Flush the app server's work cache.

        Flushing the work cache forces the app server to recompile JSPs, which is when auto-instrumentation occurs.

    **Disable auto-instrumentation only on specific pages**

    Use the `disabled_auto_pages` flag under the `browser_monitoring` section. For example, to disable auto-instrumentation on `testpage_1.jsp` and `testpage_2.jsp`, use the following:

    ````yml
    browser_monitoring:
      disabled_auto_pages: /WEB-INF/jsp/testpage_1.jsp, /WEB-INF/jsp/testpage_2.jsp
    ```

    ````
3.  [Access the Java agent API class](https://docs.newrelic.com/docs/agents/java-agent/api-guides/guide-using-java-agent-api#api) by adding `newrelic-api.jar` to your application class path.
4.  Add the `com.newrelic.api.agent.NewRelic.getBrowserTimingHeader()` method to enable time tracking. See below for procedures for specific frameworks.

    > #### ⚠️ IMPORTANT
    >
    > As of [Java agent 8.9.0](https://docs.newrelic.com/docs/release-notes/agent-release-notes/java-release-notes/java-agent-890), the [getBrowserTimingFooter() API](https://newrelic.github.io/java-agent-api/javadoc/index.html?com/newrelic/api/agent/NewRelic.html) method call is deprecated. The entire browser script is now included when calling `getBrowserTimingHeader()`.

    Find the appropriate methods based on the framework you are using:

    **JSP manual instrumentation**

    Insert the header as close to the top of `<head>` tag as possible but after the meta tags.

    ````html
    <!DOCTYPE html>

    <html>
    <head>
      <title>EXAMPLE</title>
      <meta charset="utf-8">
      <meta name="description" content="Example header and footer call">;
      <%= com.newrelic.api.agent.NewRelic.getBrowserTimingHeader() %>
    </head>

    <body>

    </body>
    </html>
    ```

    ````

    **Raw HTML manual instrumentation**

    To manually instrument using raw HTML, [include page load timing code in the HTTP response](https://docs.newrelic.com/docs/agents/java-agent/custom-instrumentation/java-agent-api-example-program#include-browser).

    **Thymeleaf 2.1 or higher manual instrumentation**

    ````html
    <head>
      ...
      <div th:remove="tag" th:utext="${header}" th:with="header=${T(com.newrelic.api.agent.NewRelic).getBrowserTimingHeader()}"></div>
    </head>

    <body>
      ...
    </body>
    ```

    ````

    **ColdFusion CFML page manual instrumentation**

    ````html
    <cfobject type="Java" class="com.newrelic.api.agent.NewRelic" name="newRelic" />
    <cfset header=newRelic.getBrowserTimingHeader() />
    <cfoutput>#header#</cfoutput>
    ...
    <cfoutput>#footer#</cfoutput>
    ```

    ````

    **Java Server Faces (JSF) frameworks**

    Manual instrumentation is not available with JSF. To insert the Browser JavaScript snippet into your app's webpages, use the [copy/paste method](https://docs.newrelic.com/docs/browser/new-relic-browser/installation-configuration/add-apps-new-relic-browser#copy-paste-app).

    **Play Groovy template manual instrumentation**

    ````groovy
    ${com.newrelic.api.agent.NewRelic.getBrowserTimingHeader().raw()}
    ...
    ```

    ````

    **Play 2.2 or higher Scala template manual instrumentation**

    Add the following to `build.sbt`, substituting the [latest agent version](https://docs.newrelic.com/docs/release-notes/agent-release-notes/java-release-notes) for 8.x.x:

    ````scala
    libraryDependencies += "com.newrelic.agent.java" % "newrelic-api" % "8.x.x"
    ```

    In `main.scala.html`, add the following:

    ```java
    @Html(com.newrelic.api.agent.NewRelic.getBrowserTimingHeader())
    ...
    ```

    ````

    **Play 2.1 Scala template manual instrumentation**

    Add the following to `Build.scala`, substituting the [lastest agent version](https://docs.newrelic.com/docs/release-notes/agent-release-notes/java-release-notes) for 8.x.x:

    ````scala
    appDependencies += "com.newrelic.agent.java" % "newrelic-api" % "8.x.x"
    ```

    In `main.scala.html`, add the following:

    ```java
    @Html(com.newrelic.api.agent.NewRelic.getBrowserTimingHeader())
    ...
    ```

    ````

    **Apache Velocity Tools manual instrumentation**

    If you are using Velocity Tools, define a `NewRelic` tool in `toolbox.xml`:

    ````xml
    <toolbox>
      <tool>
        <key>NewRelic</key>
        <scope>application</scope>
        <class>com.newrelic.api.agent.NewRelic</class>
      </tool>
    </toolbox>
    ```

    Call the `NewRelic` API in your velocity template:

    ```java
    $NewRelic.getBrowserTimingHeader()
    ...
    ```

    ````

    **Apache Velocity manual instrumentation**

    1.  To ensure that all of your browser page views are not grouped under a single `/velocity` transaction (and to avoid [metric grouping issues](https://docs.newrelic.com/docs/agents/manage-apm-agents/troubleshooting/metric-grouping-issues)), disable your `enable_auto_transaction_naming` setting in your `newrelic.yml` file:

        ```yml
        enable_auto_transaction_naming: false
        ```
    2.  Add `newrelic-api.jar` to the classpath of Tomcat, typically in `.../tomcat/bin/setenv.sh`:

        ```sh
        CLASSPATH=$CLASSPATH:/opt/newrelic/newrelic-api.jar
        ```

        OR

        Add the reference to this jar file to the end of an existing `CLASSPATH=...` line within that file.
    3.  In each app, find `WEB-INF/tools.xml` and add the following tag:

        ```xml
        <toolbox scope="application"> 
          <tool class="com.newrelic.api.agent.NewRelic" key="NewRelic" /> 
        </toolbox>
        ```

        If a `<toolbox scope="application">` tag already exists, put the `<tool ... />` within the tag.
    4.  In each app, edit the Velocity template that the other pages in the application use. Add this tag within the `<head>` section before any `<link>` and `<script>` declarations and after any meta tags:

        ```java
        $NewRelic.getBrowserTimingHeader()
        ```
    5.  Restart the Tomcat server:

        -   If you are using `Linux/Unix`, add the following command line to the `Users/Tomcat/bin` directory:

            ```
            ./shutdown.sh
            ./startup.sh
            ```
        -   If you are using `Windows`, go to your `Users/Tomcat/bin` directory, and add the following command line:

            ```
            % shutdown.bat
            % startup.bat
            ```
5.  Check the log file to verify that the agent is instrumenting correctly. Set the `log_level` setting in `newrelic.yml` to `log_level: finer` and check for the following messages:

    **When the JSP engine compiles a JSP**

    ````
    May 17, 2011 9:33:18 AM NewRelic FINE: Compiling JSP: /jsp/header.jsp
    May 17, 2011 9:33:18 AM NewRelic FINE: Injecting browser timing header into: /jsp/header.jsp

    May 17, 2011 9:33:36 AM NewRelic FINE: Compiling JSP: /jsp/footer.jsp
    May 17, 2011 9:33:36 AM NewRelic FINE: Injecting browser timing footer into: /jsp/footer.jsp
    ```

    ````

    **After the agent connects to the server**

    The JavaScript snippet is not inserted into the HTML response until after the agent connects to the server.

    ````
    May 17, 2011 9:43:21 AM NewRelic INFO: Real user monitoring is enabled with auto instrumentation
    ```

    ````

    **When New Relic invokes the JSP**

    ````
    May 17, 2011 9:43:23 AM NewRelic FINER: Injecting browser timing header in a JSP.  Content type is text/html
    May 17, 2011 9:43:23 AM NewRelic FINER: Injecting browser timing footer in a JSP.  Content type is text/html
    ```

    ````

    **When the agent calls the header and footer API**

    ````
    May 17, 2011 9:43:23 AM NewRelic FINEST: Got browser timing header in NewRelic API: <script>var    NREUMQ=[];NREUMQ.push(["mark","firstbyte",new Date().getTime()]);(function(){var d=document;var e=d.createElement("script");e.type="text/javascript";e.async=true;e.src="https://d7p9czrvs14ne.cloudfront.net/11/eum/rum.js";var s=d.getElementsByTagName("script")[0];s.parentNode.insertBefore(e,s);})();</script>

    May 17, 2011 9:43:23 AM NewRelic FINEST: Got browser timing footer in NewRelic API: <script type="text/javascript" charset="utf-8">NREUMQ.push(["nrf2","staging-beacon-1.newrelic.com","8512b4d93f",7449,"M1RTYksFDEYDUxdcDgoeZERQSwhGEmQGRhVL",0,413])</script>
    ```

    ````
