### Installation and Setup Source: https://posthog.com/docs/libraries/capacitor Instructions for installing the PostHog Capacitor plugin and initializing it with your project token and host. ```APIDOC ## Installation Run the following command to install the package: ```bash npm install @capawesome/capacitor-posthog posthog-js ``` After installing the package, you need to update your Capacitor project: ```bash npx cap update ``` ### Configuration To use the plugin, you need to call the `setup(...)` method with your PostHog project token and host. ```typescript import { Posthog } from '@capawesome/capacitor-posthog'; const setup = async () => { await Posthog.setup({ apiKey: '', host: 'https://us.i.posthog.com', }); }; ``` The `host` key specifies the PostHog instance. The default host is `https://us.i.posthog.com`. ``` -------------------------------- ### Install PostHog Go SDK Source: https://posthog.com/docs/libraries/go Install the PostHog Go SDK using the go get command. ```bash go get github.com/posthog/posthog-go ``` -------------------------------- ### Install PostHog Wizard Source: https://posthog.com/docs/libraries/django Use the PostHog wizard for quick integration. This command-line tool automates setup steps. ```bash npx @posthog/wizard@latest ``` -------------------------------- ### Install @posthog/nuxt with Bun Source: https://posthog.com/docs/libraries/nuxt-js Install the PostHog Nuxt module using Bun. ```bash bun add @posthog/nuxt ``` -------------------------------- ### Install PostHog Packages with Bun Source: https://posthog.com/docs/libraries/react-router/react-router-v7-data-mode Install the necessary PostHog JavaScript and React packages using Bun. ```bash bun add posthog-js @posthog/react ``` -------------------------------- ### Install @posthog/nuxt with npm Source: https://posthog.com/docs/libraries/nuxt-js Install the PostHog Nuxt module using npm. ```bash npm install @posthog/nuxt ``` -------------------------------- ### Install @posthog/nuxt with Yarn Source: https://posthog.com/docs/libraries/nuxt-js Install the PostHog Nuxt module using Yarn. ```bash yarn add @posthog/nuxt ``` -------------------------------- ### Example of script tag PostHog installation Source: https://posthog.com/docs/libraries/js/types This HTML snippet demonstrates how PostHog is typically loaded into a webpage using a script tag, which is the scenario where the `@posthog/types` package is beneficial for TypeScript users. ```html ``` -------------------------------- ### Install @posthog/nuxt with pnpm Source: https://posthog.com/docs/libraries/nuxt-js Install the PostHog Nuxt module using pnpm. ```bash pnpm add @posthog/nuxt ``` -------------------------------- ### Install PostHog via Wally Source: https://posthog.com/docs/libraries/roblox Add the PostHog dependency to your wally.toml file. This is the recommended installation method. ```toml [dependencies] PostHog = "posthog/posthog-roblox@0.1.0" ``` -------------------------------- ### Install PostHog Packages Source: https://posthog.com/docs/libraries/react-router/react-router-v7-framework-mode Install the necessary PostHog packages for your project using npm, Yarn, pnpm, or Bun. ```bash npm install --save posthog-js @posthog/react ``` ```bash yarn add posthog-js @posthog/react ``` ```bash pnpm add posthog-js @posthog/react ``` ```bash bun add posthog-js @posthog/react ``` -------------------------------- ### Install PostHog Packages with Yarn Source: https://posthog.com/docs/libraries/react-router/react-router-v7-data-mode Install the necessary PostHog JavaScript and React packages using Yarn. ```bash yarn add posthog-js @posthog/react ``` -------------------------------- ### Install PostHog Packages with npm Source: https://posthog.com/docs/libraries/react-router/react-router-v7-data-mode Install the necessary PostHog JavaScript and React packages using npm. ```bash npm install --save posthog-js @posthog/react ``` -------------------------------- ### Install @posthog/next library Source: https://posthog.com/docs/libraries/next-js/posthog-next Commands to install the @posthog/next package using various Node.js package managers. ```bash npm install --save @posthog/next ``` ```bash yarn add @posthog/next ``` ```bash pnpm add @posthog/next ``` ```bash bun add @posthog/next ``` -------------------------------- ### Install PostHog Packages with pnpm Source: https://posthog.com/docs/libraries/react-router/react-router-v7-data-mode Install the necessary PostHog JavaScript and React packages using pnpm. ```bash pnpm add posthog-js @posthog/react ``` -------------------------------- ### Install PostHog Node with Yarn Source: https://posthog.com/docs/libraries/nuxt-js-3-6 Use Yarn to install the posthog-node package for server-side integration. ```bash yarn add posthog-node ``` -------------------------------- ### PostHog React Native SDK V3 Setup Source: https://posthog.com/docs/libraries/react-native This snippet shows the setup for V3 of the PostHog React Native SDK, which uses `new PostHog` to instantiate the client. Similar to V2, it emphasizes using the created instance for capturing events. ```jsx import PostHog from 'posthog-react-native' const posthog = new PostHog('', { // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' host: 'https://us.i.posthog.com', // Add any other options here. }) // Use created instance rather than the PostHog class posthog.capture("foo") ``` -------------------------------- ### Install posthog-node Package Source: https://posthog.com/docs/libraries/nestjs Install the necessary PostHog Node.js package using npm, yarn, or pnpm. ```bash npm install posthog-node # or yarn add posthog-node # or pnpm add posthog-node ``` -------------------------------- ### Install Flags SDK PostHog Adapter Source: https://posthog.com/docs/libraries/vercel Install the necessary npm packages for the Flags SDK and its PostHog adapter. This is the first step in setting up feature flags. ```bash npm i flags @flags-sdk/posthog ``` -------------------------------- ### Install PostHog Node with npm Source: https://posthog.com/docs/libraries/nuxt-js-3-6 Use npm to install the posthog-node package for server-side integration. ```bash npm install posthog-node --save ``` -------------------------------- ### Generate PostHog Initializer Source: https://posthog.com/docs/libraries/ruby-on-rails Run the PostHog install generator to create the configuration file 'config/initializers/posthog.rb' with default settings. ```bash rails generate posthog:install ``` -------------------------------- ### Install posthog-node Source: https://posthog.com/docs/libraries/cloudflare-workers Install the posthog-node library using npm. This is the first step to integrating PostHog with your Cloudflare Worker. ```bash npm i posthog-node ``` -------------------------------- ### Install posthog-js Library Source: https://posthog.com/docs/libraries/nuxt-js-2 Installs the posthog-js library using various package managers. This is the first step to integrating PostHog into your project. ```bash npm install --save posthog-js ``` ```bash yarn add posthog-js ``` ```bash pnpm add posthog-js ``` ```bash bun add posthog-js ``` -------------------------------- ### Install PostHog Libraries for Frontend Source: https://posthog.com/docs/libraries/convex Install the necessary PostHog libraries for your frontend application using npm, Yarn, pnpm, or Bun. These are required to send frontend events and stitch them with backend events. ```bash npm install posthog-js @posthog/react ``` ```bash yarn add posthog-js @posthog/react ``` ```bash pnpm add posthog-js @posthog/react ``` ```bash bun add posthog-js @posthog/react ``` -------------------------------- ### Install PostHog PHP SDK Source: https://posthog.com/docs/libraries/php Install the PostHog PHP SDK using Composer. This is the first step to integrating PostHog into your PHP application. ```bash composer require posthog/posthog-php ``` -------------------------------- ### Install @posthog/convex with Bun Source: https://posthog.com/docs/libraries/convex Use this command to add the PostHog Convex component to your project using Bun. ```bash bun add @posthog/convex ``` -------------------------------- ### Install PostHog Docusaurus Plugin Source: https://posthog.com/docs/libraries/docusaurus Commands to install the PostHog Docusaurus plugin using npm or yarn. ```bash npm install --save posthog-docusaurus ``` ```bash yarn add posthog-docusaurus ``` -------------------------------- ### Install PostHog Packages Source: https://posthog.com/docs/libraries/tanstack-start Installs the necessary PostHog packages for React (client-side) and Node.js (server-side) integration. Requires npm or yarn. ```bash npm install @posthog/react posthog-node ``` -------------------------------- ### PostHog React Native SDK V2 Setup Source: https://posthog.com/docs/libraries/react-native This snippet demonstrates the setup for V2 of the PostHog React Native SDK, which uses `Posthog.initAsync` to create an instance. It highlights the change from using the class directly to using the created instance for capturing events. ```jsx import PostHog from 'posthog-react-native' const posthog = await Posthog.initAsync('', { // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' host: 'https://us.i.posthog.com', // Add any other options here. }) // Use created instance rather than the PostHog class posthog.capture("foo") ``` -------------------------------- ### Install PostHog ASP.NET Core Package Source: https://posthog.com/docs/libraries/dotnet Use the dotnet CLI to add the PostHog.AspNetCore package to your project. ```bash dotnet add package PostHog.AspNetCore ``` -------------------------------- ### Install PostHog JS with Bun Source: https://posthog.com/docs/libraries/angular Install the posthog-js package using Bun. This command adds the PostHog SDK to your project, enabling analytics features. ```bash bun add posthog-js ``` -------------------------------- ### Install PostHog Node.js SDK Source: https://posthog.com/docs/libraries/hono Installs the PostHog Node.js package using npm. This is the first step to integrating PostHog into your Hono application. ```bash npm install --save posthog-node ``` -------------------------------- ### Install PostHog Node.js SDK with Bun Source: https://posthog.com/docs/libraries/nuxt-js-3-6 Use this command to add the PostHog Node.js library to your project when using Bun as your package manager. ```bash bun add posthog-node ``` -------------------------------- ### PostHog React Native SDK V1 Setup (Deprecated) Source: https://posthog.com/docs/libraries/react-native This snippet shows the deprecated setup for V1 of the PostHog React Native SDK. It uses `PostHog.setup` and has specific options that are no longer supported or have changed in later versions. ```jsx import PostHog from 'posthog-react-native' await PostHog.setup('', { // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' host: 'https://us.i.posthog.com', captureApplicationLifecycleEvents: false, // Replaced by 'PostHogProvider' captureDeepLinks: false, // No longer supported recordScreenViews: false, // Replaced by 'PostHogProvider' supporting @react-navigation/native flushInterval: 30, // Stays the same flushAt: 20, // Stays the same android: {...}, // No longer needed iOS: {...}, // No longer needed }) PostHog.capture("foo") ``` -------------------------------- ### Install @posthog/convex with npm Source: https://posthog.com/docs/libraries/convex Use this command to add the PostHog Convex component to your project using npm. ```bash npm install @posthog/convex ``` -------------------------------- ### Initialize Posthog Client and Capture Events (Python) Source: https://posthog.com/docs/libraries/python Demonstrates how to initialize the Posthog client with project token, host, and specific options like debug and historical migration. It then shows how to capture batched events with distinct IDs and timestamps. ```python from posthog import Posthog from datetime import datetime posthog = Posthog( '', host='https://us.i.posthog.com', debug=True, historical_migration=True ) events = [ { "event": "batched_event_name", "properties": { "distinct_id": "user_id", "timestamp": datetime.fromisoformat("2024-04-02T12:00:00") } }, { "event": "batched_event_name", "properties": { "distinct_id": "used_id", "timestamp": datetime.fromisoformat("2024-04-02T12:00:00") } } ] for event in events: posthog.capture( distinct_id=event["properties"]["distinct_id"], event=event["event"], properties=event["properties"], timestamp=event["properties"]["timestamp"], ) ``` -------------------------------- ### Initialize PostHog PHP Client with Options Source: https://posthog.com/docs/libraries/php Demonstrates how to initialize the PostHog client with basic configuration options including host, debug, and SSL settings. All available options can be passed in the configuration array. ```php PostHog::init( '', [ 'host' => 'https://us.i.posthog.com', 'debug' => true, 'ssl' => false, // all options go here ], ); ``` -------------------------------- ### Initialize PostHog in main.ts (Angular v16 and below) Source: https://posthog.com/docs/libraries/angular Initialize PostHog directly in your src/main.ts file for Angular versions 16 and below. This setup ensures PostHog is ready to capture data from the start of the application lifecycle. ```typescript // src/main.ts import { bootstrapApplication } from '@angular/platform-browser'; import { appConfig } from './app/app.config'; import { AppComponent } from './app/app.component'; import { environment } from "./environments/environment"; import posthog from 'posthog-js' posthog.init(environment.posthogKey, { api_host: environment.posthogHost, defaults: '2026-01-30' }) bootstrapApplication(AppComponent, appConfig) .catch((err) => console.error(err)); ``` -------------------------------- ### PostHog JS SDK with included types Source: https://posthog.com/docs/libraries/js/types When installing PostHog via npm or yarn (e.g., `posthog-js`), TypeScript types are automatically included. This example shows importing and using the SDK without needing a separate types package. ```typescript // Types are already available when you install posthog-js import posthog from 'posthog-js' posthog.init('your-api-key') posthog.capture('my_event') // ✅ Fully typed ``` -------------------------------- ### Initialize PostHog Client in Java Source: https://posthog.com/docs/libraries/java This code snippet shows how to set up and initialize the PostHog client with a configuration, including host and API key. It also demonstrates flushing and closing the client. ```java import com.posthog.server.PostHog; import com.posthog.server.PostHogConfig; import com.posthog.server.PostHogInterface; class Sample { private static final String POSTHOG_API_KEY = ""; private static final String POSTHOG_HOST = "https://us.i.posthog.com"; public static void main(String args[]) { PostHogConfig config = PostHogConfig .builder(POSTHOG_API_KEY) .host(POSTHOG_HOST) .build(); PostHogInterface posthog = PostHog.with(config); posthog.flush(); // send any remaining events posthog.close(); // shut down the client } } ``` -------------------------------- ### Install Dependencies with Bundler Source: https://posthog.com/docs/libraries/ruby-on-rails After adding the gems to your Gemfile, run 'bundle install' to install them and their dependencies. ```bash bundle install ``` -------------------------------- ### Initialize PostHog Client Source: https://posthog.com/docs/libraries/python Import the Posthog library and initialize the client with your project token and host. This should be done before making any calls. ```python from posthog import Posthog posthog = Posthog('', host='https://us.i.posthog.com') ``` -------------------------------- ### Install @posthog/types package Source: https://posthog.com/docs/libraries/js/types Install the `@posthog/types` package using npm, yarn, or pnpm. This package provides TypeScript definitions for PostHog when using script tag installations. ```bash npm install @posthog/types # or yarn add @posthog/types # or pnpm add @posthog/types ``` -------------------------------- ### Initialize PostHog Client in Go Source: https://posthog.com/docs/libraries/go Initialize the PostHog client with your API key and configuration. The personal API key is optional but recommended for better performance. Ensure to close the client when done. ```go package main import ( "os" "github.com/posthog/posthog-go" ) func main() { client, _ := posthog.NewWithConfig( os.Getenv("POSTHOG_API_KEY"), posthog.Config{ PersonalApiKey: "your personal API key", // Optional, but much more performant. If this token is not supplied, then fetching feature flag values will be slower. Endpoint: "https://us.i.posthog.com", }, ) defer client.Close() // run commands } ``` -------------------------------- ### Initialize PostHog with Code Source: https://posthog.com/docs/libraries/unity Initialize the PostHog SDK using a configuration object in your game's startup script. Ensure no PostHogSettings.asset exists if using this method. ```csharp using PostHogUnity; using UnityEngine; public class GameManager : Monobehavior { void Start() { PostHog.Setup(new PostHogConfig { ApiKey = "", Host = "https://us.i.posthog.com" // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' }); } } ``` -------------------------------- ### Initialize PostHog Client Source: https://posthog.com/docs/libraries/rust Set up the PostHog client with your project token. The token is expected to be an environment variable. ```rust let client = posthog_rs::client(env!("")); ``` -------------------------------- ### Setting Up Feature Flags with Callback in C# Source: https://posthog.com/docs/libraries/unity Shows how to set up the PostHog SDK and receive a callback when feature flags are loaded, ensuring UI updates or other logic can proceed. ```csharp // Via callback during setup PostHog.Setup(new PostHogConfig { ApiKey = "", OnFeatureFlagsLoaded = () => Debug.Log("Flags ready!") }); // Or via event PostHog.OnFeatureFlagsLoaded += () => UpdateUI(); ``` -------------------------------- ### Install PostHog Node with pnpm Source: https://posthog.com/docs/libraries/nuxt-js-3-6 Use pnpm to install the posthog-node package for server-side integration. ```bash pnpm add posthog-node ``` -------------------------------- ### Initialize PostHog SDK Source: https://posthog.com/docs/libraries/php Initialize the PostHog SDK with your project token and host. It's recommended to use environment variables for sensitive information like API keys. ```php PostHog\PostHog::init("", ['host' => 'https://us.i.posthog.com'] ); ``` -------------------------------- ### Initialize PostHog Client and Capture Events (Node.js) Source: https://posthog.com/docs/libraries/python Shows how to initialize the PostHog client in Node.js with project token and host, enabling debug and historical migration. It then captures events with distinct IDs and timestamps, ensuring to shut down the client afterwards. ```javascript import { PostHog } from 'posthog-node' const client = new PostHog( '', { host: 'https://us.i.posthog.com', historicalMigration: true } ) client.debug() client.capture({ event: "batched_event_name", distinctId: "user_id", properties: {}, timestamp: "2024-04-03T12:00:00Z" }) client.capture({ event: "batched_event_name", distinctId: "user_id", properties: {}, timestamp: "2024-04-03T13:00:00Z" }) await client.shutdown() ``` -------------------------------- ### Configure PostHog with All Options Source: https://posthog.com/docs/libraries/unity Set up the PostHog SDK with a comprehensive configuration object, specifying API key, host, and various performance and capture settings. This can be done via code or the Unity Inspector. ```csharp PostHog.Setup(new PostHogConfig { // Required ApiKey = "", // Optional Host = "https://us.i.posthog.com", // PostHog instance URL (default: https://us.i.posthog.com) FlushAt = 20, // Events before auto-flush (default: 20) FlushIntervalSeconds = 30, // Seconds between flushes (default: 30) MaxQueueSize = 1000, // Max queued events (default: 1000) MaxBatchSize = 50, // Max events per request (default: 50) CaptureApplicationLifecycleEvents = true, // Auto-capture app lifecycle events (default: true) CaptureExceptions = true, // Auto-capture unhandled exceptions (default: true) PersonProfiles = PersonProfiles.IdentifiedOnly, // When to create person profiles PreloadFeatureFlags = true, // Fetch flags on init (default: true) SendFeatureFlagEvent = true, // Track flag usage (default: true) LogLevel = PostHogLogLevel.Warning // Log verbosity (default: Warning) }); ``` -------------------------------- ### Initialize PostHog on Server Source: https://posthog.com/docs/libraries/roblox Initialize the PostHog SDK once from a server Script. This must be run on the server; calling Init from a LocalScript is ignored. ```lua local ReplicatedStorage = game:GetService("ReplicatedStorage") local PostHog = require(ReplicatedStorage:WaitForChild("PostHog")) PostHog:Init({ apiKey = "", host = "https://us.i.posthog.com", -- usually https://us.i.posthog.com or https://eu.i.posthog.com }) ``` -------------------------------- ### Install PostHog JS via HTML Snippet Source: https://posthog.com/docs/libraries/js Recommended for quick integration. Add this script tag directly to your HTML file. Ensure you replace '' with your actual project token and configure the 'api_host' as needed. ```html ``` -------------------------------- ### Set Up Adapter and Flags in flags.js|ts Source: https://posthog.com/docs/libraries/vercel Configure the flags.js|ts file to initialize the PostHog adapter and define a feature flag. Ensure you replace 'example-flag' with your actual feature flag key from PostHog. ```javascript // flags.js|ts import { flag } from 'flags/next' import { postHogAdapter } from '@flags-sdk/posthog' export const exampleFlag = flag({ key: 'example-flag', defaultValue: false, adapter: postHogAdapter.isFeatureEnabled(), identify: { distinctId: 'user_distinct_id', // Replace with real ID }, }) ``` -------------------------------- ### Set PostHog Project Token and Host Source: https://posthog.com/docs/libraries/convex Configure your Convex environment with your PostHog project token and host URL using the Convex CLI. ```bash npx convex env set POSTHOG_PROJECT_TOKEN npx convex env set POSTHOG_HOST https://us.i.posthog.com ``` -------------------------------- ### Initialize PostHog with cross-subdomain cookie properties Source: https://posthog.com/docs/libraries/js/persistence Shows how to initialize PostHog and define specific properties that should be persisted in a cross-subdomain cookie. ```javascript posthog.init('', { api_host: 'https://us.i.posthog.com', persistence: 'localStorage+cookie', cookie_persisted_properties: ['user_preferences', 'signup_source'], }) ``` -------------------------------- ### PostHog Initialization Configuration Options Source: https://posthog.com/docs/libraries/roblox Configure PostHog initialization with various options for connection, event queue, feature flags, autocapture, error tracking, identity, client relay, and diagnostics. Only apiKey is required. ```lua PostHog:Init({ -- Required apiKey = "", -- Connection host = "https://us.i.posthog.com", -- Event queue flushAt = 20, -- Flush once this many events are queued (default: 20) flushIntervalSeconds = 30, -- Flush at least this often (default: 30) maxQueueSize = 1000, -- Drop the oldest events past this size (default: 1000) maxBatchSize = 50, -- Maximum events per HTTP request (default: 50) -- Feature flags preloadFeatureFlags = true, -- Fetch a player's flags when they join (default: true) sendFeatureFlagEvents = true, -- Emit $feature_flag_called when a flag is read (default: true) sendDefaultPersonPropertiesForFlags = true, -- Autocapture and error tracking captureLifecycleEvents = true, -- player_joined/left/idle, server_started/shutdown (default: true) captureErrors = true, -- Capture unhandled errors as $exception events (default: true) errorDebounceSeconds = 1, -- Minimum gap between auto-captured server errors (default: 1) -- Identity personProfiles = "identified_only", -- "identified_only" | "always" | "never" serverDistinctId = "server", -- distinct_id used for server-scoped (nil subject) events -- Client relay enableClientRelay = true, -- Create the RemoteEvent that lets clients relay events (default: true) clientRateLimitPerSecond = 20, -- Per-player rate limit for relayed messages (default: 20) maxClientPropertyCount = 100, -- Maximum properties accepted from one client message (default: 100) -- Diagnostics logLevel = "warn", -- "debug" | "info" | "warn" | "error" | "none" }) ``` -------------------------------- ### Install PostHog for Expo Apps Source: https://posthog.com/docs/libraries/react-native Use this command to install the posthog-react-native package and its required peer dependencies for Expo projects. ```bash npx expo install posthog-react-native expo-file-system expo-application expo-device expo-localization ``` -------------------------------- ### Wrap Application with PostHogProvider Source: https://posthog.com/docs/libraries/next-js/posthog-next Initializes PostHog in the root layout or _app file, enabling feature flag bootstrapping and automatic page view tracking. ```jsx import { PostHogProvider, PostHogPageView } from '@posthog/next' export default function RootLayout({ children }: { children: React.ReactNode }) { return ( {children} ) } ``` ```jsx import type { AppProps } from 'next/app' import { PostHogProvider, PostHogPageView } from '@posthog/next/pages' export default function App({ Component, pageProps }: AppProps) { return ( ) } ``` -------------------------------- ### Install PostHog JS with npm Source: https://posthog.com/docs/libraries/angular Install the posthog-js package using npm. This is the first step to integrating PostHog into your Angular project. ```bash npm install --save posthog-js ``` -------------------------------- ### Install PostHog JS with pnpm Source: https://posthog.com/docs/libraries/angular Install the posthog-js package using pnpm. This command ensures the PostHog SDK is available in your Angular project. ```bash pnpm add posthog-js ``` -------------------------------- ### Initialize PostHog Client in Vercel Functions Source: https://posthog.com/docs/libraries/vercel Set up a PostHog client instance for use in serverless environments. Ensure immediate flushing by setting `flushAt` and `flushInterval` to 0. ```javascript // app/posthog.js import { PostHog } from 'posthog-node' export default function PostHogClient() { const posthogClient = new PostHog('', { host: 'https://us.i.posthog.com', flushAt: 1, // flush immediately in serverless environment flushInterval: 0 // same }) return posthogClient } ``` -------------------------------- ### Install PostHog JS with Yarn Source: https://posthog.com/docs/libraries/angular Install the posthog-js package using Yarn. This command adds the necessary PostHog SDK to your project dependencies. ```bash yarn add posthog-js ``` -------------------------------- ### Install PostHog Node.js Package Source: https://posthog.com/docs/libraries/angular Install the `posthog-node` package for server-side operations in your Angular SSR project using npm, Yarn, pnpm, or Bun. ```bash npm install posthog-node --save ``` ```bash yarn add posthog-node ``` ```bash pnpm add posthog-node ``` ```bash bun add posthog-node ``` -------------------------------- ### Initialize PostHog with Project Token and API Host Source: https://posthog.com/docs/libraries/js/config Basic initialization of the PostHog JavaScript library, specifying the project token and the API host. ```javascript posthog.init('', { api_host: 'https://us.i.posthog.com', }) ``` -------------------------------- ### Force Refresh Feature Flags on App Start Source: https://posthog.com/docs/libraries/react-native Ensure fresh feature flag values by forcing a refresh on application start using `reloadFeatureFlagsAsync()`. ```javascript // Force refresh on app start await posthog.reloadFeatureFlagsAsync() ``` -------------------------------- ### Install PostHog Python SDK Source: https://posthog.com/docs/libraries/django Install the PostHog Python SDK using pip. Ensure your Python version is 3.10 or higher for SDK version 7.x. ```bash pip install posthog ``` -------------------------------- ### Install PostHog for React Native Apps (NPM) Source: https://posthog.com/docs/libraries/react-native Use this command to install the posthog-react-native package and its required peer dependencies for standard React Native projects using NPM. ```bash npm i -s posthog-react-native @react-native-async-storage/async-storage react-native-device-info react-native-localize ``` -------------------------------- ### Install PostHog for React Native Apps (Yarn) Source: https://posthog.com/docs/libraries/react-native Use this command to install the posthog-react-native package and its required peer dependencies for standard React Native projects using Yarn. ```bash yarn add posthog-react-native @react-native-async-storage/async-storage react-native-device-info react-native-localize ``` -------------------------------- ### Install PostHog Dependencies Source: https://posthog.com/docs/libraries/remix Installs the necessary PostHog JavaScript Web SDK and React integration packages using npm. This is the first step for integrating PostHog into your Remix application. ```bash npm install --save posthog-js @posthog/react ``` -------------------------------- ### Initialize PostHog with Event Sampling Source: https://posthog.com/docs/libraries/js/usage Add this script to your site to initialize PostHog and sample specific events. Ensure the script source matches your API host. ```javascript // Add this script to your site, may need to change the script src to match your API host: // posthog.init('', { // capture only half of dead click and web vitals events before_send: posthogCustomizations.sampleByEvent(['$dead_click', '$web_vitals'], 0.5) }) ``` -------------------------------- ### Install PostHog JavaScript Snippet Source: https://posthog.com/docs/libraries/phoenix The standard PostHog initialization snippet to be placed in the of your Phoenix layout file. It initializes the client with your project token and API host. ```javascript ``` -------------------------------- ### Install PostHog Capacitor Plugin Source: https://posthog.com/docs/libraries/capacitor Commands to install the PostHog Capacitor plugin and update your project. This involves adding the necessary npm packages and running the Capacitor update command. ```bash npm install @capawesome/capacitor-posthog posthog-js npx cap update ``` -------------------------------- ### Start Session Recording with Overrides Source: https://posthog.com/docs/libraries/js/usage Override default session recording behavior to start a recording regardless of feature flags or sampling. Useful for debugging or specific user sessions. ```javascript posthog.startSessionRecording({ linked_flag: true, sampling: true }) ``` -------------------------------- ### Initialize PostHog Client in Application Source: https://posthog.com/docs/libraries/android Initialize the PostHog client in your Application subclass using your API key and host. Ensure you import the necessary classes. ```kotlin import android.app.Application import com.posthog.android.PostHogAndroid import com.posthog.android.PostHogAndroidConfig class SampleApp : Application() { companion object { const val POSTHOG_API_KEY = "" // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' const val POSTHOG_HOST = "https://us.i.posthog.com" } override fun onCreate() { super.onCreate() val config = PostHogAndroidConfig( apiKey = POSTHOG_API_KEY, host = POSTHOG_HOST ) PostHogAndroid.setup(this, config) } } ``` -------------------------------- ### Install TypeScript Types for PostHog JS Source: https://posthog.com/docs/libraries/js If using TypeScript with the script tag, install the '@posthog/types' package for type safety. Create a type declaration file to augment the global Window interface. ```bash npm install @posthog/types ``` ```typescript // posthog.d.ts import type { PostHog } from '@posthog/types' declare global { interface Window { posthog?: PostHog } } export {} ``` -------------------------------- ### Initialize PostHog JS from Package Manager Source: https://posthog.com/docs/libraries/js Import and initialize the PostHog JavaScript SDK after installing it via a package manager. Replace '' with your project token and configure the 'api_host'. ```javascript import posthog from 'posthog-js' posthog.init('', { api_host: 'https://us.i.posthog.com', defaults: '2026-01-30' }) ``` -------------------------------- ### Sample Events by Session ID (Script) Source: https://posthog.com/docs/libraries/js/usage Add this script to your site to initialize PostHog and sample events by session ID. Ensure the script source matches your API host. ```typescript // Add this script to your site, may need to change the script src to match your API host: // posthog.init('', { before_send: posthogCustomizations.sampleBySessionId(0.25) }) ``` -------------------------------- ### Configure PostHog Environment Variables Source: https://posthog.com/docs/libraries/react-router/react-router-v7-data-mode Add your PostHog project token and host to your .env.local file. Prefix with VITE_ for Vite projects. ```shell VITE_POSTHOG_PROJECT_TOKEN= VITE_POSTHOG_HOST=https://us.i.posthog.com ``` -------------------------------- ### PostHog React Native SDK V4 Setup and New Methods Source: https://posthog.com/docs/libraries/react-native This snippet illustrates the setup for V4 of the PostHog React Native SDK, using `new PostHog`. It also introduces new methods like `setPersonPropertiesForFlags` and `setGroupPropertiesForFlags`, and notes that `captureAppLifecycleEvents` is enabled by default in recent versions. ```jsx import PostHog from 'posthog-react-native' const posthog = new PostHog('', { // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' host: 'https://us.i.posthog.com', // captureAppLifecycleEvents is enabled by default since version 4.39.0 (previously named `captureNativeAppLifecycleEvents` or `autocapture={{ captureLifecycleEvents: true }}`) // captureMode: 'json', // No longer supported // maskPhotoLibraryImages: true, // No longer supported }) posthog.setPersonPropertiesForFlags(...) // instead of `personProperties` posthog.setGroupPropertiesForFlags(...) // instead of `groupProperties` ```