Tracking codes in DEBUG-RELEASE mode

While you work on a website which includes some JavaScript for counters and statistics is better not to allow or to "hide" them during the development time.

One solution is to comment the source code and every time before the page is published, to uncomment the relevant lines. This leads to errors sooner or later, we use to forget things (uncomment things).

A better option is to use preprocessor directives #if, #endif with the DEBUG compile option.
A protected(at least) boolean field must be added in the code behind.

public partial class home : System.Web.UI.Page

#if (DEBUG)
protected static readonly bool Debug = true;
protected static readonly bool Debug = false;

Now the field Debug can be used in the web form. The following google analytics code will exist only when the source code is compiled in Release mode.

<%if (!Debug) { %>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));

<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXX-1");
} catch (err) { }


Popular posts from this blog