javascript Google Analytics 跟踪代码不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20136690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:50:02  来源:igfitidea点击:

Google Analytics Tracking Code is Not Working

javascripthtmlgoogle-analytics

提问by Frank G.

I setup a Google Analytics account and I setup the website and got the Analytics code which is:

我设置了一个 Google Analytics 帐户并设置了网站并获得了 Analytics 代码,它是:

 <script>
      (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', 'UA-4XXXXXXX-1', 'mydomain.com');
      ga('send', 'pageview');
 </script>

I pasted into the Header of all my pages but Analytics keeps saying "Tracking Not Installed".

我粘贴到所有页面的页眉中,但 Analytics 一直说“未安装跟踪”。

I viewed and verified the source code is within the header of the home page and the code is there. The site is built on Classic ASP (default.asp) and I have several other sites setup in GA and they worked fine. So I don't know why this isn't working. The code above was provided by GA.

我查看并验证了源代码在主页的标题中,并且代码在那里。该站点建立在 Classic ASP (default.asp) 上,我在 GA 中设置了其他几个站点,它们运行良好。所以我不知道为什么这不起作用。上面的代码由 GA 提供。

Can someone please tell me why this isn't working?

有人可以告诉我为什么这不起作用吗?

回答by Cody Bonney

Give this a try.

试试这个。

<script>
    var _gaq = _gaq || [];  
    _gaq.push(['_setAccount', 'UA-4XXXXXXX-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>

If you just set this up, you should be able to see analytics in the "Realtime" panel. Many of the other panels can take over a day before analytics are visible.

如果您刚刚进行设置,您应该能够在“实时”面板中看到分析。许多其他面板可以在分析可见之前接管一天。

If you're using Google Chrome, you might also try the tag debugger plugin by ObservePointor the Official Tag assistant plugin by Google

如果你使用谷歌浏览器,你也可以试试ObservePoint的标签调试器插件或谷歌官方标签助手插件

回答by Blexy

Have you tried using a tool like HTTPFox(firefox) Google's Google Analytics Debugger(chrome) to monitor http requests? This will tell you if it's firing or not. If it is, check the real-time reports as mentioned in the comments. If it's not, then it's an issue with the code or your page.

您是否尝试过使用HTTPFox(firefox) Google 的Google Analytics Debugger(chrome) 之类的工具来监控 http 请求?这会告诉你它是否在发射。如果是,请检查评论中提到的实时报告。如果不是,则是代码或您的页面的问题。

回答by Frank G.

I figured out what the problem was. I setup an Account with GA using there new Beta Feature and I guess the account wouldn't work with the code they gave me. So I deleted that account and recreated a Classic Account and took the code I posted above and it worked fine. Thank you all for your help!!

我想出了问题所在。我使用新的 Beta 功能在 GA 上设置了一个帐户,我猜该帐户无法使用他们给我的代码。因此,我删除了该帐户并重新创建了一个经典帐户,并使用了我在上面发布的代码,并且运行良好。谢谢大家的帮助!!

回答by Tyler Buchea

New Analytics Means New APIs

新的分析意味着新的 API

You are using the "Universal Analytics" snippet which is Google's new system they are trying to transition everyone over to. Some of the APIs have changed including event tracking.

您正在使用“Universal Analytics”片段,这是 Google 的新系统,他们正试图将每个人转移到。某些 API 已更改,包括事件跟踪。

Make sure you are using this:

确保你正在使用这个:

ga('send', 'event', category, action, label);

Instead of this:

取而代之的是:

_gaq.push(['_trackEvent', category, action, label]);

For event tracking. Here is a thorough blog post on the subject http://blog.tylerbuchea.com/tracking-events-in-googles-new-universal-analytics/

用于事件跟踪。这是关于该主题的详尽博客文章http://blog.tylerbuchea.com/tracking-events-in-googles-new-universal-analytics/

回答by jmunsch

I had a similar issue, I was hitting the wrong url, so I am redirecting somedomain.comto www.somedomain.com

我有一个类似的问题,我打错了网址,所以我重定向somedomain.comwww.somedomain.com

see: https://support.google.com/tagmanager/answer/6103683?vid=1-635771588228302150-2080886913&rd=1

参见:https: //support.google.com/tagmanager/answer/6103683?vid=1-635771588228302150-2080886913&rd=1

Make sure that you have published your tag, and that your trigger is formed properly to allow it to fire on the page you are testing. Note that it can take a few seconds for the container to publish, and that you should do a hard refresh (e.g. ctrl+F5) of the page to see the changes.

Check that your trigger isn't unnecessarily specific. For example, if you define a URL based trigger and begin the URL with "http://www.example.com", the tag will not fire for URLs "https://www.example.com" (using "https") and "http://example.com" (without the "www").

Google Tag Manager can only fire tags within the capabilities of the browser. Most browsers will not open more than six to eight HTTP requests to a single domain at a time. If you have a high number of tags on the same domain firing under the same conditions, tags will only fire within this browser limitation.

确保您已发布您的代码,并且您的触发器已正确形成以允许它在您正在测试的页面上触发。请注意,容器发布可能需要几秒钟的时间,您应该对页面进行硬刷新(例如 ctrl+F5)以查看更改。

检查您的触发器是否具有不必要的特定性。例如,如果您定义基于 URL 的触发器并以“ http://www.example.com”开头,则不会针对 URL“ https://www.example.com”(使用“https”)触发代码) 和“ http://example.com”(不带“www”)。

Google 跟踪代码管理器只能触发浏览器功能范围内的代码。大多数浏览器一次不会向单个域打开超过 6 到 8 个 HTTP 请求。如果您在相同条件下在同一域上触发了大量代码,则代码只会在此浏览器限制内触发。

回答by Andrew

Just ran into a new version of the "not tracking" issue.

刚刚遇到了“不跟踪”问题的新版本。

Apparently now with the new Google Site Tag (gtag.js) tracking code, Analytics will only start tracking if you're signed up to Google Tag Manager as well and enable the domain there.

显然,现在有了新的 Google 站点标签 (gtag.js) 跟踪代码,只有在您同时注册了 Google 标签管理器并在那里启用域时,Analytics 才会开始跟踪。

My new Analytics property didn't track for half a day after I added the tracking code. After enabling the domain in Google Tag Manager, it started tracking instantly.

添加跟踪代码后,我的新 Google Analytics(分析)媒体资源有半天没有跟踪。在 Google Tag Manager 中启用域后,它立即开始跟踪。

I think this is probably a bug. Hopefully they will fix.

我认为这可能是一个错误。希望他们会修复。

回答by rmaes4

I found out in my case that my content blocker (Wipr on Safari) was blocking the tracking request. Disabling the content blocker fixed this.

我发现我的内容阻止程序(Safari 上的 Wipr)阻止了跟踪请求。禁用内容拦截器解决了这个问题。

回答by exarhis

add a type="text/javascript"to your code

在您的代码中添加type="text/javascript"

  <script type="text/javascript" >
    (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', 'UA-XXXXXXXX-X', 'auto');
    ga('send', 'pageview');
</script>