weirdboi_posthog
Bevy integration with Posthog analytics.
Install
Add to Cargo.toml:
[]
= "0.1"
Quick start
- Create a PosthogConfig and insert it as a Bevy resource.
- Add the PosthogPlugin.
- Send events using the track! macro and either
CommandsorPosthogsystem params, via SendAnalyticsExt trait.
Usage
Simple Config
use *;
use ;
Using the Posthog system param
use *;
use Posthog;
Real world example
This example conditionally initialises the posthog system and sets custom global params to include
use *;
use ;
/// Perform some logic to generate an anonymous per-install identifier. In a real project:
/// - Fetch and return if it exists
/// - Create and store if it does not
///
/// Using a random per-install identifier allows correlation of game sessions and events
/// without making a user identifiable. There are a variety of events such as reinstalling
/// the game that should regenerate such an identifier.
///
/// weirdboi_posthog will generate a new ID each time it is initialised if no user_id is provided.
///
/// Fun fact: a UUID is a 128 bit integer, consider using the `uuid` library where you can call
/// `Uuid::as_u128` to get a valid user id
Configuration
Environment variables supported by PosthogConfig::from_env():
- POSTHOG_HOST: e.g. https://app.posthog.com or your self-hosted URL
- POSTHOG_KEY: Project API key
Common configuration methods:
- identify(user_id: u128) — set the distinct_id
- set_property(key, value) — set a default property for all events
- set_enabled(bool), enable(), disable() — control sending
- user_id() — current distinct_id string
Event batching
- Events are stored in an Analytics resource and flushed roughly once per second by the plugin.
- If sending fails or serialization errors occur, events are re-queued.
Notes
- Requires Bevy (uses bevy_app, bevy_ecs, bevy_time, and tasks).
- Uses ehttp for HTTP requests.