javascriptruby-on-railsrubytwitter-bootstrapimpressions

Ruby on Rails: Counting how many times an images appears on screen


I have a Ruby on Rails project with a bootstrap theme. The top of the index page has a carousel which rotates a set of images on screen. I need to be able to count how many times an image appears onscreen. This will be used to determine how many people are seeing particular advertisements. Is there a way to do this?

I am pretty new to Rails so I'm not sure what its capabilities are. I was thinking that one way to do it is to have the JavaScript which runs the Carousel image switching trigger an event on the server so Rails can increment a counter.

I have also been looking into https://github.com/charlotte-ruby/impressionist but I am not sure how to trigger the impression counter.

Does anyone know how I can count how many times an images appears on screen? Thanks


Solution

  • Imho the easiest way for you is to go with https://github.com/charlotte-ruby/impressionist gem as u mentioned above. If i were you i would do maybe separate controller with some simple actions to serve only images for carousel which you want to log.

    CarouselController < ApplicationController
      impressionist :actions=>[:show]
    
      def show
        # u can also play with params[:id] to serve many images depending on id
        send_file Rails.root.join("public", "file.gif"), type: "image/gif", disposition: "inline"
      end
    end
    

    after you set-up routes for this controller in you app, then on client/js code u can use routes helper path like carousel_path(1) or something similar to serve images from rails

    Yes, the solution above will log when you load your page with carousel no matter if the picture will be on slide 3 or slide 1 and even show up. If you want to log this particular events then you need hook some callback with carousel(slide 3 show) if possible and then trigger some custom ajax call to rails to log. If you can't hook callback then u probably need some write some javascript code to check when element change on page(slide 3 show)..