代码之家  ›  专栏  ›  技术社区  ›  YodasMyDad

Google Analytics PageTracker未定义?

  •  34
  • YodasMyDad  · 技术社区  · 14 年前

    有点困惑…我试图跟踪正在单击的mailto链接,但始终显示“pagetracker is not defined”。我在结束body标记()之前有以下代码

    <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>
    

    然后我在我的邮件链接中使用这个

    <a href="mailto:hello@mydomain.co.uk" onClick="javascript:pageTracker._trackPageview('/mailto/hello');">hello@mydomain.co.uk</a>
    

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

    4 回复  |  直到 14 年前
        1
  •  75
  •   joshperry    14 年前

    新的AsyncGoogle分析代码(您正在使用)的工作方式与非Async稍有不同。任何时候如果你想调用pagetracker上的方法,你只需将一条“消息”推到“gaq”队列中。

    <a href="mailto:hello@mydomain.co.uk" onClick="_gaq.push(['_trackPageview', '/mailto/hello'])">hello@mydomain.co.uk</a>
    

    尽管如此,跟踪mailto链接作为一个事件可能会更好地工作:

    <a href="mailto:hello@mydomain.co.uk" onClick="_gaq.push(['_trackEvent', 'mailto', 'home'])">hello@mydomain.co.uk</a>
    

    有关更多信息,请查看 Async Tracking Users Guide .

        2
  •  20
  •   Merlinox    14 年前

    我们还可以添加:

    //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();
    

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

        3
  •  3
  •   joran    13 年前

    代码如下:

    onClick="_gaq.push(['_trackEvent', 'pdf', 'download', '/pdf/myPdf'])">myPdf</a>
    
        4
  •  1
  •   Brian Webster Jason    13 年前

    我也需要一种方法来固定下载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>
    

    有关更多信息 γ径迹 希尔斯 API Doc page