2016-08-17 24 views
0

我使用下面的代碼來跟蹤我使用JavaScript事件:如何在Google分析中記錄.net事件和例外情況?

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

相同的實現,我想在我的C#代碼,在那裏我可以登錄我的谷歌事件,異常的事情。 在.net中是否有任何預建庫可用?

回答

0

我已經在Logger.Net上擴展自己的類來登錄文件系統和谷歌分析。

你可以使用下面的類:如果你連接什麼GoogleTracking/GoogleEvent命名空間和在哪裏可以找到他們

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

將是有益的。我無法在nuget搜索中找到它們。你能幫助我嗎? – rolls

+1

@rolls GoogleAnalyticsDotNet是包。我也必須尋找它。 –

+1

並確保將''添加到您的配置中,否則它將不起作用。 –

相關問題