This project provides basic API integration for using Google Analytics track event from .NET applications.
Supported Platform : UWP, .NET(4.5.1)
- Make basic calls to https://developers.google.com/analytics/devguides/collection/protocol/v1/ and track events
Step 1. install nuget:
- UWP: https://www.nuget.org/packages/GoogleAnalyticsClientDotNet.Universal/
- WPF: https://www.nuget.org/packages/GoogleAnalyticsClientDotNet.Net45/
Step 2. new the AnalyticsService instance.
AnalyticsService service = new AnalyticsService();
service.Initialize("{tracking id}", "{appName}", "{appId}", "{appVersion}");
Step 3. new the EventParameter, and set properties.
EventParameter eventData = new EventParameter();
eventData.Category = "";
eventData.Action = "";
eventData.Label = "";
eventData.ScreenName = "";
eventData.ClientId = "";
eventData.UserAgent = deviceService.ModelName;
service.TrackEvent(eventData);
Step 4. For WPF, need call method: SaveTempEventsData() to keep not upload events.
private async void MainWindow_Closed(object sender, EventArgs e)
{
await service?.SaveTempEventsData();
}
Step 5. For Universal Windows Platform. User need to add SaveTempEventsData method in the Suspened event.
/// <summary>
/// Invoked when application execution is being suspended. Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
await service.SaveTempEventsData();
deferral.Complete();
}