4
votes

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);

4

4 Answers

3
votes

A quick comment, Google changed the tracking code a few months ago to support asycn updates.

The NEW code is in the header, the OLD code at the bottom of the page. The discussion above is a confusion between the two (top block is the new, and bottom block the old).

2
votes

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

1
votes

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.

1
votes

Thanks a lot, it worked for me in aspx pages by giving the like this.

Under web.config, <appSettings> ... </appSettings>add:

<add key="GoogleAnalyticsCode" value="**xx-xxxxxxxx-x**"/>

Where xx-xxxxxxxx-x is google analytics code, getting during domain addition

Under aspx master pages only, copy the below code right before the </body> tag:

<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>