The last thing my application does before it launches is start a new thread that performs a simple HttpWebRequest that I would like to track with Google Analytics or Piwik:
Thread thread2 = new Thread(new ThreadStart(this.ThreadAnalytics));
thread2.IsBackground = true; thread2.Start();
public void ThreadAnalytics()
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://netlauncher.org/version1.0.0.html");
request.Method = "GET";
WebResponse response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(),System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
response.Close();
}
catch (Exception) { /* do nothing */ }
}
Note: Although StreamReader is not required, I have included it for testing purposes.
I can see the HTTP GET with WireShark, however I do not see anything in Google Analytics. This lead me to believe it was a Java issue. I switched to Piwik and used their Image Tracker Code that does not require Java:
<img src="http://mydomain.com/piwik.php?idsite=1&rec=1" style="border:0" alt="" />
As with Google Analytics I can navigate to this page using IE for Firefox and see the hit appear. However the hit from my application does not. I have also tried specifying the user agent, language, etc. Is there something I am over looking?
Google Analytics does not require Java either (it uses JavaScript).
The problem is that GA, Piwik, and most other analytics tools require some sort of callback to be made by a resource that is loaded as part of the HTML page – typically either an image or a JavaScript call. By only reading the HTTP response, your code is currently not loading any of the dependent resources that would be used by one of these tools.
Google Analytics may provide an API that you can call directly for what you’re attempting. Similarly, with your Piwik code, you may be able to get it to work by directly calling the
http://mydomain.com/piwik.php?idsite=1&rec=1URL as part of your HttpWebRequest – instead of calling an HTML page that contains an image reference to that URL. Otherwise, you will need to use additional code or libraries to act as a browser and actually “load” the HTML that you’re receiving in the response to your request.