railslove / rack-tracker

Tracking made easy: Don’t fool around with adding tracking and analytics partials to your app and concentrate on the things that matter.
https://www.railslove.com/open-source
MIT License
647 stars 121 forks source link

How to use outside of controller? #124

Closed olimart closed 5 years ago

olimart commented 5 years ago

Hi, Thanks for the great work. I'm wondering if we could track event from a service object. I'm calling the service in my controller and it looks like this.

class RackTracker
  include Rack::Tracker::Controller

  def call(event, opts = {})
    tracker do |t|
      t.facebook_pixel :track, { type: event, options: opts }
    end
  end
end

However, it is raising the following error:

undefined local variable or method `env' for #<RackTracker:0x00007fb3796d3f88 @event="PageView", @options={}>

I guess I need to pass somehow current environment but how? Thanks

olimart commented 5 years ago

@DonSchado @bumi any suggestion? Thanks

bumi commented 5 years ago

Heja,

as rack-tracker uses the rack env hash your service class would need to have access to that object. So somehow you would need to pass it in from the rails controller.

I've never used it, but something like this could work:

class RackTracker
  include Rack::Tracker::Controller
  attr_accessor :env 

  def initialize(env)
    @env = env
  end 

  def call(event, opts = {})
    tracker do |t|
      t.facebook_pixel :track, { type: event, options: opts }
    end
  end
end

and in the controller:

  RackTracker.new(request.env).call(...)

You can also access the env hash directly (without including the Rack::Tracker::Controller) as described here (end)

olimart commented 5 years ago

Thanks for the explanation. I’ll guve it a try. Thanks a lot

Le mer. 17 oct. 2018 à 09:15, Michael Bumann notifications@github.com a écrit :

Heja,

as rack-tracker uses the rack env https://www.rubydoc.info/github/rack/rack/master/file/SPEC#label-The+Environment hash your service class would need to have access to that object. So somehow you would need to pass it in from the rails controller.

I've never used it, but something like this could work:

class RackTracker include Rack::Tracker::Controller attr_accessor :env

def initialize(env) @env = env end

def call(event, opts = {}) tracker do |t| t.facebook_pixel :track, { type: event, options: opts } end endend

and in the controller:

RackTracker.new(request.env).call(...)

You can also access the env hash directly (without including the Rack::Tracker::Controller) as described here https://github.com/railslove/rack-tracker#sinatra--rack (end)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/railslove/rack-tracker/issues/124#issuecomment-430623563, or mute the thread https://github.com/notifications/unsubscribe-auth/AAhbqn-B6Y35FlI9ZW8IbzFocnOv8eCcks5uly2BgaJpZM4XaY4l .

-- Regards,

Olivier SIMART

(819) 962-1981 Blog http://blog.yafoy.com/ | T•Surprise https://tsurprise.com/ | Les Bonbecs https://lesbonbecs.com/ | Yapay https://yapay.xyz/ | Formtastic https://formz.herokuapp.com/

bumi commented 5 years ago

great, please report how you've solved it and close this issue if it works. and maybe it is something that should be added to the readme? (a PR would be appreciated! ;)) thanks!

olimart commented 5 years ago

Thanks @bumi I haven't had the chance yet to make the change but appreciate your help.