如何在 Analytics 中跟踪 # 个 ajax 链接?

The*_*Kid 0 ajax seo jquery google-analytics

我有一个带有普通 Google 分析代码的基本单页网站:

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-##-##']);
  _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);
  })();
Run Code Online (Sandbox Code Playgroud)

然而,我的链接就像书签——它们基本上是从单页滑下。单击时我的链接本质上是页面,但在同一模板内 - 它们滑动以显示内容元素。

<a href="#section-3">about us</a>
Run Code Online (Sandbox Code Playgroud)

有没有办法更新分析跟踪器或以某种方式跟踪链接?所以在分析中,我会看到人们在一个页面上花费了很多时间等等。

Mak*_*sym 5

尝试这个。在 HEAD 中放置:

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-##-##']);
  _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);
  })();
Run Code Online (Sandbox Code Playgroud)

在页脚或任何其他地方 - 这个:

jQuery(function($){

  $("a[href*='#']").on("click", function(){
        var $url = $(this);

        _gaq.push(['_trackEvent', 'ajaxLink', $url.text(), $url.attr('href')]);
        console.log('_trackEvent', $url.text(), $url.attr('href'));
  });
});?
Run Code Online (Sandbox Code Playgroud)

并且 HTML 应该是对链接的 #Id 引用。如:

<a href="#home-page">Home</a>
<br />
<a href="#about-us-page">About Us</a>
Run Code Online (Sandbox Code Playgroud)

我用这个来学习它。

UPD:这是示例:http : //jsfiddle.net/K2PcL/

谢谢。