google-analytics - 在Google Analytics(分析)的评估池中设置自定义维度

标签 google-analytics server pool dimension measurement

我需要从服务器端设置自定义维度,我正在尝试以下请求:

http://www.google-analytics.com/collect?&v=1&tid=UA-XXXXXXXX-X&cid=111111111.111111111&t=all&cd1=MyCustomDimension

在“ 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方法,它将起作用。我不确定自定义维度的发布请求中是否缺少某些内容。

提前致谢。

最佳答案

我不确定您所说的“无效”是什么意思,因为您的意思对我来说很好。

无论如何,您都应尝试使用Measurement Protocol Hit Builder在发送匹配之前创建并验证您的匹配,以确保所有必填字段都在其中。

如果要验证代码中的匹配,请将匹配发送到Measurement Protocol Validation Server以检查其有效性,然后再将其发送到Google Analytics(分析)。命中生成器实际上是在后台使用此工具来验证命中。

关于google-analytics - 在Google Analytics(分析)的评估池中设置自定义维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32207575/

相关文章:

ajax - 从 Google Analytics 跟踪 Ajax Popover 中的页面

regex - 基于Google Analytic URL的目标无法涵盖一切

c - 第一个客户端请求未在 c 中的套接字中得到服务

php - 每次服务器表加载传输的数据时,如何向收件人发送包含表数据的电子邮件?

c# - 用作池的无序线程安全集合?

c++ - 对象池的替代方案?

google-analytics - Google Analytics 未接收事件(命令被忽略。未知目标 : undefined)

google-analytics - Google Analytics(分析)报告和页面计数器中的访问次数差异

php - php.ini 中 PHP 扩展的默认路径是什么?

c++ - boost::fast_pool_allocator 抛出访问冲突异常