1

I am using the mixpanel gem for my application. It acts as middleware and dynamically inserts the code into the head for any action. I'd like to be able to turn it off for specific actions (for instance, we have an action that sends an email and we'd rather not have the code there). Any ideas for how to accomplish this?

Thanks a lot.

John Bachir
  • 22,495
  • 29
  • 154
  • 227
Jason Logsdon
  • 507
  • 5
  • 19

3 Answers3

1

it seems that mixpanel have updated their gem

Prevent middleware from inserting code
Note: Only applies when Rack Middleware is setup.
Occasionally you may need to send a request for HTML that you don't want the middleware to alter. In your AJAX request include the header "SKIP_MIXPANEL_MIDDLEWARE" to prevent the mixpanel code from being inserted.

   $.ajax("/path/to/api/endpoint", {
     headers: {"Skip-Mixpanel-Middleware": true}, // valid http headers don't allow underscores and get filtered by some webservers
     success: function(data) {
       // Process data here
     }   }); 

    //Alternatively, you can add this line of code to your controller to temporarily disable the middleware:

    Mixpanel::Middleware.skip_this_request

Taken from: https://github.com/zevarito/mixpanel#prevent-middleware-from-inserting-code

gef
  • 7,025
  • 4
  • 41
  • 48
  • 1
    Awesome, looks like that would solve it. I'm not working on that project any more so I can't verify but I'll mark yours as correct since it's an actual solution. Thanks! – Jason Logsdon Jun 12 '13 at 15:52
0

We couldn't figure out a way to do this and ended up stripping it out (using gsub) after the fact. If anyone else has a better solution down the road I will definitely mark yours as right, I just want to close the question. Thanks

Jason Logsdon
  • 507
  • 5
  • 19
0

From the Mixpanel docs:

In your application_controller class add a method to instance mixpanel.

before_filter :initialize_mixpanel

def initialize_mixpanel
  @mixpanel = Mixpanel::Tracker.new("YOUR_MIXPANEL_API_TOKEN", request.env, true)
end

Since it's initialized by a before_filter you can use skip_before_filter in your other controllers to, well, skip it for certain actions, or for all except a certain action, e.g.:

class SomeController < ActionController::Base
  skip_before_filter :initialize_mixpanel, :only => [ :create, :new ]

  # or

  skip_before_filter :initialize_mixpanel, :except => [ :update ]

end
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • This doesn't seem to do it. It's actually called via middleware, not through a before_filter. Like this: config.middleware.use "Mixpanel::Tracker::Middleware", "API_KEY" – Jason Logsdon Mar 06 '12 at 15:53
  • Oh, sorry about that. The documentation was unclear (or, as likely, my memory of how middleware works is fuzzy). Hopefully someone who knows more will come along; otherwise you could just ask the developers directly. [This might be useful](http://stackoverflow.com/questions/920719/how-do-i-use-a-rack-middleware-only-for-certain-paths) to you, too (e.g. you could subclass Mixpanel::Tracker), or [this](http://stackoverflow.com/questions/4598264/use-some-middleware-only-for-specific-rack-website) (similar). Good luck! – Jordan Running Mar 06 '12 at 17:21
  • Thanks for the links Jordan, I'll check them out – Jason Logsdon Mar 06 '12 at 18:38