Rails: How can I log on which site my javascript is embedded?
-
07-07-2019 - |
Question
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"></script>
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?
Solution
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).
OTHER TIPS
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