c# - 从服务器端以编程方式发送 Google Analytics 事件不起作用

标签 c# google-analytics measurement-protocol

我们想从服务器端向谷歌分析发送自定义事件跟踪信息。

为此我引用了this所以发布,并提出以下代码片段,但不知何故它没有将事件的信息发送到 GA。我调试了代码以查看响应,它返回 200 (OK) 状态,响应类似于通过客户端跟踪事件时的响应。我们已经等了几天,看看是否跟踪到事件,但没有。

    public static void TrackEvent(string category, string action, string label)
    {
        string gaCodeTest = "UA-xxxxxx-2";
        ASCIIEncoding encoding = new ASCIIEncoding();
        string cid = Guid.NewGuid().ToString();

        string postData =
            "v=1&tid=" + gaCodeTest + " &cid=" + cid + "&t=event" +
            "&ec=" + category +
            "&ea=" + action +
            "&el=" + label;

        byte[] data = encoding.GetBytes(postData);
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://www.google-analytics.com/collect");

        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.ContentLength = data.Length;
        Stream newStream = myRequest.GetRequestStream();
        newStream.Write(data, 0, data.Length);

        var response = (HttpWebResponse)myRequest.GetResponse();

        //var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

        newStream.Close();

    }

最佳答案

您的请求。

v=1&tid=UA-1111111-2 &cid=555&t=event&ec=MyCategory&ea=MyAction&el=MyLabel

在调试端测试它

https://www.google-analytics.com/debug/collect?v=1&tid=UA-11111-2 &cid=555&t=event&ec=MyCategory&ea=MyAction&el=MyLabel

结果

{
  "hitParsingResult": [ {
    "valid": false,
    "parserMessage": [ {
      "messageType": "ERROR",
      "description": "The value provided for parameter 'tid' is invalid. Please see  for details.",
      "messageCode": "VALUE_INVALID",
      "parameter": "tid"
    } ],
    "hit": "/debug/collect?v=1\u0026tid=UA-76874663-2%20\u0026cid=555\u0026t=event\u0026ec=MyCategory\u0026ea=MyAction\u0026el=MyLabel"
  } ],
  "parserMessage": [ {
    "messageType": "INFO",
    "description": "Found 1 hit in the request."
  } ]
}

你在tid之后有一个空格

关于c# - 从服务器端以编程方式发送 Google Analytics 事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50941663/

相关文章:

c# - 在 IDialog 中使用 ILifetimeScope

c# - AutoSuggestBox 查询选中的文本框

android - 找不到与包名称匹配的客户端 (Google Analytics) - 多个 productFlavors & buildTypes

google-analytics - 谷歌分析测量协议(protocol) session 超时和查询时间限制

c# - 如何从 ConcurrentBag 中删除所有项目?

javascript - Analytics API(Apps 脚本)文档丢失? | "getItems"与 "items"和其他问题

google-analytics - 使用 Google Analytics 4 自动增强测量事件时数据洞察中的下载报告 URL?

post - 如何指定 Google 测量协议(protocol)交易命中的来源?

c# - 了解异常 "An entity object cannot be referenced by multiple instances of IEntityChangeTracker."