Google Analytics代码 - 是否将它放入MVC 5应用程序中是正确的地方? [英] Google Analytics Code - Is the correct place to put it in an MVC 5 app?
问题描述
我有一个MVC 5应用程序。我将Google Analytics(分析)代码和跟踪代码放在一个单独的.js文件中,如下所示:
// Google Analytics代码
(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,文档,'脚本','//www.google-analytics.com/analytics.js','ga');
ga('create','-------------','auto');
ga('send','pageview');
// Google Analytics跟踪代码
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(新的ScriptBundle(〜/ bundles / modernizr).include(
〜/ Scripts / Misc / googleAnalytics.js ,
〜/ Scripts / modernizr- *
));
在我的_Layout.cshtml文件中,我引用了BundleConfig文件,如下所示:
< head>
< meta charset =utf-8/>
< meta name =viewportcontent =width = device-width,initial-scale = 1.0>
< meta http-equiv =no-cache>
< meta http-equiv =Expirescontent = - 1>
< meta http-equiv =Cache-Controlcontent =no-cache>
< title>< / title>
@ Styles.Render(〜/ Content / css)
@ Scripts.Render(〜/ bundles / modernizr)
< / head>
这看起来正确吗?我想确定这是否正常运作。
在< head> ;
没有问题,所以尽管位置不错,但您的跟踪代码不正确。您既包含旧的跟踪代码,又包含新的跟踪代码,则应删除旧的跟踪代码并仅保留新的跟踪代码。删除一行后的所有内容。
// Google Analytics跟踪代码
新的(通用)跟踪代码使用名为analytics.js的谷歌JavaScript文件,旧的(经典)JavaScript文件使用名为ga.js的JavaScript文件。因此,如果您看到ga.js,请按照升级说明进行操作。 https://developers.google.com/analytics/devguides/collection/upgrade/guide
I have an MVC 5 app. I have the Google Analytics code and the tracking code in a separate .js file as follows:
// 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);
})();
This .js file is in my BundleConfig.cs file as follows:
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/Misc/googleAnalytics.js",
"~/Scripts/modernizr-*"
));
In my _Layout.cshtml file, I have the BundleConfig file referenced as follows:
<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>
Does this look correct? I want to make sure this is working as it's supposed to.
Anywhere in the <head>
is fine, so you're good as far as location goes, however your tracking code is not correct. You're including both the old and the new tracking codes, you should remove the old tracking code and only keep the new one. Remove everything after the line that says
// Google Analytics tracking code
The new (universal) tracking code uses google's javascript file named "analytics.js" the old (classic) one uses the javascript file named "ga.js". So if you ever see ga.js, follow the instructions for upgrade. https://developers.google.com/analytics/devguides/collection/upgrade/guide
这篇关于Google Analytics代码 - 是否将它放入MVC 5应用程序中是正确的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!