diff --git a/README.md b/README.md index 223fed8..2506f4b 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,20 @@ public MainViewModel(IInsights insights) this.insights = insights; } ``` + +#### Track sessions +When the app starts, a new session will be created. To create a new session, you should call the method below in the ***OnResume*** method in the ***App.xaml.cs*** file. This will create a new session every time the user is returning to the app. + +```csharp +protected override void OnResume() +{ + base.OnResume(); + + var insights = serviceProvider.GetRequiredService(); + insights.CreateNewSession(); +} +``` + #### Track page views ```csharp await insights.TrackPageViewAsync("MainView"); diff --git a/TinyInsights.TestApp/App.xaml.cs b/TinyInsights.TestApp/App.xaml.cs index 57a297e..4535032 100644 --- a/TinyInsights.TestApp/App.xaml.cs +++ b/TinyInsights.TestApp/App.xaml.cs @@ -4,14 +4,25 @@ namespace TinyInsights.TestApp; public partial class App : Application { private readonly AppShell shell; + private readonly IServiceProvider serviceProvider; - public App(AppShell shell) + public App(AppShell shell, IServiceProvider serviceProvider) { InitializeComponent(); this.shell = shell; + this.serviceProvider = serviceProvider; } + protected override void OnResume() + { + base.OnResume(); + + var insights = serviceProvider.GetRequiredService(); + insights.CreateNewSession(); + } + + protected override Window CreateWindow(IActivationState? activationState) { return new Window(shell); diff --git a/TinyInsights/ApplicationInsightsProvider.cs b/TinyInsights/ApplicationInsightsProvider.cs index 15e1b13..777b34b 100644 --- a/TinyInsights/ApplicationInsightsProvider.cs +++ b/TinyInsights/ApplicationInsightsProvider.cs @@ -153,6 +153,7 @@ private static void OnAppearing(object? sender, Page e) client.Context.GlobalProperties.TryAdd("AppVersion", AppInfo.VersionString); client.Context.GlobalProperties.TryAdd("AppBuildNumber", AppInfo.BuildString); client.Context.GlobalProperties.TryAdd("OperatingSystemVersion", DeviceInfo.VersionString); + client.Context.Session.Id = Guid.NewGuid().ToString(); return client; @@ -243,6 +244,16 @@ public string GenerateNewAnonymousUserId() return userId; } + public void CreateNewSession() + { + if (Client is null) + { + return; + } + + Client.Context.Session.Id = Guid.NewGuid().ToString(); + } + private async Task SendCrashes() { try diff --git a/TinyInsights/IInsights.cs b/TinyInsights/IInsights.cs index 90972bc..d26c206 100644 --- a/TinyInsights/IInsights.cs +++ b/TinyInsights/IInsights.cs @@ -25,4 +25,5 @@ Task TrackDependencyAsync(string dependencyType, string dependencyName, string d void OverrideAnonymousUserId(string userId); void GenerateNewAnonymousUserId(); + void CreateNewSession(); } \ No newline at end of file diff --git a/TinyInsights/IInsightsProvider.cs b/TinyInsights/IInsightsProvider.cs index 80e6b42..9d3ceb6 100644 --- a/TinyInsights/IInsightsProvider.cs +++ b/TinyInsights/IInsightsProvider.cs @@ -27,4 +27,5 @@ public interface IInsightsProvider void OverrideAnonymousUserId(string userId); string GenerateNewAnonymousUserId(); + void CreateNewSession(); } \ No newline at end of file diff --git a/TinyInsights/Insights.cs b/TinyInsights/Insights.cs index eb9a9af..d75bbbf 100644 --- a/TinyInsights/Insights.cs +++ b/TinyInsights/Insights.cs @@ -131,7 +131,7 @@ public Dependency CreateDependencyTracker(string dependencyType, string dependen public void OverrideAnonymousUserId(string userId) { - foreach (var provider in insightsProviders.Where(x => x.IsTrackDependencyEnabled)) + foreach (var provider in insightsProviders) { provider.OverrideAnonymousUserId(userId); } @@ -139,9 +139,17 @@ public void OverrideAnonymousUserId(string userId) public void GenerateNewAnonymousUserId() { - foreach (var provider in insightsProviders.Where(x => x.IsTrackDependencyEnabled)) + foreach (var provider in insightsProviders) { provider.GenerateNewAnonymousUserId(); } } + + public void CreateNewSession() + { + foreach (var provider in insightsProviders) + { + provider.CreateNewSession(); + } + } } \ No newline at end of file