asp.net-mvc - Google Analytics 代码 - 将其放在 MVC 5 应用程序中的正确位置是否正确?

标签 asp.net-mvc google-analytics

我有一个 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/

相关文章:

c# - 应用程序 Dispose 调用阻止

c# - ASP.NET : Error - Server Error in '/' Application

asp.net-mvc - 如果我需要从自定义模型绑定(bind)器中检索对象,绑定(bind)器应该与服务层、存储库层还是...交互?

c# - 在 ASP.NET MVC 5 中填充 User.Identity

c# - 将 DTO 传递给我的 ViewModels 构造函数以映射属性

node.js - Nodejs实时在线人数统计

google-app-engine - 使用来自 Google Analytics 的数据按国家/地区返回查看最多的项目

android - Google Analytics V4 广告系列测量测试不起作用

node.js - 谷歌分析步骤从 API 获取每页的浏览量

wordpress - 将 Google 分析与网站 API 集成