Skip to content

Instantly share code, notes, and snippets.

@0liver
Last active February 12, 2025 17:32

Revisions

  1. 0liver revised this gist Oct 5, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion GoogleAnalyticsApi.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    /* see https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide */
    /* based on the docs at: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide */
    /*
    * LICENSE: MIT
    * AUTOHR: [email protected]
    */

    using System;
    using System.Collections.Generic;
  2. 0liver renamed this gist Apr 23, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. 0liver revised this gist Apr 23, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions GoogleAnalyticsApi
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,8 @@ namespace Generic.Core.WebContext
    Track(HitType.@pageview, category, action, label, value);
    }

    private static void Track(
    HitType type, string category, string action, string label, int? value = null)
    private static void Track(HitType type, string category, string action, string label,
    int? value = null)
    {
    if (string.IsNullOrEmpty(category)) throw new ArgumentNullException("category");
    if (string.IsNullOrEmpty(action)) throw new ArgumentNullException("action");
  4. 0liver revised this gist Apr 23, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion GoogleAnalyticsApi
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,8 @@ namespace Generic.Core.WebContext
    Track(HitType.@pageview, category, action, label, value);
    }

    private static void Track(HitType type, string category, string action, string label, int? value = null)
    private static void Track(
    HitType type, string category, string action, string label, int? value = null)
    {
    if (string.IsNullOrEmpty(category)) throw new ArgumentNullException("category");
    if (string.IsNullOrEmpty(action)) throw new ArgumentNullException("action");
  5. 0liver revised this gist Apr 23, 2014. 1 changed file with 68 additions and 68 deletions.
    136 changes: 68 additions & 68 deletions GoogleAnalyticsApi
    Original file line number Diff line number Diff line change
    @@ -10,81 +10,81 @@ using System.Web;

    namespace Generic.Core.WebContext
    {
    public class GoogleAnalyticsApi
    {
    public static void TrackEvent(string category, string action, string label, int? value = null)
    {
    Track(HitType.@event, category, action, label, value);
    }
    public class GoogleAnalyticsApi
    {
    public static void TrackEvent(string category, string action, string label, int? value = null)
    {
    Track(HitType.@event, category, action, label, value);
    }

    public static void TrackPageview(string category, string action, string label, int? value = null)
    {
    Track(HitType.@pageview, category, action, label, value);
    }
    public static void TrackPageview(string category, string action, string label, int? value = null)
    {
    Track(HitType.@pageview, category, action, label, value);
    }

    private static void Track(HitType type, string category, string action, string label, int? value = null)
    {
    if (string.IsNullOrEmpty(category)) throw new ArgumentNullException("category");
    if (string.IsNullOrEmpty(action)) throw new ArgumentNullException("action");
    private static void Track(HitType type, string category, string action, string label, int? value = null)
    {
    if (string.IsNullOrEmpty(category)) throw new ArgumentNullException("category");
    if (string.IsNullOrEmpty(action)) throw new ArgumentNullException("action");

    var request = (HttpWebRequest) WebRequest.Create("http://www.google-analytics.com/collect");
    request.Method = "POST";
    var request = (HttpWebRequest) WebRequest.Create("http://www.google-analytics.com/collect");
    request.Method = "POST";

    // the request body we want to send
    var postData = new Dictionary<string, string>
    {
    { "v", "1" },
    { "tid", "UA-XXXXXX-XX" },
    { "cid", "555" },
    { "t", type.ToString() },
    { "ec", category },
    { "ea", action },
    };
    if (!string.IsNullOrEmpty(label))
    {
    postData.Add("el", label);
    }
    if (value.HasValue)
    {
    postData.Add("ev", value.ToString());
    }
    // the request body we want to send
    var postData = new Dictionary<string, string>
    {
    { "v", "1" },
    { "tid", "UA-XXXXXX-XX" },
    { "cid", "555" },
    { "t", type.ToString() },
    { "ec", category },
    { "ea", action },
    };
    if (!string.IsNullOrEmpty(label))
    {
    postData.Add("el", label);
    }
    if (value.HasValue)
    {
    postData.Add("ev", value.ToString());
    }

    var postDataString = postData
    .Aggregate("", (data, next) => string.Format("{0}&{1}={2}", data, next.Key,
    HttpUtility.UrlEncode(next.Value)))
    .TrimEnd('&');
    var postDataString = postData
    .Aggregate("", (data, next) => string.Format("{0}&{1}={2}", data, next.Key,
    HttpUtility.UrlEncode(next.Value)))
    .TrimEnd('&');

    // set the Content-Length header to the correct value
    request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);
    // set the Content-Length header to the correct value
    request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);

    // write the request body to the request
    using (var writer = new StreamWriter(request.GetRequestStream()))
    {
    writer.Write(postDataString);
    }
    // write the request body to the request
    using (var writer = new StreamWriter(request.GetRequestStream()))
    {
    writer.Write(postDataString);
    }

    try
    {
    var webResponse = (HttpWebResponse) request.GetResponse();
    if (webResponse.StatusCode != HttpStatusCode.OK)
    {
    throw new HttpException((int) webResponse.StatusCode,
    "Google Analytics tracking did not return OK 200");
    }
    }
    catch (Exception ex)
    {
    // do what you like here, we log to Elmah
    // ElmahLog.LogError(ex, "Google Analytics tracking failed");
    }
    }
    try
    {
    var webResponse = (HttpWebResponse) request.GetResponse();
    if (webResponse.StatusCode != HttpStatusCode.OK)
    {
    throw new HttpException((int) webResponse.StatusCode,
    "Google Analytics tracking did not return OK 200");
    }
    }
    catch (Exception ex)
    {
    // do what you like here, we log to Elmah
    // ElmahLog.LogError(ex, "Google Analytics tracking failed");
    }
    }

    private enum HitType
    {
    // ReSharper disable InconsistentNaming
    @event,
    @pageview,
    // ReSharper restore InconsistentNaming
    }
    }
    private enum HitType
    {
    // ReSharper disable InconsistentNaming
    @event,
    @pageview,
    // ReSharper restore InconsistentNaming
    }
    }
    }
  6. 0liver created this gist Apr 23, 2014.
    90 changes: 90 additions & 0 deletions GoogleAnalyticsApi
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    /* see https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide */

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web;

    namespace Generic.Core.WebContext
    {
    public class GoogleAnalyticsApi
    {
    public static void TrackEvent(string category, string action, string label, int? value = null)
    {
    Track(HitType.@event, category, action, label, value);
    }

    public static void TrackPageview(string category, string action, string label, int? value = null)
    {
    Track(HitType.@pageview, category, action, label, value);
    }

    private static void Track(HitType type, string category, string action, string label, int? value = null)
    {
    if (string.IsNullOrEmpty(category)) throw new ArgumentNullException("category");
    if (string.IsNullOrEmpty(action)) throw new ArgumentNullException("action");

    var request = (HttpWebRequest) WebRequest.Create("http://www.google-analytics.com/collect");
    request.Method = "POST";

    // the request body we want to send
    var postData = new Dictionary<string, string>
    {
    { "v", "1" },
    { "tid", "UA-XXXXXX-XX" },
    { "cid", "555" },
    { "t", type.ToString() },
    { "ec", category },
    { "ea", action },
    };
    if (!string.IsNullOrEmpty(label))
    {
    postData.Add("el", label);
    }
    if (value.HasValue)
    {
    postData.Add("ev", value.ToString());
    }

    var postDataString = postData
    .Aggregate("", (data, next) => string.Format("{0}&{1}={2}", data, next.Key,
    HttpUtility.UrlEncode(next.Value)))
    .TrimEnd('&');

    // set the Content-Length header to the correct value
    request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);

    // write the request body to the request
    using (var writer = new StreamWriter(request.GetRequestStream()))
    {
    writer.Write(postDataString);
    }

    try
    {
    var webResponse = (HttpWebResponse) request.GetResponse();
    if (webResponse.StatusCode != HttpStatusCode.OK)
    {
    throw new HttpException((int) webResponse.StatusCode,
    "Google Analytics tracking did not return OK 200");
    }
    }
    catch (Exception ex)
    {
    // do what you like here, we log to Elmah
    // ElmahLog.LogError(ex, "Google Analytics tracking failed");
    }
    }

    private enum HitType
    {
    // ReSharper disable InconsistentNaming
    @event,
    @pageview,
    // ReSharper restore InconsistentNaming
    }
    }
    }