Javascript 未定义 Google Analytics pageTracker?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3503511/
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-08-23 04:56:26  来源:igfitidea点击:

Google Analytics pageTracker is not defined?

javascriptgoogle-analytics

提问by leen3o

Little bit confused... I am trying to track mailto links being clicked, but constantly 'pageTracker is not defined' is shown. I have the following code just before my end body tag ()

有点困惑......我试图跟踪被点击的mailto链接,但不断显示“pageTracker is not defined”。我在结束正文标签之前有以下代码()

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-000000']); // This is my account number, I have added the zeros in this editor
  _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>

Then I am using this in my mailto links

然后我在我的mailto链接中使用它

<a href="mailto:[email protected]" onClick="javascript:pageTracker._trackPageview('/mailto/hello');">[email protected]</a>

I cannot see why its not working? Any help would be appreciated

我不明白为什么它不起作用?任何帮助,将不胜感激

回答by joshperry

The new Async Google Analytics code (that you're using) works a bit differently than the non-Async. Any time that you want to call a method on pageTracker you simply push a "message" onto the "_gaq" queue.

新的 Async Google Analytics 代码(您正在使用)与非 Async 的工作方式略有不同。任何时候您想调用 pageTracker 上的方法时,您只需将“消息”推送到“_gaq”队列即可。

<a href="mailto:[email protected]" onClick="_gaq.push(['_trackPageview', '/mailto/hello'])">[email protected]</a>

Although, tracking a mailto link may work better as an event:

虽然,跟踪 mailto 链接可能会更好地作为事件:

<a href="mailto:[email protected]" onClick="_gaq.push(['_trackEvent', 'mailto', 'home'])">[email protected]</a>

For more info take a look at the Async Tracking Users Guide.

有关更多信息,请查看异步跟踪用户指南

回答by Merlinox

We can also add:

我们还可以添加:

//mantain syntax between old and new asynch methods
//http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html#Syntax
function _pageTracker (type) {
    this.type = type;
    this._trackEvent = function(a,b,c) {
       _gaq.push(['_trackEvent', a, b, c]);
    };
}

var pageTracker = new _pageTracker();

in new code to mantain old code in pages.

在新代码中维护页面中的旧代码。

回答by Nanda

Here is the code :

这是代码:

onClick="_gaq.push(['_trackEvent', 'pdf', 'download', '/pdf/myPdf'])">myPdf</a>

回答by Osura

I needed a way to tack downloading PDFs too and heres what I used:

我也需要一种方法来解决下载 PDF 的问题,这是我使用的方法:

<a href="http://www.domain.com/assets/downloads/filename.pdf" target="_blank" onClick="_gaq.push(['_trackEvent', 'Downloads', 'Download', 'Price Brochure PDF'])">Download Brochure</a>

For more info about _trackEvent, heres the API Doc page

有关_trackEvent 的更多信息,请访问API 文档页面