views:

121

answers:

2

My site provides a javascript that shows the rate of the Dutch equivalent of the Dow Jones index. Users can embed this script in their website.

It looks like this:

<script type="text/javascript" src="http://www.aexscript.nl/r/gratis"&gt;&lt;/script&gt;

The corresponding controller action looks like this:

def show
  @script = Script.find_by_code(params[:code])
  @rate = Rate.find(:first)

  respond_to do |format|
    format.js # show.js.erb
  end
end

I want to log the URL of the site the javascript has been embedded on. How can I do this?

+2  A: 

Your webserver should give you access logs; which will show you every HTTP request it received.

Then you can just grep for requests for this file.

Example for Apache

matt b
Is it this simple? Since the script can be embedded in any webpage (of which I have no knowlegde), I didn't expect this to be logged. I am only using Mongrel as a webserver at the moment. I will have a look at production.log. Thanks.
captaintokyo
Right, the script can be referenced from any server in the world, but if they are referring to your domain name, the requests have to come to your web server... meaning you have the chance to log it.
matt b
+2  A: 

Make a database table that logs your hits. Whenever someone hits show, log it.

You can rip my code for something similar from http://github.com/saizai/hyperdictionary - take the four_oh_four controller, model, & migration.

If you have other information (eg you know the logged on user somehow? you know whose link it is?) you can easily add it to that table as a foreign key. Then you'd do something like

user.js_hits.find(:all, :select => "name, count(id) as count", :group => 'name')

And drop it in a simple view (see my app/views/four_oh_fours/index.html.erb for a simple example).

Sai Emrys
Thanks, this is what I was looking for. Apparently the URL of the site my javascript is embedded on is in request.env['HTTP_REFERER'].
captaintokyo
Oh, yeah. A nicer way to refer to it is request.referer (I haven't finished cleaning up this code yet :-P).See http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html#M000478
Sai Emrys