Google Analytics .Net SDK Exception


Please Share

Google AnalyticsAre you working on a .Net core console application. Have you been wondering how you can send exceptions or error logging to Google Analytics from your project? Well your in luck using the Google Analytics .Net sdk You can have Google analytics exception tracking added to your project in a matter of minutes.

Setup

For this project I recommend creating a mobile Google Analytics account. Unfortunately Google thinks that all Google Analytics mobile accounts should be routed though Firebase. We don’t want to do that, so here is the work around. In Google Analytics create a property and chooses type web. After you have done that go to the views tab and create a new view you can create one of type mobile here and not have it linked to firebase. Note your web property id it should look something like this UA-123456789-1 you will need it soon

NuGet Package

Create a new console application and add the NuGet package Google Analytics .Net sdk This will add everything you need to track your application.

Code

Before we can send our Exception hits you need to create yourself a tracker. You only need one tracker, this tracker will be used to send hits to Google analytics from your application.

Tracker

Here is where you will need your web property id from Google analytics. As well as some basic information about your application.

private const string DemoDataApplicationWebProplerty = "UA-123456-1";
private const string Applicationname = "QuickStart core Events";
private const string ApplicationVersion = "1.0";
private const string ApplicationId = "1.0.0";
var tracker = TrackerBuilder.BuildMobileTracker(DemoDataApplicationWebProplerty, Applicationname, ApplicationVersion, ApplicationId);



You are now ready to track Google Analytics data.

Exception hit

Exception tracking allows you to measure the number and type of crashes or errors that occur on your property.

public static async Task  SendAsync(ITracker tracker, string exceptionDescription, bool fatal)
        {
            var hit = new ExceptionHit(exceptionDescription, fatal)
            {                
                DataSource = "app",
            };

            // create the hit request.
            var request = (HitRequestBase)tracker.CreateHitRequest(hit);

            // Run a debug check to ensure its valid.
            var debugResponse = await request.ExecuteDebugAsync();
            if (!((DebugResult)debugResponse).IsValid()) return false;
            
            // Send hit.
            var collectRequest = await request.ExecuteCollectAsync();
            Console.Write(collectRequest.RawResponse);
            return true;
        }

There are two different end points we can send to with the measurement protocol. The first is the debug endpoint which allows us to check that our request is correct before actually sending it to the collect endpoint. The debug end point does not log data to Google analytics its just here to tell use if its a valid hit. Also the debug endpoint is not perfect you can get a false positive.

The collect endpoint is the end point that actually registers hits to Google analytics. The collect end point does not return an error if there is anything wrong with your hit.

Putting it all together

Here we build our tracker and then send a hit to google analytics.

 class Program
    {
        private const string DemoDataApplicationWebProplerty = "UA-53766825-1";
        private const string Applicationname = "QuickStart core";
        private const string ApplicationVersion = "1.0";
        private const string ApplicationId = "1.0.0";

        public static async Task Main(string[] args)
        {
            Console.WriteLine("Hello Google Analatics SDK!");
            var tracker = TrackerBuilder.BuildMobileTracker(DemoDataApplicationWebProplerty, Applicationname, ApplicationVersion, ApplicationId);
            
            if (!await ExceptionHitHelper.SendAsync(tracker, "Something Failed", true))
            {
                Console.WriteLine("Send Hit Failed");
                return 1;
            }

            Console.WriteLine("Hit Sent");
            return 0;
        }
    }

GitHub Sample Project

The sample project for this and all the other Quick starts for the Google Analytics .net SDK can be found on Github Google.Analytics.SDK.QuickStarts

Conclusion

Sending hits to Google analytics from a .net console application is quite strait forward using the Google Analytics .Net sdk

You can have a little fun with this code and run it in a for loop.

Please Share


Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.