Skip to content

feat: add Aptabase.Core #12

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions .github/workflows/nuget_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,60 @@ on:
workflow_dispatch:

jobs:
build:
build-maui:
runs-on: windows-latest
name: Update NuGet package
name: Update Aptabase.Maui NuGet package
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup .NET Core @ Latest
uses: actions/setup-dotnet@v4

- name: Setup JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
java-version: '11'
distribution: 'adopt'

- name: Extract Version from csproj
id: get_version
run: |
$version = Select-String -Path "./src/Aptabase.Maui.csproj" -Pattern '<Version>(.*)</Version>' | ForEach-Object { $_.Matches.Groups[1].Value }
$version = Select-String -Path "./src/Aptabase.Maui/Aptabase.Maui.csproj" -Pattern '<Version>(.*)</Version>' | ForEach-Object { $_.Matches.Groups[1].Value }
echo "PackageVersion=$version" | Out-File -Append -FilePath $env:GITHUB_ENV
shell: powershell

- name: Build and Publish
- name: Build and Publish Aptabase.Maui
run: |
cd ./src/
cd ./src/Aptabase.Maui
dotnet restore Aptabase.Maui.csproj
dotnet pack Aptabase.Maui.csproj -c Release -o artifacts -p:PackageVersion=${{ env.PackageVersion }}

- name: Push
run: dotnet nuget push ./src/artifacts/Aptabase.Maui.${{ env.PackageVersion }}.nupkg -k ${{ secrets.NUGET_APIKEY }} -s https://api.nuget.org/v3/index.json
- name: Push Aptabase.Maui Package
run: dotnet nuget push ./src/Aptabase.Maui/artifacts/Aptabase.Maui.${{ env.PackageVersion }}.nupkg -k ${{ secrets.NUGET_APIKEY }} -s https://api.nuget.org/v3/index.json

build-core:
runs-on: ubuntu-latest
name: Build and Publish Aptabase.Core NuGet package
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup .NET Core @ Latest
uses: actions/setup-dotnet@v4

- name: Extract Version from csproj
id: get_version_core
run: |
$version = Select-String -Path "./src/Aptabase.Core/Aptabase.Core.csproj" -Pattern '<Version>(.*)</Version>' | ForEach-Object { $_.Matches.Groups[1].Value }
echo "PackageVersion=$version" | Out-File -Append -FilePath $env:GITHUB_ENV
shell: bash

- name: Build and Publish Aptabase.Core
run: |
cd ./src/Aptabase.Core
dotnet restore Aptabase.Core.csproj
dotnet pack Aptabase.Core.csproj -c Release -o artifacts -p:PackageVersion=${{ env.PackageVersion }}

