Adding user-defined attributes to PageView events
In this Level Up Solution, one common question customers ask me is “how can I send create and send custom attributes from my website to Insights”. This is possible using the Browser agent API (Browser Pro or Browser Pro Trial subscription required) we can create our custom attributes for PageView events which can then filter and view them in Insights.
Why do I need custom attributes?
The Browser agent has a predefined set of Browser API calls. These calls let you report additional data and sent this data to Insights. However, sometimes you may need additional data within the context of an initial page load or a route change(SPA agent required for route change), for example, user email address, user account information.
What are the prerequisites for custom attributes?
In order for the Browser agent to send custom attributes to Insights, the following must be true:
- The Browser agent script need to be inline in the head tag, before other script tags.
- The call needs to be made before the window load event fires (when that data is transmitted) in order for the attributes to be included in the PageView event.
- PageView events require an Insights Pro subscription with your New Relic Browser subscription.
What are the parameters for custom attribute API call?
The API call requires two string values
$name Required. Name of the attribute. Appears as a column in the PageView event. It will also appear as a column in the PageAction event if you are using it.
$value Required. Value of the attribute. Appears as the value in the named attribute column in the PageView event.
Here is an example of how you might call the Browser API and send it to Insights. I defined a custom attribute and named it 'userName, I inserted the API call in the header tag as the call needs to be made before the window load event fires.
Query Data with Insights*
The PageView event is a default event that the Browser agent reports to Insights. You can add custom attributes to the PageView event. Any custom attributes you add to the PageView event are also automatically added to the PageAction event.
View All Errors
SELECT `userName` from PageView WHERE `userName` IS NOT NULL and appName = 'Browser Level Up Post' SINCE 24 hours ago
Counts of different types of errors
SELECT count(`userName`) from PageView since 1 week ago FACET `userName
How many visits are we seeing for different browsers
SELECT count(*) from PageView where `userName` = 'Darren' since 1 week ago FACET `userAgentName
Your Browser Pro subscription provides you with 8 days of Insights event retention for Browser events