Alex Gaynor wrote: > On Tue, Apr 14, 2009 at 1:21 PM, ab3...@gmail.com <ab3...@gmail.com> wrote: > >> I'm new to Django. I'm trying to figure out if there is a well >> established design paradigm to collect statistics on hyperlink usage. >> >> When a visitor follows a <a href=...> link to an external website, I >> would like to insert the following information into a mysql database >> table: >> >> - the datetime of the click >> - the visitor's IP address >> - perhaps the visitor's url if it is easily available >> - key identifying which external link was selected >> >> Note: All visitors are anonymous. My site does not ask them to log in >> or support username / password. >> >> I'd also like to be able to collect similar stats for page visits, >> although I have a less-detailed workaround for this using Google >> Analytics. > The only way to know when an external link is clicked is to have some > javascript fire that sends off an AJAX request to your site to tell it an > external link was pressed, you then need to have a view that handles that > data and inserts it into the DB.
In addition to JavaScript, there's a "ping" attribute[1] in HTML5 <a href="http://example.com" ping="http://mysite.example.org/ping/some_id">thing</a> which is supported to varying degrees by browsers. Both of these are brittle -- if the browser doesn't support the ping attribute, or if your visitors browse with JavaScript disabled (yay, NoScript!) The only sure way to track outbound linking is to link to an internal page (that does the link tracking) which then returns a 3xx result code[2] (303 or 307), setting the Location header to the external URL. Thus, instead of linking with <a href="http://external.example.com/wherever">thing</a> you link to <a href="/external_url/42">thing</a> and then /external_url/42 returns the 303/307 with the Location pointed at "http://external.example.com/wherever". It requires your app to internally track the mapping from ID to external URL. It also has the weakness of spoofability -- I could put <img src="http://yoursite.example.org/external_url/42"> on my page, and every time my page was hit, it would look like somebody was doing an outbound link from your page to an external site. You might be able to use Referer[3] (sic) tracking at the redirector link, but again, it's only an optional header, not a required header, so us privacy buffs may have "referer"s disabled. The only other way I know around this is produce some sort of single-use token for each URL displayed, but then you have to track a huge number of URLs with their unique token. And if a page is cached by something like Squid, only the first person to view click that link behind that cache counts. As you can see, there are lots of options, but all of them have holes in them. Choose your favorite poison. Hope this helps give you some ideas of how to do it. -tim [1] http://whatwg.org/specs/web-apps/current-work/#ping [2] http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4 [3] http://en.wikipedia.org/wiki/Referer --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---