Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track page visit duration #49

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

IeuanWalker
Copy link
Contributor

@IeuanWalker IeuanWalker commented Feb 12, 2025

A New custom event called PageVisitTime was added, with custom metrics added for the Duration in milliseconds.

In application insights, the new event looks like this -
image

This fixes the timeline history of events, Page View is tracked as the page loads and PageVisitTime is tracked as the user leaves the page -
image

#48

- Added a no-op constructor for unit testing in net8.0.
- Introduced `_pageVisitTimeTracking` to replace `_lastPageAppearing`.
- Modified `OnAppearing` and `OnDisappearing` methods for improved page visit duration tracking.
- Replaced `TrackPageViewAsync` with `TrackPageVisitTime` to log page visit times.
- Updated `IInsightsProvider` interface to include the new tracking method.
- Made various formatting and null-checking improvements.
Updated `TrackPageVisitTime` to accept `pageFullName` and `pageDisplayName` parameters for improved telemetry. Modified the `IInsightsProvider` interface to reflect these changes and added XML documentation for better readability. Removed the `duration` parameter from `TrackPageViewAsync` to simplify its signature. Updated the properties dictionary in `ApplicationInsightsProvider` to utilize the new parameters for more informative tracking.
- Updated `ApplicationInsightsProvider` to change the key for page URL and consolidate metrics into a single "Duration" metric.
- Modified `IInsights` interface to remove `duration` from `TrackPageViewAsync` and added `TrackPageVisitTime` method.
- Adjusted `Insights` class to reflect interface changes and implemented `TrackPageVisitTime`.
- Improved formatting in `HasCrashed`, `SendCrashes`, and `ResetCrashes` methods for better readability.
Updated the `OnDisappearing` method in the `ApplicationInsightsProvider` class to use an index-based approach for retrieving the last page added. This change replaces the nullable tuple logic with `FindLastIndex`, improving efficiency by directly manipulating the list. The method now returns early if no last page is found, and the duration calculation is adjusted accordingly.
@IeuanWalker IeuanWalker marked this pull request as ready for review February 12, 2025 13:01
{ "Duration", pageVisitTime }
};

Client.TrackEvent("PageVisitTime", properties, metrics);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhindrik I've mostly copied what MS has done in their react library, the main difference is they are using TrackMetric whereas I've used TrackEvent, with custom metric parameters.

TrackMetric can be used instead just by doing this -

  Client.TrackMetric("PageVisitTime", pageVisitTime, properties);

I decided not to do this, due to the comment on the TrackMetric method. As I haven't used metrics before I thought it be safer to stick with events, but feel free to change it to TrackMetric instead (I'm just not sure about the ramifications of it)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant