jQuery:使用PHP单击跟踪

nee*_*zer 9 php jquery click-tracking

是的,我知道Google Analytics.我们将其用于整体网站指标,我知道我们可以跟踪各个链接.然而,我们需要非常具体的链接跟踪解决方案,我们需要跟踪数据提供给我们的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'); });
    });
  };
Run Code Online (Sandbox Code Playgroud)

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

现在,这个代码可以在我的PHP后端和我的机器上正常工作,但它似乎并不能为其他人可靠地工作.有时通过jQuery传入的参数不会被传入,导致数据库中的记录没有name,urlref.

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

我已经在我的工作场所访问的每个浏览器上对它进行了实时测试,并且所有这些浏览器都适用于我; 但是,创建的所有记录(不是我的计算机)中大约有75%是空白的(大多数是使用相同的浏览器).

为什么会发生这种情况?

nee*_*zer 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'));
Run Code Online (Sandbox Code Playgroud)

...这似乎比我上面写的jQuery插件更加快捷,到目前为止已足够快,可以跟踪我抛出的所有请求.

希望能帮助别人!