在访问网站上 X 个页面后执行此操作

发布于 09-25 19:47 字数 208 浏览 9 评论 0原文

有没有一种简单的方法可以跟踪您网站上的用户使用 Javascript(最好是 jQuery)访问了多少页面?

我需要在用户访问网站上超过 5 个页面后执行一个函数。

我正在考虑将 window.location.href 存储到一个变量中并检查它以查看它是否与下一页匹配。如果没有,我会将其注册为一页更改等。这在理论上听起来对我来说是合理的,但如果我走错了路,请指导我。

Is there an easy way to track how many pages a user on your site has visited with Javascript (preferably jQuery)?

I need to execute a function after a user has visited more than 5 pages on the site.

I was thinking of storing the window.location.href into a variable and checking it to see if it matches the next page. If it doesn't I would register it as one page change and so on. This sounds theoretically sound to me but please do direct me if I'm on the wrong path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝影如岚2024-10-02 19:47:02

jQuery 没有本机 cookie 功能。

虽然我完全同意这应该在服务器端完成,并且我自己永远不会这样做...您可以完全使用普通的 Javascript 来完成此操作。

我已经整理了一个适合您目的的函数。仅当他们访问了 6 个页面,其中每个页面都与前一个页面不同时,它才会返回 true。

function mySiteTracker() {
    var pageCounter = readCookie('pageCounter');
    var pageLast = readCookie('pageLast');
    var pageInt = 0;
    if (pageCounter) {
        pageInt = parseInt(pageCounter);
        if (pageInt > 5) return true;
        if (window.location.href == pageLast) return false;
    }
    createCookie('pageCounter',(pageInt + 1));
    createCookie('pageLast',window.location.href);
    return false;
}


//The following 2 functions providing the get/set 
//functionality are MODIFIED from: http://www.quirksmode.org/js/cookies.html

function createCookie(name,value) {
    document.cookie = name+"="+value+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

我没有专门对此进行测试...如果您有任何问题,请告诉我。

jQuery does not have native cookie functions.

While I completely agree this should be done server-side and would never do this myself... You can do this entirely with plain-old Javascript.

I've put together a function that should suit your purposes. It returns true only if they've visited 6 pages where each page was different than the immediately preceding page.

function mySiteTracker() {
    var pageCounter = readCookie('pageCounter');
    var pageLast = readCookie('pageLast');
    var pageInt = 0;
    if (pageCounter) {
        pageInt = parseInt(pageCounter);
        if (pageInt > 5) return true;
        if (window.location.href == pageLast) return false;
    }
    createCookie('pageCounter',(pageInt + 1));
    createCookie('pageLast',window.location.href);
    return false;
}


//The following 2 functions providing the get/set 
//functionality are MODIFIED from: http://www.quirksmode.org/js/cookies.html

function createCookie(name,value) {
    document.cookie = name+"="+value+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

I didn't specifically test this... let me know if you have any issues.

温柔戏命师2024-10-02 19:47:02

如果您想跨网站页面跟踪用户,您最好使用 Cookie。虽然您不需要 jquery,但有一个很好的 jQuery 插件可以帮助您管理 cookie。 (http://code.google.com/p/cookies/wiki/Documentation)。

使用 cookie,您可以在每个页面上运行 JavaScript 来检查站点 cookie 是否存在。如果它不存在,您将创建一个值为 1 的值。如果它存在,您将增加它的值。然后,一旦您看到该值为 5,您就知道他们访问了 5 个页面。

If you want to track a user across pages on your site you are best to use Cookies. While you don't need jquery for this there is a nice jQuery plugin the helps you manage cookies. (http://code.google.com/p/cookies/wiki/Documentation).

With cookies, on each page you would run JavaScript that would check for the existence of a site cookie. If this doesn't exist you would create it with a value of 1. If it does exist you would increment its value. Then once you see the value is 5 you know they visited 5 pages.

逐鹿2024-10-02 19:47:02
$(document).ready(
function(){
    if ($.cookie('userViews')) {
        var views = parseInt($.cookie('userViews')) + 1;
    }
    else {
        var views = 1;
    }

    $.cookie('userViews',views,{expires:30});

    if (views > 5) {
        // do whatever you want to do after 5 views.
    }

}
);

仅当使用 jQuery cookies 插件时,上述代码才有效。

值得指出的是 - 也许明显 - 任何基于 JavaScript 的解决方案都容易受到用户干扰、脚本可以更改、cookie 被操纵等等。如果您要求这是至少相对安全的解决方案1,那么服务器端解决方案(使用您喜欢的任何服务器端脚本语言)可能是更好的选择方法。


  1. 显然,基于服务器的观看次数解决方案很容易被利用,特别是因为F5非常容易获得。
$(document).ready(
function(){
    if ($.cookie('userViews')) {
        var views = parseInt($.cookie('userViews')) + 1;
    }
    else {
        var views = 1;
    }

    $.cookie('userViews',views,{expires:30});

    if (views > 5) {
        // do whatever you want to do after 5 views.
    }

}
);

The above code works only if the jQuery cookies plug-in is used.

It's worth making the -perhaps obvious- point that any JavaScript based solution is prone to user-interference, scripts can be changed, cookies manipulated, etcetera. If you require this to be at least relatively secure solution1, then a server-side solution (using whatever server-side scripting language you prefer) is likely to be a better approach.


  1. Obviously a server-based view-count solution can also be easily gamed, particularly since F5 is so easily available.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文