jQuery的:点击使用PHP跟踪
-
19-09-2019 - |
题
是的,我知道谷歌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'); });
});
};
...使用jQuery URLEncode的插件( http://www.digitalbart.com/ jquery的和 - 用urlencode / )。
现在,这个代码工作正常与我的PHP后台和我的机器上,但它似乎并不可靠地为其他人工作。有时通过jQuery传递的参数不传递,导致在没有name
,url
或ref
数据库中的记录。
有关我的生活,我不明白为什么这可能发生;我知道$.post
被触发,因为有记录在数据库中(在PHP中,我还记录请求的IP与时间戳一起),但在许多情况下,PHP脚本从jQuery的接收空白$_POST
变量。
我测试过它生活在每一个浏览器我有我的工作场所访问,并且所有的工作对我罚款;然而,创建(不是我的电脑)中的所有记录,约75%是通过空白来(大部分都是用我同样的浏览器)。
为什么会这样发生?
解决方案
我想,到最后,我的问题结束了,这时间太长,请求才能通过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插件迅捷得多,到目前为止,已经足够快的速度来跟踪所有我在它抛出的请求。
希望帮助别人!
其他提示
这些“点击”可以从机器人被到来,或者有人用JS禁用。如果您单击的链接你为什么不考虑JS只链接,即必须跟踪。把URL不同ATTR以外的href,然后用你的点击处理程序来处理它,在你的track.php添加转诊检查
也有你检查是否所有元素都是链接?