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();
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With