views:

33

answers:

2

I fairly new to Google Analytics. I'm looking for a way to add GA to my ASP.Net (C#) site, but keep the numbers for different environments from getting mixed up.

I'd like to only have to set up the script on my master page once and then use either the full URL of the request or a web.config setting to put each environment into a seperate reporting bin.

-- Edit --

I've attempted to use the suggestion from aj_whiz, but ran into an issue when opening a page with an AJAXControl Toolkit control on it. Here's the code I was trying to use.

<%@ Master Language="C#" AutoEventWireup="true" Codebehind="MasterPage2.master.cs" Inherits="TruckMo.MasterPage2" %> <%@ Register Src="LinkMenu.ascx" TagName="LinkMenu" TagPrefix="uc1" %> TRAC Connect

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '<%=ConfigurationManager.AppSettings["GoogleAnalyticsCode"]%>']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>    

The error I get is "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." The location is in AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(...) The line is header.Controls.Add(link);

A: 

The easiest way is to create Dev and Prod profiles. Profiles are a feature that lets you create a view of your Analytics data that only shows traffic where the URL matches a given regular expression. See the documentation here.

jacobm
A: 

Googly Analytics code looks like

<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">
            var pageTracker = _gat._getTracker('<%=ConfigurationManager.AppSettings["GoogleAnalyticsCode"]%>');
            pageTracker._initData();
            pageTracker._trackPageview();
            </script>

you can put the above code in master page and the Google Analytics code in web.config file notice the line

_gat._getTracker('<%=ConfigurationManager.AppSettings["GoogleAnalyticsCode"]%>');

where the anatytics code will be picked up from web.config

ajay_whiz
This doesn't work if the AJAX Control Toolkit is used on a page. It throws an error that <% and %> are not allowed inside a control.
Brad Bruce
can you post the aspx source?
ajay_whiz
By moving the script to the end of the body section almost everything works. Google only looks in the head section when checking to see if the site contains the tracking code. Now that I see my data going in, I have plenty of proof. I deployed the code last night and already can see traffic reporting.
Brad Bruce
@Brad even Google recommends to put the tracking code just before the end of html tag
ajay_whiz
The text right above the code to copy states "Copy the following code, then paste it onto every page you want to track immediately before the closing </head> tag" Where do they recommend right before the </body>tag? I have several people questioning why I didn't follow Google's instructions. (Besides the fact that it works where I put it)
Brad Bruce
ajay_whiz