ruby on rails - RoR Impressionist capture external click -
i'm using impressionist gem record page views.
let's have question model , answer model:
question.rb:
class question< activerecord::base has_many :answers is_impressionable
answer.rb:
class answer< activerecord::base belongs_to :question is_impressionable
in questions\show.html.erb, have list of answers.
when user hovers on answer, see 2 links appear: details , go to.
details link opens answer show page (which provides more details) , records show impression , works correctly:
answers_controller.rb:
impressionist(@answer)
the go page tricker. when user clicks on link:
<%= link_to "go to", :class => 'answer_link', :remote=>:true, :data=>{outval: answer.type_url, cuid: answer.id.to_s, refval: user_question_path(@question.user, @question), txtval: answer.img_url} %> <span class="small_image"> <%= image_tag(answer.img_url, :alt => 'go answer', :class =>'img_answer') %> </span> <% end %>
, run following jquery snippet:
$('.answer_link').click (-> url_val='' url_val = "http://api.somewebservice.com/api/click?key=abc123" url_val += "&out=" + $(this).data('outval') url_val += "&loc=" + $(this).data('refval') url_val += "&cuid=" + $(this).data('cuid') url_val += "&ref=" + $(this).data('refval') url_val += "&txt=" + $(this).data('txtval') window.open url_val )
this works correctly , runs web service , opens returned external link in new window.
problem trying capture external click impression.
best way this? currently, when click external link, questions controller, show action runs.
specific question #1: "seems" correct in don't want go anywhere (another window has opened external link). correct use questions controller, show action , if params[:id]=="go to" stay put.
specific question #2: if correct, there way send impressionist :action_name (i'd send "go to" instead of default it's sending of "show":
specific question #3: if it's not correct, should doing? perhaps going answers controller, custom-defined "go_to"?
tia
ok - have solution (not sure if it's best 1 works)
- question.rb , answer.rb above stay same
to count impressions of answer show.html.erb page, still use in answer_controller, :
impressionist(@answer)
to count impressions when redirect answer, created new def in answer_controller:
def visit_external_site @answer= answer.find(params[:answer_id]) impressionist(@answer) respond_to |format| format.html {render :nothing => true, :status => 200} end end
(the part i'm not sure format.html (i don't want go anywhere still....)
my routes.rb has this:
match '/:answer_id' => 'answers#visit_external_site', :as => 'visit_external_site'
- the questions\show.html.erb page has now:
<% @question.answers.each |answer| %> <%= link_to url_for(:controller => :answers, :action => :visit_external_site, :answer_id => answer), :class => 'answer_link', :remote => true, :data => {outval: answer.type_url, cuid: answer.id.to_s, refval: user_question_path(@question.user, @question), txtval: answer.img_url} %> <span class="small_image"> <%= image_tag(answer.img_url, :alt => 'go answer', :class => 'img_answer') %> </span> <% end %> <% end %>
- my jquery still same above
if has better way doing this, tell; i'm not sure if way "kosher"
Comments
Post a Comment