Report mobile monitoring custom events and attributes
By default, New Relic collects some event data from your mobile app to New Relic, such as interactions, sessions, crashes, and request errors. However, you can also create your own custom attributes and events for more detailed querying and analysis.
Create custom attributes and events
You can create custom session-level attributes for default events using the mobile agent SDK. For example, to record a username attribute for some part of your iOS or Android app, you would use the setAttribute API. These attributes are session-related information and are shared by multiple mobile event types.
You can also create entirely new custom event types and assign them their own custom attributes, using the recordCustomEvent API.
To help with crash analysis, you can use the SDK to create MobileBreadcrumb and MobileHandledException events. These events are available for querying and also displayed in the crash event trail UI.
For more on creating custom attributes and custom events, see:
Here are some examples of using NRQL to query your mobile app events and attributes:
To track purchases in your app, use recordCustomEvent to create an event type (such as "UserAction") and associate attributes such as "name" (with value "Purchase"), price, quantity, and SKU.
ヒント
For performance reasons, you should limit the total number of event types to maybe one or two. The recordCustomEvent parameter eventType is meant to be used for high-level categories. For example, you might create an event typeGestures, and then create many different custom event names under the Gesture event type.
New Relic reports a custom event of type UserAction and name Purchase, which allows you to query all purchases made in your app in the last day:
SELECT * from UserAction where name = 'Purchase' since 1 day ago
Replace deprecated recordEvent method:
As of Android agent version 5.12.0 and iOS agent version 5.12.0, use the recordCustomEvent method to create these custom events. If you have replaced the deprecated recordEvent method for your custom events, be sure to also replace its corresponding NRQL query with the new format.
Look for queries used with recordEvent method, such as:
SELECT * from Mobile where category = 'Custom' and name = 'Purchase' since 1 day ago
Replace them with the query format used with recordCustomEvent:
SELECT * from UserAction where name = 'Purchase' since 1 day ago
You can create a custom attribute to track a custom user identifier across the session, and then query for all that user's interactions. To add an attribute for the userId, call the setUserId method:
With this attribute, you can use a WHERE clause to see all actions performed by that username in the last day:
SELECT * from Mobile WHERE userId = 'jsmith' since 1 day ago
You can create a custom attribute to track a store id across the session, and then query for all that store's interactions. To add an attribute for the storeId, call the setAttribute method:
With this attribute, you can use a WHERE clause to see all actions performed by that storeId in the last day:
SELECT * from Mobile WHERE storeId = 'NY0531' since 1 day ago
You can use custom attributes to track the number of times that a specific action occurs in your application. For example, you can track the number of times a button was clicked or the number of times a level was completed in a game.
To track completing a game level, call incrementAttribute with no value specified. This creates an attribute with a default value of 1:
When querying, use this level attribute to filter your data. For example, if you have a username and level attribute, use the max() function to find the highest level the user had reached:
SELECT max(level) from Mobile where username = 'jsmith'
By default, New Relic transmits event data in any of these situations:
A session has been ongoing for 600 seconds.
The app session ends by backgrounding.
The app crashes.
If the app crashes, New Relic collects the attributes and events for that session. (On iOS, this happens the next time the app is launched). You can then use NRQL to query and analyze the event and attribute data.
To set the maximum time (in seconds) that the agent will store events in memory, see Set max buffer time.
Privacy considerations
If you want to collect personal data via custom attributes, please consult with your privacy or legal teams. Be sure to follow your organization's obligations for notices and consent regulations.