zevarito / mixpanel

Simple lib to track events in Mixpanel service. It can be used in any rack based framework.
MIT License
274 stars 84 forks source link

How to make alias work ? #124

Closed jeremybdk closed 3 years ago

jeremybdk commented 11 years ago

Hello, I am trying to make mixpanel.alias work when I set my user on mixpanel so that his history get added to his profile on mixpanel.

In my Create method I do this : mixpanel.track 'User Created', { :username => resource.username, :distinct_id => resource.email, :time => resource.created_at, :name => resource.full_name } mixpanel.set({:distinct_id => resource.email, :ip => resource.current_sign_in_ip}, { :username => resource.username, :created => resource.created_at, :name => resource.full_name})

I now wonder how and when to execute the mixpanel.alias call so that a user history gets added to his profile. I have tried several different option but was not able to get it working.

Thanks a lot,

Jeremy

brunomac commented 11 years ago

+1

brunomac commented 11 years ago

I am facing the exact same problem. I use rails 3.2.14, mixpanel gem version 4.0.7 I use Rack defined in an initializer this way:

MIXPANEL_OPTIONS = {persist: true}

Patientric::Application.config.middleware.use "Mixpanel::Middleware", MIXPANEL_API_TOKEN, MIXPANEL_OPTIONS

Than in _applicationcontroller I set the instance this way:

def mixpanel
    env = {
      'REMOTE_ADDR' => request.env['REMOTE_ADDR'],
      'HTTP_X_FORWARDED_FOR' => request.env['HTTP_X_FORWARDED_FOR'],
      'rack.session' => request.env['rack.session'],
      'mixpanel_events' => request.env['mixpanel_events']
    }
    @mixpanel ||= Mixpanel::Tracker.new MIXPANEL_API_TOKEN, MIXPANEL_OPTIONS.merge(env: env)
end

BTW: If I user the option async: true in the Class, I get an error when there is no user registered.

In my workflow I track when a user reaches the signup form (this user has a mixpanel random id), by overriding devise registration controller:

  # GET /resource/sign_up
  def new
    resource = build_resource({})
    ...
    mixpanel.track 'Sign up form', {user_type: 'XXXXXXXX', distinct_id: resource.mixpanel_id}
    respond_with resource
  end

then I track the signup by overriding _after_sign_up_pathfor devise registration controller:

  def after_sign_up_path_for(resource)
    mixpanel.append('alias', resource.mixpanel_id)
    mixpanel.set resource.mixpanel_id, {ip: request.env['HTTP_CF_CONNECTING_IP'] || request.remote_ip } # because of Clouflare
    mixpanel.track 'Signed up', {distinct_id: resource.mixpanel_id, user_type: 'XXXXXXXX'}
    super
  end

I my user model, when the user is created I save his info in mixpanel (this happens before the call to _after_sign_up_pathfor happens) this works fine has I have all the user info in mixpanel

class XXXXXX < ActiveRecord::Base
  ...
  after_save    :mixpanelize
  ...
  ...
  def mixpanelize
    @mixpanel = Mixpanel::Tracker.new MIXPANEL_API_TOKEN
    @mixpanel.set self.mixpanel_id, {
      type: 'XXXXXX',
      first_name: self.first_name,
      last_name: self.last_name,
      age: self.age,
      email: self.email,
      gender: self.gender,
      created: self.created_at,
      sign_ins: self.sign_in_count
    }
  end

Everytime a users signs in I run this by overriding devise signin:

  def after_sign_in_path_for(resource)
    if @user
      mixpanel.append_identify(@user.mixpanel_id)
      mixpanel.set @user.mixpanel_id, {ip: request.env['HTTP_CF_CONNECTING_IP'] || request.remote_ip }
      mixpanel.track 'Signed in', {user_type: @user.class.to_s, distinct_id: @user.mixpanel_id}
    end
    super
  end

But in mixpanel I was never able to have the signup funnel complete. Mixpanel just doesn't map the anonymous user (random id) with the new id given by my app.

Can anyone help me with this? Where and how does the mixpanel alias gets executed? Thanks

reconbot commented 11 years ago

@brunomac this helps a lot

brunomac commented 11 years ago

Thanks, but I still don't know how to solve this issue. Another thing, error/bug, when using distinct_id in track calls, in mixpanel I have a big string uuid style that has nothing to do with the one I am passing in the track call.

reconbot commented 11 years ago

Mixpanel won't show you your alias, they'll always have the uuid as their actual distinct_id, but will let you use the alias when you make tracking calls. If you downgrade to 4.0.6 I think this issue should be fixed. I'm still working up a test case however so I can't verify that. (maybe you can?)

brunomac commented 11 years ago

just edited my first question to the most recent use case. thanks @reconbot

reconbot commented 11 years ago

4.0.5 maybe ...

brunomac commented 11 years ago

@reconbot, I will try... then I will post back if it worked. thanks

brunomac commented 11 years ago

updated my question again with more info (after_sign_in_path_for())

brunomac commented 11 years ago

@reconbot, exactly the same, no map between mixpanel random id and new distinct id given by the app. But, with v 4.0.5, I no longer get the id UUID style (ie. 1f145416-dd48-45f1-b8db-8fc31bc3938b...)

brunomac commented 11 years ago

@reconbot, correction, still the same problem. Example: user 1 signs up. I give him distinct_id u_1 user 1 signs in, after a while, and he gets distinct_id 14114014e134f7-02f72850bef759-3b32715f-c0000-14114014e235fc

this doesn't happens everytime, just for some users. This ends up creating 2 users, one with distinct_id u_1 and another with distinct_id 14114014e134f7-02f72850bef759-3b32715f-c0000-14114014e235fc for the same user... any thoughts?

PS: this happened with v4.0.7 and now with v4.0.5 PS2: alias still doesn't work, I can't have a funnel working with events "sign up form" and "signed up" because mixpanel can't map same user with distinct id.

domness commented 11 years ago

I'm having some pretty big issues with the same as the above. There seems to be absolutely no way of alias to work on this gem. Not to mention I can't seem to find the distinct_id from anywhere, the JS gives me a huge UUID, and the Ruby part seems to have no way of getting the distinct_id, otherwise I'd just set the distinct_id in the user details on our database and use that.

pentium10 commented 10 years ago

any workaround to get this alias working? or how do I access previous_distinct_id?