2012-01-01 46 views
2

我有這樣的片段:Store帳戶ID爲谷歌Analytics(分析)在Web.config中(MVC 3)

<script type="text/javascript"> 
     var _gaq = _gaq || []; 
     _gaq.push(['_setAccount', 'UA-11111111-1']); 
     _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> 

如何存放我的帳戶ID在web.config中? 我怎樣才能從web.config恢復到JS(而不是直接寫入ID)?

謝謝!

回答

3

有可能是沒有結構化的方式來做到這一點,我只是把它叫做從web.config中(不使用任何動作,但只是寫它)像這樣:

in web.config:

<appSettings> 
<add key="GoogleAnalyticsAccountId" value="UA-11111111-1" /> 
</appSettings> 

in js:

_gaq.push(['_setAccount', '@System.Configuration.ConfigurationManager.AppSettings["GoogleAnalyticsAccountId"]']); 
1

ActionResult回到你的ID

[HttpPost] 
public ActionResult GetAnalyticId(){ 
var id = ConfigutationManager.ApplicationSetting["analyticID"].ToString(); 
return Content(id); 
} 

在JavaScript

$.post("/Controller/GetAnalyticId",function(data){ 
//data contains the id use it where you want to 
    var _gaq = _gaq || []; 
     _gaq.push(['_setAccount', 'UA-11111111-1']); 
     _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); 
     })(); 

});