5

We want to send custom event tracking information to google analytics from server side.

For this I have referred this SO post, and come up with the following code snippet, but somehow it is not sending the events' information to GA. I've debugged the code to see the response, and it is returning 200 (OK) status, with response similar to what when the event is tracked through client side. We have waited for a couple of days to see if the event is tracked, but it doesn't.

    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();

    }
Yogi
  • 9,174
  • 2
  • 46
  • 61
  • can you see it in the realtime report? do you have it set to filter bot requests? – Linda Lawton - DaImTo Jun 20 '18 at 06:34
  • @DaImTo - We did'nt see even in the realtime reports. How to set filter bot requests? – Yogi Jun 20 '18 at 06:45
  • 1
    Google analytics admin View settings -> bot filtering. I am working on an SDK for .Net its almost ready to release the first beta NuGet package. I just wanted to do a bit more testing. You are welcome to dig around in my code a little events are working [Google Analytics SDK](https://github.com/LindaLawton/google-analytics-dotnet-sdk) – Linda Lawton - DaImTo Jun 20 '18 at 06:50
  • 1
    If that doesnt work try sending your hit to https://www.google-analytics.com/debug/collect it goes as a HTTP Get. You can just put it in a browser it will tell you if there is anything wrong with the hit itself. – Linda Lawton - DaImTo Jun 20 '18 at 06:59
  • @DaImTo - We haven't set any filters. Didn't get your previous comment to make sure that the value is long – Yogi Jun 20 '18 at 07:01
  • Bot filtering isnt a filter its a setting (Checkbox) in the google analytics profile. [ev](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ev) must be an int sorry not a long – Linda Lawton - DaImTo Jun 20 '18 at 07:06
  • mind editing your question and adding whats in content? – Linda Lawton - DaImTo Jun 20 '18 at 07:08
  • @DaImTo - Sure, will edit the Q. BTW, we actually don't need `ev` so can we exclude it outright? – Yogi Jun 20 '18 at 07:10
  • ev is not required you can remove it. – Linda Lawton - DaImTo Jun 20 '18 at 07:19
  • @DaImTo - Modified my Q with response details. The `content` in the second snipped is just a series of bytes. A byte array. – Yogi Jun 20 '18 at 07:32
  • i just need the string from var content = new FormUrlEncodedContent(values); <--- that – Linda Lawton - DaImTo Jun 20 '18 at 07:59
  • it should just be a string like this **v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome** Just give me that string that you are posting to the server I want to test it to the debug endpoint – Linda Lawton - DaImTo Jun 20 '18 at 07:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173450/discussion-between-yogi-and-daimto). – Yogi Jun 20 '18 at 08:08
  • i also tried this code but didn't worked, then used the code in this link it worked: https://www.technical-recipes.com/2017/tracking-events-in-desktop-applications-using-google-analytics/ – Harry .Naeem Nov 13 '18 at 11:58

1 Answers1

8

Your request.

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

test it at the debug end point

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

results in

{
  "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."
  } ]
}

You have a space after tid

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449