在 Google Analytics 的测量池中设置自定义维度
Set Custom Dimension in measurement pool in Google Analytics
我需要从服务器端设置一个自定义维度,我正在尝试这个请求:
在 "t" 值中我也尝试 "event" 和 "pageview" 但它不起作用。
我创建了这个 class 来完成请求。
public static class GoogleAnalyticsServerSide
{
private static string googleURL = "http://www.google-analytics.com/collect";
private static string googleVersion = "1";
private static string googleTrackingID = "UA-XXXXXX-X";
private static string googleClientID = "111111111.11111111";
private static Dictionary<string, string> baseValues()
{
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("v", googleVersion); // Version.
data.Add("tid", googleTrackingID); // Tracking ID / Web property / Property ID.
data.Add("cid", googleClientID); // Anonymous Client ID.
return data;
}
public static void TrackEvent(string category, string action, string label)
{
Dictionary<string, string> postData = new Dictionary<string, string>();
postData = baseValues();
postData.Add("t", "event");
postData.Add("ec", category);
postData.Add("ea", action);
postData.Add("el", label);
Track(postData);
}
public static void TrackCustomDimension(string index, string value)
{
Dictionary<string, string> postData = new Dictionary<string, string>();
postData = baseValues();
postData.Add("t", "all");
postData.Add("cd" + index, value);
Track(postData);
}
private static void Track(Dictionary<string, string> postData)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(googleURL);
request.Method = "POST";
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)
{
}
}
catch (Exception e)
{
}
}
}
class 效果很好,因为如果我使用 TrackEvent
方法,它会起作用。我不确定我是否在自定义维度的 post 请求中遗漏了某些内容。
提前致谢。
我不确定你说的 "it doesn't work" 是什么意思,因为我觉得你的东西不错。
无论如何,您应该在发送之前尝试使用 Measurement Protocol Hit Builder 创建和验证您的点击,只是为了确保所有必填字段都在那里。
如果您想在代码中验证您的点击,请将您的点击发送到 Measurement Protocol Validation Server 以检查其有效性,然后再将它们发送到 Google Analytics。命中生成器实际上在后台使用此工具来验证命中。
我需要从服务器端设置一个自定义维度,我正在尝试这个请求:
在 "t" 值中我也尝试 "event" 和 "pageview" 但它不起作用。
我创建了这个 class 来完成请求。
public static class GoogleAnalyticsServerSide
{
private static string googleURL = "http://www.google-analytics.com/collect";
private static string googleVersion = "1";
private static string googleTrackingID = "UA-XXXXXX-X";
private static string googleClientID = "111111111.11111111";
private static Dictionary<string, string> baseValues()
{
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("v", googleVersion); // Version.
data.Add("tid", googleTrackingID); // Tracking ID / Web property / Property ID.
data.Add("cid", googleClientID); // Anonymous Client ID.
return data;
}
public static void TrackEvent(string category, string action, string label)
{
Dictionary<string, string> postData = new Dictionary<string, string>();
postData = baseValues();
postData.Add("t", "event");
postData.Add("ec", category);
postData.Add("ea", action);
postData.Add("el", label);
Track(postData);
}
public static void TrackCustomDimension(string index, string value)
{
Dictionary<string, string> postData = new Dictionary<string, string>();
postData = baseValues();
postData.Add("t", "all");
postData.Add("cd" + index, value);
Track(postData);
}
private static void Track(Dictionary<string, string> postData)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(googleURL);
request.Method = "POST";
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)
{
}
}
catch (Exception e)
{
}
}
}
class 效果很好,因为如果我使用 TrackEvent
方法,它会起作用。我不确定我是否在自定义维度的 post 请求中遗漏了某些内容。
提前致谢。
我不确定你说的 "it doesn't work" 是什么意思,因为我觉得你的东西不错。
无论如何,您应该在发送之前尝试使用 Measurement Protocol Hit Builder 创建和验证您的点击,只是为了确保所有必填字段都在那里。
如果您想在代码中验证您的点击,请将您的点击发送到 Measurement Protocol Validation Server 以检查其有效性,然后再将它们发送到 Google Analytics。命中生成器实际上在后台使用此工具来验证命中。