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);
}
}
}


