keolo / mixpanel_client

Ruby interface to the Mixpanel Data API
MIT License
148 stars 72 forks source link

Add an optional hash attribute to each MixpanelClient instance #16

Closed nateberkopec closed 12 years ago

nateberkopec commented 12 years ago

In our Rails app, each User instance has it's own instance of MixpanelClient, because we use the Data Export API to give our Users data specific to them.

So, our request looked something like this:

result = client.request do
      ...
      on 'properties["user"]'
end

So, that would pull down the data for EVERY user. Obviously, this is pretty inefficient if you've got a ton of users, and we wanted to pull down just one user's mixpanel events.

So, my solution was to add an optional hash to the MixpanelClient when it's instantiated, which can then be accessed inside of its request blocks:

result = client.request do
      ...
      on 'properties["user"]'
      where %{"#{self.options[:username]}" == properties["user"]}
end

I'm pretty new to Ruby and contributing to open source, so if there's a better way of doing this, please let me know! The syntax (especially self.options[:username] seems kind of ugly anyway.

keolo commented 12 years ago

Looks interesting. I'll take a closer look hopefully this week. Would you know enough to write a spec for this (using rspec)?

nateberkopec commented 12 years ago

Sure! I'll write up a spec this week.

keolo commented 12 years ago

You can now pass arbitrary options to MixpanelClient#request including 'on' and 'where'. I think this satisfies your requirement?