Always Hide Your Webstats Code On Your Dev Server

While Google Analytics may have been the bees knees when it comes to web stats in the past, it seems to have taken an almighty tumble from the top spot in recent years. The interface has gone from being one of the cleanest, easiest to use, to one of the most confusing out there.

I decided it was time to consider moving away from Google Analytics, so recently I have been playing around with a few different web stat packages, running multiple at once on the same page. In doing so, I began to notice that some packages were reporting a higher number of views than others.

When I started to dig closer to see what was causing this, I discovered that some of the packages were showing a large number of hits to pages `localhost:page_name`.

Yep, it seems that not all of the packages are clever enough to filter out page views from your development server.

To resolve the issue – I found a nice little hack that will only display the tracking code on the live server.

The ASP.NET property `Request.Url.Host` will reveal the URL by which the site was accessed – by simply checking this value you can tell whether the site is live, or running on a dev server. You can then hide the tracking code accordingly.

@if(Request.Url.Host != "localhost")
{
// show tracking code - site is live
}

Read More

Accessing an ActiveMQ Stomp Feed In C# With Apache.NMS

The following tutorial explains how to connect to a Stomp data feed in C#, by using the Apache.NMS client.

To start with, download the Apache.NMS.Stomp Client.

Once downloaded, open the zip file and extract the following DLLs – copy them to the desktop for now:

/build/<.NET or Mono Version Required>/Release/Apache.NMS.Stomp.dll
/lib/Apache.NMS/<.NET or Mono Version Required>/Apache.NMS.dll

Next, start a new Console Application project in Visual Studio. Add a reference to the two DLLs selected earlier. Also add the following using statement – `using Apache.NMS;`

NOTE: You may receive a compile error that “apache” could not be found. this seems to occur when your project target type is set to “.NET Framework 4 Client Profile”. If you change it to “.NET Framework 4″ this error goes away.

Ok, lets write some code.

In your main() function, create a ConnectionFactory, which we will then use to create a connection and session:

IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeed:61618"));

IConnection connection = factory.CreateConnection("username", "password");
ISession session = connection.CreateSession();

Next subscribe to the feed you wish to access:

IDestination destination = session.GetDestination("topic://topic_name");
IMessageConsumer consumer = session.CreateConsumer(destination);
connection.Start();

We have now connected to the data source and subscribed to a particular feed. Next we need to handle any messages that are received from the feed. We do this by creating a MessageListener:

consumer.Listener += new MessageListener(OnMessage);

That is all that we need in main – simply add a readline statement to stop the program from exiting immediately:

Console.ReadLine();
connection.Close();

We now create a new function called OnMessage, which will be called every time a message is received:

private static void OnMessage(IMessage message)
{
ITextMessage msg = (ITextMessage)message;
message.Acknowledge();

Console.WriteLine(msg.Text);
}

And that is all there is to it! If you run this now, you should see messages arriving from your data feed. Of course you may need to do additional processing on these to extract the data in a more useful format, but that’s a tutorial for another day.

Full application code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Apache.NMS;

namespace ApacheNMS
{
class Program
{
static void Main(string[] args)
{
IConnectionFactory factory = new NMSConnectionFactory(new Uri("stomp:tcp://datafeed:61618"));

IConnection connection = factory.CreateConnection("username", "password");
ISession session = connection.CreateSession();

IDestination destination = session.GetDestination("topic://" + "DATA_FEED");
IMessageConsumer consumer = session.CreateConsumer(destination);

connection.Start();
consumer.Listener += new MessageListener(OnMessage);
Console.WriteLine("Consumer started, waiting for messages... (Press ENTER to stop.)");

Console.ReadLine();
connection.Close();
}

private static void OnMessage(IMessage message)
{
try
{
Console.WriteLine("Median-Server (.NET): Message received");

ITextMessage msg = (ITextMessage)message;
message.Acknowledge();

Console.WriteLine(msg.Text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("---");
Console.WriteLine(ex.InnerException);
Console.WriteLine("---");
Console.WriteLine(ex.InnerException.Message);
}
}
}
}

Read More