如何记录超链接点击?

时间:2022-09-09 08:04:50

I have a hyperlink which I need to log when it's clicked.

我有一个超链接,我点击它时需要记录。

I created a small prototype, and the problem is repeatable by creating a new MVC 2 Web app project.

我创建了一个小型原型,通过创建一个新的MVC 2 Web应用程序项目可以重复该问题。

Add

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

to the Site.Master file.

到Site.Master文件。

And

public ActionResult LogSomething()
{
    string doNothing = DateTime.Now.ToString();
    return new EmptyResult();
}

to the HomeController.cs file

到HomeController.cs文件

And

<p>
    <a id="lnkTestPost" href="/home/about">Test Post</a>

    <script type="text/javascript">
        $("#lnkTestPost").click(function() {
            $.post("/home/LogSomething");
        });
    </script>
</p>

in Home/Index.aspx

Put a break point in the LogSomething() action method in the HomeController.

在HomeController的LogSomething()操作方法中放置一个断点。

But when I run it, sometimes the breakpoint is hit, other times it isn't.

但是当我运行它时,有时断点被击中,有时则不然。

I'm assuming it's not being hit due to the actual link sending the browser to another page, but shouldn't the post be made before the link is fired?

我假设由于实际链接将浏览器发送到另一个页面而没有被击中,但是在链接被触发之前不应该发布帖子?

Is there anyway I can ensure the logging code is fired?

无论如何我可以确保触发日志代码吗?

Please note; adding the logging functionality in the link target is not an option.

请注意;在链接目标中添加日志记录功能不是一种选择。

EDIT: The original hyperlink is actually to a custom protocol for an app installed on the user PC. So the link is to something like "myapp:myArgs". I didn't mention that in order to keep things simple, unfortunately, since none of the answers really apply to me, I now think it's necessary to mention.

编辑:原始超链接实际上是安装在用户PC上的应用程序的自定义协议。所以这个链接就像“myapp:myArgs”。不幸的是,为了简单起见,我没有提到这一点,因为没有一个答案真的适用于我,我现在认为有必要提一下。

5 个解决方案

#1


10  

If I were doing it, I would probably do what, eg, Google does and setup a URL which logs then redirects. For example:

如果我这样做,我可能会做什么,例如,Google会做什么并设置一个记录然后重定向的URL。例如:

<a href="/home/about">stuff</a>
<script>
    $("a:href").each(function(){
        this.attr("href", "/log?url=" + encodeURIComponent(this.attr("href")));
    });
</script>

Then /log handler would do something like:

那么/ log handler会做类似的事情:

def log(request):
    target_url = request.GET["url"]
    log_link(target_url)
    return HttpRedirect(target_url)

You'd need to put some thought into dealing with external links (eg, how you want to handle http://example.com/log?url=http://evil.com/)… But that's a solvable problem.

