Stop New Relic injecting javascript into your AMP pages

If you use New Relic to monitor your website, you may run into issues when serving AMP content. AMP (Accelerated Mobile Pages) are very strict by nature, so the code that New Relic injects causes validation to fail. There is a way round this. This fix is for those who use PHP.

When validating your pages in your production environment, you may receive this error:

The tag 'script' is disallowed except in specific forms

Upon viewing the page source, you will also see the familiar JavaScript as injected by New Relic. This is because AMP really doesn’t like custom JavaScript. Read more about the JavaScript critical path on the AMP Project website.

The tag ‘script’ is disallowed except in specific forms fix for New Relic

The fix is very straightforward. We will run a PHP function in our code to prevent New Relic injecting additional JavaScript that will upset Google AMP verification.

// Disables newrelic on AMP pages
if (extension_loaded('newrelic')) {

For example, I have a site that serves AMP content. I added the above snippet to the controller method that handles requests for AMP pages. This disabled the New Relic JavaScript injection only where I needed it to be disabled. By wrapping in a check to see if the ‘newrelic’ extension exists, I get no ‘undefined function’ errors when working locally or on my CI environment.

That’s it. Good luck. Any feedback, add to the comments below.



Web Developer living in Manchester, working for Studio Skylab ( Views and thoughts are my own.

You may also like...

1 Response

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.