ankane / ahoy

Simple, powerful, first-party analytics for Rails
MIT License
4.24k stars 377 forks source link

utm params lost when redirected to login (devise) #510

Closed silva96 closed 2 years ago

silva96 commented 2 years ago

Hi, I have a situation where I send an email to a user with a link like

https://mydomain.com/notifications?utm_campaign=campaign1 which needs the user to be logged in.

Then Devise redirects to https://mydomain.com/users/sign_in and a visit is created with landing https://mydomain.com/users/sign_in

After successful login it redirects back to https://mydomain.com/notifications?utm_campaign=campaign1

After login, the visit is automatically asociated with the user, but utm_campaign is not.

How would you solve this problem? I need to know how many users saw the page "notifications" with that campaign.

ankane commented 2 years ago

Hey @silva96, a few things you could try:

  1. Add skip_before_action :track_ahoy_visit to the Devise controller
  2. Update the visit if the campaign is present
if params[:utm_campaign] && current_visit && !current_visit.utm_campaign
  current_visit.update!(utm_campaign: params[:utm_campaign])
end
  1. Track a separate event
ahoy.track("Viewed notifications", params.slice(:utm_campaign))
silva96 commented 2 years ago

Thanks! I will try