You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have added posthog client initialization in config/initializers/posthog.rb
config/initializers/posthog.rb
posthog = PostHog::Client.new({
api_key: "phc_XXXX",
api_host: "https://app.posthog.com", # You can remove this line if you're using app.posthog.com
on_error: Proc.new { |status, msg| print msg }
})
How can I have the posthog variable available from all controllers/models in my projects? Maybe using a singleton?
It needs to be memory efficient and avoid instantiating multiple times the same PostHog::client object.
app/controllers/test_controller.rb
class ApiController < ApplicationController
def test
posthog.capture({
distinct_id: 1,
event: 'test',
properties: {}
end
end
The text was updated successfully, but these errors were encountered:
leoplct
changed the title
PostHog Client is not a class/module
Help: PostHog Client available from all controllers/models
Jul 5, 2022
SOLVED.
I suggest to add this in the documentation.
config/initializers/posthog.rb
::PostHogApi = PostHog::Client.new({
api_key: "phc_XXXX",
api_host: "https://app.posthog.com", # You can remove this line if you're using app.posthog.com
on_error: Proc.new { |status, msg| print msg }
})
services/dev/analytics.rb
class Dev::Analytics
def self.track(user_id, event, properties={})
::PostHogApi.capture({
distinct_id: user_id,
event: event,
properties: properties
})
end
end
I have added posthog client initialization in config/initializers/posthog.rb
config/initializers/posthog.rb
How can I have the
posthog
variable available from all controllers/models in my projects? Maybe using a singleton?It needs to be memory efficient and avoid instantiating multiple times the same PostHog::client object.
app/controllers/test_controller.rb
The text was updated successfully, but these errors were encountered: