ankane / ahoy

Simple, powerful, first-party analytics for Rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReferenceError: ahoy is not defined for JS events in a Rails app

scarrick68 opened this issue · comments

Hi, I'm having an issue with js events in a Rails app. I yarn installed, and this is my application.js

import Rails from "@rails/ujs"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
import "controllers"
import "chartkick/chart.js"
import "@hotwired/turbo-rails"
import ahoy from "ahoy.js"

require('packs/browser_timezone')

Rails.start()
ActiveStorage.start()

ahoy.start() // tried with and without this

I get a ReferenceError: ahoy is not defined when trying to call ahoy.track('event', {stuff: things}). I am using Webpacker and Rails 7 with Stimulus.

Hey @scarrick68, I'm not sure how to reproduce. What version of Ahoy.js and Webpacker are you on?

Edit: Also, is ahoy defined if you just do import "ahoy.js"?

Hey @ankane, thanks for getting back to me.

from package.json
"ahoy.js": "^0.4.2", from package.json
from Gemfile / Gemfile.lock
gem "ahoy_matey", "~> 4.2.0"  / ahoy_matey (4.2.1)
gem 'webpacker', '~> 5.4' / webpacker (5.4.3)

with this in application.js

import ahoy from "ahoy.js"
ahoy.start()

The application loads without error, but then I get a ReferenceError: ahoy is not defined when ahoy.track() is called in my stimulus controller.

with this in application.js

import "ahoy.js"
ahoy.start()

I get a reference error on app load and when calling track().

Also, probably worth noting that I'm not seeing a reference to ahoy in the console with ahoy, window.ahoy, or Ahoy

With Webpacker, you'll need to import Ahoy.js in any JS file that uses it (import ahoy from "ahoy.js").