Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Elixir client for Posthog

License

NotificationsYou must be signed in to change notification settings

PostHog/posthog-elixir

Repository files navigation

Hex.pmDocumentation

A powerful Elixir SDK forPostHog

Features

  • Analytics and Feature Flags support
  • Error tracking support
  • Powerful process-based context propagation
  • Asynchronous event sending with built-in batching
  • Overridable HTTP client
  • Support for multiple PostHog projects

Getting Started

AddPostHog to your dependencies:

defdepsdo[{:posthog,"~> 2.0"}]end

Configure thePostHog application environment:

config:posthog,enable:true,enable_error_tracking:true,api_host:"https://us.i.posthog.com",# Or `https://eu.i.posthog.com` or your self-hosted PostHog instance URLapi_key:"phc_my_api_key",in_app_otp_apps:[:my_app]

For test environment, you want to enable test_mode:

config:posthog,test_mode:true

Optionally, enablePlug integration.

You're all set! 🎉 For more information on configuration, check thePostHog.Config moduledocumentation and theadvanced configuration guide.

Capturing Events

To capture an event, usePostHog.capture/2:

iex>PostHog.capture("user_signed_up",%{distinct_id:"distinct_id_of_the_user"})

You can pass additional properties in the last argument:

iex>PostHog.capture("user_signed_up",%{distinct_id:"distinct_id_of_the_user",login_type:"email",is_free_trial:true})

Special Events

PostHog.capture/2 is very powerful and allows you to send events that havespecial meaning. For example:

Create Alias

iex>PostHog.capture("$create_alias",%{distinct_id:"frontend_id",alias:"backend_id"})

Group Analytics

iex>PostHog.capture("$groupidentify",%{distinct_id:"static_string_used_for_all_group_events","$group_type":"company","$group_key":"company_id_in_your_db"})

Context

Carryingdistinct_id around all the time might not be the most convenientapproach, soPostHog lets you store it and other properties in acontext.The context is stored in theLogger metadata, and PostHog will automaticallyattach these properties to any events you capture withPostHog.capture/3, as long as theyhappen in the same process.

iex>PostHog.set_context(%{distinct_id:"distinct_id_of_the_user"})iex>PostHog.capture("page_opened")

You can scope context by event name. In this case, it will only be attached to a specific event:

iex>PostHog.set_event_context("sensitive_event",%{"$process_person_profile":false})

You can always inspect the context:

iex>PostHog.get_context()%{distinct_id:"distinct_id_of_the_user"}iex>PostHog.get_event_context("sensitive_event")%{distinct_id:"distinct_id_of_the_user","$process_person_profile":true}

Feature Flags

PostHog.FeatureFlags.check/2 is the main function for checking a feature flag.

# Simple boolean feature flagiex>PostHog.FeatureFlags.check("example-feature-flag-1","user123"){:ok,true}# Note how it automatically sets `$feature/example-feature-flag-1` property in the contextiex>PostHog.get_context()%{"$feature/example-feature-flag-1"=>true}# It will attempt to take distinct_id from the context if it's not providediex>PostHog.set_context(%{distinct_id:"user123"}):okiex>PostHog.FeatureFlags.check("example-feature-flag-1"){:ok,true}# You can also pass a map with body parameters that will be sent to the /flags API as-isiex>PostHog.FeatureFlags.check("example-feature-flag-1",%{distinct_id:"user123",groups:%{group_type:"group_id"}}){:ok,true}# It returns variant if it's setiex>PostHog.FeatureFlags.check("example-feature-flag-2","user123"){:ok,"variant2"}# Returns error if feature flag doesn't existiex>PostHog.FeatureFlags.check("example-feature-flag-3","user123"){:error,%PostHog.UnexpectedResponseError{message:"Feature flag example-feature-flag-3 was not found in the response",response:...}}

If you're feeling adventurous and/or is simply writing a script you can use thePostHog.FeatureFlags.check!/2 helper instead and it will return a boolean or raise an error.

# Simple boolean feature flagiex>PostHog.FeatureFlags.check!("example-feature-flag-1","user123")true# Works for variants tooiex>PostHog.FeatureFlags.check!("example-feature-flag-2","user123")"variant2"# Raises error if feature flag doesn't existiex>PostHog.FeatureFlags.check!("example-feature-flag-3","user123")**(PostHog.UnexpectedResponseError) Feature flag example-feature-flag-3wasnotfoundintheresponse

Error Tracking

Error Tracking is enabled by default.

You can always disable it by settingenable_error_tracking to false:

config:posthog,enable_error_tracking:false

Multiple PostHog Projects

If your app works with multiple PostHog projects, PostHog can accommodate you. Forsetup instructions, consult theadvanced configuration guide.

Developing locally

You should be able to fetch dependencies and run tests right away:

mix deps.getmix test

To run integration test suite that sends real events to the API:

  1. Create a test PostHog project and obtain an API key.
  2. Createconfig/integration.exs config that will be used for integration tests:
cp config/integration.example.exs config/integration.exs
  1. Put API key intoconfig/integration.exs
  2. Run integration tests
mix test --only integration

If you want to play with PostHog events in IEx, just createconfig/dev.override.exs and tweak it to point to the instance of your liking.This config will be gitignored. Here's a minimal example:

# config/dev.override.exsimportConfigconfig:posthog,enable:trueapi_host:"https://us.i.posthog.com",api_key:"phc_XXXX"

About

Elixir client for Posthog

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors14


[8]ページ先頭

©2009-2025 Movatter.jp