Skip to content

Commit

Permalink
Track page duration
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Dec 12, 2024
1 parent 21a062a commit 39a3d34
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
1 change: 1 addition & 0 deletions TinyInsights.TestApp/Platforms/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="30" android:targetSdkVersion="35" />
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Expand Down
4 changes: 2 additions & 2 deletions TinyInsights.TestApp/TinyInsights.TestApp.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net9.0-android;net9.0-ios;net9.0-maccatalyst</TargetFrameworks>
<TargetFrameworks>net9.0-ios;net9.0-maccatalyst;net9.0-android35.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net9.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
Expand Down Expand Up @@ -34,7 +34,7 @@

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">30.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
Expand Down
42 changes: 37 additions & 5 deletions TinyInsights/ApplicationInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,32 @@ public void Initialize()
{
throw new NullReferenceException("Unable to configure `IsAutoTrackPageViewsEnabled` as `Application.Current` is null. You can either set `IsAutoTrackPageViewsEnabled` to false to ignore this issue, or check out this link for a possible reason - https://github.com/dhindrik/TinyInsights.Maui/issues/21");
}
WeakEventHandler<Page> weakHandler = new(OnAppearing);
Application.Current.PageAppearing += weakHandler.Handler;

WeakEventHandler<Page> weakOnAppearingHandler = new(OnAppearing);
Application.Current.PageAppearing += weakOnAppearingHandler.Handler;

WeakEventHandler<Page> weakOnDisappearingHandler = new(OnDisappearing);
Application.Current.PageDisappearing += weakOnDisappearingHandler.Handler;
}

Task.Run(SendCrashes);

IsInitialized = true;
}

private static DateTime? _lastPageAppearing;

private static void OnAppearing(object? sender, Page e)
{
_lastPageAppearing = DateTime.Now;
}

private static void OnDisappearing(object? sender, Page e)
{
var duration = DateTime.Now - _lastPageAppearing;

var pageType = e.GetType();
provider?.TrackPageViewAsync(pageType.FullName ?? pageType.Name, new Dictionary<string, string> { { "DisplayName", pageType.Name } });
provider?.TrackPageViewAsync(pageType.FullName ?? pageType.Name, new Dictionary<string, string> { { "DisplayName", pageType.Name } }, duration);
}

readonly Dictionary<string, string> _globalProperties = [];
Expand Down Expand Up @@ -425,7 +438,7 @@ public async Task TrackEventAsync(string eventName, Dictionary<string, string>?
}
}

public async Task TrackPageViewAsync(string viewName, Dictionary<string, string>? properties = null)
public async Task TrackPageViewAsync(string viewName, Dictionary<string, string>? properties = null, TimeSpan? duration = null)
{
try
{
Expand All @@ -437,7 +450,26 @@ public async Task TrackPageViewAsync(string viewName, Dictionary<string, string>
if (EnableConsoleLogging)
Console.WriteLine($"TinyInsights: tracking page view {viewName}");

Client.TrackPageView(viewName);
var pageView = new PageViewTelemetry(viewName)
{
Timestamp = new DateTimeOffset(_lastPageAppearing ?? DateTime.Now),
};

if (duration is not null)
{
pageView.Duration = duration.Value;
}

if (properties is not null)
{
foreach (var property in properties)
{
pageView.Properties.Add(property.Key, property.Value);
}
}

Client.TrackPageView(pageView);

await Client.FlushAsync(CancellationToken.None);
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion TinyInsights/IInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IInsights

Task TrackErrorAsync(Exception ex, Dictionary<string, string>? properties = null);

Task TrackPageViewAsync(string viewName, Dictionary<string, string>? properties = null);
Task TrackPageViewAsync(string viewName, Dictionary<string, string>? properties = null, TimeSpan? duration = null);

Task TrackEventAsync(string eventName, Dictionary<string, string>? properties = null);
Task TrackErrorAsync(Exception ex, ErrorSeverity severity, Dictionary<string, string>? properties = null);
Expand Down
2 changes: 1 addition & 1 deletion TinyInsights/IInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IInsightsProvider

Task TrackErrorAsync(Exception ex, Dictionary<string, string>? properties = null);

Task TrackPageViewAsync(string viewName, Dictionary<string, string>? properties = null);
Task TrackPageViewAsync(string viewName, Dictionary<string, string>? properties = null, TimeSpan? duration = null);

Task TrackEventAsync(string eventName, Dictionary<string, string>? properties = null);

Expand Down
4 changes: 2 additions & 2 deletions TinyInsights/Insights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public Task TrackErrorAsync(Exception ex, ErrorSeverity severity, Dictionary<str
return Task.CompletedTask;
}

public Task TrackPageViewAsync(string viewName, Dictionary<string, string>? properties = null)
public Task TrackPageViewAsync(string viewName, Dictionary<string, string>? properties = null, TimeSpan? duration = null)
{
var tasks = new List<Task>();

foreach (var provider in insightsProviders.Where(x => x.IsTrackPageViewsEnabled))
{
var task = provider.TrackPageViewAsync(viewName, properties);
var task = provider.TrackPageViewAsync(viewName, properties, duration);
tasks.Add(task);
}

Expand Down

0 comments on commit 39a3d34

Please sign in to comment.