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;
#else
protected static readonly bool Debug = false;
#endif
}



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 + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

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

Comments

Popular posts from this blog

IIS 7.5, HTTPS Bindings and ERR_CONNECTION_RESET

Verify ILogger calls with Moq.ILogger

Table Per Hierarchy Inheritance with Column Discriminator and Associations used in Derived Entity Types