GoogleAnalyticsTracker icon indicating copy to clipboard operation
GoogleAnalyticsTracker copied to clipboard

No documentation for setup on ASP.NET Web API

Open vanillajonathan opened this issue 9 years ago • 11 comments

There is no documentation for how to setup GoogleAnalyticsTracker on ASP.NET Web API.

vanillajonathan avatar Apr 21 '16 09:04 vanillajonathan

I'll write down few tips on how to set it up

gandarez avatar Apr 25 '16 16:04 gandarez

Thanks, it would nice with information on how to ensure it is properly setup and actually working though.

And if this can be verified in testing environment too or only in production environment.

vanillajonathan avatar Apr 26 '16 10:04 vanillajonathan

@vanillajonathan you can create an attribute and decorate all classes you need to track or just create a base class and decorate it.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class GoogleAnalyticsTracking : ActionTrackingAttribute
{
    public GoogleAnalyticsTracking() : base("UA-XXXXXXXX-Y")
    { }
}

[GoogleAnalyticsTracking]
public class ApiBase : ApiController { }

gandarez avatar May 15 '16 19:05 gandarez

Yes, but how and what to do to Startup.cs, Global.asax, etc.

How to register it globally, or on all /api/* routes.

vanillajonathan avatar May 21 '16 00:05 vanillajonathan

Do you have an example to share of the above GoogleAnalyticsTrackingAttribute combined with a custom IAnalyticsSession to track user sessions? I don't know where to start with creating the custom IAnalyticsSession let alone combine it with the api controller attribute.

Thanks!

tmulholl avatar Jun 02 '16 00:06 tmulholl

@vanillajonathan you can set it globally like MVC does.

http://stackoverflow.com/questions/8786192/asp-net-mvc-register-action-filter-without-modifying-controller

gandarez avatar Jun 02 '16 01:06 gandarez

@tmulholl you can invoke its base constructor passing Tracker object which naturally accepts an IAnalyticsSession.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class GoogleAnalyticsTracking : ActionTrackingAttribute
{
    public GoogleAnalyticsTracking()
        : base(new Tracker("UA-XXXXXXXX-Y", null, new MyCustomAnalyticsSession(), new AspNetWebApiTrackerEnvironment()))
    {
    }
}

gandarez avatar Jun 02 '16 01:06 gandarez

How is this going? Has any documentation been written yet?

vanillajonathan avatar Jun 02 '16 08:06 vanillajonathan

What should be inside the "MyCustomAnalyticsSession" class?

I am getting this error also. Looks like it takes string, string, string, string.

The best overloaded method match for 'GoogleAnalyticsTracker.WebApi.ActionTrackingAttribute.ActionTrackingAttribute(string, string, string, string)' has some invalid arguments

mikemike396 avatar Nov 07 '16 14:11 mikemike396

This seems like a really cool project... It's just too bad that I have absolutely no idea how to use it, because there is no documentation.

What is an "tracker environment" and how do I create one?

How do I track users?

How do I send a Goal Event?

Tons of potential, just needs documentation!

CyberBill avatar Jun 17 '19 05:06 CyberBill

With GoogleAnalyticsTracker.WebAPI2 I use the following code to setup a global action tracking filter for all controllers in the /api/ path.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
        config.Filters.Add(new ActionTrackingAttribute(
            "UA-XXXXXXX-XX", action => action.ControllerContext.Request.RequestUri.AbsolutePath.StartsWith("/api/")));
    }
}

vanillajonathan avatar Jun 17 '19 11:06 vanillajonathan