FACET-ing New Relic PageViewTiming Data By 'Page Type'

Published: November 7, 2024

New Relic’s PageViewTiming data set provides excellent visibility into important performance metrics such as Core Web Vitals. When analyzing this data it can be useful to segment it by “page type” — for example, on an ecommerce website it can be helpful to know LCP or CLS scores for the product detail page, or product listing page individually. While it’s possible to view performance metrics for specific page URLs via the default pageUrl field, a website can have thousands (or more) of unique URLs for a given page type. Unless a predictable and consistent pattern is used for all URLs of a specific page type, by default it is not possible to segment data this way.

New Relic “Custom Attributes”

This issue can be solved by using New Relic custom attributes, which will be available when querying the PageViewTiming data set. The browser agent provides a setCustomAttribute function, which can be used for this purpose.

For example, in the case of Magento / Adobe Commerce, the “page type” can be identified consulting the list of classList of the <body> element. The below snippet can be used to quickly and easily set the pageType attribute for the product and category pages.

<script>
    (function() {
        if (!window.newrelic) {
            return;
        }

        if (document.body.classList.contains('catalog-category-view')) {
            newrelic.setCustomAttribute('pageType', 'catalog/category/view');
            return;
        }

        if (document.body.classList.contains('catalog-product-view')) {
            newrelic.setCustomAttribute('pageType', 'catalog/product/view');
            return;
        }
    })();
</script>

Once in place, pageType can be used for FACET-ing and WHERE clauses in your NRQL queries.

Max Chadwick Hi, I'm Max!

I'm a software developer who mainly works in PHP, but loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Rightpoint where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.