From the course: Creating a DevOps Ecosystem for .NET MAUI Developers

Using additional functionality of Dynatrace - .NET MAUI Tutorial

From the course: Creating a DevOps Ecosystem for .NET MAUI Developers

Using additional functionality of Dynatrace

- [Instructor] With Dynatrace setup to capture the default metrics, we're going to look at another piece of functionality, the action. The action allows us to capture information about a specific operation. Since we open and close an action, we can capture vital information, like how long it took to complete. We can also have child actions. Those allow us to capture an overarching action and then specific information about its components that are grouped as part of that overarching action. For example, a checkout process, and then have child actions within it to capture things like credit card entry as part of the process. Also, within an action, we can capture vital piece of information, like the IDs and quantities of the items purchased. Actions can provide vital information about the performance of our application and how it is being used. These can tie into key performance indicators to help make our application successful. So let's look at how to set one up. So the first thing is, by default, user opt-in is enabled. And so we'd actually have to go out and ask the user if they wanted to opt-in to collect in this information. Instead, for now, or at least for this demo, we're just going to report that we did ask the user and they said that they wanted to opt-in to everything. So to do that, we're going to add that inside the Android and iOS conditional compilation block right under when we start. So we'll go Dynatrace, and MAUI.Agent.Instance, and we're going to go ApplyUserPrivacyOptions, and we're going to give it a new UserPrivacyOptions object. And inside there, we'll say the DataCollectionLevel, we're going to set that equal to UserBehavior. So that's going to get performance metrics and user behavior metrics. And we're also going to say that they're going to opt-in to crash reporting too, just like that. So like I said, normally we'd ask what level of data the user wants to collect about this, but since this is our own app for testing, we're just going to report that the user said that they're great, they're going to collect all data. We can also tie in a user ID if we wanted to. We're not going to do this for this particular instance, but if we had something to identify the user, and we'll close that line, we could do something like this, and then we could say user, or IdentifyUser here, and then we could pass in some string that uniquely identifies this user. Once again, we're not going to do that for this. Right now, they're going to get a unique ID that's generated by Dynatrace. In our application, we have the ability to save or change a QR code entry. And so we're going to create a custom action that tracks how long it takes for that process to happen. And we're going to record the ID of which QR code entry was changed. And this happens in the save method, in the view model for the view our item detail. And it happens in the OnSave method. Now, like we did before, we want to make sure that this only happens for Android and iOS, and we're also going to put the entire thing in a try-catch block. And the reason why we're going to do that is so if the user action fails, we can close it up. Now we're going to start and create a IRootAction, which is a Dynatrace object. And we'll say myAction, and it will be equal to Dynatrace.MAUI.Agent. And we'll say that we're entering an action, and we need to give it a string name. So we're going to call this QR Item Updated, just like that. And now we're going to open our try block. And now later on, after we're trying to save this, we're going to do another additional compilation for Android and iOS. We're here, we're going to capture the ID of the item into the action. So we're going to go myAction, and we're going to report a value. And these values can be anything that's a string or an integer or a double. If we want to capture anything else, we have to convert it to one of those three types. And so we need two pieces of information. And the first piece is going to be some name to call it. So we're going to call this Item Id. And the other piece is just the information we want to save. And in this case, it's the QRCodeItem we are saving, the ID of it. And then we'll close this endif. Also we're going to throw in a catch block. So we'll put that in the of statement as well, or #if, just for Android and iOS. We'll do a close the normal box, do catch, Exception ex. And here what we're going to say is we're going to go myAction, and ReportError, and we'll use the description of the exception, or at least the message. And also we can send up an integer error code. So we'll do ex, and we'll do I think the HResult. And since we don't want to change how the error handling normally works, we'll just throw this, and let the exception continue up the stack. Also, we'll add it finally. And this is where we can go in and close up that action. And this is going to end up, it'll end up adding it to the queue and it'll eventually get sent up to the portal on whatever the interval is that it sends this at. So we'll go myAction, and we can report the event, and that will close it all up. Now the other thing we could do is cancel the action. So we've got the action, and what we're going to do here is we're going to do LeaveAction. And LeaveAction is going to send up the results of what we just did. Now if we wanted to cancel the action, we could do that as well. So there's a myAction.Cancel, and if that happens, it won't report it instead. So we can either leave the action, which will report it, or we can cancel the action. So now we have everything we need to capture these custom events. We do want to make sure to close up our #if here, just like that. And that should have a compiled and running application. For the editors, I'm going to change Windows here. And here we are back at the dashboard, and I've actually run the app a couple times and captured some actions, and we can go and see, there's been several different users on here. And under Top 3 actions, we can see we've got the main loading one and touch on save. But now we have this one called QR Item Updated, and this is our custom action. And we can see that that is one of the top ones being captured, mostly, because I kept on doing that action several different times. We can also go in and take a look at these users, and we can analyze their sessions. So we're going to go in. And we'll take one of the last sessions that says it's currently running. That just might mean it's been stopped and it hasn't actually closed it out internally inside Dynatrace yet. So we'll click on it, and we can see that we've got the user session, but also we've got all the events that that user's done. So it's got the MainActivity where they entered the application. I've got a Touch on Save button, a couple of these. And then they've updated the QR button a couple times. So we can see a lot about it, what the user's doing in the application. But just remember if we have it turned on, the require user opt-in, which is the default, we actually have to ask the user if they want to opt-in reporting all these data to us. We shouldn't be reporting data such as this without the user's knowledge.

Contents