你需要考虑处理外部链接(例如,你想如何处理http://example.com/log?url=http://evil.com/)......但这是一个可以解决的问题。

Also, for bonus points, you could swap the URL as the link is clicked (this is what Google does in their search results), so the mouse-over link-preview looks correct.

此外,对于奖励积分,您可以在点击链接时交换URL(这是Google在搜索结果中执行的操作),因此鼠标悬停链接预览看起来是正确的。

#2


3  

Alternatively, you could do what Google Reader does, and put a "from url" in each link:

或者,您可以执行Google Reader所做的操作,并在每个链接中添加“from url”:

<script>
$("a:href").each(function(){
    this.attr("href", this.attr("href") + "?from=" + encodeURIComponent(window.location));
    // (note: this exact code won't handle links which already have GET vars…)
});
</script>

Then use some middleware to log the "from" in inbound requests.

然后使用一些中间件记录入站请求中的“from”。

This has the disadvantage that it won't be able to log clicks going to another domain… But that might be alright.

这样做的缺点是它无法将点击记录到另一个域......但这可能没问题。

#3


1  

I think the browser could easily navigate to the URL before logging the click. How about:

我认为浏览器可以在记录点击之前轻松导航到URL。怎么样:

<p>
    <a id="lnkTestPost" href="#">Test Post</a>

    <script type="text/javascript">
        $("#lnkTestPost").click(function() {
            $.post("/home/LogSomething", function(data) {
              // only tell the browse to navigate away once its logged
              window.location = '/home/about';
            });
        });
    </script>
</p>

#4


0  

An approach that I have seen used by many sites, including google, is to have a "redirector" page, so all the links go through that page/controller, you log it, then from there you can redirect them

我见过许多网站使用的方法,包括谷歌,都有一个“重定向”页面,所以所有链接都通过该页面/控制器,你记录它,然后从那里你可以重定向它们

#5


0  

How 'bout an optional "log" parm on each page that is included as part of the links so you don't have to have any Javascript at all? Granted, each page could be logging something from the referrer page, but it shouldn't care, since you could have it just pass off whatever's in the log parm to the logging infra and go on.

如何在每个页面上包含一个可选的“log”parm作为链接的一部分,这样你就不必拥有任何Javascript了?当然,每个页面都可以从引用者页面记录一些东西,但它不应该关心,因为你可以让它只是将log parm中的任何内容传递给日志记录,然后继续。

#1


10  

If I were doing it, I would probably do what, eg, Google does and setup a URL which logs then redirects. For example:

如果我这样做,我可能会做什么,例如,Google会做什么并设置一个记录然后重定向的URL。例如:

<a href="/home/about">stuff</a>
<script>
    $("a:href").each(function(){
        this.attr("href", "/log?url=" + encodeURIComponent(this.attr("href")));
    });
</script>

Then /log handler would do something like:

那么/ log handler会做类似的事情:

def log(request):
    target_url = request.GET["url"]
    log_link(target_url)
    return HttpRedirect(target_url)

You'd need to put some thought into dealing with external links (eg, how you want to handle http://example.com/log?url=http://evil.com/)… But that's a solvable problem.

你需要考虑处理外部链接(例如,你想如何处理http://example.com/log?url=http://evil.com/)......但这是一个可以解决的问题。

Also, for bonus points, you could swap the URL as the link is clicked (this is what Google does in their search results), so the mouse-over link-preview looks correct.

此外,对于奖励积分,您可以在点击链接时交换URL(这是Google在搜索结果中执行的操作),因此鼠标悬停链接预览看起来是正确的。

#2


3  

Alternatively, you could do what Google Reader does, and put a "from url" in each link:

或者,您可以执行Google Reader所做的操作,并在每个链接中添加“from url”:

<script>
$("a:href").each(function(){
    this.attr("href", this.attr("href") + "?from=" + encodeURIComponent(window.location));
    // (note: this exact code won't handle links which already have GET vars…)
});
</script>

Then use some middleware to log the "from" in inbound requests.

然后使用一些中间件记录入站请求中的“from”。

This has the disadvantage that it won't be able to log clicks going to another domain… But that might be alright.

这样做的缺点是它无法将点击记录到另一个域......但这可能没问题。

#3


1  

I think the browser could easily navigate to the URL before logging the click. How about:

我认为浏览器可以在记录点击之前轻松导航到URL。怎么样:

<p>
    <a id="lnkTestPost" href="#">Test Post</a>

    <script type="text/javascript">
        $("#lnkTestPost").click(function() {
            $.post("/home/LogSomething", function(data) {
              // only tell the browse to navigate away once its logged
              window.location = '/home/about';
            });
        });
    </script>
</p>

#4


0  

An approach that I have seen used by many sites, including google, is to have a "redirector" page, so all the links go through that page/controller, you log it, then from there you can redirect them

我见过许多网站使用的方法,包括谷歌,都有一个“重定向”页面,所以所有链接都通过该页面/控制器,你记录它,然后从那里你可以重定向它们

#5


0  

How 'bout an optional "log" parm on each page that is included as part of the links so you don't have to have any Javascript at all? Granted, each page could be logging something from the referrer page, but it shouldn't care, since you could have it just pass off whatever's in the log parm to the logging infra and go on.

如何在每个页面上包含一个可选的“log”parm作为链接的一部分,这样你就不必拥有任何Javascript了?当然,每个页面都可以从引用者页面记录一些东西,但它不应该关心,因为你可以让它只是将log parm中的任何内容传递给日志记录,然后继续。