ankane / ahoy_email

First-party email analytics for Rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Extra data not saving in Ahoy::Message table while using instance variables

kunal-bluebash opened this issue · comments

Hi,
I am facing issue after upgrading ahoy from 1.0 to latest version.
I am trying to add extra data using this approach(https://github.com/ankane/ahoy_email#extra-data)

Previously on version 1.0 I am using track extra: { resource: @resource }, But now I have change it to has_history extra: { resource: @resource }, but it doesn't works for me.

But If I pass static value like this: has_history extra: { resource: 'Some Content' }, it gets work.

So, In order to make it work I have to override the save_ahoy_options method and add extra data like this:

Safely.safely do
      options = {}
      call_ahoy_options(options, :message)
      call_ahoy_options(options, :utm_params)
      call_ahoy_options(options, :click)

      options[:extra][:resource] = @resource if @resource

      AhoyEmail::Processor.new(self, options).perform if options[:message] || options[:utm_params] || options[:click]
    end

Can anyone help me out with the best approach to add extra data using instance variables.

Thanks In Advance.

Hi @kunal-bluebash, did you try to pass the attribute you wish to save instead of the actual @resource object?

Something like:

  has_history extra: { resource_id: @resource.id } 

Or, in case it's polymorphic:

  has_history extra: { resource_type: @resource.type } 
  has_history extra: { resource_id: @resource.id }

Hello @duarme , Yeah I tried this way as well but it didn't work in my case.

@kunal-bluebash Did you also try with a proc?

has_history extra: -> { { resource_id: @resource.id } }

Yes @duarme , tried with has_history extra: lambda { { resource_id: @resource.id } } and has_history extra: -> { { resource_id: @resource.id } } both of them didn't work.

Hey @kunal-bluebash, the lambda/proc form should work. If you can add a failing test case, I can look into it more (current test and mailer).