views:

1248

answers:

2

I'm currently writing a website that allows people to download Excel and text files. Is there a way to redirect to a different page when they click, so that we run javascript and do analytics (i.e. keep download count)? Currently, nothing prevents the user from simply right-clicking and saving.

Edit:

To be more specific, it would be nice for a single or double click of a file link to redirect to a temporary download page for analytics, then have the file be downloaded.

A: 

I'm not sure what you are asking here, are you trying to figure out how to redirect in the controller or are you trying to override the right-click behavior in the browser?

To redirect in the controller you can do something like this documented here.

redirect(controller:"book",action:"list")

If you are trying to change button or link behavior that's client side and will require some Javascript most likely.

If you clarify I might be able to help more.

Joe Skora
Just added more comments, what I wanted to do is redirect to a temp download page on a single or double click, then have the file be downloaded. Similar to how - say - savefile does it. Thanks!
echoblaze
+2  A: 

I started describing how you might do this in Grails but then remembered most analytics services (Google, Omniture, etc.) will let you track downloaded files by using the onclick event. If you have some custom javascript based tracking you're doing, you can do the same thing. The onclick will get called before the document starts downloading. For example:

<a href="/path-to-download-file" onclick="record_download('filename')">myfile.txt</a>

More specifically for Google Analytics, here's some javascript to do this automatically: http://www.goodwebpractices.com/downloads/gatag.js

mbrevoort