代码之家  ›  专栏  ›  技术社区  ›  Yogi nikunj pansuriya

从服务器端以编程方式发送谷歌分析事件不起作用

  •  1
  • Yogi nikunj pansuriya  · 技术社区  · 8 年前

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

    为此我已经提到 this 因此,post,并提出以下代码片段,但不知何故它没有将事件的信息发送给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();
    
        }
    
    1 回复  |  直到 8 年前
        1
  •  4
  •   Linda Lawton - DaImTo    8 年前

    你的要求。

    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之后还有空位