C# jquerymobile+;MVC 3&x2B;请求路径
我正在尝试使用jquerymobile+mvc3来运行一个新的移动站点。其中一个要求是安装Google Analytics并正确跟踪页面浏览量。我像往常一样插入了GA代码,但它似乎只跟踪主页,而没有跟踪其他网站页面。经过一些审查,我似乎需要将GA javascript代码分成两个独立的块。因此,在元素中,我有:C# jquerymobile+;MVC 3&x2B;请求路径,c#,jquery,asp.net-mvc,google-analytics,jquery-mobile,C#,Jquery,Asp.net Mvc,Google Analytics,Jquery Mobile,我正在尝试使用jquerymobile+mvc3来运行一个新的移动站点。其中一个要求是安装Google Analytics并正确跟踪页面浏览量。我像往常一样插入了GA代码,但它似乎只跟踪主页,而没有跟踪其他网站页面。经过一些审查,我似乎需要将GA javascript代码分成两个独立的块。因此,在元素中,我有: <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_set
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'xx-xxxxxx-x']);
(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>
<script type="text/javascript">
$('[data-role=page]').live('pageshow', function (event, ui) {
try {
_gaq.push(['_trackPageview', '@Model.RequestPath']);
} catch (err) { }
});
</script>
var _gaq=_gaq | |[];
_gaq.push([''u setAccount',xx-xxxxxx-x']);
(功能(){
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 type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'xx-xxxxxx-x']);
(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>
<script type="text/javascript">
$('[data-role=page]').live('pageshow', function (event, ui) {
try {
_gaq.push(['_trackPageview', '@Model.RequestPath']);
} catch (err) { }
});
</script>
$('[data role=page]')。live('pageshow',函数(事件,ui){
试一试{
_gaq.push([''u trackPageview','@Model.RequestPath']);
}捕获(错误){}
});
问题:@Model.RequestPath始终返回“/”。我已经通过调试器查看了该属性,发现该属性填充了适当的路径(即/somedirectory/somepage),但是当它点击视图时,只会得到一个“/”
重要信息:
- Model.RequestPath配置为返回Request.Url.AbsolutePath的当前值
- 更改内部页面的操作方法以将RequestPath属性设置为常量没有任何效果。就像使用视图模型填充页面一样(页面按预期呈现)但是RequestPath属性似乎在某个时候被修改了。我不明白调试器如何显示它填充了正确的路径,但在到达视图时输出“/”
- 我有两个页面:index.cshtml,它是主页,是第一个提供服务的页面。其余的内部页面使用SiteMaster.cshtml。它们都有相同的GA代码
- 当我加载页面时,调试器为我的请求点击控制器操作(正如我所期望的),并且模型填充了正确的路径(但是视图仍然输出“/”
- 当我在FireFox中执行“查看源代码”时,调试器会再次启动,但这次它会触发我主页的控制器操作
- 我将SiteMaster.cshtml上的GA代码更改为调用“trackPageview1”,将index.cshtml上的GA代码更改为调用“trackPageview2”“这样我就可以在查看源代码时看到调用的是哪一个。它总是trackPageview2,这意味着它总是从index.html中提取,即使我在使用SiteMaster.cshtml布局文件的页面上。这将有助于解释为什么RequestPath总是“/”但我仍然不理解为什么它不从主布局文件输出标记
如果有必要,我会在回答时或在我找到更多信息时,用更多的细节来澄清这篇文章。你应该可以在主文件中删除Google Analytics代码,而无需任何修改。您将代码放在从母版继承的页面上有什么原因吗?GA将跟踪加载的不同页面,您所要做的就是将代码放在布局页面上。您应该能够在不做任何修改的情况下将Google Analytics代码放在主页面中。您将代码放在从母版继承的页面上有什么原因吗?GA将跟踪加载的不同页面,您所要做的就是将代码放到布局页面上。如何使用location.href结合使用Google Analytics with jQuery Mobile entry中的代码:
var gaJsHost=((“https:==document.location.protocol)?”https://ssl." : "http://www.");
write(unescape(“%3Cscript src=”+gaJsHost+“google analytics.com/ga.js”type='text/javascript'%3E%3C/script%3E”);
$('[data role=page]')。live('pageshow',函数(事件,ui){
试一试{
var pageTracker=_gat._getTracker(“您的用户名”);
页面跟踪器._trackPageview(location.href.replace('#','');
}捕捉(错误){
}
});
如何将location.href与使用Google Analytics with jQuery Mobile entry的代码结合使用:
var gaJsHost=((“https:==document.location.protocol)?”https://ssl." : "http://www.");
write(unescape(“%3Cscript src=”+gaJsHost+“google analytics.com/ga.js”type='text/javascript'%3E%3C/script%3E”);
$('[data role=page]')。live('pageshow',函数(事件,ui){
试一试{
var pageTracker=_gat._getTracker(“您的用户名”);
页面跟踪器._trackPageview(location.href.replace('#','');
}捕捉(错误){
}
});
这是我使用的代码。(与尼古拉斯非常相似)
如果它在windows.location中找到散列(#),它将解析出散列后的路径。示例:,url='/contact' 如果散列不存在,例如第一次加载页面时,它将使用windows.location.pathname 示例:,url='/about'
<script type="text/javascript">
$('[data-role=page]').live('pageshow', function(event, ui) {
var url = window.location.toString();
var hashLocation = url.indexOf("#");
if (hashLocation != -1) {
url = url.substring(hashLocation + 1);
}
else {
url = window.location.pathname.toString();
}
try {
var pageTracker = _gat._getTracker("UA-XXXXXXX-XX");
pageTracker._trackPageview(url);
} catch (err) { }
});
</script>
$('[data role=page]')。live('pageshow',函数(事件,ui){
var url=window.location.toString();
var hashLocation=url.indexOf(“#”);
if(hashLocation!=-1){
url=url.substring(hashLocation+1);
}
否则{
url=window.location.pathname.toString();
}
试一试{
var pageTracker=_gat._getTracker(“UA-XXXXXXX-XX”);
页面跟踪器._trackPageview(url);
}捕获(错误){}