Skip to content

Instantly share code, notes, and snippets.

@0liver
Last active March 24, 2023 09:34
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save 0liver/11229128 to your computer and use it in GitHub Desktop.
Save 0liver/11229128 to your computer and use it in GitHub Desktop.
C# wrapper around the Google Analytics Measurement Protocol API
/* based on the docs at: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide */
/*
* LICENSE: MIT
* AUTOHR: oliver@teamaton.com
*/
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
}
}
}
@BorisKozo
Copy link

Hi,
I found your gist very useful.
Just two things you might want to add to save others some debug time.

  1. request.KeepAlive = false;
  2. webResponse.close();

Otherwise the third call to the function will get stuck until the request timeout.

Thanks :)

@cgoasduff
Copy link

Hi There, thanks for the code but no events are recorded when I use it. I think this may be to do with "cid", "555" -- where do I get the value which should overwrite the "555" from your sample code ? thanks C

@markalanevans
Copy link

Also you should update this to be https:

According to their docs:
https://developers.google.com/analytics/devguides/collection/protocol/v1/reference

Transport

URL Endpoint

You send data using the Measurement Protocol by making HTTP requests to the following end point:


https://www.google-analytics.com/collect

All data should be sent securely with the HTTPS protocol.

You can send data using either POST or GET requests.

@Arimov
Copy link

Arimov commented Feb 24, 2019

Hi,
I think for tracking pages, the code is not correct. Need to pass parameters dh, dp, dt
See documentation:
https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide

v=1              // Version.
&tid=UA-XXXXX-Y  // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.

&t=pageview      // Pageview hit type.
&dh=mydemo.com   // Document hostname.
&dp=/home        // Page.
&dt=homepage     // Title.

@kiquenet
Copy link

Google Analytics 4 ?

@0liver
Copy link
Author

0liver commented Nov 17, 2022

No 😁 The gist was last edited in 2015 - I think we had GAv2 back then 😉

@ParmarNikhil
Copy link

Will this work with GA-4 key ?

@0liver
Copy link
Author

0liver commented Mar 24, 2023

Please try it out for yourself - I am not maintaining this piece of code. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment