简体   繁体   中英

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

I'm using 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 availbale in .net?

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

source: https://askgif.com/blog/214/how-can-i-log-net-events-and-exceptions-in-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);
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM