我需要从服务器端设置自定义维度,我正在尝试此请求:
在" t"价值我也尝试"事件"和"网页浏览"但它没有用。
我创建了这个类来执行请求。
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)
{
}
}
}
该类运行良好,因为如果我使用TrackEvent
方法,它可以工作。我不确定我是否在Custom Dimension的帖子请求中遗漏了一些内容。
提前致谢。
答案 0 :(得分:0)
我不确定你的意思是“它不起作用”,因为你看起来很好。
无论如何,您应该尝试使用Measurement Protocol Hit Builder在发送之前创建并验证您的匹配,只是为了确保所有必填字段都在那里。
如果您要验证代码中的匹配,请将匹配结果发送到Measurement Protocol Validation Server以检查其有效性,然后再将其发送到Google Analytics。命中构建器实际上使用此工具来验证命中。