Here is what I finally did (I did not like the idea of embedding .Net code inside javascript) :
- I setup two Google Analytics account, one for development/testing and one for production
- Create a page which returns the Google Analytics account id (from the web.config) as its body.
- On $(document).ready, make an ajax call to the page
- On Ajax complete, call the google analytics script with the response (which contains the account id)
So depending on the environment I deploy to, I use either version of the web.config file containing the right account id.
It's a bit more complicated, but it does not involve generating javascript from .NET, and it's been very simple with jQuery.
Here is the final code :
var setupGoogleAnalytics = function (clientNumber) {
var gaq = gaq || [];
gaq.push(['_setAccount', clientNumber]);
gaq.push(['_setDomainName', 'none']);
gaq.push(['_trackPageview']);
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);
}
$(document).ready(function(){
$.ajax({
url: "GetGAClientNumber.aspx",
success: function(data) {
setupGoogleAnalytics(data);
}
});
});
And it works perfectly !