Segment.IO provides a nice server-oriented library in Analytics-CSharp. This project wraps that to provide some standard behavior needed by .Net desktop applications:
- Supplies guids for user ids, saves that in a Settings file
- Looks up the machine's external IP address or geolocation and sends that along
- Set $Browser to the Operating System Version (e.g. "Windows 10").
- Auto events
- First launch ("Create" to fit MixPanel's expectation)
- Version Upgrade
PM> Install-Package DesktopAnalytics
using (new Analytics("mySegmentIOSecret"), userInfo)
{
// other setup, and eventually
Application.Run();
}
If you want to go beyond anonymous users, you can feed information about your user into DA like this:
var userInfo = new UserInfo()
{
FirstName = "John",
LastName = "Smith",
Email="[email protected]",
UILanguageCode= "fr"
};
userInfo.OtherProperties.Add("FavoriteColor","blue");
using (new Analytics("mySegmentIOSecret"), userInfo)
{
// other setup, and eventually
Application.Run();
}
If you have a way of letting users (or testers) disable tracking, pass that value as the second argument:
using (new Analytics("mySegmentIOSecret", allowTracking))
In this example, we use an environment variable so that testers and developers don't get counted:
#if DEBUG
//always track if this is a debug built, but track to a different segment.io project
using (new Analytics("(the secret for the debug version)"))
#else
// if this is a release build, then allow an envinroment variable to be set to false
// so that testers aren't generating false analytics
string feedbackSetting = System.Environment.GetEnvironmentVariable("FEEDBACK");
var allowTracking = string.IsNullOrEmpty(feedbackSetting) || feedbackSetting.ToLower() == "yes" || feedbackSetting.ToLower() == "true";
using (new Analytics("(the secret for the release version)", RegistrationDialog.GetAnalyticsUserInfo(), allowTracking))
{
// other setup, and eventually
Application.Run();
}
#endif
Wherever you want to register that something happened, call Track on the static object named "Analytics":
Analytics.Track("Create New Image");
If you have properties you need to record, add them by passing in a Dictionary<string, string>, like this:
Analytics.Track("Save PDF", new Dictionary<string, string>() {
{"PageCount", pageCount},
{"Layout", "A4Landscape"}
});
Analytics.ReportException(error);
If you've also got LibPalaso in your app, hook up its ExceptionHandler like this:
ExceptionHandler.AddDelegate((w,e) => DesktopAnalytics.Analytics.ReportException(e.Exception));
The project is currently built for .net 4 client profile. If you get the solution, nuget should auto-restore the two dependencies when you build; they are not part of the source tree.
When changes are merged to master a github action will run dotnet pack and publish to Nuget. The publishing task can also be triggered by pushing a tag to the master branch.
MIT Licensed (As are the dependencies, Analytics.Net and Json.Net).
DesktopAnalytics.net determines the user's ip by querying a website. Previously, it used ipecho.net, but that has gone down. It is currently using icanhazip.com. If that goes down too, we can change again. Your client can also use any other service you want by setting UrlThatReturnsExternalIpAddress
.