- name: Push Aptabase.Core Package
run: dotnet nuget push ./src/Aptabase.Core/artifacts/Aptabase.Core.${{ env.PackageVersion }}.nupkg -k ${{ secrets.NUGET_APIKEY }} -s https://api.nuget.org/v3/index.json
94 changes: 54 additions & 40 deletions Aptabase.MAUI.sln → Aptabase.NET.sln
Original file line number Diff line number Diff line change
@@ -1,40 +1,54 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33530.505
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aptabase.Maui", "src\Aptabase.Maui.csproj", "{CDBA84BA-F326-4162-B509-31892E9CAF2E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HelloWorld", "test\HelloWorld\HelloWorld.csproj", "{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8BFFEF39-1EA0-4D6D-9988-322A74A6558D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{19E5FDF2-25B6-48AA-BB1B-8BBA9E8323E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CDBA84BA-F326-4162-B509-31892E9CAF2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDBA84BA-F326-4162-B509-31892E9CAF2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDBA84BA-F326-4162-B509-31892E9CAF2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDBA84BA-F326-4162-B509-31892E9CAF2E}.Release|Any CPU.Build.0 = Release|Any CPU
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CDBA84BA-F326-4162-B509-31892E9CAF2E} = {8BFFEF39-1EA0-4D6D-9988-322A74A6558D}
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E} = {19E5FDF2-25B6-48AA-BB1B-8BBA9E8323E1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C696E39C-8A3B-4FE3-BBB1-95FE24E67DCD}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33530.505
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aptabase.Maui", "src\Aptabase.Maui\Aptabase.Maui.csproj", "{CDBA84BA-F326-4162-B509-31892E9CAF2E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aptabase.Core", "src\Aptabase.Core\Aptabase.Core.csproj", "{D6DE2CBA-F461-4E04-9A96-6C0EE7E32B9E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HelloMaui", "test\HelloMaui\HelloMaui.csproj", "{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8BFFEF39-1EA0-4D6D-9988-322A74A6558D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{19E5FDF2-25B6-48AA-BB1B-8BBA9E8323E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld", "test\HelloWorld\HelloWorld.csproj", "{E6EB4FC9-CF24-4735-B8DB-EC78674A94F1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CDBA84BA-F326-4162-B509-31892E9CAF2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDBA84BA-F326-4162-B509-31892E9CAF2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDBA84BA-F326-4162-B509-31892E9CAF2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDBA84BA-F326-4162-B509-31892E9CAF2E}.Release|Any CPU.Build.0 = Release|Any CPU
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E}.Release|Any CPU.Build.0 = Release|Any CPU
{D6DE2CBA-F461-4E04-9A96-6C0EE7E32B9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6DE2CBA-F461-4E04-9A96-6C0EE7E32B9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6DE2CBA-F461-4E04-9A96-6C0EE7E32B9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6DE2CBA-F461-4E04-9A96-6C0EE7E32B9E}.Release|Any CPU.Build.0 = Release|Any CPU
{E6EB4FC9-CF24-4735-B8DB-EC78674A94F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6EB4FC9-CF24-4735-B8DB-EC78674A94F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6EB4FC9-CF24-4735-B8DB-EC78674A94F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6EB4FC9-CF24-4735-B8DB-EC78674A94F1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CDBA84BA-F326-4162-B509-31892E9CAF2E} = {8BFFEF39-1EA0-4D6D-9988-322A74A6558D}
{E9D22AE5-0B04-4ED6-B1BC-4242BE3D321E} = {19E5FDF2-25B6-48AA-BB1B-8BBA9E8323E1}
{D6DE2CBA-F461-4E04-9A96-6C0EE7E32B9E} = {8BFFEF39-1EA0-4D6D-9988-322A74A6558D}
{E6EB4FC9-CF24-4735-B8DB-EC78674A94F1} = {19E5FDF2-25B6-48AA-BB1B-8BBA9E8323E1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C696E39C-8A3B-4FE3-BBB1-95FE24E67DCD}
EndGlobalSection
EndGlobal
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## 0.2.2

* Fix JsonElement not being included in AptabaseContext for NativeAOT.

## 0.2.1

* Add NativeAOT support by using JsonSerializer source generator API.

## 0.2.0

* Add `Aptabase.Core` for generic `.NET 8` support (Does not support crash reporter)

## 0.1.0

* Add `EnablePersistence` to persist events on disk before sending them to the server
Expand Down
47 changes: 44 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Aptabase](https://raw.githubusercontent.com/aptabase/aptabase-com/main/public/og.png)

# MAUI SDK for Aptabase
# .NET SDK for Aptabase

[![NuGet](https://img.shields.io/nuget/v/Aptabase.Maui)](https://www.nuget.org/packages/Aptabase.Maui)
[![GitHub](https://img.shields.io/github/license/aptabase/aptabase-maui)](https://github.com/aptabase/aptabase-maui/blob/main/LICENSE)
Expand All @@ -12,10 +12,51 @@ Instrument your apps with Aptabase, an Open Source, Privacy-First and, Simple An
Start by adding the Aptabase NuGet package to your .csproj:

```xml
<PackageReference Include="Aptabase.Maui" Version="0.1.0" />
<PackageReference Include="Aptabase.Core" Version="0.2.0" />
```

## Usage
Or, if you're using MAUI

```xml
<PackageReference Include="Aptabase.Maui" Version="0.2.0" />
```

## Usage (.NET)

First, you need to get your `App Key` from Aptabase, you can find it in the `Instructions` menu on the left side menu.

Change your `Program.cs` to add Aptabase:

```csharp
// Create a ServiceCollection
var services = new ServiceCollection();

services.AddLogging(); // If you haven't registered a logger yet

// Add Aptabase to the service collection
services.UseAptabase("<YOUR_APP_KEY>", new AptabaseOptions
{
#if DEBUG
IsDebugMode = true,
#else
IsDebugMode = false,
#endif
EnableCrashReporting = false, // ❌ Not supported with Aptabase.Core, only Aptabase.Maui
EnablePersistence = true,
});

// ... Register other services you need ...

// Build the service provider
var serviceProvider = services.BuildServiceProvider();

// Get an instance of the Aptabase service (if you need it directly)
var aptabaseClient = serviceProvider.GetRequiredService<IAptabaseClient>();
...
}
```

## Usage (Maui)

First, you need to get your `App Key` from Aptabase, you can find it in the `Instructions` menu on the left side menu.

Expand Down
33 changes: 33 additions & 0 deletions src/Aptabase.Core/Aptabase.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>Aptabase.Core</PackageId>
<Version>0.2.2</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<TargetFramework>net8.0</TargetFramework>
<Description>Dotnet SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps</Description>
<Authors>Aptabase Team</Authors>
<Company>Aptabase</Company>
<PackageProjectUrl>https://aptabase.com</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>logo.png</PackageIcon>
<RepositoryUrl>https://github.com/aptabase/aptabase-dotnet</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>analytics, privacy, dotnet, sdk, telemetry</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
<None Include="..\..\etc\logo.png" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="DotNext.Threading" Version="5.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Logging;
using System.Threading.Channels;

namespace Aptabase.Maui;
namespace Aptabase.Core;

public class AptabaseClient : IAptabaseClient
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.Extensions.Logging;
using System.Net;
using System.Net;
using System.Net.Http.Json;
using System.Reflection;
using Microsoft.Extensions.Logging;

namespace Aptabase.Maui;
namespace Aptabase.Core;

internal class AptabaseClientBase : IAsyncDisposable
{
Expand All @@ -20,7 +20,7 @@ internal class AptabaseClientBase : IAsyncDisposable
{
{ "US", "https://us.aptabase.com" },
{ "EU", "https://eu.aptabase.com" },
{ "DEV", DeviceInfo.Platform == DevicePlatform.Android ? "https://10.0.2.2:3000" : "https://localhost:3000" },
{ "DEV", OperatingSystem.IsAndroid() ? "https://10.0.2.2:3000" : "https://localhost:3000" },
{ "SH", "" },
};

Expand Down Expand Up @@ -65,7 +65,7 @@ internal async Task TrackEvent(EventData eventData)
eventData.SessionId = _sessionId;
eventData.SystemProps = _sysInfo;

var body = JsonContent.Create(eventData);
var body = JsonContent.Create(eventData, AptabaseContext.Default.EventData);

var response = await _http.PostAsync("/api/v0/event", body);

Expand All @@ -81,7 +81,8 @@ internal async Task TrackEvent(EventData eventData)

var responseBody = await response.Content.ReadAsStringAsync();

_logger?.LogError("Failed to perform TrackEvent due to {StatusCode} and response body {Body}", response.StatusCode, responseBody);
_logger?.LogError("Failed to perform TrackEvent due to {StatusCode} and response body {Body}",
response.StatusCode, responseBody);
}
}

Expand Down Expand Up @@ -119,7 +120,8 @@ private static string NewSessionId()
{
if (string.IsNullOrEmpty(options?.Host))
{
_logger?.LogWarning("Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.");
_logger?.LogWarning(
"Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.");

return null;
}
Expand All @@ -129,4 +131,4 @@ private static string NewSessionId()

return _hosts[region];
}
}
}
38 changes: 38 additions & 0 deletions src/Aptabase.Core/AptabaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Aptabase.Core;


public static class AptabaseExtensions
{
public static IServiceCollection UseAptabase(this IServiceCollection services, string appKey, AptabaseOptions? options = null)
{
services.AddSingleton<IAptabaseClient>(serviceProvider =>
{
IAptabaseClient client;
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();

if (options?.EnablePersistence != true)
{
client = new AptabaseClient(appKey, options, loggerFactory.CreateLogger<AptabaseClient>());
}
else
{
client = new AptabasePersistentClient(appKey, options, loggerFactory.CreateLogger<AptabasePersistentClient>());
}

if (options?.EnableCrashReporting == true)
{
throw new NotImplementedException("Crash reporting is only for Aptabase.Maui");
}

return client;
});

return services;
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Aptabase.Maui;
namespace Aptabase.Core;

/// <summary>
/// Initialization options for the Aptabase Client
Expand Down
Loading