9

是的,我知道谷歌分析。我们将它用于我们的整体网站指标,我知道我们可以跟踪单个链接。但是,我们需要一个针对非常具体的链接的跟踪解决方案,并且我们需要我们的 Web 应用程序实时可用的跟踪数据,所以我编写了自己的解决方案:

jQuery:

  $.fn.track = function () {
    var source, url, name, ref, $this;
    $this = $(this);
    if (window.location.search.substring(1) != '') {
      source = window.location.pathname + "?" + window.location.search.substring(1);
    } else {
      source = window.location.pathname;
    }
    url = jQuery.URLEncode($this.attr('href'));
    name = $this.attr('name');
    ref = jQuery.URLEncode(source);
    $this.live('click', function (click) {
      click.preventDefault();
      $.post('/lib/track.php', {
        url: url,
        name: name,
        ref: ref
      }, function () { window.location = $this.attr('href'); });
    });
  };

... 使用 jQuery URLEncode 插件 ( http://www.digitalbart.com/jquery-and-urlencode/ )。

现在,这段代码在我的 PHP 后端和我的机器上运行良好,但对于其他人来说似乎并不可靠。有时通过 jQuery 传入的参数没有传入,导致数据库中的记录没有name,urlref.

对于我的一生,我无法弄清楚为什么会发生这种情况;我知道这$.post是触发的,因为数据库中有记录(在 PHP 中,我还记录了请求的 IP 以及时间戳),但在许多情况下,PHP 脚本$_POST从 jQuery 接收空白变量。

我已经在工作场所可以访问的每个浏览器上对其进行了实时测试,所有这些对我来说都很好;然而,大约 75% 的所有记录(不是由我的计算机创建的)都是空白的(其中大多数都使用与我相同的浏览器)。

为什么会发生这种情况?

4

2 回答 2

7

我认为,最后,我的问题最终是 jQuery 解析请求所花费的时间太长,而且我非常坚持不希望使链接“依赖”于 javascript(要么他们不会'没有它就无法工作,或者用户必须等待跟踪请求完成才能访问新页面)。

在网上浏览了许多其他解决方案(借鉴了一些解决方案并受到其他解决方案的启发)之后,我在本机 javascript 中找到了以下解决方案:

if (document.getElementsByClassName === undefined) { // get elements by class name, adjusted for IE's incompetence
    document.getElementsByClassName = function(className) {
      var hasClassName, allElements, results, element;

        hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
        allElements = document.getElementsByTagName("*");
        results = [];

        for (var i = 0; (element = allElements[i]) !== null; i++) {
            var elementClass = element.className;
            if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass)) {
                results.push(element);
            }
        }

        return results;
    };
}

function addTracker(obj, type, fn) { // adds a tracker to the page, like $('xxx').event
  if (obj.addEventListener) {
    obj.addEventListener(type, fn, false);
  } else if (obj.addEventListener) {
    obj['e' + type + fn] = fn;
    obj[type + fn] = function() {
      obj['e' + type + fn]( window.event );
    };
    obj.attachEvent('on' + type, obj[type + fn]);
  }
}

function save_click(passed_object) { // this function records a click
  var now, then, path, encoded, to, from, name, img;

  now = new Date();
  path = '/lib/click.php';
  from = (window.decode) ? window.decodeURI(document.URL) : document.URL;
  to = (window.decodeURI) ? window.decodeURI(passed_object.href) : passed_object.href;
  name = (passed_object.name && passed_object.name != '') ? passed_object.name : '[No Name]';

  // timestamp the path!
  path += '?timestamp=' + now.getTime();

  path += '&to=' + escape(to) + '&from=' + escape(from) + '&name=' + name; // compile the path with the recorded information
  img = new Image();
  img.src = path; // when we call the image, we poll the php page; genius!

  while (now.getTime() < then) {
    now = new Date(); // resets the timer for subsequent clicks
  }
}

function get_targeted_links(target) { // finds targeted elements and wires them up with an event handler
  var links, link;
  if (document.getElementsByClassName) {
    links = document.getElementsByClassName(target);
    for (var i = 0; i < links.length; i++) {
      link = links[i];
      if (link.href) {
        addTracker(links[i], 'mousedown', save_click(links[i])); 
      }
    }
  }
}

addTracker(window, 'load', get_targeted_links('trackit'));

... 这似乎比我上面写的 jQuery 插件要快得多,并且到目前为止已经足够快以跟踪我向它抛出的所有请求。

希望对其他人有所帮助!

于 2010-01-25T15:55:10.210 回答
0

这些“点击”可能来自机器人或禁用 JS 的人。如果您必须跟踪单击的链接,为什么不考虑仅 JS 链接,即。将 URL 放在 href 以外的其他属性中,然后使用您的点击处理程序来处理它,在您的 track.php 中添加推荐检查

您是否还检查过所有元素是否都是链接?

于 2009-12-11T17:11:52.437 回答