How can I log .net events and exceptions in google analytics?
💻 coding

How can I log .net events and exceptions in google analytics?

2 min read 369 words
2 min read
ShareWhatsAppPost on X
  • 1To log .NET events and exceptions in Google Analytics, a custom logging class can be created extending Logger.Net.
  • 2The implementation involves checking configuration settings to determine if Google Analytics logging is enabled before sending logs.
  • 3The provided code snippet demonstrates how to track events and errors using Google Analytics in a C# application.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"To log .NET events and exceptions in Google Analytics, a custom logging class can be created extending Logger.Net."

How can I log .net events and exceptions in google analytics?

How to log .net events and exceptions in google analytics?

I am having a requirement of logging the events as well as exceptions in my dot net application. I tried so many ways but none is working in dot net. I want to implement the same way am using in my javascript application.

I'm using the following code for tracking my event using javascript :

(function () {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', googleAnalyticsAppID, 'auto', { 'cookieDomain': 'none' });
ga('require', 'displayfeatures');
ga('send', 'pageview');
})();

window.onload = function () {
$('.googleAnalytics').click(function () {
 var event = this.getAttribute("ga-event");
 var category = this.getAttribute("ga-category");
 var label = this.getAttribute("ga-label"); 
 gaWeb(category, event, label); 
});
};

function gaWeb(category, event, label) {
 ga('send', 'event', category, event, label);
}

function gaPageView(page, title) {
 ga('send', 'pageview', {
 'page': page,
 'title': title
});
}

The same implementation I want to do in my C# coding, where I can log my exception in google events. is there any prebuild library available in .net?

Solution:

I've extended my own class over Logger.Net to log in both file system and google analytics.

you can use following class :

namespace M2E.Common.Logger
{
public class Logger : ILogger
{
 private string _currentClassName; 
 bool GALoggin;
 ILog logger = null;
 public Logger(string currentClassName)
 {
 this._currentClassName = currentClassName;
 GALoggin = Convert.ToBoolean(ConfigurationManager.AppSettings["GALogging"]);

 logger = LogManager.GetLogger(_currentClassName);
 BasicConfigurator.Configure();
 log4net.Config.XmlConfigurator.Configure();

 }

 public void Info(string message)
 {
 if (GALoggin && Convert.ToBoolean(ConfigurationManager.AppSettings["GAInfoLogging"]))
 { 
 TrackGoogleEvents("Logger-Info", "Info", message); 
 }
 else
 {
 logger.Info(message);
 }
 }

 public void Error(string message, Exception ex)
 {
 if (GALoggin)
 {
 TrackGoogleEvents("Logger-Error", message, ex.Message.ToString(CultureInfo.InvariantCulture));
 }
 else
 {
 logger.Error(message, ex);
 }
 try
 {
 SendAccountCreationValidationEmail.SendExceptionEmailMessage(
 ConfigurationManager.AppSettings["ExceptionsSendToEmail"].ToString(CultureInfo.InvariantCulture),ex.Message);
 }
 catch (Exception)
 {

 }
 }

 public void Debug(string message, Exception ex)
 {
 if (GALoggin)
 {
 TrackGoogleEvents("Logger-Debug", message, ex.Message.ToString(CultureInfo.InvariantCulture));
 }
 else
 {
 logger.Debug(message, ex);
 } 
 }

 public void Fatal(string message, Exception ex)
 {
 if (GALoggin)
 {
 TrackGoogleEvents("Logger-Fatal", message, ex.Message.ToString(CultureInfo.InvariantCulture));
 }
 else
 {
 logger.Fatal(message, ex);
 } 
 }

 private void TrackGoogleEvents(string category, string action, string label)
 {
 try
 {
 AsyncTrackGoogleEvents(category, action, label); // to make it async call if required..
 }
 catch (Exception ex)
 {
 logger.Fatal("Google Analytics Event Tracking Exception", ex);
 }

 }

 public void AsyncTrackGoogleEvents(string category, string action, string label)
 {
 var googleEvent = new GoogleEvent("MadeToEarn.com", category, action, label, 1);
 var requestEvent = new RequestFactory().BuildRequest(googleEvent);
 GoogleTracking.FireTrackingEvent(requestEvent);
 }
}
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 4 January 2019 · 2 min read · 369 words

Part of AskGif Blog · coding

You might also like