Pregunta

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.

¿Fue útil?

Solución

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

Otros consejos

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

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top