private IDictionary <string, string> CreateQueryStringArgsForGaCollect(GaRouteTrackingArg gaRouteArgs)
        {
            var dict = new Dictionary <string, string>
            {
                { "v", "1" },                          // Version
                { "ds", "api" },                       // Data Source
                { "t", "pageview" },                   // Hit Type
                { "tid", _gaTrackingCode },            // Tracking ID
                { "cid", gaRouteArgs.ClientId },       // ClientID
                { "dh", gaRouteArgs.Host },            // Document Host Name
                { "dl", gaRouteArgs.AbsoluteUrl },     // Document Location
                { "ua", gaRouteArgs.UserAgent },       // User-Agent Override
                { "ul", "en-gb" },                     // User Language
                { "an", gaRouteArgs.ApplicationName }, // Application Name
                { "av", gaRouteArgs.ApiVersion },      // Application Version
                { "dp", gaRouteArgs.Path },            // Document Path
                { "dt", $"{gaRouteArgs.CtrlName}-{gaRouteArgs.ActionName}" } // Document Title
            };

            if (!string.IsNullOrEmpty(gaRouteArgs.RemoteIp))
            {
                dict.Add("uip", gaRouteArgs.RemoteIp); // IP Override
            }

            if (!string.IsNullOrEmpty(gaRouteArgs.UrlReferrer))
            {
                dict.Add("dr", gaRouteArgs.UrlReferrer); // Document Referrer
            }

            return(dict);
        }
        public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            var incomingReq = HttpContext.Current.Request;

            RemoveRestrictedHeaders(incomingReq);

            var assembly        = Assembly.GetExecutingAssembly();
            var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
            var assemblyInformationalVersion = fileVersionInfo.ProductVersion;

            var gaRouteArgs = new GaRouteTrackingArg
            {
                ApplicationName        = AppDomain.CurrentDomain.FriendlyName,
                CtrlName               = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                ActionName             = actionContext.ActionDescriptor.ActionName,
                ApiVersion             = assemblyInformationalVersion,
                ClientId               = incomingReq.ServerVariables[OriginHeaderKey] ?? DefaultClientId,
                Host                   = incomingReq.Url.Host,
                RawUrl                 = incomingReq.RawUrl,
                AbsoluteUrl            = incomingReq.Url.AbsoluteUri,
                UrlReferrer            = incomingReq.UrlReferrer?.AbsoluteUri ?? string.Empty,
                RemoteIp               = (incomingReq.IsLocal ? string.Empty : incomingReq.ServerVariables[RemoteAddressHeaderKey]) ?? string.Empty,
                UserAgent              = incomingReq.UserAgent,
                Path                   = incomingReq.ApplicationPath,
                OriginalRequestHeaders = incomingReq.Headers
            };

            _analyticsService.TrackApiCall(gaRouteArgs);

            return(base.OnActionExecutingAsync(actionContext, cancellationToken));
        }
        public async Task TrackApiCall(GaRouteTrackingArg gaRouteArgs)
        {
            try
            {
                var dict = CreateQueryStringArgsForGaCollect(gaRouteArgs);
                var qs   = UrlHelper.ToQueryString(dict);

                var collectUrl = $"{GaCollectPrefixUrl}{qs}";

                _logger.Debug(collectUrl);

                var gaReq = WebRequest.Create(collectUrl);
                gaReq.Headers = new WebHeaderCollection {
                    gaRouteArgs.OriginalRequestHeaders
                };

                await gaReq.GetResponseAsync();
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error with GA collection call.");
            }
        }