ankane / ahoy_email

First-party email analytics for Rails
MIT License
1.11k stars 137 forks source link

Proper way to globally set user mapping logic? #151

Closed philipithomas closed 3 years ago

philipithomas commented 3 years ago

Hi,

I have a one-to-many association between users and email addresses. So, I need to update the way AhoyEmail detects what it considers a "user".

In config/initializers/ahoy_email.rb I have this code:

AhoyEmail.default_options[:user]  = proc { |message, mailer| (message.to.size == 1 ? EmailAddress.first : nil) rescue nil }

However, it doesn't seem to be working.

How should the user detection query get updated globally?

(And thanks for the help! 🙏)

ankane commented 3 years ago

Hey @philipithomas, you probably want to use something like EmailAddress.find_by(email: message.to.first) instead of EmailAddress.first. You can also try removing the rescue in development to see if there are any errors.

philipithomas commented 3 years ago

Hey - yes, I had done that filtering originally, but I don't believe the code is executing at all. (I even through a byebug in there that didn't execute). So, my more overarching question is - what's the correct way to replace the user identification function?

I can do more debugging later to investigate further.

ankane commented 3 years ago

AhoyEmail.default_options[:user] is the way - https://github.com/ankane/ahoy_email#options

philipithomas commented 3 years ago

Thank you! The issue seemed to be that I was using a proc in instead of a lambda. I'm new to ruby, so I don't know well why this is the case - but I got the code working!