Skip to content

Commit

Permalink
Set SessionId
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Oct 18, 2024
1 parent 12efbfb commit 67cd060
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<IInsights>();
insights.CreateNewSession();
}
```

#### Track page views
```csharp
await insights.TrackPageViewAsync("MainView");
Expand Down
13 changes: 12 additions & 1 deletion TinyInsights.TestApp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IInsights>();
insights.CreateNewSession();
}


protected override Window CreateWindow(IActivationState? activationState)
{
return new Window(shell);
Expand Down
11 changes: 11 additions & 0 deletions TinyInsights/ApplicationInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions TinyInsights/IInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ Task TrackDependencyAsync(string dependencyType, string dependencyName, string d
void OverrideAnonymousUserId(string userId);

void GenerateNewAnonymousUserId();
void CreateNewSession();
}
1 change: 1 addition & 0 deletions TinyInsights/IInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ public interface IInsightsProvider
void OverrideAnonymousUserId(string userId);

string GenerateNewAnonymousUserId();
void CreateNewSession();
}
12 changes: 10 additions & 2 deletions TinyInsights/Insights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,25 @@ 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);
}
}

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();
}
}
}

0 comments on commit 67cd060

Please sign in to comment.