<commit_before>﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web.Mvc;

namespace NuGetGallery.Filters
{
  public class GoogleAnalyticsMeasurementProtocol : ActionFilterAttribute
  {
    private static readonly Uri GA_ENDPOINT = new Uri("https://www.google-analytics.com/collect");

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
      var gaPropertyId = ConfigurationManager.AppSettings["Gallery.GoogleAnalyticsPropertyId"];
      if (string.IsNullOrEmpty(gaPropertyId))
        return;
      var webClient = new WebClient
      {
        Headers =
        {
          { HttpRequestHeader.UserAgent, filterContext.HttpContext.Request.UserAgent },
          { "X-Forwarded-For", filterContext.HttpContext.Request.UserHostAddress }
        }
      };

      var baseParameters = new NameValueCollection
      {
        { "v",   "1" },
        { "tid", gaPropertyId },
        { "cid", filterContext.HttpContext.Request.Headers["X-NuGet-ApiKey"] ?? filterContext.HttpContext.Request.Headers["NuGet-ApiKey"]  ?? Guid.NewGuid().ToString() },
        { "ni",  "1" },
      };
      
      var viewParameters = new NameValueCollection(baseParameters)
      {
        { "t",  "pageview" },
        { "dl", filterContext.HttpContext.Request.RawUrl },
      };
      webClient.UploadValuesAsync(GA_ENDPOINT, viewParameters);

      var packageId = string.Join(",", 
        new [] { filterContext.RouteData.Values["id"], filterContext.RouteData.Values["version"] }
          .Select(_ => _ != null)
          .Select(_ => Convert.ToString(_)));

      var eventParameters = new NameValueCollection(baseParameters)
      {
        { "t",  "event" },
        { "ec", "NuGet" },
        { "ea", filterContext.HttpContext.Request.Headers["NuGet-Operation"] ?? "Unknown" },
        { "ev", packageId },
      };
      webClient.UploadValuesAsync(GA_ENDPOINT, eventParameters);
    }
  }
}<commit_msg>Sync GA info posting<commit_after>﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web.Mvc;

namespace NuGetGallery.Filters
{
  public class GoogleAnalyticsMeasurementProtocol : ActionFilterAttribute
  {
    private static readonly Uri GA_ENDPOINT = new Uri("https://www.google-analytics.com/collect");

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
      var gaPropertyId = ConfigurationManager.AppSettings["Gallery.GoogleAnalyticsPropertyId"];
      if (string.IsNullOrEmpty(gaPropertyId))
        return;
      var webClient = new WebClient
      {
        Headers =
        {
          { HttpRequestHeader.UserAgent, filterContext.HttpContext.Request.UserAgent },
          { "X-Forwarded-For", filterContext.HttpContext.Request.UserHostAddress }
        }
      };

      var baseParameters = new NameValueCollection
      {
        { "v",   "1" },
        { "tid", gaPropertyId },
        { "cid", filterContext.HttpContext.Request.Headers["X-NuGet-ApiKey"] ?? filterContext.HttpContext.Request.Headers["NuGet-ApiKey"]  ?? Guid.NewGuid().ToString() },
        { "ni",  "1" },
      };

      try
      {
        var viewParameters = new NameValueCollection(baseParameters)
        {
          { "t",  "pageview" },
          { "dl", filterContext.HttpContext.Request.RawUrl },
        };
        webClient.UploadValues(GA_ENDPOINT, viewParameters);
      }
      catch { }

      try
      {
        var packageId = string.Join(",", 
          new [] { filterContext.RouteData.Values["id"], filterContext.RouteData.Values["version"] }
            .Select(_ => _ != null)
            .Select(_ => Convert.ToString(_)));

        var eventParameters = new NameValueCollection(baseParameters)
        {
          { "t",  "event" },
          { "ec", "NuGet" },
          { "ea", filterContext.HttpContext.Request.Headers["NuGet-Operation"] ?? "Unknown" },
          { "ev", packageId },
        };
        webClient.UploadValuesAsync(GA_ENDPOINT, eventParameters);
      }
      catch { }
    }
  }
}
{'fraction_non_alphanumeric': 0.12681510164569215, 'fraction_numerical': 0.000968054211035818, 'mean_word_length': 2.716726618705036, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5039709', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1155, 'n_words': 195}