Pergunta

I'm using Impressionist gem to record page views.
Let's say I have a Question model and an 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 the questions\show.html.erb, I have a list of answers.
When a user hovers over an answer, they see 2 links appear: details and go to.
The details link just opens the answer show page (which provides more details) and records a show impression and works correctly:

answers_controller.rb:

 impressionist(@answer)

The go to page is tricker. When the user clicks on the 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} do %>
            <span class="small_image">
               <%= image_tag(answer.img_url, :alt => 'go to answer', :class =>'img_answer')  %>
             </span>
  <% end %>

, I run the 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 and runs a web service and then opens the returned external link in a new window.
My problem is trying to capture this external click as an impression.
What is the best way to do this? Currently, when I click the external link, the Questions controller, show action runs.

Specific question #1: This "seems" correct in that I don't want to go anywhere (another window has already opened with the external link). Is it correct to use the questions controller, show action and if params[:id]=="Go to" then just stay put.

Specific question #2: If this is correct, then is there a way to send Impressionist an :action_name (I'd send "go to" instead of the default that it's sending now of "show":

Specific question #3: If it's not correct, then what should I be doing? Perhaps going to the Answers controller, to a custom-defined "go_to"?

TIA

Foi útil?

Solução

OK - I have a solution (not sure if it's the best one but it works)

  1. question.rb and answer.rb from above stay the same
  2. To count the impressions of the answer show.html.erb page, I still use in the answer_controller, :

     impressionist(@answer)
    
  3. To count the impressions when I redirect from answer, I created a new def in the answer_controller:

    def visit_external_site
      @answer= Answer.find(params[:answer_id])
      impressionist(@answer)
      respond_to do |format|
        format.html {render :nothing => true, :status => 200}
      end
    end
    

(The part I'm not sure about is the format.html (I don't want to go anywhere but still....)

  1. My routes.rb has this:

    match '/:answer_id' => 'answers#visit_external_site', :as => 'visit_external_site'
    
    1. The questions\show.html.erb page has this now:

 <% @question.answers.each do |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} do %>
                <span class="small_image">
                    <%= image_tag(answer.img_url, :alt => 'go to answer', :class => 'img_answer') %>
                </span>
              <% end %>
<% end %>
  1. My jQuery is still the same as above

If anyone has a better way to doing this, do tell; I'm not sure if this way is "kosher"

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top