我有一个 MVC 5 应用程序。我将 Google Analytics 代码和跟踪代码放在单独的 .js 文件中,如下所示:
// Google Analytics code
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', '-------------', 'auto');
ga('send', 'pageview');
// Google Analytics tracking code
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '-------------']);
_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);
})();
这个 .js 文件位于我的 BundleConfig.cs 文件中,如下所示:
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/Misc/googleAnalytics.js",
"~/Scripts/modernizr-*"
));
在我的 _Layout.cshtml 文件中,我引用了 BundleConfig 文件,如下所示:
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Cache-Control" content="no-cache">
<title></title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
这看起来正确吗?我想确保它按预期工作。
最佳答案
来自谷歌分析documentation :
Paste your snippet (unaltered, in it’s entirety) into every web page that you want to track. Paste it immediately before the closing
</head>
tag. If you use templates to dynamically generate pages for your site (like if you use PHP, ASP or a similar technology), you can paste the tracking code snippet into it's own file, then include it in your page header.
所以我认为它应该是你的包中的最后一个脚本:
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*",
"~/Scripts/Misc/googleAnalytics.js"
));
关于asp.net-mvc - Google Analytics 代码 - 将其放在 MVC 5 应用程序中的正确位置是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26539152/