2

I have a User model with uuid for id column.

Ahoy gem creates visits as expected but the user_id is wrong.

Any ideas?

Piotr Brudny
  • 654
  • 4
  • 16

2 Answers2

2

ok. Got that. Ahoy gem doesn't work with user_id as UUID. It takes the first digits from uuid and stores that in user_id for Ahoy::Visit which could look like random value. The solution is to change the user_id type to uuid.

This migration would do the trick:

class ChangeAhoyVisits < ActiveRecord::Migration[5.2]
  def change
    Ahoy::Visit.destroy_all

    remove_column :ahoy_visits, :user_id, :bigint
    add_column :ahoy_visits, :user_id, :uuid, foreign_key: true, null: true
    add_index :ahoy_visits, :user_id
  end
end
Piotr Brudny
  • 654
  • 4
  • 16
1

Probably need to add the same type: :uuid to the user_id column in the ahoy_events table as well. After a few rake db:rollback's I ended up modifying the original migration file that is created by rails generate ahoy:install to look like this before I ran the migration:

def change
  create_table :ahoy_visits do |t|
    t.string :visit_token
    t.string :visitor_token

    # the rest are recommended but optional
    # simply remove any you don't want

    # user
    t.references :user, type: :uuid, foreign_key: true, index: true

    # standard
    t.string :ip
    t.text :user_agent
    t.text :referrer
    t.string :referring_domain
    t.text :landing_page

    # technology
    t.string :browser
    t.string :os
    t.string :device_type

    # location
    t.string :country
    t.string :region
    t.string :city
    t.float :latitude
    t.float :longitude

    # utm parameters
    t.string :utm_source
    t.string :utm_medium
    t.string :utm_term
    t.string :utm_content
    t.string :utm_campaign

    # native apps
    t.string :app_version
    t.string :os_version
    t.string :platform

    t.datetime :started_at
  end

  add_index :ahoy_visits, :visit_token, unique: true

  create_table :ahoy_events do |t|
    t.references :visit
    t.references :user, type: :uuid, foreign_key: true, index: true

    t.string :name
    t.jsonb :properties
    t.datetime :time
  end

  add_index :ahoy_events, [:name, :time]
  add_index :ahoy_events, :properties, using: :gin, opclass: :jsonb_path_ops
end

And after running this slightly modified migration rather than original everything seemed to populate properly on an 'ahoy.track' in the db.