AttackPattern / CSharpAnalytics

Google Analytics for Windows 8, Windows Phone & desktop applications
142 stars 36 forks source link

ASP.NET support #21

Closed Jericho closed 9 years ago

Jericho commented 10 years ago

Has anybody considered adding support for ASP.NET projects so we track our server-side C# code in webform and/or MVC applications?

damieng commented 10 years ago

It's certainly possible although you'd likely want to exclude a bunch of tracking stats such as screen resolution etc.

Background requests could be dispatched to GA with QueueBackgroundWorkItem and you'd have to decide whether you wanted the concept of a user/session. If so you'd need to fake that out - the simplest would be to hash the session id and not worry about returning/new visitors or set your own cookie.

I'd start with what I wanted to see in GA and how it would look and work back from there.

ddobrev commented 9 years ago

Hello @damieng . I need GA for ASP.NET too but I am afraid I don't really understand your comment. Is it or is it not possible to use CSharpAnalytics in ASP.NET projects (ASP.NET MVC in particular)? I can see there's a BaseAutoMeasurement sub-class per platform but there isn't one for ASP.NET.

damieng commented 9 years ago

What information are you trying to track in GA that you would want to send from the server via MVC?

If you want to track visitor data like screen resolution etc. then putting the GA Javascript code in your page is the best option. The reason for this is that a web server simply doesn't have access to a lot of this information (screen res, flash version, java installed, visitor cookie, etc)

If instead you wanted to track say WebAPI usage and didn't care about visitor specific data like screen res and visitor/new user then all you'd need to do is to create a class that would dispatch the background queue to GA.

ddobrev commented 9 years ago

@damieng I need to track arbitrary info using GA events (an example at https://developers.google.com/analytics/devguides/collection/android/v4/events).

damieng commented 9 years ago

If all the data you want is server side all you really need is an implementation of the background request sender that uses ASP.NET's QueueBackgroundWorkItem infrastructure and a bit of code to handle starting that up and shutting it down by hooking into the ASP.NET event system.

ddobrev commented 9 years ago

@damieng are you talking about the implementation of BaseAutoMeasurement or do I have to make other changes as well?

damieng commented 9 years ago

The point of AutoMeasurement is to provide a simple one-stop automatic system for that platform. If there are no events built-in to ASP.NET MVC you want to track (app start, session start, request start etc). then it's not quite what you want.

What you need are two classes:

  1. A class to send the requests in background. This is based on BackgroundUriRequester but would use QueueBackgroundWorkItem instead of Task.Run
  2. A class to stop and start that queue as well as provide a global entry point in. This could be based on one of the AutoMeasurement classes but without the auto-event stuff or the offline storage of unsent requests.
ddobrev commented 9 years ago

I think what I need is simpler than either. I only need to construct an event - category, label, action - and manually send it - for example, on a clicked button. Here is a code example (for Android): https://developers.google.com/analytics/devguides/collection/android/v4/events#implementation . Can CSharpAnalytics do it?

damieng commented 9 years ago

You can create a URL for an event quite easily.

  1. Create an instance of MeasurementUriBuilder with configuration, session manager and fake/dummy IEnvironment implementation. You can keep this around on a static if you like to make it easier to use.
  2. Call the BuildUri method on that builder passing in a new EventActivity with the details you want to track.
  3. Make a http request with that Url.

e.g.

// Get tracker
var t = new MeasurementUriBuilder(myConfiguration, new SessionManager(), new MyEnvironment());
// Build an Event URI
var uri = t.BuildUri(new EventActivity("Hello", "There"));
// TODO: Make a HTTP request to that Uri without blocking this request
ddobrev commented 9 years ago

@damieng thank you very much, I'll definitely try it.

ddobrev commented 9 years ago

I've just seen SessionManager does not have an empty constructor. What SessionState and sample rate do I need to pass?

damieng commented 9 years ago

Just pass null and 100.0m into the constructor

ddobrev commented 9 years ago

Thank you.