C# Google Analytics 事件类别为空

问题描述 投票:0回答:1

我尝试通过我的 C# 代码添加一些事件跟踪。

这是我的代码:

GoogleTracker 类:

public class GoogleTracker
{
    private string googleURL = "www.google-analytics.com";
    private string googleVersion = "1";
    private string googleTrackingID = "<my-google-tracking-id>";
    private string googleClientID = "<my-google-cleint-id>";

    public GoogleTracker() { }

    public void trackEvent(string category, string action, string label, string value)
    {
        Hashtable ht = baseValues();

        ht.Add("t", "event");                   // Event hit type
        ht.Add("ec", category);                 // Event Category. Required.
        ht.Add("ea", action);                   // Event Action. Required.
        if (label != null) ht.Add("el", label); // Event label.
        if (value != null) ht.Add("ev", value); // Event value.

        postData(ht);
    }
    public void trackPage(string hostname, string page, string title)
    {
        Hashtable ht = baseValues();

        ht.Add("t", "pageview");                // Pageview hit type.
        ht.Add("dh", hostname);                 // Document hostname.
        ht.Add("dp", page);                     // Page.
        ht.Add("dt", title);                    // Title.

        postData(ht);
    }

    public void ecommerceTransaction(string id, string affiliation, string revenue, string shipping, string tax, string currency)
    {
        Hashtable ht = baseValues();

        ht.Add("t", "transaction");       // Transaction hit type.
        ht.Add("ti", id);                 // transaction ID.            Required.
        ht.Add("ta", affiliation);        // Transaction affiliation.
        ht.Add("tr", revenue);            // Transaction revenue.
        ht.Add("ts", shipping);           // Transaction shipping.
        ht.Add("tt", tax);                // Transaction tax.
        ht.Add("cu", currency);           // Currency code.

        postData(ht);
    }
    public void ecommerceItem(string id, string name, string price, string quantity, string code, string category, string currency)
    {
        Hashtable ht = baseValues();

        ht.Add("t", "item");              // Item hit type.
        ht.Add("ti", id);                 // transaction ID.            Required.
        ht.Add("in", name);               // Item name.                 Required.
        ht.Add("ip", price);              // Item price.
        ht.Add("iq", quantity);           // Item quantity.
        ht.Add("ic", code);               // Item code / SKU.
        ht.Add("iv", category);           // Item variation / category.
        ht.Add("cu", currency);           // Currency code.

        postData(ht);
    }

    public void trackSocial(string action, string network, string target)
    {
        Hashtable ht = baseValues();

        ht.Add("t", "social");                // Social hit type.
        ht.Add("dh", action);                 // Social Action.         Required.
        ht.Add("dp", network);                // Social Network.        Required.
        ht.Add("dt", target);                 // Social Target.         Required.

        postData(ht);
    }

    public void trackException(string description, bool fatal)
    {
        Hashtable ht = baseValues();

        ht.Add("t", "exception");             // Exception hit type.
        ht.Add("dh", description);            // Exception description.         Required.
        ht.Add("dp", fatal ? "1" : "0");      // Exception is fatal?            Required.

        postData(ht);
    }

    private Hashtable baseValues()
    {
        Hashtable ht = new Hashtable();
        ht.Add("v", googleVersion);         // Version.
        ht.Add("tid", googleTrackingID);    // Tracking ID / Web property / Property ID.
        ht.Add("cid", googleClientID);      // Anonymous Client ID.
        return ht;
    }
    private bool postData(Hashtable values)
    {
        string data = "";
        foreach (var key in values.Keys)
        {
            if (data != "") data += "&";
            if (values[key] != null) data += key.ToString() + "=" + HttpUtility.UrlEncode(values[key].ToString());
        }

        using (var client = new WebClient())
        {
            var result = client.UploadString(googleURL, "POST", data);
        }

        return true;
    }
}

我的课程:

static class Program
{
    static void Main()
    {
        GoogleTracker ga = new GoogleTracker();
        ga.trackEvent("Event", "Click", "", "5");
    }
}

代码运行无异常。但是,当我在报告页面 (https://analytics.google.com/analytics/) 中看到时,我的事件类别和事件操作仍然为空。 enter image description here

我不明白为什么没有数据。有没有错误?

c# google-analytics measurement-protocol
1个回答
3
投票

请记住,数据可能需要 24 - 48 小时才能显示在标准报告中。 为了进行调试,您可以检查实时报告,这将显示您的请求是否被收到。

我的验证命中方法。

  public static object send(string postData, bool isValidate)
        {
            string url = "http://www.google-analytics.com/collect";
            if (isValidate)
            {
                url = "https://www.google-analytics.com/debug/collect";
            }

            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create(url);
            // Set the Method property of the request to POST.

            request.Method = "POST";

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
            try
            {
                return JsonConvert.DeserializeObject<ValidateResponse>(responseFromServer);                
            }
            catch (Exception ex)
            {
                throw new Exception("SendHitFailed",ex);
            }          
        }

验证响应类

 public class ValidateResponse
    {
        public List<HitParsingResult> hitParsingResult { get; set; }
        public List<ParserMessage2> parserMessage { get; set; }

        public class ParserMessage
        {
            public string messageType { get; set; }
            public string description { get; set; }
            public string messageCode { get; set; }
            public string parameter { get; set; }
        }

        public class HitParsingResult
        {
            public bool valid { get; set; }
            public List<ParserMessage> parserMessage { get; set; }
            public string hit { get; set; }
        }

        public class ParserMessage2
        {
            public string messageType { get; set; }
            public string description { get; set; }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.