How to Conditionally Load Google Analytics in a Web Application

If you are a developer that uses Google Analytics in your web applications then you know that loading up the Google Analytics in your web page should not happen when you are building the application. In the past I have always just commented out the section for Google Analytics until publishing the site. Then I would go in and uncomment the lines. This works if you do not forget to uncomment the lines in production. (This happened to me).

I decided that there had to be a better way to automatically do this. After searching the internet and pulling together prior knowledge of how an web site works I decided to use the web.config to help me. First add the following lines to your web.config production transformation file. (See here if you do not understand the transformation idea).

Note: In your development instance just change the Instance to equal something other than production.

[sourcecode language=”xml”]
<add key=”GoogleAnalytics” value=”UA-111111-11″/>
<add key=”Instance” value=”Prod”/>

With this in place then add the following to each page of the site or to the master page right above the closing body tag

[sourcecode language=”vb”]
<!–Google Analytic Code –>
<% If UCase(ConfigurationManager.AppSettings(“Instance”).ToString()) = “PROD” Then%>
<script type=”text/javascript”>

var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘<% =ConfigurationManager.AppSettings(“GoogleAnalytics”).ToString() %>’]);

(function () {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
<% End If %>


This will not load the Google Analytics until you deploy it to production. Then your web.config will be set to production and will load cleanly.

NOTE: The nice thing about this is you are not limited to Google Analytics conditional load. You can use it for a wide range of things.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , | Posted under ASP .NET 3.5, ASP .NET 4.0, DotNet/Windows Hosting, Web Deploy | RSS 2.0

Author Spotlight

David Bauernschmidt

I live in the historical triangle of Virginia where I am married with two daughters. I have spent over 13 years working for a Fortune 500 company in the computer area. I started in VB 6.0 and by the time I ended my employment I was supervising a development team where we built many web applications. When my first daughter was born I wanted to spend more time with her so I left and became a programmer analyst for local government as well as launch my own company. Since then I have grown James River Webs into a profitable web design and application company helping small businesses create a big presence on the internet. As an employee I have created web application used by citizens and other companies. I enjoy fly fishing, and spending time with my family. I also enjoy learning new approaches and development tools when it comes to developing applications.

Leave a Reply

Your email address will not be published. Required fields are marked *