New Relic Browser and the PHP agent

With the PHP agent, you can add New Relic Browser instrumentation to your webpages either automatically or manually. To enable Browser in the user interface, see Browser settings. Then follow the procedures in this section to set up the PHP agent.

To use Browser with your PHP agent, ensure you have the latest release. For more information, see the agent installation instructions.

Use auto-instrumentation

To configure the agent to automatically instrument pages with the Browser monitoring script, add or edit the auto-instrumentation flag in your INI file. The PHP agent will automatically inject the Browser JavaScript into your pages.

newrelic.browser_monitoring.auto_instrument=1

Auto-instrumentation does not work when the HTTP header field Content-Length is set. To use Browser monitoring in this situation, disable auto-instrumentation and manually insert the Browser JavaScript header and footer into your templates.

Manually instrument via agent API

To manually configure Browser monitoring, include the appropriate scripts in your pages. The New Relic PHP agent has an API to automatically generate the required Browser JavaScript header and footer in place.

Calls to the PHP agent API are conditional on the newrelic extension being loaded so that the PHP agent is running. If the agent is not running, calls to generate the Browser JavaScript header and footer will result in errors, as the functions will not be defined.

For more information, see:

Manually instrument for various frameworks

This document does not provide detailed procedures to manually insert the Browser agent via the agent API. This is because there too many possible ways to construct a framework.

The manual instrumentation examples show how to add instrumentation to frameworks that already support auto-instrumentation. If you are using another framework or modified versions of Drupal or WordPress, be sure to call the API functions for instrumentation at the right place.

  1. Insert the return value of newrelic_get_browser_timing_header() as part of the output page's <head> tag, preferably as the very first thing.
  2. As the very last thing before the closing </body> tag in the document, or as close to it as possible, insert the return value of newrelic_get_browser_timing_footer().

Here are some basic examples.

WordPress

This example shows how to call the PHP agent API to generate headers and footers for a WordPress installation using the default twentyten theme.

  1. Insert a call to generate the Browser JavaScript header in website home dir/wordpress/wp-content/themes/twentyten/header.php.

    <body <?php body_class(); ?>>
    <div id="wrapper" class="hfeed">
      <div id="header">
        <?php if( extension_loaded('newrelic') ) { echo newrelic_get_browser_timing_header(); } ?>
        <div id="masthead">
        ...
  2. Insert a call to generate the end user monitoring footer in website home dir/wordpress/wp-content/themes/twentyten/footer.php.

    ...
        </div><!-- #site-generator -->
      </div><!-- #colophon -->
      <?php if( extension_loaded('newrelic') ) { echo newrelic_get_browser_timing_footer(); } ?>
    </div><!-- #footer -->
Drupal

This example shows how to call the PHP agent API to generate headers and footers for a Drupal installation using the default garland theme.

  1. Insert a call to generate the JavaScript header right after the opening header tag in website home dir/drupal/themes/garland/page.tpl.php.

    <div id="container" class="clear-block">
      <div id="header">
        <?php if( extension_loaded('newrelic') ) { echo newrelic_get_browser_timing_header(); } ?>
        <div id="logo-floater">
        ...
  2. Insert a call to generate the footer right before the last closing layout tag:

    ...
    </div>
    <?php if( extension_loaded('newrelic') ) { echo newrelic_get_browser_timing_footer(); } ?>
    <!-- /layout -->
      <?php print $closure ?>
      </body>
    </html>

In Drupal 7.15, Compress cached pages is turned on by default. If you also select Cache pages for anonymous users, the JavaScript (newrelic.js) is not inserted into the served pages for anonymous users. This is because Drupal's pages are compressed directly from the database before they are stored in the cache (with gzip), so New Relic's PHP agent does not have a chance to parse any HTML. In this situation, manual instrumentation provides a better opportunity to capture data for anonymous users.

View Browser data

Once you enable Browser, data should be available almost immediately. There is a short delay the first time you start the daemon (or when the PHP agent encounters a new application) while the daemon verifies the settings. After that, the Browser JavaScript is available.

Disable auto-instrumentation

The default auto-instrumentation value is 1 (true). To disable auto-instrumentation, set newrelic.browser_monitoring.auto_instrument=0 (false).

To disable auto-instrumentation with an agent API call, use newrelic_disable_autorum().

For more help

Additional documentation resources include:

Join the discussion about PHP in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.